Skip to content

Commit

Permalink
#2976. Add grammar tests (#2990)
Browse files Browse the repository at this point in the history
Add grammar tests
  • Loading branch information
sgrekhov authored Nov 25, 2024
1 parent 9beeb2f commit 4a925ad
Show file tree
Hide file tree
Showing 5 changed files with 285 additions and 0 deletions.
54 changes: 54 additions & 0 deletions LanguageFeatures/Static-access-shorthand/grammar_A01_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

/// @assertion We introduce grammar productions of the form:
/// ```
/// <primary> ::= ... -- all current productions
/// | <staticMemberShorthand>
///
/// <constantPattern> ::= ... -- all current productions
/// | <staticMemberShorthand>
///
/// <staticMemberShorthand> ::=
/// '.' (<identifier> | 'new') -- shorthand qualified name
/// | 'const' '.' (<identifier> | 'new') <arguments> -- shorthand object creation
/// ```
///
/// @description Checks that static members and constructors of a class can be
/// accessed using the static access short syntax.
/// @author sgrekhov22@gmail.com
// SharedOptions=--enable-experiment=enum-shorthands

import '../../Utils/expect.dart';

class C {
final String value;
C(this.value);
const C.foo(this.value);

static C get staticGetter => C("Static getter");
static C staticMethod() => C("Static method");
static List<C> instances = [C("one"), C("two")];
}

main() {
C c1 = .staticGetter;
Expect.equals("Static getter", c1.value);

C c2 = .staticMethod();
Expect.equals("Static method", c2.value);

C c3 = .instances[0];
Expect.equals("one", c3.value);

C c4 = .new("new");
Expect.equals("new", c4.value);

C c5 = .foo("foo");
Expect.equals("foo", c5.value);

C c6 = const .foo("const foo");
Expect.equals("const foo", c6.value);
}
53 changes: 53 additions & 0 deletions LanguageFeatures/Static-access-shorthand/grammar_A01_t02.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

/// @assertion We introduce grammar productions of the form:
/// ```
/// <primary> ::= ... -- all current productions
/// | <staticMemberShorthand>
///
/// <constantPattern> ::= ... -- all current productions
/// | <staticMemberShorthand>
///
/// <staticMemberShorthand> ::=
/// '.' (<identifier> | 'new') -- shorthand qualified name
/// | 'const' '.' (<identifier> | 'new') <arguments> -- shorthand object creation
/// ```
///
/// @description Checks that static members of a mixin can be accessed using the
/// static access short syntax.
/// @author sgrekhov22@gmail.com
// SharedOptions=--enable-experiment=enum-shorthands

import '../../Utils/expect.dart';

class C {
final String value;
C(this.value);
const C.foo(this.value);

static C get staticGetter => C("C: static getter");
static C staticMethod() => C("C: static method");
static List<C> instances = [C("one"), C("two")];
}

mixin M on C {
static M get staticGetter => CM("M: static getter");
static M staticMethod() => CM("M: static method");
static List<M> instances = [CM("M: one"), CM("M: two")];
}

class CM = C with M;

main() {
M m1 = .staticGetter;
Expect.equals("M: static getter", m1.value);

M m2 = .staticMethod();
Expect.equals("M: static method", m2.value);

M m3 = .instances[0];
Expect.equals("M: one", m3.value);
}
48 changes: 48 additions & 0 deletions LanguageFeatures/Static-access-shorthand/grammar_A01_t03.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

/// @assertion We introduce grammar productions of the form:
/// ```
/// <primary> ::= ... -- all current productions
/// | <staticMemberShorthand>
///
/// <constantPattern> ::= ... -- all current productions
/// | <staticMemberShorthand>
///
/// <staticMemberShorthand> ::=
/// '.' (<identifier> | 'new') -- shorthand qualified name
/// | 'const' '.' (<identifier> | 'new') <arguments> -- shorthand object creation
/// ```
///
/// @description Checks that static members and values of an enum can be
/// accessed using the static access short syntax.
/// @author sgrekhov22@gmail.com
// SharedOptions=--enable-experiment=enum-shorthands

