OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, 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 // Check that ?? is compile-time constant. |
| 6 |
| 7 import "package:expect/expect.dart"; |
| 8 |
| 9 const theNull = null; |
| 10 const notNull = const Object(); |
| 11 |
| 12 // Top-level const field initializer. |
| 13 const test1 = theNull ?? notNull; |
| 14 const test2 = notNull ?? theNull; |
| 15 const test3 = theNull ?? theNull ?? notNull; |
| 16 const test4 = theNull ?? theNull ?? theNull; |
| 17 |
| 18 class P { |
| 19 final v; |
| 20 const P(this.v); |
| 21 } |
| 22 |
| 23 // Annotation parameter (not checked by test!) |
| 24 @P(theNull ?? notNull) |
| 25 @P(notNull ?? theNull) |
| 26 @P(theNull ?? theNull ?? notNull) |
| 27 @P(theNull ?? theNull ?? theNull) |
| 28 class C { |
| 29 // Static const field initializer. |
| 30 static const test5 = theNull ?? notNull; |
| 31 static const test6 = notNull ?? theNull; |
| 32 static const test7 = theNull ?? theNull ?? notNull; |
| 33 static const test8 = theNull ?? theNull ?? theNull; |
| 34 |
| 35 // (Constructor) parameter defaults. |
| 36 final test9; |
| 37 final test10; |
| 38 final test11; |
| 39 final test12; |
| 40 |
| 41 // Const constructor initializer list. |
| 42 final test13; |
| 43 final test14; |
| 44 final test15; |
| 45 final test16; |
| 46 final test17; |
| 47 |
| 48 const C(x, |
| 49 [this.test9 = theNull ?? notNull, |
| 50 this.test10 = notNull ?? theNull, |
| 51 this.test11 = theNull ?? theNull ?? notNull, |
| 52 this.test12 = theNull ?? theNull ?? theNull]) |
| 53 : test13 = theNull ?? x, |
| 54 test14 = notNull ?? x, |
| 55 test15 = x ?? notNull, |
| 56 test16 = theNull ?? theNull ?? x, |
| 57 test17 = theNull ?? x ?? notNull; |
| 58 |
| 59 List methodLocal() { |
| 60 // Method local const variable initializer. |
| 61 const test18 = theNull ?? notNull; |
| 62 const test19 = notNull ?? theNull; |
| 63 const test20 = theNull ?? theNull ?? notNull; |
| 64 const test21 = theNull ?? theNull ?? theNull; |
| 65 |
| 66 return const [test18, test19, test20, test21]; |
| 67 } |
| 68 |
| 69 List expressionLocal() { |
| 70 // Constant expression sub-expression. |
| 71 return const [theNull ?? notNull, |
| 72 notNull ?? theNull, |
| 73 theNull ?? theNull ?? notNull, |
| 74 theNull ?? theNull ?? theNull]; |
| 75 } |
| 76 } |
| 77 |
| 78 main() { |
| 79 Expect.identical(notNull, test1, "test1"); |
| 80 Expect.identical(notNull, test2, "test2"); |
| 81 Expect.identical(notNull, test3, "test3"); |
| 82 Expect.identical(theNull, test4, "test4"); |
| 83 |
| 84 Expect.identical(notNull, C.test5, "test5"); |
| 85 Expect.identical(notNull, C.test6, "test6"); |
| 86 Expect.identical(notNull, C.test7, "test7"); |
| 87 Expect.identical(theNull, C.test8, "test8"); |
| 88 |
| 89 const c1 = const C(null); |
| 90 Expect.identical(notNull, c1.test9, "test9"); |
| 91 Expect.identical(notNull, c1.test10, "test10"); |
| 92 Expect.identical(notNull, c1.test11, "test11"); |
| 93 Expect.identical(theNull, c1.test12, "test12"); |
| 94 |
| 95 Expect.identical(theNull, c1.test13, "test13"); |
| 96 Expect.identical(notNull, c1.test14, "test14"); |
| 97 Expect.identical(notNull, c1.test15, "test15"); |
| 98 Expect.identical(theNull, c1.test16, "test16"); |
| 99 Expect.identical(notNull, c1.test17, "test17"); |
| 100 |
| 101 var list = c1.methodLocal(); |
| 102 Expect.identical(notNull, list[0], "test18"); |
| 103 Expect.identical(notNull, list[1], "test19"); |
| 104 Expect.identical(notNull, list[2], "test20"); |
| 105 Expect.identical(theNull, list[3], "test21"); |
| 106 |
| 107 list = c1.expressionLocal(); |
| 108 Expect.identical(notNull, list[0], "test22"); |
| 109 Expect.identical(notNull, list[1], "test23"); |
| 110 Expect.identical(notNull, list[2], "test24"); |
| 111 Expect.identical(theNull, list[3], "test25"); |
| 112 |
| 113 const c2 = const C(42); |
| 114 Expect.identical(notNull, c2.test9, "test26"); |
| 115 Expect.identical(notNull, c2.test10, "test27"); |
| 116 Expect.identical(notNull, c2.test11, "test28"); |
| 117 Expect.identical(theNull, c2.test12, "test29"); |
| 118 |
| 119 Expect.identical(42 , c2.test13, "test30"); |
| 120 Expect.identical(notNull, c2.test14, "test31"); |
| 121 Expect.identical(42 , c2.test15, "test32"); |
| 122 Expect.identical(42 , c2.test16, "test33"); |
| 123 Expect.identical(42 , c2.test17, "test34"); |
| 124 |
| 125 list = c2.methodLocal(); |
| 126 Expect.identical(notNull, list[0], "test35"); |
| 127 Expect.identical(notNull, list[1], "test36"); |
| 128 Expect.identical(notNull, list[2], "test37"); |
| 129 Expect.identical(theNull, list[3], "test38"); |
| 130 |
| 131 list = c2.expressionLocal(); |
| 132 Expect.identical(notNull, list[0], "test39"); |
| 133 Expect.identical(notNull, list[1], "test40"); |
| 134 Expect.identical(notNull, list[2], "test41"); |
| 135 Expect.identical(theNull, list[3], "test42"); |
| 136 } |
OLD | NEW |