Skip to content

Commit

Permalink
#2976. Add more tests for records, factories and parenthesized expres…
Browse files Browse the repository at this point in the history
…sions (#3047)

Add more tests for records, factories and parenthesized expressions
  • Loading branch information
sgrekhov authored Jan 15, 2025
1 parent d26801a commit 4941d31
Show file tree
Hide file tree
Showing 6 changed files with 223 additions and 4 deletions.
32 changes: 32 additions & 0 deletions LanguageFeatures/Static-access-shorthand/grammar_A01_t01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class C {
final String value;
C(this.value);
const C.foo(this.value);
factory C.bar(String v) = C;
const factory C.baz(String v) = C.foo;

static C get staticGetter => C("Static getter");
static C staticMethod() => C("Static method");
Expand All @@ -57,4 +59,34 @@ main() {

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

C c7 = .bar("bar");
Expect.equals("bar", c7.value);

C c8 = .baz("baz");
Expect.equals("baz", c8.value);

C c9 = const .baz("const baz");
Expect.equals("const baz", c9.value);

C c10 = (.staticGetter);
Expect.equals("Static getter", c10.value);

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

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

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

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

C c15 = (.bar("bar"));
Expect.equals("bar", c15.value);

C c16 = (.baz("baz"));
Expect.equals("baz", c16.value);
}
9 changes: 9 additions & 0 deletions LanguageFeatures/Static-access-shorthand/grammar_A01_t02.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,13 @@ main() {

M m3 = .instances[0];
Expect.equals("M: one", m3.value);

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

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

M m6 = (.instances[0]);
Expect.equals("M: one", m3.value);
}
12 changes: 12 additions & 0 deletions LanguageFeatures/Static-access-shorthand/grammar_A01_t03.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,16 @@ main() {

E e3 = .values[1];
Expect.equals(E.v2, e3);

E e4 = (.v1);
Expect.equals(E.v1, e4);

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

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

E e7 = (.values[1]);
Expect.equals(E.v2, e7);
}
40 changes: 36 additions & 4 deletions LanguageFeatures/Static-access-shorthand/grammar_A01_t04.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import '../../Utils/expect.dart';
extension type ET1(int v) {
const ET1.foo(this.v);
ET1.bar(this.v);
const factory ET1.baz(int v) = ET1.foo;

static ET1 get staticGetter => ET1(1);
static ET1 staticMethod() => ET1(2);
Expand Down Expand Up @@ -65,15 +66,46 @@ main() {
ET1 et5 = .bar(5);
Expect.equals(5, et5.v);

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

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

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

ET3 et9 = .new(9);
ET2 et9 = const .new(9);
Expect.equals(9, et9.v);

ET3 et10 = const .qux(10);
Expect.equals(10, et10.v);

ET3 et11 = .new(11);
Expect.equals(11, et11.v);


ET1 et20 = (.instances[0]);
Expect.equals(0, et20.v);

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

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

ET1 et23 = (.new(23));
Expect.equals(23, et23.v);

ET1 et24 = (.bar(24));
Expect.equals(24, et24.v);

ET1 et25 = (.baz(25));
Expect.equals(25, et25.v);

ET2 et26 = (.baz(26));
Expect.equals(26, et26.v);

ET3 et27 = (.new(27));
Expect.equals(27, et27.v);
}
64 changes: 64 additions & 0 deletions LanguageFeatures/Static-access-shorthand/grammar_A01_t07.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright (c) 2025, 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:
/// ```
/// <postfixExpression> ::= ... -- all current productions
/// | <staticMemberShorthand> -- added production
///
/// <constantPattern> ::= ... -- all current productions
/// | <staticMemberShorthandValue> -- No selectors, no `.new`.
///
/// <staticMemberShorthand> ::= <staticMemberShorthandHead> <selector>*
///
/// <staticMemberShorthandHead> ::=
/// <staticMemberShorthandValue>
/// | '.' 'new' -- shorthand unnamed constructor
///
/// <staticMemberShorthandValue> ::= -- something that can potentially create a value.
/// | '.' <identifier> -- shorthand for qualified name
/// | 'const' '.' (<identifier> | 'new') <arguments> -- shorthand for constant object creation
/// ```
///
/// @description Checks that expressions of the form
/// `const '(' '.' id(arguments) ',' ')'` and
/// `const '(' '.' new(arguments) ',' ')'` are records containing constant
/// shorthand expressions.
/// @author sgrekhov22@gmail.com
// SharedOptions=--enable-experiment=enum-shorthands

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

class C {
final String value;
const C(this.value);
const C.id(this.value);
const factory C.f(String value) = C;
}

extension type const ET(String value) {
const ET.id(this.value);
const factory ET.f(String value) = ET;
}

main() {
const (C,) c1 = const (.new("one"),);
Expect.equals("one", c1.$1.value);

const (C,) c2 = const (.id("two"),);
Expect.equals("two", c2.$1.value);

const (C,) c3 = const (.f("three"),);
Expect.equals("three", c3.$1.value);

const (ET,) et1 = const (.new("new"),);
Expect.equals("new", et1.$1.value);

const (ET,) et2 = const (.id("id"),);
Expect.equals("id", et2.$1.value);

const (ET,) et3 = const (.f("f"),);
Expect.equals("f", et3.$1.value);
}
70 changes: 70 additions & 0 deletions LanguageFeatures/Static-access-shorthand/grammar_A01_t08.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright (c) 2025, 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:
/// ```
/// <postfixExpression> ::= ... -- all current productions
/// | <staticMemberShorthand> -- added production
///
/// <constantPattern> ::= ... -- all current productions
/// | <staticMemberShorthandValue> -- No selectors, no `.new`.
///
/// <staticMemberShorthand> ::= <staticMemberShorthandHead> <selector>*
///
/// <staticMemberShorthandHead> ::=
/// <staticMemberShorthandValue>
/// | '.' 'new' -- shorthand unnamed constructor
///
/// <staticMemberShorthandValue> ::= -- something that can potentially create a value.
/// | '.' <identifier> -- shorthand for qualified name
/// | 'const' '.' (<identifier> | 'new') <arguments> -- shorthand for constant object creation
/// ```
///
/// @description Checks that expressions of the form
/// `const '(' '.' id(arguments) ')'` and
/// `const '(' '.' new(arguments) ')'` are still compile-time errors.
/// @author sgrekhov22@gmail.com
// SharedOptions=--enable-experiment=enum-shorthands

class C {
final String value;
const C(this.value);
const C.id(this.value);
const factory C.f(String value) = C;
}

extension type const ET(String value) {
const ET.id(this.value);
const factory ET.f(String value) = ET;
}

main() {
const C c1 = const (.new("one"));
// ^
// [analyzer] unspecified
// [cfe] unspecified
const C c2 = const (.id("two"));
// ^
// [analyzer] unspecified
// [cfe] unspecified
const C c3 = const (.f("three"));
// ^
// [analyzer] unspecified
// [cfe] unspecified

const ET et1 = const (.new("new"));
// ^
// [analyzer] unspecified
// [cfe] unspecified
const ET et2 = const (.id("id"));
// ^
// [analyzer] unspecified
// [cfe] unspecified

const ET et3 = const (.f("f"));
// ^
// [analyzer] unspecified
// [cfe] unspecified
}

0 comments on commit 4941d31

Please sign in to comment.