| 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 @deprecated |
| 6 library analyzer.test.constant_test; |
| 7 |
| 8 import 'package:analyzer/dart/ast/ast.dart'; |
| 9 import 'package:analyzer/dart/element/element.dart'; |
| 10 import 'package:analyzer/dart/element/type.dart'; |
| 11 import 'package:analyzer/src/generated/constant.dart'; |
| 12 import 'package:analyzer/src/generated/source.dart'; |
| 13 import 'package:analyzer/src/generated/source_io.dart'; |
| 14 import 'package:test_reflective_loader/test_reflective_loader.dart'; |
| 15 import 'package:unittest/unittest.dart'; |
| 16 |
| 17 import '../utils.dart'; |
| 18 import 'resolver_test_case.dart'; |
| 19 import 'test_support.dart'; |
| 20 |
| 21 main() { |
| 22 initializeTestEnvironment(); |
| 23 defineReflectiveTests(ConstantEvaluatorTest); |
| 24 } |
| 25 |
| 26 @reflectiveTest |
| 27 class ConstantEvaluatorTest extends ResolverTestCase { |
| 28 void fail_constructor() { |
| 29 EvaluationResult result = _getExpressionValue("?"); |
| 30 expect(result.isValid, isTrue); |
| 31 DartObject value = result.value; |
| 32 expect(value, null); |
| 33 } |
| 34 |
| 35 void fail_identifier_class() { |
| 36 EvaluationResult result = _getExpressionValue("?"); |
| 37 expect(result.isValid, isTrue); |
| 38 DartObject value = result.value; |
| 39 expect(value, null); |
| 40 } |
| 41 |
| 42 void fail_identifier_function() { |
| 43 EvaluationResult result = _getExpressionValue("?"); |
| 44 expect(result.isValid, isTrue); |
| 45 DartObject value = result.value; |
| 46 expect(value, null); |
| 47 } |
| 48 |
| 49 void fail_identifier_static() { |
| 50 EvaluationResult result = _getExpressionValue("?"); |
| 51 expect(result.isValid, isTrue); |
| 52 DartObject value = result.value; |
| 53 expect(value, null); |
| 54 } |
| 55 |
| 56 void fail_identifier_staticMethod() { |
| 57 EvaluationResult result = _getExpressionValue("?"); |
| 58 expect(result.isValid, isTrue); |
| 59 DartObject value = result.value; |
| 60 expect(value, null); |
| 61 } |
| 62 |
| 63 void fail_identifier_topLevel() { |
| 64 EvaluationResult result = _getExpressionValue("?"); |
| 65 expect(result.isValid, isTrue); |
| 66 DartObject value = result.value; |
| 67 expect(value, null); |
| 68 } |
| 69 |
| 70 void fail_identifier_typeParameter() { |
| 71 EvaluationResult result = _getExpressionValue("?"); |
| 72 expect(result.isValid, isTrue); |
| 73 DartObject value = result.value; |
| 74 expect(value, null); |
| 75 } |
| 76 |
| 77 void fail_prefixedIdentifier_invalid() { |
| 78 EvaluationResult result = _getExpressionValue("?"); |
| 79 expect(result.isValid, isTrue); |
| 80 DartObject value = result.value; |
| 81 expect(value, null); |
| 82 } |
| 83 |
| 84 void fail_prefixedIdentifier_valid() { |
| 85 EvaluationResult result = _getExpressionValue("?"); |
| 86 expect(result.isValid, isTrue); |
| 87 DartObject value = result.value; |
| 88 expect(value, null); |
| 89 } |
| 90 |
| 91 void fail_propertyAccess_invalid() { |
| 92 EvaluationResult result = _getExpressionValue("?"); |
| 93 expect(result.isValid, isTrue); |
| 94 DartObject value = result.value; |
| 95 expect(value, null); |
| 96 } |
| 97 |
| 98 void fail_propertyAccess_valid() { |
| 99 EvaluationResult result = _getExpressionValue("?"); |
| 100 expect(result.isValid, isTrue); |
| 101 DartObject value = result.value; |
| 102 expect(value, null); |
| 103 } |
| 104 |
| 105 void fail_simpleIdentifier_invalid() { |
| 106 EvaluationResult result = _getExpressionValue("?"); |
| 107 expect(result.isValid, isTrue); |
| 108 DartObject value = result.value; |
| 109 expect(value, null); |
| 110 } |
| 111 |
| 112 void fail_simpleIdentifier_valid() { |
| 113 EvaluationResult result = _getExpressionValue("?"); |
| 114 expect(result.isValid, isTrue); |
| 115 DartObject value = result.value; |
| 116 expect(value, null); |
| 117 } |
| 118 |
| 119 void test_bitAnd_int_int() { |
| 120 _assertValue3(74 & 42, "74 & 42"); |
| 121 } |
| 122 |
| 123 void test_bitNot() { |
| 124 _assertValue3(~42, "~42"); |
| 125 } |
| 126 |
| 127 void test_bitOr_int_int() { |
| 128 _assertValue3(74 | 42, "74 | 42"); |
| 129 } |
| 130 |
| 131 void test_bitXor_int_int() { |
| 132 _assertValue3(74 ^ 42, "74 ^ 42"); |
| 133 } |
| 134 |
| 135 void test_divide_double_double() { |
| 136 _assertValue2(3.2 / 2.3, "3.2 / 2.3"); |
| 137 } |
| 138 |
| 139 void test_divide_double_double_byZero() { |
| 140 EvaluationResult result = _getExpressionValue("3.2 / 0.0"); |
| 141 expect(result.isValid, isTrue); |
| 142 DartObject value = result.value; |
| 143 expect(value.type.name, "double"); |
| 144 expect(value.toDoubleValue().isInfinite, isTrue); |
| 145 } |
| 146 |
| 147 void test_divide_int_int() { |
| 148 _assertValue2(1.5, "3 / 2"); |
| 149 } |
| 150 |
| 151 void test_divide_int_int_byZero() { |
| 152 EvaluationResult result = _getExpressionValue("3 / 0"); |
| 153 expect(result.isValid, isTrue); |
| 154 } |
| 155 |
| 156 void test_equal_boolean_boolean() { |
| 157 _assertValue(false, "true == false"); |
| 158 } |
| 159 |
| 160 void test_equal_int_int() { |
| 161 _assertValue(false, "2 == 3"); |
| 162 } |
| 163 |
| 164 void test_equal_invalidLeft() { |
| 165 EvaluationResult result = _getExpressionValue("a == 3"); |
| 166 expect(result.isValid, isFalse); |
| 167 } |
| 168 |
| 169 void test_equal_invalidRight() { |
| 170 EvaluationResult result = _getExpressionValue("2 == a"); |
| 171 expect(result.isValid, isFalse); |
| 172 } |
| 173 |
| 174 void test_equal_string_string() { |
| 175 _assertValue(false, "'a' == 'b'"); |
| 176 } |
| 177 |
| 178 void test_greaterThan_int_int() { |
| 179 _assertValue(false, "2 > 3"); |
| 180 } |
| 181 |
| 182 void test_greaterThanOrEqual_int_int() { |
| 183 _assertValue(false, "2 >= 3"); |
| 184 } |
| 185 |
| 186 void test_leftShift_int_int() { |
| 187 _assertValue3(64, "16 << 2"); |
| 188 } |
| 189 |
| 190 void test_lessThan_int_int() { |
| 191 _assertValue(true, "2 < 3"); |
| 192 } |
| 193 |
| 194 void test_lessThanOrEqual_int_int() { |
| 195 _assertValue(true, "2 <= 3"); |
| 196 } |
| 197 |
| 198 void test_literal_boolean_false() { |
| 199 _assertValue(false, "false"); |
| 200 } |
| 201 |
| 202 void test_literal_boolean_true() { |
| 203 _assertValue(true, "true"); |
| 204 } |
| 205 |
| 206 void test_literal_list() { |
| 207 EvaluationResult result = _getExpressionValue("const ['a', 'b', 'c']"); |
| 208 expect(result.isValid, isTrue); |
| 209 } |
| 210 |
| 211 void test_literal_map() { |
| 212 EvaluationResult result = |
| 213 _getExpressionValue("const {'a' : 'm', 'b' : 'n', 'c' : 'o'}"); |
| 214 expect(result.isValid, isTrue); |
| 215 Map<DartObject, DartObject> map = result.value.toMapValue(); |
| 216 expect(map.keys.map((k) => k.toStringValue()), ['a', 'b', 'c']); |
| 217 } |
| 218 |
| 219 void test_literal_null() { |
| 220 EvaluationResult result = _getExpressionValue("null"); |
| 221 expect(result.isValid, isTrue); |
| 222 DartObject value = result.value; |
| 223 expect(value.isNull, isTrue); |
| 224 } |
| 225 |
| 226 void test_literal_number_double() { |
| 227 _assertValue2(3.45, "3.45"); |
| 228 } |
| 229 |
| 230 void test_literal_number_integer() { |
| 231 _assertValue3(42, "42"); |
| 232 } |
| 233 |
| 234 void test_literal_string_adjacent() { |
| 235 _assertValue4("abcdef", "'abc' 'def'"); |
| 236 } |
| 237 |
| 238 void test_literal_string_interpolation_invalid() { |
| 239 EvaluationResult result = _getExpressionValue("'a\${f()}c'"); |
| 240 expect(result.isValid, isFalse); |
| 241 } |
| 242 |
| 243 void test_literal_string_interpolation_valid() { |
| 244 _assertValue4("a3c", "'a\${3}c'"); |
| 245 } |
| 246 |
| 247 void test_literal_string_simple() { |
| 248 _assertValue4("abc", "'abc'"); |
| 249 } |
| 250 |
| 251 void test_logicalAnd() { |
| 252 _assertValue(false, "true && false"); |
| 253 } |
| 254 |
| 255 void test_logicalNot() { |
| 256 _assertValue(false, "!true"); |
| 257 } |
| 258 |
| 259 void test_logicalOr() { |
| 260 _assertValue(true, "true || false"); |
| 261 } |
| 262 |
| 263 void test_minus_double_double() { |
| 264 _assertValue2(3.2 - 2.3, "3.2 - 2.3"); |
| 265 } |
| 266 |
| 267 void test_minus_int_int() { |
| 268 _assertValue3(1, "3 - 2"); |
| 269 } |
| 270 |
| 271 void test_negated_boolean() { |
| 272 EvaluationResult result = _getExpressionValue("-true"); |
| 273 expect(result.isValid, isFalse); |
| 274 } |
| 275 |
| 276 void test_negated_double() { |
| 277 _assertValue2(-42.3, "-42.3"); |
| 278 } |
| 279 |
| 280 void test_negated_integer() { |
| 281 _assertValue3(-42, "-42"); |
| 282 } |
| 283 |
| 284 void test_notEqual_boolean_boolean() { |
| 285 _assertValue(true, "true != false"); |
| 286 } |
| 287 |
| 288 void test_notEqual_int_int() { |
| 289 _assertValue(true, "2 != 3"); |
| 290 } |
| 291 |
| 292 void test_notEqual_invalidLeft() { |
| 293 EvaluationResult result = _getExpressionValue("a != 3"); |
| 294 expect(result.isValid, isFalse); |
| 295 } |
| 296 |
| 297 void test_notEqual_invalidRight() { |
| 298 EvaluationResult result = _getExpressionValue("2 != a"); |
| 299 expect(result.isValid, isFalse); |
| 300 } |
| 301 |
| 302 void test_notEqual_string_string() { |
| 303 _assertValue(true, "'a' != 'b'"); |
| 304 } |
| 305 |
| 306 void test_parenthesizedExpression() { |
| 307 _assertValue4("a", "('a')"); |
| 308 } |
| 309 |
| 310 void test_plus_double_double() { |
| 311 _assertValue2(2.3 + 3.2, "2.3 + 3.2"); |
| 312 } |
| 313 |
| 314 void test_plus_int_int() { |
| 315 _assertValue3(5, "2 + 3"); |
| 316 } |
| 317 |
| 318 void test_plus_string_string() { |
| 319 _assertValue4("ab", "'a' + 'b'"); |
| 320 } |
| 321 |
| 322 void test_remainder_double_double() { |
| 323 _assertValue2(3.2 % 2.3, "3.2 % 2.3"); |
| 324 } |
| 325 |
| 326 void test_remainder_int_int() { |
| 327 _assertValue3(2, "8 % 3"); |
| 328 } |
| 329 |
| 330 void test_rightShift() { |
| 331 _assertValue3(16, "64 >> 2"); |
| 332 } |
| 333 |
| 334 void test_stringLength_complex() { |
| 335 _assertValue3(6, "('qwe' + 'rty').length"); |
| 336 } |
| 337 |
| 338 void test_stringLength_simple() { |
| 339 _assertValue3(6, "'Dvorak'.length"); |
| 340 } |
| 341 |
| 342 void test_times_double_double() { |
| 343 _assertValue2(2.3 * 3.2, "2.3 * 3.2"); |
| 344 } |
| 345 |
| 346 void test_times_int_int() { |
| 347 _assertValue3(6, "2 * 3"); |
| 348 } |
| 349 |
| 350 void test_truncatingDivide_double_double() { |
| 351 _assertValue3(1, "3.2 ~/ 2.3"); |
| 352 } |
| 353 |
| 354 void test_truncatingDivide_int_int() { |
| 355 _assertValue3(3, "10 ~/ 3"); |
| 356 } |
| 357 |
| 358 void _assertValue(bool expectedValue, String contents) { |
| 359 EvaluationResult result = _getExpressionValue(contents); |
| 360 DartObject value = result.value; |
| 361 expect(value.type.name, "bool"); |
| 362 expect(value.toBoolValue(), expectedValue); |
| 363 } |
| 364 |
| 365 void _assertValue2(double expectedValue, String contents) { |
| 366 EvaluationResult result = _getExpressionValue(contents); |
| 367 expect(result.isValid, isTrue); |
| 368 DartObject value = result.value; |
| 369 expect(value.type.name, "double"); |
| 370 expect(value.toDoubleValue(), expectedValue); |
| 371 } |
| 372 |
| 373 void _assertValue3(int expectedValue, String contents) { |
| 374 EvaluationResult result = _getExpressionValue(contents); |
| 375 expect(result.isValid, isTrue); |
| 376 DartObject value = result.value; |
| 377 expect(value.type.name, "int"); |
| 378 expect(value.toIntValue(), expectedValue); |
| 379 } |
| 380 |
| 381 void _assertValue4(String expectedValue, String contents) { |
| 382 EvaluationResult result = _getExpressionValue(contents); |
| 383 DartObject value = result.value; |
| 384 expect(value, isNotNull); |
| 385 ParameterizedType type = value.type; |
| 386 expect(type, isNotNull); |
| 387 expect(type.name, "String"); |
| 388 expect(value.toStringValue(), expectedValue); |
| 389 } |
| 390 |
| 391 EvaluationResult _getExpressionValue(String contents) { |
| 392 Source source = addSource("var x = $contents;"); |
| 393 LibraryElement library = resolve2(source); |
| 394 CompilationUnit unit = |
| 395 analysisContext.resolveCompilationUnit(source, library); |
| 396 expect(unit, isNotNull); |
| 397 NodeList<CompilationUnitMember> declarations = unit.declarations; |
| 398 expect(declarations, hasLength(1)); |
| 399 CompilationUnitMember declaration = declarations[0]; |
| 400 EngineTestCase.assertInstanceOf((obj) => obj is TopLevelVariableDeclaration, |
| 401 TopLevelVariableDeclaration, declaration); |
| 402 NodeList<VariableDeclaration> variables = |
| 403 (declaration as TopLevelVariableDeclaration).variables.variables; |
| 404 expect(variables, hasLength(1)); |
| 405 ConstantEvaluator evaluator = new ConstantEvaluator( |
| 406 source, analysisContext.typeProvider, |
| 407 typeSystem: analysisContext.typeSystem); |
| 408 return evaluator.evaluate(variables[0].initializer); |
| 409 } |
| 410 } |
| OLD | NEW |