-
Notifications
You must be signed in to change notification settings - Fork 28
/
constant_A02_t03.dart
91 lines (81 loc) · 2.91 KB
/
constant_A02_t03.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright (c) 2022, 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 constantPattern ::= booleanLiteral
/// | nullLiteral
/// | '-'? numericLiteral
/// | stringLiteral
/// | symbolLiteral
/// | identifier
/// | qualifiedName
/// | constObjectExpression
/// | 'const' typeArguments? '[' elements? ']'
/// | 'const' typeArguments? '{' elements? '}'
/// | 'const' '(' expression ')'
///
/// A constant pattern determines if the matched value is equal to the
/// constant's value. We don't allow all expressions here because many
/// expression forms syntactically overlap other kinds of patterns. We avoid
/// ambiguity while supporting terse forms of the most common constant
/// expressions like so:
/// ...
/// Named constants are also allowed because they aren't ambiguous. That
/// includes simple identifiers like someConstant, prefixed constants like
/// some_library.aConstant, static constants on classes like
/// SomeClass.aConstant, and prefixed static constants like
/// some_library.SomeClass.aConstant. Simple identifiers would be ambiguous with
/// variable patterns that aren't marked with var, final, or a type, but
/// unmarked variable patterns are only allowed in irrefutable contexts where
/// constant patterns are prohibited.
///
/// @description Check named constants in constant patterns. Test switch
/// expression
/// @author [email protected]
// SharedOptions=--enable-experiment=patterns
import "../../Utils/expect.dart";
const Zero = 0;
const Pi = 3.14;
const Answer = 42;
const Negative = -1;
const NegativePi = -3.14;
const MaxJSInt = 0x1FFFFFFFFFFFFF;
const Melody = "Lily was here";
const True = true;
const False = false;
String testBool(bool value) {
return switch (value) {
True => "true",
False => "false"
};
}
String testNum(num value) {
return switch (value) {
Zero => "zero",
Pi =>"pi",
Answer => "answer",
Negative => "nagative",
NegativePi => "nagative-pi",
MaxJSInt => "max_int",
_ => "default"
};
}
String testString(String value) {
return switch (value) {
Melody => "Melody",
_ => "default"
};
}
main() {
Expect.equals("true", testBool(true));
Expect.equals("false", testBool(false));
Expect.equals("zero", testNum(0));
Expect.equals("zero", testNum(0.0));
Expect.equals("pi", testNum(3.14));
Expect.equals("answer", testNum(42));
Expect.equals("negative", testNum(-1));
Expect.equals("negative-pi", testNum(-3.14));
Expect.equals("max_int", testNum(9007199254740991));
Expect.equals("default", testNum(1));
Expect.equals("Melody", testString("Lily was here"));
Expect.equals("default", testString(""));
}