OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library constant_expression_test; | 5 library constant_expression_test; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'package:async_helper/async_helper.dart'; | 8 import 'package:async_helper/async_helper.dart'; |
9 import 'package:expect/expect.dart'; | 9 import 'package:expect/expect.dart'; |
10 import 'package:compiler/src/constants/expressions.dart'; | 10 import 'package:compiler/src/constants/expressions.dart'; |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 const ConstantData( | 73 const ConstantData( |
74 'const <int, int>{0: 1, 2: 3}', ConstantExpressionKind.MAP), | 74 'const <int, int>{0: 1, 2: 3}', ConstantExpressionKind.MAP), |
75 const ConstantData('const bool.fromEnvironment("foo", defaultValue: false)', | 75 const ConstantData('const bool.fromEnvironment("foo", defaultValue: false)', |
76 ConstantExpressionKind.BOOL_FROM_ENVIRONMENT), | 76 ConstantExpressionKind.BOOL_FROM_ENVIRONMENT), |
77 const ConstantData('const int.fromEnvironment("foo", defaultValue: 42)', | 77 const ConstantData('const int.fromEnvironment("foo", defaultValue: 42)', |
78 ConstantExpressionKind.INT_FROM_ENVIRONMENT), | 78 ConstantExpressionKind.INT_FROM_ENVIRONMENT), |
79 const ConstantData( | 79 const ConstantData( |
80 'const String.fromEnvironment("foo", defaultValue: "bar")', | 80 'const String.fromEnvironment("foo", defaultValue: "bar")', |
81 ConstantExpressionKind.STRING_FROM_ENVIRONMENT), | 81 ConstantExpressionKind.STRING_FROM_ENVIRONMENT), |
82 ]), | 82 ]), |
83 const TestData( | 83 const TestData(''' |
84 ''' | |
85 class A { | 84 class A { |
86 const A(); | 85 const A(); |
87 } | 86 } |
88 class B { | 87 class B { |
89 final field1; | 88 final field1; |
90 const B(this.field1); | 89 const B(this.field1); |
91 } | 90 } |
92 class C extends B { | 91 class C extends B { |
93 final field2; | 92 final field2; |
94 const C({field1: 42, this.field2: false}) : super(field1); | 93 const C({field1: 42, this.field2: false}) : super(field1); |
95 const C.named([field = false]) : this(field1: field, field2: field); | 94 const C.named([field = false]) : this(field1: field, field2: field); |
96 } | 95 } |
97 ''', | 96 ''', const [ |
98 const [ | 97 const ConstantData('const Object()', ConstantExpressionKind.CONSTRUCTED, |
99 const ConstantData('const Object()', ConstantExpressionKind.CONSTRUCTED, | 98 type: 'Object', fields: const {}), |
100 type: 'Object', fields: const {}), | 99 const ConstantData('const A()', ConstantExpressionKind.CONSTRUCTED, |
101 const ConstantData('const A()', ConstantExpressionKind.CONSTRUCTED, | 100 type: 'A', fields: const {}), |
102 type: 'A', fields: const {}), | 101 const ConstantData('const B(0)', ConstantExpressionKind.CONSTRUCTED, |
103 const ConstantData('const B(0)', ConstantExpressionKind.CONSTRUCTED, | 102 type: 'B', fields: const {'field(B#field1)': '0'}), |
104 type: 'B', fields: const {'field(B#field1)': '0'}), | 103 const ConstantData('const B(const A())', ConstantExpressionKind.CONSTRUCTED, |
105 const ConstantData( | 104 type: 'B', fields: const {'field(B#field1)': 'const A()'}), |
106 'const B(const A())', ConstantExpressionKind.CONSTRUCTED, | 105 const ConstantData('const C()', ConstantExpressionKind.CONSTRUCTED, |
107 type: 'B', fields: const {'field(B#field1)': 'const A()'}), | 106 type: 'C', |
108 const ConstantData('const C()', ConstantExpressionKind.CONSTRUCTED, | 107 fields: const { |
109 type: 'C', | 108 'field(B#field1)': '42', |
110 fields: const { | 109 'field(C#field2)': 'false', |
111 'field(B#field1)': '42', | 110 }), |
112 'field(C#field2)': 'false', | 111 const ConstantData( |
113 }), | 112 'const C(field1: 87)', ConstantExpressionKind.CONSTRUCTED, |
114 const ConstantData( | 113 type: 'C', |
115 'const C(field1: 87)', ConstantExpressionKind.CONSTRUCTED, | 114 fields: const { |
116 type: 'C', | 115 'field(B#field1)': '87', |
117 fields: const { | 116 'field(C#field2)': 'false', |
118 'field(B#field1)': '87', | 117 }), |
119 'field(C#field2)': 'false', | 118 const ConstantData( |
120 }), | 119 'const C(field2: true)', ConstantExpressionKind.CONSTRUCTED, |
121 const ConstantData( | 120 type: 'C', |
122 'const C(field2: true)', ConstantExpressionKind.CONSTRUCTED, | 121 fields: const { |
123 type: 'C', | 122 'field(B#field1)': '42', |
124 fields: const { | 123 'field(C#field2)': 'true', |
125 'field(B#field1)': '42', | 124 }), |
126 'field(C#field2)': 'true', | 125 const ConstantData('const C.named()', ConstantExpressionKind.CONSTRUCTED, |
127 }), | 126 type: 'C', |
128 const ConstantData( | 127 fields: const { |
129 'const C.named()', ConstantExpressionKind.CONSTRUCTED, | 128 'field(B#field1)': 'false', |
130 type: 'C', | 129 'field(C#field2)': 'false', |
131 fields: const { | 130 }), |
132 'field(B#field1)': 'false', | 131 const ConstantData('const C.named(87)', ConstantExpressionKind.CONSTRUCTED, |
133 'field(C#field2)': 'false', | 132 type: 'C', |
134 }), | 133 fields: const { |
135 const ConstantData( | 134 'field(B#field1)': '87', |
136 'const C.named(87)', ConstantExpressionKind.CONSTRUCTED, | 135 'field(C#field2)': '87', |
137 type: 'C', | 136 }), |
138 fields: const { | 137 ]), |
139 'field(B#field1)': '87', | 138 const TestData(''' |
140 'field(C#field2)': '87', | |
141 }), | |
142 ]), | |
143 const TestData( | |
144 ''' | |
145 class A<T> implements B { | 139 class A<T> implements B { |
146 final field1; | 140 final field1; |
147 const A({this.field1:42}); | 141 const A({this.field1:42}); |
148 } | 142 } |
149 class B<S> implements C { | 143 class B<S> implements C { |
150 const factory B({field1}) = A<B<S>>; | 144 const factory B({field1}) = A<B<S>>; |
151 const factory B.named() = A<S>; | 145 const factory B.named() = A<S>; |
152 } | 146 } |
153 class C<U> { | 147 class C<U> { |
154 const factory C({field1}) = A<B<double>>; | 148 const factory C({field1}) = A<B<double>>; |
155 } | 149 } |
156 ''', | 150 ''', const [ |
157 const [ | 151 const ConstantData('const A()', ConstantExpressionKind.CONSTRUCTED, |
158 const ConstantData('const A()', ConstantExpressionKind.CONSTRUCTED, | 152 type: 'A<dynamic>', fields: const {'field(A#field1)': '42'}), |
159 type: 'A<dynamic>', fields: const {'field(A#field1)': '42'}), | 153 const ConstantData( |
160 const ConstantData( | 154 'const A<int>(field1: 87)', ConstantExpressionKind.CONSTRUCTED, |
161 'const A<int>(field1: 87)', ConstantExpressionKind.CONSTRUCTED, | 155 type: 'A<int>', fields: const {'field(A#field1)': '87'}), |
162 type: 'A<int>', fields: const {'field(A#field1)': '87'}), | 156 const ConstantData('const B()', ConstantExpressionKind.CONSTRUCTED, |
163 const ConstantData('const B()', ConstantExpressionKind.CONSTRUCTED, | 157 type: 'A<B<dynamic>>', |
164 type: 'A<B<dynamic>>', | 158 fields: const { |
165 fields: const { | 159 'field(A#field1)': '42', |
166 'field(A#field1)': '42', | 160 }), |
167 }), | 161 const ConstantData('const B<int>()', ConstantExpressionKind.CONSTRUCTED, |
168 const ConstantData('const B<int>()', ConstantExpressionKind.CONSTRUCTED, | 162 type: 'A<B<int>>', |
169 type: 'A<B<int>>', | 163 fields: const { |
170 fields: const { | 164 'field(A#field1)': '42', |
171 'field(A#field1)': '42', | 165 }), |
172 }), | 166 const ConstantData( |
173 const ConstantData( | 167 'const B<int>(field1: 87)', ConstantExpressionKind.CONSTRUCTED, |
174 'const B<int>(field1: 87)', ConstantExpressionKind.CONSTRUCTED, | 168 type: 'A<B<int>>', |
175 type: 'A<B<int>>', | 169 fields: const { |
176 fields: const { | 170 'field(A#field1)': '87', |
177 'field(A#field1)': '87', | 171 }), |
178 }), | 172 const ConstantData( |
179 const ConstantData( | 173 'const C<int>(field1: 87)', ConstantExpressionKind.CONSTRUCTED, |
180 'const C<int>(field1: 87)', ConstantExpressionKind.CONSTRUCTED, | 174 type: 'A<B<double>>', |
181 type: 'A<B<double>>', | 175 fields: const { |
182 fields: const { | 176 'field(A#field1)': '87', |
183 'field(A#field1)': '87', | 177 }), |
184 }), | 178 const ConstantData( |
185 const ConstantData( | 179 'const B<int>.named()', ConstantExpressionKind.CONSTRUCTED, |
186 'const B<int>.named()', ConstantExpressionKind.CONSTRUCTED, | 180 type: 'A<int>', |
187 type: 'A<int>', | 181 fields: const { |
188 fields: const { | 182 'field(A#field1)': '42', |
189 'field(A#field1)': '42', | 183 }), |
190 }), | 184 ]), |
191 ]), | |
192 ]; | 185 ]; |
193 | 186 |
194 main() { | 187 main() { |
195 asyncTest(() => Future.forEach(DATA, testData)); | 188 asyncTest(() => Future.forEach(DATA, testData)); |
196 } | 189 } |
197 | 190 |
198 Future testData(TestData data) async { | 191 Future testData(TestData data) async { |
199 StringBuffer sb = new StringBuffer(); | 192 StringBuffer sb = new StringBuffer(); |
200 sb.write('${data.declarations}\n'); | 193 sb.write('${data.declarations}\n'); |
201 Map constants = {}; | 194 Map constants = {}; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
246 String expected = data.fields[name]; | 239 String expected = data.fields[name]; |
247 Expect.equals( | 240 Expect.equals( |
248 expected, | 241 expected, |
249 expression, | 242 expression, |
250 "Unexpected field expression ${expression} for field '$name' in " | 243 "Unexpected field expression ${expression} for field '$name' in " |
251 "constant `${constant.toDartText()}`, expected '${expected}'."); | 244 "constant `${constant.toDartText()}`, expected '${expected}'."); |
252 }); | 245 }); |
253 } | 246 } |
254 }); | 247 }); |
255 } | 248 } |
OLD | NEW |