OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 import 'expect.dart'; |
| 6 |
| 7 const int42 = 40 + 2; |
| 8 const stringAB = 'a' + 'b'; |
| 9 const stringAB2 = 'a' + 'b'; |
| 10 const list123 = const [1, 2, 3]; |
| 11 const mapABC = const {'a' : 'b', 'b': 'c'}; |
| 12 |
| 13 const boxInt42 = const Box(int42); |
| 14 const boxStringAB = const Box(stringAB); |
| 15 |
| 16 class Box { |
| 17 final value; |
| 18 const Box(this.value); |
| 19 } |
| 20 |
| 21 returnPositional([a = const Box('posi' + 'tional')]) => a; |
| 22 |
| 23 returnNamed({a: const Box('nam' + 'ed')}) => a; |
| 24 |
| 25 returnSwitchCasedValue(value) { |
| 26 switch (value) { |
| 27 case const Box(42): return 42; |
| 28 case const Box('abc'): return 'abc'; |
| 29 case const Box(const Box('abc')): return const Box('abc'); |
| 30 default: return 'default'; |
| 31 } |
| 32 } |
| 33 |
| 34 testConstantExpressions() { |
| 35 Expect.isTrue(identical(const Box(40 + 2), const Box(40 + 2))); |
| 36 Expect.isTrue(identical(const Box('a' + 'b'), const Box('ab'))); |
| 37 Expect.isTrue(identical(const Box(const Box(40 + 2)), |
| 38 const Box(const Box(42)))); |
| 39 Expect.isTrue(identical(const Box(const Box('a' + 'b')), |
| 40 const Box(const Box('ab')))); |
| 41 } |
| 42 |
| 43 testConstantFieldValues() { |
| 44 Expect.isTrue(identical(42, int42)); |
| 45 Expect.isTrue(identical(stringAB, stringAB2)); |
| 46 Expect.isTrue(identical(const Box(42), boxInt42)); |
| 47 Expect.isTrue(identical(const Box('ab'), boxStringAB)); |
| 48 } |
| 49 |
| 50 testConstantFunctionParameters() { |
| 51 Expect.isTrue(identical(const Box('positional'), returnPositional())); |
| 52 Expect.isTrue(identical(const Box('named'), returnNamed())); |
| 53 Expect.isTrue(identical(const Box('abc'), |
| 54 returnPositional(const Box('abc')))); |
| 55 Expect.isTrue(identical(const Box('def'), |
| 56 returnNamed(a: const Box('def')))); |
| 57 } |
| 58 |
| 59 testConstantSwitchExpressions() { |
| 60 Expect.isTrue(returnSwitchCasedValue(const Box(42)) == 42); |
| 61 Expect.isTrue(returnSwitchCasedValue(const Box('abc')) == 'abc'); |
| 62 Expect.isTrue(returnSwitchCasedValue(const Box(const Box('abc'))) |
| 63 == const Box('abc')); |
| 64 Expect.isTrue(returnSwitchCasedValue(const Box('go-to-default')) |
| 65 == 'default'); |
| 66 } |
| 67 |
| 68 testConstantLocalVariables() { |
| 69 const a = 'a'; |
| 70 const b = a + 'b'; |
| 71 const c = b + 'c'; |
| 72 const box = const Box(c); |
| 73 Expect.isTrue(identical(const Box('abc'), box)); |
| 74 } |
| 75 |
| 76 testComplextConstLiterals() { |
| 77 Expect.isTrue(identical(const [1, 2, 3], const [1, 2, 3])); |
| 78 Expect.isTrue(identical(const [1, 2, 3], list123)); |
| 79 Expect.isTrue(identical(const {'a': 'b', 'b': 'c'}, const {'a': 'b', 'b': 'c'}
)); |
| 80 Expect.isTrue(identical(const {'a': 'b', 'b': 'c'}, mapABC)); |
| 81 |
| 82 Expect.isTrue(mapABC['a'] == 'b'); |
| 83 Expect.isTrue(mapABC['b'] == 'c'); |
| 84 Expect.isTrue(mapABC.length == 2); |
| 85 |
| 86 Expect.isTrue(list123[0] == 1); |
| 87 Expect.isTrue(list123[1] == 2); |
| 88 Expect.isTrue(list123[2] == 3); |
| 89 Expect.isTrue(list123.length == 3); |
| 90 } |
| 91 |
| 92 main() { |
| 93 testConstantExpressions(); |
| 94 testConstantFieldValues(); |
| 95 testConstantFunctionParameters(); |
| 96 testConstantSwitchExpressions(); |
| 97 testComplextConstLiterals(); |
| 98 testConstantExpressions(); |
| 99 testConstantLocalVariables(); |
| 100 } |
OLD | NEW |