Skip to content

Commit

Permalink
dart-lang#2976. Add patterns tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrekhov committed Dec 30, 2024
1 parent e07a25b commit d72d207
Show file tree
Hide file tree
Showing 15 changed files with 1,472 additions and 0 deletions.
133 changes: 133 additions & 0 deletions LanguageFeatures/Static-access-shorthand/patterns_A01_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
// 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 A constant pattern `<staticMemberShorthandValue>` is treated the
/// same as that static member shorthand as an expression that has no following
/// selectors, except with the matched value type is set as the shorthand
/// context of the `<staticMemberShorthandHead>`.
///
/// The restriction to `<staticMemberShorthandValue>` is intended to match the
/// existing allowed constant patterns, `<qualifiedIdentifier>` and
/// `<constObjectExpression>`, and nothing more, which is why it omits the
/// `.new` which is guaranteed to be a constructor tear-off. The shorthand
/// constant pattern `'.' <identifier>` must satisfy the same restrictions as
/// the `<qualifiedIdentifier>` constant pattern, mainly that it must denote a
/// constant getter.
///
/// @description Checks that for a constant pattern
/// `<staticMemberShorthandValue>` the matched value type is set as the
/// shorthand context of the `<staticMemberShorthandHead>`
/// @author [email protected]
// SharedOptions=--enable-experiment=enum-shorthands

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

class C {
final int value;
const C(this.value);

static const C one = C(1);

@override
bool operator ==(Object other) {
if (other is C) {
return other.value == value;
}
return false;
}
}

mixin M on C {
static const M one = MC(1);
}

class MC extends C with M {
const MC(super.value);
}

enum E {
e1,
e2;

static const E one = E.e1;
}

extension type const ET(int v) implements int {
static const ET one = ET(1);
}

main() {
String res = "";
switch (C(1)) {
case .one:
res = "C one";
default:
res = "default";
}
Expect.equals("C one", res);

res = switch (C(1)) {
.one => "C one",
_ => "default"
};
Expect.equals("C one", res);

M m = MC(1);
switch (m) {
case .one:
res = "M one";
default:
res = "default";
}
Expect.equals("M one", res);

res = switch (m) {
.one => "M one",
_ => "default"
};
Expect.equals("M one", res);

switch (E.e1) {
case .one:
res = "E one";
default:
res = "default";
}
Expect.equals("E one", res);

res = switch (E.e1) {
.one => "E one",
_ => "default"
};
Expect.equals("E one", res);

switch (E.e1) {
case .e1:
res = "E one";
default:
res = "default";
}
Expect.equals("E one", res);

res = switch (E.e1) {
.e1 => "E one",
_ => "default"
};
Expect.equals("E one", res);

switch (ET(1)) {
case .one:
res = "ET one";
default:
res = "default";
}
Expect.equals("ET one", res);

res = switch (ET(1)) {
.one => "ET one",
_ => "default"
};
Expect.equals("ET one", res);
}
134 changes: 134 additions & 0 deletions LanguageFeatures/Static-access-shorthand/patterns_A01_t02.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// 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 A constant pattern `<staticMemberShorthandValue>` is treated the
/// same as that static member shorthand as an expression that has no following
/// selectors, except with the matched value type is set as the shorthand
/// context of the `<staticMemberShorthandHead>`.
///
/// The restriction to `<staticMemberShorthandValue>` is intended to match the
/// existing allowed constant patterns, `<qualifiedIdentifier>` and
/// `<constObjectExpression>`, and nothing more, which is why it omits the
/// `.new` which is guaranteed to be a constructor tear-off. The shorthand
/// constant pattern `'.' <identifier>` must satisfy the same restrictions as
/// the `<qualifiedIdentifier>` constant pattern, mainly that it must denote a
/// constant getter.
///
/// @description Checks that for a constant pattern
/// `<staticMemberShorthandValue>` the matched value type is set as the
/// shorthand context of the `<staticMemberShorthandHead>`. Test that `!`
/// selector is allowed.
/// @author [email protected]
// SharedOptions=--enable-experiment=enum-shorthands

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

class C {
final int value;
const C(this.value);

static const C? one = C(1);

@override
bool operator ==(Object other) {
if (other is C) {
return other.value == value;
}
return false;
}
}

mixin M on C {
static const M? one = MC(1);
}

class MC extends C with M {
const MC(super.value);
}

enum E {
e1,
e2;

static const E? one = E.e1;
}

extension type const ET(int v) implements int {
static const ET? one = ET(1);
}

main() {
String res = "";
switch (C(1)) {
case .one!: // ignore: unnecessary_null_assert_pattern
res = "C one";
default:
res = "default";
}
Expect.equals("C one", res);

res = switch (C(1)) {
.one! => "C one", // ignore: unnecessary_null_assert_pattern
_ => "default"
};
Expect.equals("C one", res);

M m = MC(1);
switch (m) {
case .one!: // ignore: unnecessary_null_assert_pattern
res = "M one";
default:
res = "default";
}
Expect.equals("M one", res);

res = switch (m) {
.one! => "M one", // ignore: unnecessary_null_assert_pattern
_ => "default"
};
Expect.equals("M one", res);

switch (E.e1) {
case .one!: // ignore: unnecessary_null_assert_pattern
res = "E one";
default:
res = "default";
}
Expect.equals("E one", res);

res = switch (E.e1) {
.one! => "E one", // ignore: unnecessary_null_assert_pattern
_ => "default"
};
Expect.equals("E one", res);

switch (E.e1) {
case .e1!: // ignore: unnecessary_null_assert_pattern
res = "E one";
default:
res = "default";
}
Expect.equals("E one", res);

res = switch (E.e1) {
.e1! => "E one", // ignore: unnecessary_null_assert_pattern
_ => "default"
};
Expect.equals("E one", res);

switch (ET(1)) {
case .one!: // ignore: unnecessary_null_assert_pattern
res = "ET one";
default:
res = "default";
}
Expect.equals("ET one", res);

res = switch (ET(1)) {
.one! => "ET one", // ignore: unnecessary_null_assert_pattern
_ => "default"
};
Expect.equals("ET one", res);
}
57 changes: 57 additions & 0 deletions LanguageFeatures/Static-access-shorthand/patterns_A01_t03.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 A constant pattern `<staticMemberShorthandValue>` is treated the
/// same as that static member shorthand as an expression that has no following
/// selectors, except with the matched value type is set as the shorthand
/// context of the `<staticMemberShorthandHead>`.
///
/// The restriction to `<staticMemberShorthandValue>` is intended to match the
/// existing allowed constant patterns, `<qualifiedIdentifier>` and
/// `<constObjectExpression>`, and nothing more, which is why it omits the
/// `.new` which is guaranteed to be a constructor tear-off. The shorthand
/// constant pattern `'.' <identifier>` must satisfy the same restrictions as
/// the `<qualifiedIdentifier>` constant pattern, mainly that it must denote a
/// constant getter.
///
/// @description Checks that it is a compile-time error if for a constant
/// pattern the matched value type has no appropriate member.
/// @author [email protected]
// SharedOptions=--enable-experiment=enum-shorthands

class C {
final int value;
const C(this.value);

static const C one = C(1);
}

mixin M on C {
static const M one = MC(1);
}

class MC extends C with M {
const MC(super.value);
}

main() {
String res = "";

switch (MC(1)) {
case .one:
// ^
// [analyzer] unspecified
// [cfe] unspecified
default:
}

String res = switch (MC(1)) {
.one => "M one",
// ^
// [analyzer] unspecified
// [cfe] unspecified
_ => "default"
};
}
Loading

0 comments on commit d72d207

Please sign in to comment.