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 dart2js.constants.expressions.evaluate_test; | 5 library dart2js.constants.expressions.evaluate_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 20 matching lines...) Expand all Loading... |
31 final Map<Map<String, String>, String> expectedValues; | 31 final Map<Map<String, String>, String> expectedValues; |
32 | 32 |
33 const ConstantData(this.code, | 33 const ConstantData(this.code, |
34 this.expectedValues); | 34 this.expectedValues); |
35 } | 35 } |
36 | 36 |
37 class MemoryEnvironment implements Environment { | 37 class MemoryEnvironment implements Environment { |
38 final Compiler compiler; | 38 final Compiler compiler; |
39 final Map<String, String> env; | 39 final Map<String, String> env; |
40 | 40 |
41 MemoryEnvironment(this.compiler, [this.env = const <String, String>{}]); | 41 MemoryEnvironment(this.compiler, |
| 42 [this.env = const <String, String>{}]); |
42 | 43 |
43 @override | 44 @override |
44 String readFromEnvironment(String name) => env[name]; | 45 String readFromEnvironment(String name) => env[name]; |
45 } | 46 } |
46 | 47 |
47 const List<TestData> DATA = const [ | 48 const List<TestData> DATA = const [ |
48 const TestData('', const [ | 49 const TestData('', const [ |
49 const ConstantData('null', const { const {} : 'NullConstant' }), | 50 const ConstantData('null', const { const {} : 'NullConstant' }), |
50 const ConstantData('false', const { const {} : 'BoolConstant(false)' }), | 51 const ConstantData('false', const { const {} : 'BoolConstant(false)' }), |
51 const ConstantData('true', const { const {} : 'BoolConstant(true)' }), | 52 const ConstantData('true', const { const {} : 'BoolConstant(true)' }), |
52 const ConstantData('0', const { const {} : 'IntConstant(0)' }), | 53 const ConstantData('0', const { const {} : 'IntConstant(0)' }), |
53 const ConstantData('0.0', const { const {} : 'DoubleConstant(0.0)' }), | 54 const ConstantData('0.0', const { const {} : 'DoubleConstant(0.0)' }), |
54 const ConstantData('"foo"', const { const {} : 'StringConstant("foo")' }), | 55 const ConstantData('"foo"', const { const {} : 'StringConstant("foo")' }), |
55 const ConstantData('1 + 2', const { const {} : 'IntConstant(3)' }), | 56 const ConstantData('1 + 2', const { const {} : 'IntConstant(3)' }), |
56 const ConstantData('-(1)', const { const {} : 'IntConstant(-1)' }), | 57 const ConstantData('-(1)', const { const {} : 'IntConstant(-1)' }), |
| 58 const ConstantData('"foo".length', const { const {} : 'IntConstant(3)' }), |
57 const ConstantData('identical(0, 1)', | 59 const ConstantData('identical(0, 1)', |
58 const { const {} : 'BoolConstant(false)' }), | 60 const { const {} : 'BoolConstant(false)' }), |
59 const ConstantData('"a" "b"', const { const {} : 'StringConstant("ab")' }), | 61 const ConstantData('"a" "b"', const { const {} : 'StringConstant("ab")' }), |
60 const ConstantData('identical', | 62 const ConstantData('identical', |
61 const { const {} : 'FunctionConstant(identical)' }), | 63 const { const {} : 'FunctionConstant(identical)' }), |
62 const ConstantData('true ? 0 : 1', const { const {} : 'IntConstant(0)' }), | 64 const ConstantData('true ? 0 : 1', const { const {} : 'IntConstant(0)' }), |
63 const ConstantData('proxy', | 65 const ConstantData('proxy', |
64 const { const {} : 'ConstructedConstant(_Proxy())' }), | 66 const { const {} : 'ConstructedConstant(_Proxy())' }), |
65 const ConstantData('Object', const { const {} : 'TypeConstant(Object)' }), | 67 const ConstantData('Object', const { const {} : 'TypeConstant(Object)' }), |
66 const ConstantData('const [0, 1]', | 68 const ConstantData('const [0, 1]', |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 'ConstructedConstant(C(field1=BoolConstant(false),' | 139 'ConstructedConstant(C(field1=BoolConstant(false),' |
138 'field2=IntConstant(87)))', }), | 140 'field2=IntConstant(87)))', }), |
139 ]), | 141 ]), |
140 const TestData(''' | 142 const TestData(''' |
141 class A<T> implements B { | 143 class A<T> implements B { |
142 final field1; | 144 final field1; |
143 const A({this.field1:42}); | 145 const A({this.field1:42}); |
144 } | 146 } |
145 class B<S> implements C { | 147 class B<S> implements C { |
146 const factory B({field1}) = A<B<S>>; | 148 const factory B({field1}) = A<B<S>>; |
147 // TODO(johnniwinther): Enable this when the constructor evaluator doesn't | 149 const factory B.named() = A<S>; |
148 // crash: | |
149 /*const factory B.named() = A<S>;*/ | |
150 } | 150 } |
151 class C<U> { | 151 class C<U> { |
152 const factory C({field1}) = A<B<double>>; | 152 const factory C({field1}) = A<B<double>>; |
153 } | 153 } |
154 ''', const [ | 154 ''', const [ |
155 const ConstantData('const A()', | 155 const ConstantData('const A()', |
156 const { const {} : | 156 const { const {} : |
157 'ConstructedConstant(A<dynamic>(field1=IntConstant(42)))' }), | 157 'ConstructedConstant(A<dynamic>(field1=IntConstant(42)))' }), |
158 const ConstantData('const A<int>(field1: 87)', | 158 const ConstantData('const A<int>(field1: 87)', |
159 const { const {} : | 159 const { const {} : |
160 'ConstructedConstant(A<int>(field1=IntConstant(87)))' }), | 160 'ConstructedConstant(A<int>(field1=IntConstant(87)))' }), |
161 const ConstantData('const B()', | 161 const ConstantData('const B()', |
162 const { const {} : | 162 const { const {} : |
163 'ConstructedConstant(A<B<dynamic>>(field1=IntConstant(42)))' }), | 163 'ConstructedConstant(A<B<dynamic>>(field1=IntConstant(42)))' }), |
164 const ConstantData('const B<int>()', | 164 const ConstantData('const B<int>()', |
165 const { const {} : | 165 const { const {} : |
166 'ConstructedConstant(A<B<int>>(field1=IntConstant(42)))' }), | 166 'ConstructedConstant(A<B<int>>(field1=IntConstant(42)))' }), |
167 const ConstantData('const B<int>(field1: 87)', | 167 const ConstantData('const B<int>(field1: 87)', |
168 const { const {} : | 168 const { const {} : |
169 'ConstructedConstant(A<B<int>>(field1=IntConstant(87)))' }), | 169 'ConstructedConstant(A<B<int>>(field1=IntConstant(87)))' }), |
170 const ConstantData('const C<int>(field1: 87)', | 170 const ConstantData('const C<int>(field1: 87)', |
171 const { const {} : | 171 const { const {} : |
172 'ConstructedConstant(A<B<double>>(field1=IntConstant(87)))' }), | 172 'ConstructedConstant(A<B<double>>(field1=IntConstant(87)))' }), |
173 // TODO(johnniwinther): Enable this when the constructor evaluator doesn't | 173 const ConstantData('const B<int>.named()', |
174 // crash: | |
175 /*const ConstantData('const B<int>.named()', | |
176 const { const {} : | 174 const { const {} : |
177 'ConstructedConstant(A<int>(field1=IntConstant(42)))' }),*/ | 175 'ConstructedConstant(A<int>(field1=IntConstant(42)))' }), |
178 ]), | 176 ]), |
179 const TestData(''' | 177 const TestData(''' |
180 const c = const int.fromEnvironment("foo", defaultValue: 5); | 178 const c = const int.fromEnvironment("foo", defaultValue: 5); |
181 const d = const int.fromEnvironment("bar", defaultValue: 10); | 179 const d = const int.fromEnvironment("bar", defaultValue: 10); |
182 | 180 |
183 class A { | 181 class A { |
184 final field; | 182 final field; |
185 const A(a, b) : field = a + b; | 183 const A(a, b) : field = a + b; |
186 } | 184 } |
187 | 185 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 ConstantValue value = | 228 ConstantValue value = |
231 constant.evaluate(environment, DART_CONSTANT_SYSTEM); | 229 constant.evaluate(environment, DART_CONSTANT_SYSTEM); |
232 String valueText = value.toStructuredString(); | 230 String valueText = value.toStructuredString(); |
233 Expect.equals(expectedText, valueText, | 231 Expect.equals(expectedText, valueText, |
234 "Unexpected value '${valueText}' for contant " | 232 "Unexpected value '${valueText}' for contant " |
235 "`${constant.getText()}`, expected '${expectedText}'."); | 233 "`${constant.getText()}`, expected '${expectedText}'."); |
236 }); | 234 }); |
237 }); | 235 }); |
238 }); | 236 }); |
239 } | 237 } |
OLD | NEW |