Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#2976. Add patterns tests #3031

Merged
merged 2 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
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"
};
}
88 changes: 88 additions & 0 deletions LanguageFeatures/Static-access-shorthand/patterns_A02_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// 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 If a static member shorthand expression occurs elsewhere in a
/// pattern where a constant expression is generally allowed, like
/// `const (big ? .big : .little)` or `< .one`, except for the relational
/// pattern `== e`, it's treated as a normal constant expression, using the
/// context type it's given. The expression of `const (...)` will have the
/// matched value type as context type. The relational pattern expressions,
/// other than for `==` and `!=`, will have the parameter type of the
/// corresponding operator of the matched value type as context type.
///
/// @description Checks that if a static member shorthand expression occurs in a
/// logical-or pattern it's treated as a normal constant expression, using the
/// context type it's given.
/// @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);
static const C two = C(2);

@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);
static const M two = MC(2);
}

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

enum E {
e1,
e2;

static const E one = E.e1;
static const E two = E.e2;
}

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

main() {
String res = "";
if (C(1) case .one || .two) {
res = "C one or two";
}
Expect.equals("C one or two", res);

M m = MC(2);
if (m case .one || .two) {
res = "M one or two";
}
Expect.equals("M one or two", res);

if (E.e1 case .one || .two) {
res = "E one or two";
}
Expect.equals("E one or two", res);

if (E.e2 case .e1 || .e2) {
res = "E one or two again";
}
Expect.equals("E one or two again", res);

if (ET(1) case .one || .two) {
res = "ET one or two";
}
Expect.equals("ET one or two", res);
}
Loading
Loading