Skip to content

Commit

Permalink
dart-lang#2976. Add more grammar, semantics and type inference test (d…
Browse files Browse the repository at this point in the history
…art-lang#3002)

Add more grammar, semantics and type inference test
  • Loading branch information
sgrekhov authored Nov 29, 2024
1 parent d50a305 commit 2d721bd
Show file tree
Hide file tree
Showing 14 changed files with 767 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ extension type ET1(int v) {

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

extension type ET2.baz(int v) {
Expand Down
102 changes: 102 additions & 0 deletions LanguageFeatures/Static-access-shorthand/grammar_A01_t05.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// 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:
/// ```
/// <postfixExpression> ::= ... -- all current productions
/// | <staticMemberShorthand>
///
/// <constantPattern> ::= ... -- all current productions
/// | <staticMemberShorthand>
///
/// <staticMemberShorthand> ::= <staticMemberShorthandHead> <selector*>
///
/// <staticMemberShorthandHead> ::=
/// '.' (<identifier> | 'new') -- shorthand qualified name
/// | 'const' '.' (<identifier> | 'new') <arguments> -- shorthand object creation
/// ```
///
/// @description Checks that static members of a class, mixin, enum or extension
/// type can be accessed using the static access short syntax of the form
/// `await <staticMemberShorthand>`.
/// @author [email protected]
// SharedOptions=--enable-experiment=enum-shorthands

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

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

static FutureOr<C> get staticGetter => C("C: static getter");
static FutureOr<C> staticMethod() => C("C; static method");
static FutureOr<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;

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

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

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

extension type ET(int v) {
static FutureOr<ET> get staticGetter => ET(1);
static FutureOr<ET> staticMethod() => ET(2);
static FutureOr<List<ET>> instances = [ET(0), ET(1)];
}

main() async {
C c1 = await .staticGetter;
Expect.equals("C: static getter", c1.value);

C c2 = await .staticMethod();
Expect.equals("C: static method", c2.value);

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

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

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

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

E e0 = await .v1;
Expect.equals(E.v1, e0);

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

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

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

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

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

ET et3 = await .instances[0];
Expect.equals(0, et3.v);
}
22 changes: 11 additions & 11 deletions LanguageFeatures/Static-access-shorthand/semantics_A01_t01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ typedef CAlias<T> = C<T>;
typedef CInt = C<int>;

mixin M<T> on C<T> {
static M<int> get id1 => MC(1);
static M<int> get id1 => MC(2);
static M<X> id2<X>(X x) => MC<X>(x);
}

Expand All @@ -51,7 +51,7 @@ typedef MAlias<T> = M<T>;
typedef MInt = M<int>;

enum E<T> {
e1(1), e2("2");
e1(3), e2("3");
final T t;
const E(this.t);

Expand All @@ -63,7 +63,7 @@ typedef EAlias<T> = E<T>;
typedef EInt = E<int>;

extension type ET<T>(T t) {
static ET<int> get id1 => ET(1);
static ET<int> get id1 => ET(4);
static ET<X> id2<X>(X x) => ET<X>(x);
}

Expand All @@ -74,24 +74,24 @@ main() {
CAlias<int> c1 = .id1;
Expect.equals(1, c1.t);

CInt c2 = .id2<int>(2);
Expect.equals(2, c2.t);
CInt c2 = .id2<int>(1);
Expect.equals(1, c2.t);

MAlias<int> m1 = .id1;
Expect.equals(1, m1.t);
Expect.equals(2, m1.t);

MInt m2 = .id2<int>(2);
Expect.equals(2, m2.t);

EInt e1 = .id1;
Expect.equals(1, e1.t);
Expect.equals(3, e1.t);

EAlias<String> e2 = .id2();
Expect.equals("2", e2.t);
Expect.equals("3", e2.t);

ETAlias<int> et1 = .id1;
Expect.equals(1, et1.t);
Expect.equals(4, et1.t);

ETInt et2 = .id2<int>(2);
Expect.equals(2, et2.t);
ETInt et2 = .id2<int>(4);
Expect.equals(4, et2.t);
}
16 changes: 8 additions & 8 deletions LanguageFeatures/Static-access-shorthand/semantics_A01_t02.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,24 @@ main() {
CAlias<int> c1 = .id1;
Expect.equals(1, c1.t);

CInt c2 = .id2<int>(2);
Expect.equals(2, c2.t);
CInt c2 = .id2<int>(1);
Expect.equals(1, c2.t);

MAlias<int> m1 = .id1;
Expect.equals(1, m1.t);
Expect.equals(2, m1.t);

MInt m2 = .id2<int>(2);
Expect.equals(2, m2.t);

EInt e1 = .id1;
Expect.equals(1, e1.t);
Expect.equals(3, e1.t);

EAlias<String> e2 = .id2();
Expect.equals("2", e2.t);
Expect.equals("3", e2.t);

ETAlias<int> et1 = .id1;
Expect.equals(1, et1.t);
Expect.equals(4, et1.t);

ETInt et2 = .id2<int>(2);
Expect.equals(2, et2.t);
ETInt et2 = .id2<int>(4);
Expect.equals(4, et2.t);
}
16 changes: 8 additions & 8 deletions LanguageFeatures/Static-access-shorthand/semantics_A01_t03.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,24 @@ main() {
p.CAlias<int> c1 = .id1;
Expect.equals(1, c1.t);

p.CInt c2 = .id2<int>(2);
Expect.equals(2, c2.t);
p.CInt c2 = .id2<int>(1);
Expect.equals(1, c2.t);

p.MAlias<int> m1 = .id1;
Expect.equals(1, m1.t);
Expect.equals(2, m1.t);

p.MInt m2 = .id2<int>(2);
Expect.equals(2, m2.t);

p.EInt e1 = .id1;
Expect.equals(1, e1.t);
Expect.equals(3, e1.t);

p.EAlias<String> e2 = .id2();
Expect.equals("2", e2.t);
Expect.equals("3", e2.t);

p.ETAlias<int> et1 = .id1;
Expect.equals(1, et1.t);
Expect.equals(4, et1.t);

p.ETInt et2 = .id2<int>(2);
Expect.equals(2, et2.t);
p.ETInt et2 = .id2<int>(4);
Expect.equals(4, et2.t);
}
Loading

0 comments on commit 2d721bd

Please sign in to comment.