import '../../Utils/expect.dart';

enum E {
v1("v1"), v2("v2");

final String value;
const E(this.value);

static E get staticGetter => v1;
static E staticMethod() => v2;
}

main() {
E e0 = .v1;
Expect.equals(E.v1, e0);

E e1 = .staticGetter;
Expect.equals("v1", e1.value);

E e2 = .staticMethod();
Expect.equals("v2", e2.value);

E e3 = E.values[1];
Expect.equals(E.v2, e3);
}
73 changes: 73 additions & 0 deletions LanguageFeatures/Static-access-shorthand/grammar_A01_t04.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

/// @assertion We introduce grammar productions of the form:
/// ```
/// <primary> ::= ... -- all current productions
/// | <staticMemberShorthand>
///
/// <constantPattern> ::= ... -- all current productions
/// | <staticMemberShorthand>
///
/// <staticMemberShorthand> ::=
/// '.' (<identifier> | 'new') -- shorthand qualified name
/// | 'const' '.' (<identifier> | 'new') <arguments> -- shorthand object creation
/// ```
///
/// @description Checks that static members and constructors of an extension
/// type can be accessed using the static access short syntax.
/// @author sgrekhov22@gmail.com
// SharedOptions=--enable-experiment=enum-shorthands

import '../../Utils/expect.dart';

extension type ET1(int v) {
const ET1.foo(this.v);
ET1.bar(this.v);

static ET1 get staticGetter => ET1(1);
static ET1 staticMethod() => ET1(2);
static ET1 instances = [ET1(0), ET1(1)];
}

extension type ET2.baz(int v) {
const ET2(this.v);
}

extension type const ET3.qux(int v) {
ET3.new(this.v);
}

main() {
ET1 et0 = .instances[0];
Expect.equals(0, et0.v);

ET1 et1 = .staticGetter;
Expect.equals(1, et1.v);

ET1 et2 = .staticMethod();
Expect.equals(2, et2.v);

ET1 et3 = .new(3);
Expect.equals(3, et3.v);

ET1 et4 = const .foo(4);
Expect.equals(4, et4.v);

ET1 et5 = .bar(5);
Expect.equals(5, et5.v);

ET2 et6 = .baz(6);
Expect.equals(6, et6.v);

ET2 et7 = const .new(7);
Expect.equals(7, et7.v);

ET3 et8 = const .qux(8);
Expect.equals(8, et8.v);

ET3 et9 = .new(9);
Expect.equals(9, et9.v);
}
57 changes: 57 additions & 0 deletions LanguageFeatures/Static-access-shorthand/grammar_A02_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

/// @assertion We introduce grammar productions of the form:
/// ```
/// <primary> ::= ... -- all current productions
/// | <staticMemberShorthand>
///
/// <constantPattern> ::= ... -- all current productions
/// | <staticMemberShorthand>
///
/// <staticMemberShorthand> ::=
/// '.' (<identifier> | 'new') -- shorthand qualified name
/// | 'const' '.' (<identifier> | 'new') <arguments> -- shorthand object creation
/// ```
///
/// @description Checks that it is a syntax error to use a `new` keyword in the
/// static access short syntax.
/// @author sgrekhov22@gmail.com
// SharedOptions=--enable-experiment=enum-shorthands

class C {
final String value;
C(this.value);
const C.foo(this.value);
}

extension type ET(int v) {
const ET.foo(this.v);
ET.bar(this.v);
}

main() {
C c1 = new .new("new");
// ^^^
// [analyzer] unspecified
// [cfe] unspecified
C c2 = new .foo("foo");
// ^^^
// [analyzer] unspecified
// [cfe] unspecified

ET et1 = new .new(1);
// ^^^
// [analyzer] unspecified
// [cfe] unspecified
ET et2 = new .foo(2);
// ^^^
// [analyzer] unspecified
// [cfe] unspecified
ET et3 = new .bar(3);
// ^^^
// [analyzer] unspecified
// [cfe] unspecified
}

0 comments on commit 4a925ad

Please sign in to comment.