Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(72)

Side by Side Diff: pkg/analyzer/test/generated/constant_test.dart

Issue 1816923002: Move constant implementation out of generated (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file 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 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 analyzer.test.constant_test; 5 library analyzer.test.constant_test;
6 6
7 import 'package:analyzer/dart/ast/ast.dart'; 7 import 'package:analyzer/dart/ast/ast.dart';
8 import 'package:analyzer/dart/ast/token.dart';
9 import 'package:analyzer/dart/element/element.dart'; 8 import 'package:analyzer/dart/element/element.dart';
10 import 'package:analyzer/dart/element/type.dart'; 9 import 'package:analyzer/dart/element/type.dart';
11 import 'package:analyzer/src/dart/ast/token.dart';
12 import 'package:analyzer/src/dart/element/element.dart';
13 import 'package:analyzer/src/generated/constant.dart'; 10 import 'package:analyzer/src/generated/constant.dart';
14 import 'package:analyzer/src/generated/engine.dart';
15 import 'package:analyzer/src/generated/error.dart';
16 import 'package:analyzer/src/generated/resolver.dart';
17 import 'package:analyzer/src/generated/source.dart'; 11 import 'package:analyzer/src/generated/source.dart';
18 import 'package:analyzer/src/generated/source_io.dart'; 12 import 'package:analyzer/src/generated/source_io.dart';
19 import 'package:analyzer/src/generated/testing/ast_factory.dart';
20 import 'package:analyzer/src/generated/testing/element_factory.dart';
21 import 'package:analyzer/src/generated/testing/test_type_provider.dart';
22 import 'package:analyzer/src/generated/utilities_collection.dart';
23 import 'package:analyzer/src/task/dart.dart';
24 import 'package:path/path.dart';
25 import 'package:unittest/unittest.dart'; 13 import 'package:unittest/unittest.dart';
26 14
27 import '../reflective_tests.dart'; 15 import '../reflective_tests.dart';
28 import '../utils.dart'; 16 import '../utils.dart';
29 import 'engine_test.dart';
30 import 'resolver_test_case.dart'; 17 import 'resolver_test_case.dart';
31 import 'test_support.dart'; 18 import 'test_support.dart';
32 19
33 main() { 20 main() {
34 initializeTestEnvironment(); 21 initializeTestEnvironment();
35 runReflectiveTests(ConstantEvaluatorTest); 22 runReflectiveTests(ConstantEvaluatorTest);
36 runReflectiveTests(ConstantFinderTest);
37 runReflectiveTests(ConstantValueComputerTest);
38 runReflectiveTests(ConstantVisitorTest);
39 runReflectiveTests(DartObjectImplTest);
40 runReflectiveTests(DeclaredVariablesTest);
41 runReflectiveTests(ReferenceFinderTest);
42 }
43
44 const int LONG_MAX_VALUE = 0x7fffffffffffffff;
45
46 /**
47 * Implementation of [ConstantEvaluationValidator] used during unit tests;
48 * verifies that any nodes referenced during constant evaluation are present in
49 * the dependency graph.
50 */
51 class ConstantEvaluationValidator_ForTest
52 implements ConstantEvaluationValidator {
53 final InternalAnalysisContext context;
54 ConstantValueComputer computer;
55 ConstantEvaluationTarget _nodeBeingEvaluated;
56
57 ConstantEvaluationValidator_ForTest(this.context);
58
59 @override
60 void beforeComputeValue(ConstantEvaluationTarget constant) {
61 _nodeBeingEvaluated = constant;
62 }
63
64 @override
65 void beforeGetConstantInitializers(ConstructorElement constructor) =>
66 _checkPathTo(constructor);
67
68 @override
69 void beforeGetEvaluationResult(ConstantEvaluationTarget constant) =>
70 _checkPathTo(constant);
71
72 @override
73 void beforeGetFieldEvaluationResult(FieldElementImpl field) =>
74 _checkPathTo(field);
75
76 @override
77 void beforeGetParameterDefault(ParameterElement parameter) =>
78 _checkPathTo(parameter);
79
80 void _checkPathTo(ConstantEvaluationTarget target) {
81 if (computer.referenceGraph.containsPath(_nodeBeingEvaluated, target)) {
82 return; // pass
83 }
84 // print a nice error message on failure
85 StringBuffer out = new StringBuffer();
86 out.writeln("missing path in constant dependency graph");
87 out.writeln("from $_nodeBeingEvaluated to $target");
88 for (var s in context.analysisCache.sources) {
89 String text = context.getContents(s).data;
90 if (text != "") {
91 out.writeln('''
92 === ${s.shortName}
93 $text''');
94 }
95 }
96 fail(out.toString());
97 }
98 } 23 }
99 24
100 @reflectiveTest 25 @reflectiveTest
101 class ConstantEvaluatorTest extends ResolverTestCase { 26 class ConstantEvaluatorTest extends ResolverTestCase {
102 void fail_constructor() { 27 void fail_constructor() {
103 EvaluationResult result = _getExpressionValue("?"); 28 EvaluationResult result = _getExpressionValue("?");
104 expect(result.isValid, isTrue); 29 expect(result.isValid, isTrue);
105 DartObject value = result.value; 30 DartObject value = result.value;
106 expect(value, null); 31 expect(value, null);
107 } 32 }
(...skipping 367 matching lines...) Expand 10 before | Expand all | Expand 10 after
475 TopLevelVariableDeclaration, declaration); 400 TopLevelVariableDeclaration, declaration);
476 NodeList<VariableDeclaration> variables = 401 NodeList<VariableDeclaration> variables =
477 (declaration as TopLevelVariableDeclaration).variables.variables; 402 (declaration as TopLevelVariableDeclaration).variables.variables;
478 expect(variables, hasLength(1)); 403 expect(variables, hasLength(1));
479 ConstantEvaluator evaluator = new ConstantEvaluator( 404 ConstantEvaluator evaluator = new ConstantEvaluator(
480 source, analysisContext.typeProvider, 405 source, analysisContext.typeProvider,
481 typeSystem: analysisContext.typeSystem); 406 typeSystem: analysisContext.typeSystem);
482 return evaluator.evaluate(variables[0].initializer); 407 return evaluator.evaluate(variables[0].initializer);
483 } 408 }
484 } 409 }
485
486 @reflectiveTest
487 class ConstantFinderTest {
488 AstNode _node;
489 TypeProvider _typeProvider;
490 AnalysisContext _context;
491 Source _source;
492
493 void setUp() {
494 _typeProvider = new TestTypeProvider();
495 _context = new _TestAnalysisContext();
496 _source = new TestSource();
497 }
498
499 /**
500 * Test an annotation that consists solely of an identifier (and hence
501 * represents a reference to a compile-time constant variable).
502 */
503 void test_visitAnnotation_constantVariable() {
504 CompilationUnitElement compilationUnitElement =
505 ElementFactory.compilationUnit('/test.dart', _source)..source = _source;
506 ElementFactory.library(_context, 'L').definingCompilationUnit =
507 compilationUnitElement;
508 ElementAnnotationImpl elementAnnotation =
509 new ElementAnnotationImpl(compilationUnitElement);
510 _node = elementAnnotation.annotationAst = AstFactory.annotation(
511 AstFactory.identifier3('x'))..elementAnnotation = elementAnnotation;
512 expect(_findAnnotations(), contains(_node));
513 }
514
515 /**
516 * Test an annotation that represents the invocation of a constant
517 * constructor.
518 */
519 void test_visitAnnotation_invocation() {
520 CompilationUnitElement compilationUnitElement =
521 ElementFactory.compilationUnit('/test.dart', _source)..source = _source;
522 ElementFactory.library(_context, 'L').definingCompilationUnit =
523 compilationUnitElement;
524 ElementAnnotationImpl elementAnnotation =
525 new ElementAnnotationImpl(compilationUnitElement);
526 _node = elementAnnotation.annotationAst = AstFactory.annotation2(
527 AstFactory.identifier3('A'), null, AstFactory.argumentList())
528 ..elementAnnotation = elementAnnotation;
529 expect(_findAnnotations(), contains(_node));
530 }
531
532 void test_visitAnnotation_partOf() {
533 // Analyzer ignores annotations on "part of" directives.
534 Annotation annotation = AstFactory.annotation2(
535 AstFactory.identifier3('A'), null, AstFactory.argumentList());
536 _node = AstFactory.partOfDirective2(
537 <Annotation>[annotation], AstFactory.libraryIdentifier2(<String>['L']));
538 expect(_findConstants(), isEmpty);
539 }
540
541 void test_visitConstructorDeclaration_const() {
542 ConstructorElement element = _setupConstructorDeclaration("A", true);
543 expect(_findConstants(), contains(element));
544 }
545
546 void test_visitConstructorDeclaration_nonConst() {
547 _setupConstructorDeclaration("A", false);
548 expect(_findConstants(), isEmpty);
549 }
550
551 void test_visitVariableDeclaration_const() {
552 VariableElement element = _setupVariableDeclaration("v", true, true);
553 expect(_findConstants(), contains(element));
554 }
555
556 void test_visitVariableDeclaration_final_inClass() {
557 _setupFieldDeclaration('C', 'f', Keyword.FINAL);
558 expect(_findConstants(), isEmpty);
559 }
560
561 void test_visitVariableDeclaration_final_inClassWithConstConstructor() {
562 VariableDeclaration field = _setupFieldDeclaration('C', 'f', Keyword.FINAL,
563 hasConstConstructor: true);
564 expect(_findConstants(), contains(field.element));
565 }
566
567 void test_visitVariableDeclaration_final_outsideClass() {
568 _setupVariableDeclaration('v', false, true, isFinal: true);
569 expect(_findConstants(), isEmpty);
570 }
571
572 void test_visitVariableDeclaration_noInitializer() {
573 _setupVariableDeclaration("v", true, false);
574 expect(_findConstants(), isEmpty);
575 }
576
577 void test_visitVariableDeclaration_nonConst() {
578 _setupVariableDeclaration("v", false, true);
579 expect(_findConstants(), isEmpty);
580 }
581
582 void test_visitVariableDeclaration_static_const_inClass() {
583 VariableDeclaration field =
584 _setupFieldDeclaration('C', 'f', Keyword.CONST, isStatic: true);
585 expect(_findConstants(), contains(field.element));
586 }
587
588 void
589 test_visitVariableDeclaration_static_const_inClassWithConstConstructor() {
590 VariableDeclaration field = _setupFieldDeclaration('C', 'f', Keyword.CONST,
591 isStatic: true, hasConstConstructor: true);
592 expect(_findConstants(), contains(field.element));
593 }
594
595 void
596 test_visitVariableDeclaration_static_final_inClassWithConstConstructor() {
597 VariableDeclaration field = _setupFieldDeclaration('C', 'f', Keyword.FINAL,
598 isStatic: true, hasConstConstructor: true);
599 expect(_findConstants(), isNot(contains(field.element)));
600 }
601
602 void
603 test_visitVariableDeclaration_uninitialized_final_inClassWithConstConstruc tor() {
604 VariableDeclaration field = _setupFieldDeclaration('C', 'f', Keyword.FINAL,
605 isInitialized: false, hasConstConstructor: true);
606 expect(_findConstants(), isNot(contains(field.element)));
607 }
608
609 void test_visitVariableDeclaration_uninitialized_static_const_inClass() {
610 _setupFieldDeclaration('C', 'f', Keyword.CONST,
611 isStatic: true, isInitialized: false);
612 expect(_findConstants(), isEmpty);
613 }
614
615 List<Annotation> _findAnnotations() {
616 Set<Annotation> annotations = new Set<Annotation>();
617 for (ConstantEvaluationTarget target in _findConstants()) {
618 if (target is ElementAnnotationImpl) {
619 expect(target.context, same(_context));
620 expect(target.source, same(_source));
621 annotations.add(target.annotationAst);
622 }
623 }
624 return new List<Annotation>.from(annotations);
625 }
626
627 List<ConstantEvaluationTarget> _findConstants() {
628 ConstantFinder finder = new ConstantFinder(_context, _source, _source);
629 _node.accept(finder);
630 List<ConstantEvaluationTarget> constants = finder.constantsToCompute;
631 expect(constants, isNotNull);
632 return constants;
633 }
634
635 ConstructorElement _setupConstructorDeclaration(String name, bool isConst) {
636 Keyword constKeyword = isConst ? Keyword.CONST : null;
637 ConstructorDeclaration constructorDeclaration =
638 AstFactory.constructorDeclaration2(
639 constKeyword,
640 null,
641 null,
642 name,
643 AstFactory.formalParameterList(),
644 null,
645 AstFactory.blockFunctionBody2());
646 ClassElement classElement = ElementFactory.classElement2(name);
647 ConstructorElement element =
648 ElementFactory.constructorElement(classElement, name, isConst);
649 constructorDeclaration.element = element;
650 _node = constructorDeclaration;
651 return element;
652 }
653
654 VariableDeclaration _setupFieldDeclaration(
655 String className, String fieldName, Keyword keyword,
656 {bool isInitialized: true,
657 bool isStatic: false,
658 bool hasConstConstructor: false}) {
659 VariableDeclaration variableDeclaration = isInitialized
660 ? AstFactory.variableDeclaration2(fieldName, AstFactory.integer(0))
661 : AstFactory.variableDeclaration(fieldName);
662 VariableElement fieldElement = ElementFactory.fieldElement(
663 fieldName,
664 isStatic,
665 keyword == Keyword.FINAL,
666 keyword == Keyword.CONST,
667 _typeProvider.intType);
668 variableDeclaration.name.staticElement = fieldElement;
669 FieldDeclaration fieldDeclaration = AstFactory.fieldDeclaration2(
670 isStatic, keyword, <VariableDeclaration>[variableDeclaration]);
671 ClassDeclaration classDeclaration =
672 AstFactory.classDeclaration(null, className, null, null, null, null);
673 classDeclaration.members.add(fieldDeclaration);
674 _node = classDeclaration;
675 ClassElementImpl classElement = ElementFactory.classElement2(className);
676 classElement.fields = <FieldElement>[fieldElement];
677 classDeclaration.name.staticElement = classElement;
678 if (hasConstConstructor) {
679 ConstructorDeclaration constructorDeclaration =
680 AstFactory.constructorDeclaration2(
681 Keyword.CONST,
682 null,
683 AstFactory.identifier3(className),
684 null,
685 AstFactory.formalParameterList(),
686 null,
687 AstFactory.blockFunctionBody2());
688 classDeclaration.members.add(constructorDeclaration);
689 ConstructorElement constructorElement =
690 ElementFactory.constructorElement(classElement, '', true);
691 constructorDeclaration.element = constructorElement;
692 classElement.constructors = <ConstructorElement>[constructorElement];
693 } else {
694 classElement.constructors = ConstructorElement.EMPTY_LIST;
695 }
696 return variableDeclaration;
697 }
698
699 VariableElement _setupVariableDeclaration(
700 String name, bool isConst, bool isInitialized,
701 {isFinal: false}) {
702 VariableDeclaration variableDeclaration = isInitialized
703 ? AstFactory.variableDeclaration2(name, AstFactory.integer(0))
704 : AstFactory.variableDeclaration(name);
705 SimpleIdentifier identifier = variableDeclaration.name;
706 VariableElement element = ElementFactory.localVariableElement(identifier);
707 identifier.staticElement = element;
708 Keyword keyword = isConst ? Keyword.CONST : isFinal ? Keyword.FINAL : null;
709 AstFactory.variableDeclarationList2(keyword, [variableDeclaration]);
710 _node = variableDeclaration;
711 return element;
712 }
713 }
714
715 @reflectiveTest
716 class ConstantValueComputerTest extends ResolverTestCase {
717 void test_annotation_constConstructor() {
718 CompilationUnit compilationUnit = resolveSource(r'''
719 class A {
720 final int i;
721 const A(this.i);
722 }
723
724 class C {
725 @A(5)
726 f() {}
727 }
728 ''');
729 EvaluationResultImpl result =
730 _evaluateAnnotation(compilationUnit, "C", "f");
731 Map<String, DartObjectImpl> annotationFields = _assertType(result, 'A');
732 _assertIntField(annotationFields, 'i', 5);
733 }
734
735 void test_annotation_constConstructor_named() {
736 CompilationUnit compilationUnit = resolveSource(r'''
737 class A {
738 final int i;
739 const A.named(this.i);
740 }
741
742 class C {
743 @A.named(5)
744 f() {}
745 }
746 ''');
747 EvaluationResultImpl result =
748 _evaluateAnnotation(compilationUnit, "C", "f");
749 Map<String, DartObjectImpl> annotationFields = _assertType(result, 'A');
750 _assertIntField(annotationFields, 'i', 5);
751 }
752
753 void test_annotation_constConstructor_noArgs() {
754 // Failing to pass arguments to an annotation which is a constant
755 // constructor is illegal, but shouldn't crash analysis.
756 CompilationUnit compilationUnit = resolveSource(r'''
757 class A {
758 final int i;
759 const A(this.i);
760 }
761
762 class C {
763 @A
764 f() {}
765 }
766 ''');
767 _evaluateAnnotation(compilationUnit, "C", "f");
768 }
769
770 void test_annotation_constConstructor_noArgs_named() {
771 // Failing to pass arguments to an annotation which is a constant
772 // constructor is illegal, but shouldn't crash analysis.
773 CompilationUnit compilationUnit = resolveSource(r'''
774 class A {
775 final int i;
776 const A.named(this.i);
777 }
778
779 class C {
780 @A.named
781 f() {}
782 }
783 ''');
784 _evaluateAnnotation(compilationUnit, "C", "f");
785 }
786
787 void test_annotation_nonConstConstructor() {
788 // Calling a non-const constructor from an annotation that is illegal, but
789 // shouldn't crash analysis.
790 CompilationUnit compilationUnit = resolveSource(r'''
791 class A {
792 final int i;
793 A(this.i);
794 }
795
796 class C {
797 @A(5)
798 f() {}
799 }
800 ''');
801 _evaluateAnnotation(compilationUnit, "C", "f");
802 }
803
804 void test_annotation_staticConst() {
805 CompilationUnit compilationUnit = resolveSource(r'''
806 class C {
807 static const int i = 5;
808
809 @i
810 f() {}
811 }
812 ''');
813 EvaluationResultImpl result =
814 _evaluateAnnotation(compilationUnit, "C", "f");
815 expect(_assertValidInt(result), 5);
816 }
817
818 void test_annotation_staticConst_args() {
819 // Applying arguments to an annotation that is a static const is
820 // illegal, but shouldn't crash analysis.
821 CompilationUnit compilationUnit = resolveSource(r'''
822 class C {
823 static const int i = 5;
824
825 @i(1)
826 f() {}
827 }
828 ''');
829 _evaluateAnnotation(compilationUnit, "C", "f");
830 }
831
832 void test_annotation_staticConst_otherClass() {
833 CompilationUnit compilationUnit = resolveSource(r'''
834 class A {
835 static const int i = 5;
836 }
837
838 class C {
839 @A.i
840 f() {}
841 }
842 ''');
843 EvaluationResultImpl result =
844 _evaluateAnnotation(compilationUnit, "C", "f");
845 expect(_assertValidInt(result), 5);
846 }
847
848 void test_annotation_staticConst_otherClass_args() {
849 // Applying arguments to an annotation that is a static const is
850 // illegal, but shouldn't crash analysis.
851 CompilationUnit compilationUnit = resolveSource(r'''
852 class A {
853 static const int i = 5;
854 }
855
856 class C {
857 @A.i(1)
858 f() {}
859 }
860 ''');
861 _evaluateAnnotation(compilationUnit, "C", "f");
862 }
863
864 void test_annotation_topLevelVariable() {
865 CompilationUnit compilationUnit = resolveSource(r'''
866 const int i = 5;
867 class C {
868 @i
869 f() {}
870 }
871 ''');
872 EvaluationResultImpl result =
873 _evaluateAnnotation(compilationUnit, "C", "f");
874 expect(_assertValidInt(result), 5);
875 }
876
877 void test_annotation_topLevelVariable_args() {
878 // Applying arguments to an annotation that is a top-level variable is
879 // illegal, but shouldn't crash analysis.
880 CompilationUnit compilationUnit = resolveSource(r'''
881 const int i = 5;
882 class C {
883 @i(1)
884 f() {}
885 }
886 ''');
887 _evaluateAnnotation(compilationUnit, "C", "f");
888 }
889
890 void test_computeValues_cycle() {
891 TestLogger logger = new TestLogger();
892 AnalysisEngine.instance.logger = logger;
893 try {
894 Source source = addSource(r'''
895 const int a = c;
896 const int b = a;
897 const int c = b;''');
898 LibraryElement libraryElement = resolve2(source);
899 CompilationUnit unit =
900 analysisContext.resolveCompilationUnit(source, libraryElement);
901 analysisContext.computeErrors(source);
902 expect(unit, isNotNull);
903 ConstantValueComputer computer = _makeConstantValueComputer();
904 computer.add(unit, source, source);
905 computer.computeValues();
906 NodeList<CompilationUnitMember> members = unit.declarations;
907 expect(members, hasLength(3));
908 _validate(false, (members[0] as TopLevelVariableDeclaration).variables);
909 _validate(false, (members[1] as TopLevelVariableDeclaration).variables);
910 _validate(false, (members[2] as TopLevelVariableDeclaration).variables);
911 } finally {
912 AnalysisEngine.instance.logger = Logger.NULL;
913 }
914 }
915
916 void test_computeValues_dependentVariables() {
917 Source source = addSource(r'''
918 const int b = a;
919 const int a = 0;''');
920 LibraryElement libraryElement = resolve2(source);
921 CompilationUnit unit =
922 analysisContext.resolveCompilationUnit(source, libraryElement);
923 expect(unit, isNotNull);
924 ConstantValueComputer computer = _makeConstantValueComputer();
925 computer.add(unit, source, source);
926 computer.computeValues();
927 NodeList<CompilationUnitMember> members = unit.declarations;
928 expect(members, hasLength(2));
929 _validate(true, (members[0] as TopLevelVariableDeclaration).variables);
930 _validate(true, (members[1] as TopLevelVariableDeclaration).variables);
931 }
932
933 void test_computeValues_empty() {
934 ConstantValueComputer computer = _makeConstantValueComputer();
935 computer.computeValues();
936 }
937
938 void test_computeValues_multipleSources() {
939 Source librarySource = addNamedSource(
940 "/lib.dart",
941 r'''
942 library lib;
943 part 'part.dart';
944 const int c = b;
945 const int a = 0;''');
946 Source partSource = addNamedSource(
947 "/part.dart",
948 r'''
949 part of lib;
950 const int b = a;
951 const int d = c;''');
952 LibraryElement libraryElement = resolve2(librarySource);
953 CompilationUnit libraryUnit =
954 analysisContext.resolveCompilationUnit(librarySource, libraryElement);
955 expect(libraryUnit, isNotNull);
956 CompilationUnit partUnit =
957 analysisContext.resolveCompilationUnit(partSource, libraryElement);
958 expect(partUnit, isNotNull);
959 ConstantValueComputer computer = _makeConstantValueComputer();
960 computer.add(libraryUnit, librarySource, librarySource);
961 computer.add(partUnit, partSource, librarySource);
962 computer.computeValues();
963 NodeList<CompilationUnitMember> libraryMembers = libraryUnit.declarations;
964 expect(libraryMembers, hasLength(2));
965 _validate(
966 true, (libraryMembers[0] as TopLevelVariableDeclaration).variables);
967 _validate(
968 true, (libraryMembers[1] as TopLevelVariableDeclaration).variables);
969 NodeList<CompilationUnitMember> partMembers = libraryUnit.declarations;
970 expect(partMembers, hasLength(2));
971 _validate(true, (partMembers[0] as TopLevelVariableDeclaration).variables);
972 _validate(true, (partMembers[1] as TopLevelVariableDeclaration).variables);
973 }
974
975 void test_computeValues_singleVariable() {
976 Source source = addSource("const int a = 0;");
977 LibraryElement libraryElement = resolve2(source);
978 CompilationUnit unit =
979 analysisContext.resolveCompilationUnit(source, libraryElement);
980 expect(unit, isNotNull);
981 ConstantValueComputer computer = _makeConstantValueComputer();
982 computer.add(unit, source, source);
983 computer.computeValues();
984 NodeList<CompilationUnitMember> members = unit.declarations;
985 expect(members, hasLength(1));
986 _validate(true, (members[0] as TopLevelVariableDeclaration).variables);
987 }
988
989 void test_computeValues_value_depends_on_enum() {
990 Source source = addSource('''
991 enum E { id0, id1 }
992 const E e = E.id0;
993 ''');
994 LibraryElement libraryElement = resolve2(source);
995 CompilationUnit unit =
996 analysisContext.resolveCompilationUnit(source, libraryElement);
997 expect(unit, isNotNull);
998 ConstantValueComputer computer = _makeConstantValueComputer();
999 computer.add(unit, source, source);
1000 computer.computeValues();
1001 TopLevelVariableDeclaration declaration = unit.declarations
1002 .firstWhere((member) => member is TopLevelVariableDeclaration);
1003 _validate(true, declaration.variables);
1004 }
1005
1006 void test_dependencyOnConstructor() {
1007 // x depends on "const A()"
1008 _assertProperDependencies(r'''
1009 class A {
1010 const A();
1011 }
1012 const x = const A();''');
1013 }
1014
1015 void test_dependencyOnConstructorArgument() {
1016 // "const A(x)" depends on x
1017 _assertProperDependencies(r'''
1018 class A {
1019 const A(this.next);
1020 final A next;
1021 }
1022 const A x = const A(null);
1023 const A y = const A(x);''');
1024 }
1025
1026 void test_dependencyOnConstructorArgument_unresolvedConstructor() {
1027 // "const A.a(x)" depends on x even if the constructor A.a can't be found.
1028 _assertProperDependencies(
1029 r'''
1030 class A {
1031 }
1032 const int x = 1;
1033 const A y = const A.a(x);''',
1034 [CompileTimeErrorCode.CONST_WITH_UNDEFINED_CONSTRUCTOR]);
1035 }
1036
1037 void test_dependencyOnConstructorInitializer() {
1038 // "const A()" depends on x
1039 _assertProperDependencies(r'''
1040 const int x = 1;
1041 class A {
1042 const A() : v = x;
1043 final int v;
1044 }''');
1045 }
1046
1047 void test_dependencyOnExplicitSuperConstructor() {
1048 // b depends on B() depends on A()
1049 _assertProperDependencies(r'''
1050 class A {
1051 const A(this.x);
1052 final int x;
1053 }
1054 class B extends A {
1055 const B() : super(5);
1056 }
1057 const B b = const B();''');
1058 }
1059
1060 void test_dependencyOnExplicitSuperConstructorParameters() {
1061 // b depends on B() depends on i
1062 _assertProperDependencies(r'''
1063 class A {
1064 const A(this.x);
1065 final int x;
1066 }
1067 class B extends A {
1068 const B() : super(i);
1069 }
1070 const B b = const B();
1071 const int i = 5;''');
1072 }
1073
1074 void test_dependencyOnFactoryRedirect() {
1075 // a depends on A.foo() depends on A.bar()
1076 _assertProperDependencies(r'''
1077 const A a = const A.foo();
1078 class A {
1079 factory const A.foo() = A.bar;
1080 const A.bar();
1081 }''');
1082 }
1083
1084 void test_dependencyOnFactoryRedirectWithTypeParams() {
1085 _assertProperDependencies(r'''
1086 class A {
1087 const factory A(var a) = B<int>;
1088 }
1089
1090 class B<T> implements A {
1091 final T x;
1092 const B(this.x);
1093 }
1094
1095 const A a = const A(10);''');
1096 }
1097
1098 void test_dependencyOnImplicitSuperConstructor() {
1099 // b depends on B() depends on A()
1100 _assertProperDependencies(r'''
1101 class A {
1102 const A() : x = 5;
1103 final int x;
1104 }
1105 class B extends A {
1106 const B();
1107 }
1108 const B b = const B();''');
1109 }
1110
1111 void test_dependencyOnInitializedFinal() {
1112 // a depends on A() depends on A.x
1113 _assertProperDependencies('''
1114 class A {
1115 const A();
1116 final int x = 1;
1117 }
1118 const A a = const A();
1119 ''');
1120 }
1121
1122 void test_dependencyOnInitializedNonStaticConst() {
1123 // Even though non-static consts are not allowed by the language, we need
1124 // to handle them for error recovery purposes.
1125 // a depends on A() depends on A.x
1126 _assertProperDependencies(
1127 '''
1128 class A {
1129 const A();
1130 const int x = 1;
1131 }
1132 const A a = const A();
1133 ''',
1134 [CompileTimeErrorCode.CONST_INSTANCE_FIELD]);
1135 }
1136
1137 void test_dependencyOnNonFactoryRedirect() {
1138 // a depends on A.foo() depends on A.bar()
1139 _assertProperDependencies(r'''
1140 const A a = const A.foo();
1141 class A {
1142 const A.foo() : this.bar();
1143 const A.bar();
1144 }''');
1145 }
1146
1147 void test_dependencyOnNonFactoryRedirect_arg() {
1148 // a depends on A.foo() depends on b
1149 _assertProperDependencies(r'''
1150 const A a = const A.foo();
1151 const int b = 1;
1152 class A {
1153 const A.foo() : this.bar(b);
1154 const A.bar(x) : y = x;
1155 final int y;
1156 }''');
1157 }
1158
1159 void test_dependencyOnNonFactoryRedirect_defaultValue() {
1160 // a depends on A.foo() depends on A.bar() depends on b
1161 _assertProperDependencies(r'''
1162 const A a = const A.foo();
1163 const int b = 1;
1164 class A {
1165 const A.foo() : this.bar();
1166 const A.bar([x = b]) : y = x;
1167 final int y;
1168 }''');
1169 }
1170
1171 void test_dependencyOnNonFactoryRedirect_toMissing() {
1172 // a depends on A.foo() which depends on nothing, since A.bar() is
1173 // missing.
1174 _assertProperDependencies(
1175 r'''
1176 const A a = const A.foo();
1177 class A {
1178 const A.foo() : this.bar();
1179 }''',
1180 [CompileTimeErrorCode.REDIRECT_GENERATIVE_TO_MISSING_CONSTRUCTOR]);
1181 }
1182
1183 void test_dependencyOnNonFactoryRedirect_toNonConst() {
1184 // a depends on A.foo() which depends on nothing, since A.bar() is
1185 // non-const.
1186 _assertProperDependencies(r'''
1187 const A a = const A.foo();
1188 class A {
1189 const A.foo() : this.bar();
1190 A.bar();
1191 }''');
1192 }
1193
1194 void test_dependencyOnNonFactoryRedirect_unnamed() {
1195 // a depends on A.foo() depends on A()
1196 _assertProperDependencies(r'''
1197 const A a = const A.foo();
1198 class A {
1199 const A.foo() : this();
1200 const A();
1201 }''');
1202 }
1203
1204 void test_dependencyOnOptionalParameterDefault() {
1205 // a depends on A() depends on B()
1206 _assertProperDependencies(r'''
1207 class A {
1208 const A([x = const B()]) : b = x;
1209 final B b;
1210 }
1211 class B {
1212 const B();
1213 }
1214 const A a = const A();''');
1215 }
1216
1217 void test_dependencyOnVariable() {
1218 // x depends on y
1219 _assertProperDependencies(r'''
1220 const x = y + 1;
1221 const y = 2;''');
1222 }
1223
1224 void test_final_initialized_at_declaration() {
1225 CompilationUnit compilationUnit = resolveSource('''
1226 class A {
1227 final int i = 123;
1228 const A();
1229 }
1230
1231 const A a = const A();
1232 ''');
1233 EvaluationResultImpl result =
1234 _evaluateTopLevelVariable(compilationUnit, 'a');
1235 Map<String, DartObjectImpl> fields = _assertType(result, "A");
1236 expect(fields, hasLength(1));
1237 _assertIntField(fields, "i", 123);
1238 }
1239
1240 void test_fromEnvironment_bool_default_false() {
1241 expect(_assertValidBool(_check_fromEnvironment_bool(null, "false")), false);
1242 }
1243
1244 void test_fromEnvironment_bool_default_overridden() {
1245 expect(
1246 _assertValidBool(_check_fromEnvironment_bool("false", "true")), false);
1247 }
1248
1249 void test_fromEnvironment_bool_default_parseError() {
1250 expect(_assertValidBool(_check_fromEnvironment_bool("parseError", "true")),
1251 true);
1252 }
1253
1254 void test_fromEnvironment_bool_default_true() {
1255 expect(_assertValidBool(_check_fromEnvironment_bool(null, "true")), true);
1256 }
1257
1258 void test_fromEnvironment_bool_false() {
1259 expect(_assertValidBool(_check_fromEnvironment_bool("false", null)), false);
1260 }
1261
1262 void test_fromEnvironment_bool_parseError() {
1263 expect(_assertValidBool(_check_fromEnvironment_bool("parseError", null)),
1264 false);
1265 }
1266
1267 void test_fromEnvironment_bool_true() {
1268 expect(_assertValidBool(_check_fromEnvironment_bool("true", null)), true);
1269 }
1270
1271 void test_fromEnvironment_bool_undeclared() {
1272 _assertValidUnknown(_check_fromEnvironment_bool(null, null));
1273 }
1274
1275 void test_fromEnvironment_int_default_overridden() {
1276 expect(_assertValidInt(_check_fromEnvironment_int("234", "123")), 234);
1277 }
1278
1279 void test_fromEnvironment_int_default_parseError() {
1280 expect(
1281 _assertValidInt(_check_fromEnvironment_int("parseError", "123")), 123);
1282 }
1283
1284 void test_fromEnvironment_int_default_undeclared() {
1285 expect(_assertValidInt(_check_fromEnvironment_int(null, "123")), 123);
1286 }
1287
1288 void test_fromEnvironment_int_ok() {
1289 expect(_assertValidInt(_check_fromEnvironment_int("234", null)), 234);
1290 }
1291
1292 void test_fromEnvironment_int_parseError() {
1293 _assertValidNull(_check_fromEnvironment_int("parseError", null));
1294 }
1295
1296 void test_fromEnvironment_int_parseError_nullDefault() {
1297 _assertValidNull(_check_fromEnvironment_int("parseError", "null"));
1298 }
1299
1300 void test_fromEnvironment_int_undeclared() {
1301 _assertValidUnknown(_check_fromEnvironment_int(null, null));
1302 }
1303
1304 void test_fromEnvironment_int_undeclared_nullDefault() {
1305 _assertValidNull(_check_fromEnvironment_int(null, "null"));
1306 }
1307
1308 void test_fromEnvironment_string_default_overridden() {
1309 expect(_assertValidString(_check_fromEnvironment_string("abc", "'def'")),
1310 "abc");
1311 }
1312
1313 void test_fromEnvironment_string_default_undeclared() {
1314 expect(_assertValidString(_check_fromEnvironment_string(null, "'def'")),
1315 "def");
1316 }
1317
1318 void test_fromEnvironment_string_empty() {
1319 expect(_assertValidString(_check_fromEnvironment_string("", null)), "");
1320 }
1321
1322 void test_fromEnvironment_string_ok() {
1323 expect(
1324 _assertValidString(_check_fromEnvironment_string("abc", null)), "abc");
1325 }
1326
1327 void test_fromEnvironment_string_undeclared() {
1328 _assertValidUnknown(_check_fromEnvironment_string(null, null));
1329 }
1330
1331 void test_fromEnvironment_string_undeclared_nullDefault() {
1332 _assertValidNull(_check_fromEnvironment_string(null, "null"));
1333 }
1334
1335 void test_instanceCreationExpression_computedField() {
1336 CompilationUnit compilationUnit = resolveSource(r'''
1337 const foo = const A(4, 5);
1338 class A {
1339 const A(int i, int j) : k = 2 * i + j;
1340 final int k;
1341 }''');
1342 EvaluationResultImpl result =
1343 _evaluateTopLevelVariable(compilationUnit, "foo");
1344 Map<String, DartObjectImpl> fields = _assertType(result, "A");
1345 expect(fields, hasLength(1));
1346 _assertIntField(fields, "k", 13);
1347 }
1348
1349 void
1350 test_instanceCreationExpression_computedField_namedOptionalWithDefault() {
1351 _checkInstanceCreationOptionalParams(false, true, true);
1352 }
1353
1354 void
1355 test_instanceCreationExpression_computedField_namedOptionalWithoutDefault( ) {
1356 _checkInstanceCreationOptionalParams(false, true, false);
1357 }
1358
1359 void
1360 test_instanceCreationExpression_computedField_unnamedOptionalWithDefault() {
1361 _checkInstanceCreationOptionalParams(false, false, true);
1362 }
1363
1364 void
1365 test_instanceCreationExpression_computedField_unnamedOptionalWithoutDefaul t() {
1366 _checkInstanceCreationOptionalParams(false, false, false);
1367 }
1368
1369 void test_instanceCreationExpression_computedField_usesConstConstructor() {
1370 CompilationUnit compilationUnit = resolveSource(r'''
1371 const foo = const A(3);
1372 class A {
1373 const A(int i) : b = const B(4);
1374 final int b;
1375 }
1376 class B {
1377 const B(this.k);
1378 final int k;
1379 }''');
1380 EvaluationResultImpl result =
1381 _evaluateTopLevelVariable(compilationUnit, "foo");
1382 Map<String, DartObjectImpl> fieldsOfA = _assertType(result, "A");
1383 expect(fieldsOfA, hasLength(1));
1384 Map<String, DartObjectImpl> fieldsOfB =
1385 _assertFieldType(fieldsOfA, "b", "B");
1386 expect(fieldsOfB, hasLength(1));
1387 _assertIntField(fieldsOfB, "k", 4);
1388 }
1389
1390 void test_instanceCreationExpression_computedField_usesStaticConst() {
1391 CompilationUnit compilationUnit = resolveSource(r'''
1392 const foo = const A(3);
1393 class A {
1394 const A(int i) : k = i + B.bar;
1395 final int k;
1396 }
1397 class B {
1398 static const bar = 4;
1399 }''');
1400 EvaluationResultImpl result =
1401 _evaluateTopLevelVariable(compilationUnit, "foo");
1402 Map<String, DartObjectImpl> fields = _assertType(result, "A");
1403 expect(fields, hasLength(1));
1404 _assertIntField(fields, "k", 7);
1405 }
1406
1407 void test_instanceCreationExpression_computedField_usesTopLevelConst() {
1408 CompilationUnit compilationUnit = resolveSource(r'''
1409 const foo = const A(3);
1410 const bar = 4;
1411 class A {
1412 const A(int i) : k = i + bar;
1413 final int k;
1414 }''');
1415 EvaluationResultImpl result =
1416 _evaluateTopLevelVariable(compilationUnit, "foo");
1417 Map<String, DartObjectImpl> fields = _assertType(result, "A");
1418 expect(fields, hasLength(1));
1419 _assertIntField(fields, "k", 7);
1420 }
1421
1422 void test_instanceCreationExpression_explicitSuper() {
1423 CompilationUnit compilationUnit = resolveSource(r'''
1424 const foo = const B(4, 5);
1425 class A {
1426 const A(this.x);
1427 final int x;
1428 }
1429 class B extends A {
1430 const B(int x, this.y) : super(x * 2);
1431 final int y;
1432 }''');
1433 EvaluationResultImpl result =
1434 _evaluateTopLevelVariable(compilationUnit, "foo");
1435 Map<String, DartObjectImpl> fields = _assertType(result, "B");
1436 expect(fields, hasLength(2));
1437 _assertIntField(fields, "y", 5);
1438 Map<String, DartObjectImpl> superclassFields =
1439 _assertFieldType(fields, GenericState.SUPERCLASS_FIELD, "A");
1440 expect(superclassFields, hasLength(1));
1441 _assertIntField(superclassFields, "x", 8);
1442 }
1443
1444 void test_instanceCreationExpression_fieldFormalParameter() {
1445 CompilationUnit compilationUnit = resolveSource(r'''
1446 const foo = const A(42);
1447 class A {
1448 int x;
1449 const A(this.x)
1450 }''');
1451 EvaluationResultImpl result =
1452 _evaluateTopLevelVariable(compilationUnit, "foo");
1453 Map<String, DartObjectImpl> fields = _assertType(result, "A");
1454 expect(fields, hasLength(1));
1455 _assertIntField(fields, "x", 42);
1456 }
1457
1458 void
1459 test_instanceCreationExpression_fieldFormalParameter_namedOptionalWithDefa ult() {
1460 _checkInstanceCreationOptionalParams(true, true, true);
1461 }
1462
1463 void
1464 test_instanceCreationExpression_fieldFormalParameter_namedOptionalWithoutD efault() {
1465 _checkInstanceCreationOptionalParams(true, true, false);
1466 }
1467
1468 void
1469 test_instanceCreationExpression_fieldFormalParameter_unnamedOptionalWithDe fault() {
1470 _checkInstanceCreationOptionalParams(true, false, true);
1471 }
1472
1473 void
1474 test_instanceCreationExpression_fieldFormalParameter_unnamedOptionalWithou tDefault() {
1475 _checkInstanceCreationOptionalParams(true, false, false);
1476 }
1477
1478 void test_instanceCreationExpression_implicitSuper() {
1479 CompilationUnit compilationUnit = resolveSource(r'''
1480 const foo = const B(4);
1481 class A {
1482 const A() : x = 3;
1483 final int x;
1484 }
1485 class B extends A {
1486 const B(this.y);
1487 final int y;
1488 }''');
1489 EvaluationResultImpl result =
1490 _evaluateTopLevelVariable(compilationUnit, "foo");
1491 Map<String, DartObjectImpl> fields = _assertType(result, "B");
1492 expect(fields, hasLength(2));
1493 _assertIntField(fields, "y", 4);
1494 Map<String, DartObjectImpl> superclassFields =
1495 _assertFieldType(fields, GenericState.SUPERCLASS_FIELD, "A");
1496 expect(superclassFields, hasLength(1));
1497 _assertIntField(superclassFields, "x", 3);
1498 }
1499
1500 void test_instanceCreationExpression_nonFactoryRedirect() {
1501 CompilationUnit compilationUnit = resolveSource(r'''
1502 const foo = const A.a1();
1503 class A {
1504 const A.a1() : this.a2();
1505 const A.a2() : x = 5;
1506 final int x;
1507 }''');
1508 Map<String, DartObjectImpl> aFields =
1509 _assertType(_evaluateTopLevelVariable(compilationUnit, "foo"), "A");
1510 _assertIntField(aFields, 'x', 5);
1511 }
1512
1513 void test_instanceCreationExpression_nonFactoryRedirect_arg() {
1514 CompilationUnit compilationUnit = resolveSource(r'''
1515 const foo = const A.a1(1);
1516 class A {
1517 const A.a1(x) : this.a2(x + 100);
1518 const A.a2(x) : y = x + 10;
1519 final int y;
1520 }''');
1521 Map<String, DartObjectImpl> aFields =
1522 _assertType(_evaluateTopLevelVariable(compilationUnit, "foo"), "A");
1523 _assertIntField(aFields, 'y', 111);
1524 }
1525
1526 void test_instanceCreationExpression_nonFactoryRedirect_cycle() {
1527 // It is an error to have a cycle in non-factory redirects; however, we
1528 // need to make sure that even if the error occurs, attempting to evaluate
1529 // the constant will terminate.
1530 CompilationUnit compilationUnit = resolveSource(r'''
1531 const foo = const A();
1532 class A {
1533 const A() : this.b();
1534 const A.b() : this();
1535 }''');
1536 _assertValidUnknown(_evaluateTopLevelVariable(compilationUnit, "foo"));
1537 }
1538
1539 void test_instanceCreationExpression_nonFactoryRedirect_defaultArg() {
1540 CompilationUnit compilationUnit = resolveSource(r'''
1541 const foo = const A.a1();
1542 class A {
1543 const A.a1() : this.a2();
1544 const A.a2([x = 100]) : y = x + 10;
1545 final int y;
1546 }''');
1547 Map<String, DartObjectImpl> aFields =
1548 _assertType(_evaluateTopLevelVariable(compilationUnit, "foo"), "A");
1549 _assertIntField(aFields, 'y', 110);
1550 }
1551
1552 void test_instanceCreationExpression_nonFactoryRedirect_toMissing() {
1553 CompilationUnit compilationUnit = resolveSource(r'''
1554 const foo = const A.a1();
1555 class A {
1556 const A.a1() : this.a2();
1557 }''');
1558 // We don't care what value foo evaluates to (since there is a compile
1559 // error), but we shouldn't crash, and we should figure
1560 // out that it evaluates to an instance of class A.
1561 _assertType(_evaluateTopLevelVariable(compilationUnit, "foo"), "A");
1562 }
1563
1564 void test_instanceCreationExpression_nonFactoryRedirect_toNonConst() {
1565 CompilationUnit compilationUnit = resolveSource(r'''
1566 const foo = const A.a1();
1567 class A {
1568 const A.a1() : this.a2();
1569 A.a2();
1570 }''');
1571 // We don't care what value foo evaluates to (since there is a compile
1572 // error), but we shouldn't crash, and we should figure
1573 // out that it evaluates to an instance of class A.
1574 _assertType(_evaluateTopLevelVariable(compilationUnit, "foo"), "A");
1575 }
1576
1577 void test_instanceCreationExpression_nonFactoryRedirect_unnamed() {
1578 CompilationUnit compilationUnit = resolveSource(r'''
1579 const foo = const A.a1();
1580 class A {
1581 const A.a1() : this();
1582 const A() : x = 5;
1583 final int x;
1584 }''');
1585 Map<String, DartObjectImpl> aFields =
1586 _assertType(_evaluateTopLevelVariable(compilationUnit, "foo"), "A");
1587 _assertIntField(aFields, 'x', 5);
1588 }
1589
1590 void test_instanceCreationExpression_redirect() {
1591 CompilationUnit compilationUnit = resolveSource(r'''
1592 const foo = const A();
1593 class A {
1594 const factory A() = B;
1595 }
1596 class B implements A {
1597 const B();
1598 }''');
1599 _assertType(_evaluateTopLevelVariable(compilationUnit, "foo"), "B");
1600 }
1601
1602 void test_instanceCreationExpression_redirect_cycle() {
1603 // It is an error to have a cycle in factory redirects; however, we need
1604 // to make sure that even if the error occurs, attempting to evaluate the
1605 // constant will terminate.
1606 CompilationUnit compilationUnit = resolveSource(r'''
1607 const foo = const A();
1608 class A {
1609 const factory A() = A.b;
1610 const factory A.b() = A;
1611 }''');
1612 _assertValidUnknown(_evaluateTopLevelVariable(compilationUnit, "foo"));
1613 }
1614
1615 void test_instanceCreationExpression_redirect_external() {
1616 CompilationUnit compilationUnit = resolveSource(r'''
1617 const foo = const A();
1618 class A {
1619 external const factory A();
1620 }''');
1621 _assertValidUnknown(_evaluateTopLevelVariable(compilationUnit, "foo"));
1622 }
1623
1624 void test_instanceCreationExpression_redirect_nonConst() {
1625 // It is an error for a const factory constructor redirect to a non-const
1626 // constructor; however, we need to make sure that even if the error
1627 // attempting to evaluate the constant won't cause a crash.
1628 CompilationUnit compilationUnit = resolveSource(r'''
1629 const foo = const A();
1630 class A {
1631 const factory A() = A.b;
1632 A.b();
1633 }''');
1634 _assertValidUnknown(_evaluateTopLevelVariable(compilationUnit, "foo"));
1635 }
1636
1637 void test_instanceCreationExpression_redirectWithTypeParams() {
1638 CompilationUnit compilationUnit = resolveSource(r'''
1639 class A {
1640 const factory A(var a) = B<int>;
1641 }
1642
1643 class B<T> implements A {
1644 final T x;
1645 const B(this.x);
1646 }
1647
1648 const A a = const A(10);''');
1649 EvaluationResultImpl result =
1650 _evaluateTopLevelVariable(compilationUnit, "a");
1651 Map<String, DartObjectImpl> fields = _assertType(result, "B<int>");
1652 expect(fields, hasLength(1));
1653 _assertIntField(fields, "x", 10);
1654 }
1655
1656 void test_instanceCreationExpression_redirectWithTypeSubstitution() {
1657 // To evaluate the redirection of A<int>,
1658 // A's template argument (T=int) must be substituted
1659 // into B's template argument (B<U> where U=T) to get B<int>.
1660 CompilationUnit compilationUnit = resolveSource(r'''
1661 class A<T> {
1662 const factory A(var a) = B<T>;
1663 }
1664
1665 class B<U> implements A {
1666 final U x;
1667 const B(this.x);
1668 }
1669
1670 const A<int> a = const A<int>(10);''');
1671 EvaluationResultImpl result =
1672 _evaluateTopLevelVariable(compilationUnit, "a");
1673 Map<String, DartObjectImpl> fields = _assertType(result, "B<int>");
1674 expect(fields, hasLength(1));
1675 _assertIntField(fields, "x", 10);
1676 }
1677
1678 void test_instanceCreationExpression_symbol() {
1679 CompilationUnit compilationUnit =
1680 resolveSource("const foo = const Symbol('a');");
1681 EvaluationResultImpl evaluationResult =
1682 _evaluateTopLevelVariable(compilationUnit, "foo");
1683 expect(evaluationResult.value, isNotNull);
1684 DartObjectImpl value = evaluationResult.value;
1685 expect(value.type, typeProvider.symbolType);
1686 expect(value.toSymbolValue(), "a");
1687 }
1688
1689 void test_instanceCreationExpression_withSupertypeParams_explicit() {
1690 _checkInstanceCreation_withSupertypeParams(true);
1691 }
1692
1693 void test_instanceCreationExpression_withSupertypeParams_implicit() {
1694 _checkInstanceCreation_withSupertypeParams(false);
1695 }
1696
1697 void test_instanceCreationExpression_withTypeParams() {
1698 CompilationUnit compilationUnit = resolveSource(r'''
1699 class C<E> {
1700 const C();
1701 }
1702 const c_int = const C<int>();
1703 const c_num = const C<num>();''');
1704 EvaluationResultImpl c_int =
1705 _evaluateTopLevelVariable(compilationUnit, "c_int");
1706 _assertType(c_int, "C<int>");
1707 DartObjectImpl c_int_value = c_int.value;
1708 EvaluationResultImpl c_num =
1709 _evaluateTopLevelVariable(compilationUnit, "c_num");
1710 _assertType(c_num, "C<num>");
1711 DartObjectImpl c_num_value = c_num.value;
1712 expect(c_int_value == c_num_value, isFalse);
1713 }
1714
1715 void test_isValidSymbol() {
1716 expect(ConstantEvaluationEngine.isValidPublicSymbol(""), isTrue);
1717 expect(ConstantEvaluationEngine.isValidPublicSymbol("foo"), isTrue);
1718 expect(ConstantEvaluationEngine.isValidPublicSymbol("foo.bar"), isTrue);
1719 expect(ConstantEvaluationEngine.isValidPublicSymbol("foo\$"), isTrue);
1720 expect(ConstantEvaluationEngine.isValidPublicSymbol("foo\$bar"), isTrue);
1721 expect(ConstantEvaluationEngine.isValidPublicSymbol("iff"), isTrue);
1722 expect(ConstantEvaluationEngine.isValidPublicSymbol("gif"), isTrue);
1723 expect(ConstantEvaluationEngine.isValidPublicSymbol("if\$"), isTrue);
1724 expect(ConstantEvaluationEngine.isValidPublicSymbol("\$if"), isTrue);
1725 expect(ConstantEvaluationEngine.isValidPublicSymbol("foo="), isTrue);
1726 expect(ConstantEvaluationEngine.isValidPublicSymbol("foo.bar="), isTrue);
1727 expect(ConstantEvaluationEngine.isValidPublicSymbol("foo.+"), isTrue);
1728 expect(ConstantEvaluationEngine.isValidPublicSymbol("void"), isTrue);
1729 expect(ConstantEvaluationEngine.isValidPublicSymbol("_foo"), isFalse);
1730 expect(ConstantEvaluationEngine.isValidPublicSymbol("_foo.bar"), isFalse);
1731 expect(ConstantEvaluationEngine.isValidPublicSymbol("foo._bar"), isFalse);
1732 expect(ConstantEvaluationEngine.isValidPublicSymbol("if"), isFalse);
1733 expect(ConstantEvaluationEngine.isValidPublicSymbol("if.foo"), isFalse);
1734 expect(ConstantEvaluationEngine.isValidPublicSymbol("foo.if"), isFalse);
1735 expect(ConstantEvaluationEngine.isValidPublicSymbol("foo=.bar"), isFalse);
1736 expect(ConstantEvaluationEngine.isValidPublicSymbol("foo."), isFalse);
1737 expect(ConstantEvaluationEngine.isValidPublicSymbol("+.foo"), isFalse);
1738 expect(ConstantEvaluationEngine.isValidPublicSymbol("void.foo"), isFalse);
1739 expect(ConstantEvaluationEngine.isValidPublicSymbol("foo.void"), isFalse);
1740 }
1741
1742 void test_length_of_improperly_typed_string_expression() {
1743 // Since type annotations are ignored in unchecked mode, the improper
1744 // types on s1 and s2 shouldn't prevent us from evaluating i to
1745 // 'alpha'.length.
1746 CompilationUnit compilationUnit = resolveSource('''
1747 const int s1 = 'alpha';
1748 const int s2 = 'beta';
1749 const int i = (true ? s1 : s2).length;
1750 ''');
1751 ConstTopLevelVariableElementImpl element =
1752 findTopLevelDeclaration(compilationUnit, 'i').element;
1753 EvaluationResultImpl result = element.evaluationResult;
1754 expect(_assertValidInt(result), 5);
1755 }
1756
1757 void test_length_of_improperly_typed_string_identifier() {
1758 // Since type annotations are ignored in unchecked mode, the improper type
1759 // on s shouldn't prevent us from evaluating i to 'alpha'.length.
1760 CompilationUnit compilationUnit = resolveSource('''
1761 const int s = 'alpha';
1762 const int i = s.length;
1763 ''');
1764 ConstTopLevelVariableElementImpl element =
1765 findTopLevelDeclaration(compilationUnit, 'i').element;
1766 EvaluationResultImpl result = element.evaluationResult;
1767 expect(_assertValidInt(result), 5);
1768 }
1769
1770 void test_non_static_const_initialized_at_declaration() {
1771 // Even though non-static consts are not allowed by the language, we need
1772 // to handle them for error recovery purposes.
1773 CompilationUnit compilationUnit = resolveSource('''
1774 class A {
1775 const int i = 123;
1776 const A();
1777 }
1778
1779 const A a = const A();
1780 ''');
1781 EvaluationResultImpl result =
1782 _evaluateTopLevelVariable(compilationUnit, 'a');
1783 Map<String, DartObjectImpl> fields = _assertType(result, "A");
1784 expect(fields, hasLength(1));
1785 _assertIntField(fields, "i", 123);
1786 }
1787
1788 void test_symbolLiteral_void() {
1789 CompilationUnit compilationUnit =
1790 resolveSource("const voidSymbol = #void;");
1791 VariableDeclaration voidSymbol =
1792 findTopLevelDeclaration(compilationUnit, "voidSymbol");
1793 EvaluationResultImpl voidSymbolResult =
1794 (voidSymbol.element as VariableElementImpl).evaluationResult;
1795 DartObjectImpl value = voidSymbolResult.value;
1796 expect(value.type, typeProvider.symbolType);
1797 expect(value.toSymbolValue(), "void");
1798 }
1799
1800 Map<String, DartObjectImpl> _assertFieldType(
1801 Map<String, DartObjectImpl> fields,
1802 String fieldName,
1803 String expectedType) {
1804 DartObjectImpl field = fields[fieldName];
1805 expect(field.type.displayName, expectedType);
1806 return field.fields;
1807 }
1808
1809 void _assertIntField(
1810 Map<String, DartObjectImpl> fields, String fieldName, int expectedValue) {
1811 DartObjectImpl field = fields[fieldName];
1812 expect(field.type.name, "int");
1813 expect(field.toIntValue(), expectedValue);
1814 }
1815
1816 void _assertNullField(Map<String, DartObjectImpl> fields, String fieldName) {
1817 DartObjectImpl field = fields[fieldName];
1818 expect(field.isNull, isTrue);
1819 }
1820
1821 void _assertProperDependencies(String sourceText,
1822 [List<ErrorCode> expectedErrorCodes = ErrorCode.EMPTY_LIST]) {
1823 Source source = addSource(sourceText);
1824 LibraryElement element = resolve2(source);
1825 CompilationUnit unit =
1826 analysisContext.resolveCompilationUnit(source, element);
1827 expect(unit, isNotNull);
1828 ConstantValueComputer computer = _makeConstantValueComputer();
1829 computer.add(unit, source, source);
1830 computer.computeValues();
1831 assertErrors(source, expectedErrorCodes);
1832 }
1833
1834 Map<String, DartObjectImpl> _assertType(
1835 EvaluationResultImpl result, String typeName) {
1836 expect(result.value, isNotNull);
1837 DartObjectImpl value = result.value;
1838 expect(value.type.displayName, typeName);
1839 return value.fields;
1840 }
1841
1842 bool _assertValidBool(EvaluationResultImpl result) {
1843 expect(result.value, isNotNull);
1844 DartObjectImpl value = result.value;
1845 expect(value.type, typeProvider.boolType);
1846 bool boolValue = value.toBoolValue();
1847 expect(boolValue, isNotNull);
1848 return boolValue;
1849 }
1850
1851 int _assertValidInt(EvaluationResultImpl result) {
1852 expect(result, isNotNull);
1853 expect(result.value, isNotNull);
1854 DartObjectImpl value = result.value;
1855 expect(value.type, typeProvider.intType);
1856 return value.toIntValue();
1857 }
1858
1859 void _assertValidNull(EvaluationResultImpl result) {
1860 expect(result.value, isNotNull);
1861 DartObjectImpl value = result.value;
1862 expect(value.type, typeProvider.nullType);
1863 }
1864
1865 String _assertValidString(EvaluationResultImpl result) {
1866 expect(result.value, isNotNull);
1867 DartObjectImpl value = result.value;
1868 expect(value.type, typeProvider.stringType);
1869 return value.toStringValue();
1870 }
1871
1872 void _assertValidUnknown(EvaluationResultImpl result) {
1873 expect(result.value, isNotNull);
1874 DartObjectImpl value = result.value;
1875 expect(value.isUnknown, isTrue);
1876 }
1877
1878 EvaluationResultImpl _check_fromEnvironment_bool(
1879 String valueInEnvironment, String defaultExpr) {
1880 String envVarName = "x";
1881 String varName = "foo";
1882 if (valueInEnvironment != null) {
1883 analysisContext2.declaredVariables.define(envVarName, valueInEnvironment);
1884 }
1885 String defaultArg =
1886 defaultExpr == null ? "" : ", defaultValue: $defaultExpr";
1887 CompilationUnit compilationUnit = resolveSource(
1888 "const $varName = const bool.fromEnvironment('$envVarName'$defaultArg);" );
1889 return _evaluateTopLevelVariable(compilationUnit, varName);
1890 }
1891
1892 EvaluationResultImpl _check_fromEnvironment_int(
1893 String valueInEnvironment, String defaultExpr) {
1894 String envVarName = "x";
1895 String varName = "foo";
1896 if (valueInEnvironment != null) {
1897 analysisContext2.declaredVariables.define(envVarName, valueInEnvironment);
1898 }
1899 String defaultArg =
1900 defaultExpr == null ? "" : ", defaultValue: $defaultExpr";
1901 CompilationUnit compilationUnit = resolveSource(
1902 "const $varName = const int.fromEnvironment('$envVarName'$defaultArg);") ;
1903 return _evaluateTopLevelVariable(compilationUnit, varName);
1904 }
1905
1906 EvaluationResultImpl _check_fromEnvironment_string(
1907 String valueInEnvironment, String defaultExpr) {
1908 String envVarName = "x";
1909 String varName = "foo";
1910 if (valueInEnvironment != null) {
1911 analysisContext2.declaredVariables.define(envVarName, valueInEnvironment);
1912 }
1913 String defaultArg =
1914 defaultExpr == null ? "" : ", defaultValue: $defaultExpr";
1915 CompilationUnit compilationUnit = resolveSource(
1916 "const $varName = const String.fromEnvironment('$envVarName'$defaultArg) ;");
1917 return _evaluateTopLevelVariable(compilationUnit, varName);
1918 }
1919
1920 void _checkInstanceCreation_withSupertypeParams(bool isExplicit) {
1921 String superCall = isExplicit ? " : super()" : "";
1922 CompilationUnit compilationUnit = resolveSource("""
1923 class A<T> {
1924 const A();
1925 }
1926 class B<T, U> extends A<T> {
1927 const B()$superCall;
1928 }
1929 class C<T, U> extends A<U> {
1930 const C()$superCall;
1931 }
1932 const b_int_num = const B<int, num>();
1933 const c_int_num = const C<int, num>();""");
1934 EvaluationResultImpl b_int_num =
1935 _evaluateTopLevelVariable(compilationUnit, "b_int_num");
1936 Map<String, DartObjectImpl> b_int_num_fields =
1937 _assertType(b_int_num, "B<int, num>");
1938 _assertFieldType(b_int_num_fields, GenericState.SUPERCLASS_FIELD, "A<int>");
1939 EvaluationResultImpl c_int_num =
1940 _evaluateTopLevelVariable(compilationUnit, "c_int_num");
1941 Map<String, DartObjectImpl> c_int_num_fields =
1942 _assertType(c_int_num, "C<int, num>");
1943 _assertFieldType(c_int_num_fields, GenericState.SUPERCLASS_FIELD, "A<num>");
1944 }
1945
1946 void _checkInstanceCreationOptionalParams(
1947 bool isFieldFormal, bool isNamed, bool hasDefault) {
1948 String fieldName = "j";
1949 String paramName = isFieldFormal ? fieldName : "i";
1950 String formalParam =
1951 "${isFieldFormal ? "this." : "int "}$paramName${hasDefault ? " = 3" : "" }";
1952 CompilationUnit compilationUnit = resolveSource("""
1953 const x = const A();
1954 const y = const A(${isNamed ? '$paramName: ' : ''}10);
1955 class A {
1956 const A(${isNamed ? "{$formalParam}" : "[$formalParam]"})${isFieldFormal ? "" : " : $fieldName = $paramName"};
1957 final int $fieldName;
1958 }""");
1959 EvaluationResultImpl x = _evaluateTopLevelVariable(compilationUnit, "x");
1960 Map<String, DartObjectImpl> fieldsOfX = _assertType(x, "A");
1961 expect(fieldsOfX, hasLength(1));
1962 if (hasDefault) {
1963 _assertIntField(fieldsOfX, fieldName, 3);
1964 } else {
1965 _assertNullField(fieldsOfX, fieldName);
1966 }
1967 EvaluationResultImpl y = _evaluateTopLevelVariable(compilationUnit, "y");
1968 Map<String, DartObjectImpl> fieldsOfY = _assertType(y, "A");
1969 expect(fieldsOfY, hasLength(1));
1970 _assertIntField(fieldsOfY, fieldName, 10);
1971 }
1972
1973 /**
1974 * Search [compilationUnit] for a class named [className], containing a
1975 * method [methodName], with exactly one annotation. Return the constant
1976 * value of the annotation.
1977 */
1978 EvaluationResultImpl _evaluateAnnotation(
1979 CompilationUnit compilationUnit, String className, String memberName) {
1980 for (CompilationUnitMember member in compilationUnit.declarations) {
1981 if (member is ClassDeclaration && member.name.name == className) {
1982 for (ClassMember classMember in member.members) {
1983 if (classMember is MethodDeclaration &&
1984 classMember.name.name == memberName) {
1985 expect(classMember.metadata, hasLength(1));
1986 ElementAnnotationImpl elementAnnotation =
1987 classMember.metadata[0].elementAnnotation;
1988 return elementAnnotation.evaluationResult;
1989 }
1990 }
1991 }
1992 }
1993 fail('Class member not found');
1994 return null;
1995 }
1996
1997 EvaluationResultImpl _evaluateTopLevelVariable(
1998 CompilationUnit compilationUnit, String name) {
1999 VariableDeclaration varDecl =
2000 findTopLevelDeclaration(compilationUnit, name);
2001 ConstTopLevelVariableElementImpl varElement = varDecl.element;
2002 return varElement.evaluationResult;
2003 }
2004
2005 ConstantValueComputer _makeConstantValueComputer() {
2006 ConstantEvaluationValidator_ForTest validator =
2007 new ConstantEvaluationValidator_ForTest(analysisContext2);
2008 validator.computer = new ConstantValueComputer(
2009 analysisContext2,
2010 analysisContext2.typeProvider,
2011 analysisContext2.declaredVariables,
2012 validator,
2013 analysisContext2.typeSystem);
2014 return validator.computer;
2015 }
2016
2017 void _validate(bool shouldBeValid, VariableDeclarationList declarationList) {
2018 for (VariableDeclaration declaration in declarationList.variables) {
2019 VariableElementImpl element = declaration.element as VariableElementImpl;
2020 expect(element, isNotNull);
2021 EvaluationResultImpl result = element.evaluationResult;
2022 if (shouldBeValid) {
2023 expect(result.value, isNotNull);
2024 } else {
2025 expect(result.value, isNull);
2026 }
2027 }
2028 }
2029 }
2030
2031 @reflectiveTest
2032 class ConstantVisitorTest extends ResolverTestCase {
2033 void test_visitBinaryExpression_questionQuestion_notNull_notNull() {
2034 Expression left = AstFactory.string2('a');
2035 Expression right = AstFactory.string2('b');
2036 Expression expression =
2037 AstFactory.binaryExpression(left, TokenType.QUESTION_QUESTION, right);
2038
2039 GatheringErrorListener errorListener = new GatheringErrorListener();
2040 ErrorReporter errorReporter =
2041 new ErrorReporter(errorListener, _dummySource());
2042 DartObjectImpl result = _evaluate(expression, errorReporter);
2043 expect(result, isNotNull);
2044 expect(result.isNull, isFalse);
2045 expect(result.toStringValue(), 'a');
2046 errorListener.assertNoErrors();
2047 }
2048
2049 void test_visitBinaryExpression_questionQuestion_null_notNull() {
2050 Expression left = AstFactory.nullLiteral();
2051 Expression right = AstFactory.string2('b');
2052 Expression expression =
2053 AstFactory.binaryExpression(left, TokenType.QUESTION_QUESTION, right);
2054
2055 GatheringErrorListener errorListener = new GatheringErrorListener();
2056 ErrorReporter errorReporter =
2057 new ErrorReporter(errorListener, _dummySource());
2058 DartObjectImpl result = _evaluate(expression, errorReporter);
2059 expect(result, isNotNull);
2060 expect(result.isNull, isFalse);
2061 expect(result.toStringValue(), 'b');
2062 errorListener.assertNoErrors();
2063 }
2064
2065 void test_visitBinaryExpression_questionQuestion_null_null() {
2066 Expression left = AstFactory.nullLiteral();
2067 Expression right = AstFactory.nullLiteral();
2068 Expression expression =
2069 AstFactory.binaryExpression(left, TokenType.QUESTION_QUESTION, right);
2070
2071 GatheringErrorListener errorListener = new GatheringErrorListener();
2072 ErrorReporter errorReporter =
2073 new ErrorReporter(errorListener, _dummySource());
2074 DartObjectImpl result = _evaluate(expression, errorReporter);
2075 expect(result, isNotNull);
2076 expect(result.isNull, isTrue);
2077 errorListener.assertNoErrors();
2078 }
2079
2080 void test_visitConditionalExpression_false() {
2081 Expression thenExpression = AstFactory.integer(1);
2082 Expression elseExpression = AstFactory.integer(0);
2083 ConditionalExpression expression = AstFactory.conditionalExpression(
2084 AstFactory.booleanLiteral(false), thenExpression, elseExpression);
2085 GatheringErrorListener errorListener = new GatheringErrorListener();
2086 ErrorReporter errorReporter =
2087 new ErrorReporter(errorListener, _dummySource());
2088 _assertValue(0, _evaluate(expression, errorReporter));
2089 errorListener.assertNoErrors();
2090 }
2091
2092 void test_visitConditionalExpression_nonBooleanCondition() {
2093 Expression thenExpression = AstFactory.integer(1);
2094 Expression elseExpression = AstFactory.integer(0);
2095 NullLiteral conditionExpression = AstFactory.nullLiteral();
2096 ConditionalExpression expression = AstFactory.conditionalExpression(
2097 conditionExpression, thenExpression, elseExpression);
2098 GatheringErrorListener errorListener = new GatheringErrorListener();
2099 ErrorReporter errorReporter =
2100 new ErrorReporter(errorListener, _dummySource());
2101 DartObjectImpl result = _evaluate(expression, errorReporter);
2102 expect(result, isNull);
2103 errorListener
2104 .assertErrorsWithCodes([CompileTimeErrorCode.CONST_EVAL_TYPE_BOOL]);
2105 }
2106
2107 void test_visitConditionalExpression_nonConstantElse() {
2108 Expression thenExpression = AstFactory.integer(1);
2109 Expression elseExpression = AstFactory.identifier3("x");
2110 ConditionalExpression expression = AstFactory.conditionalExpression(
2111 AstFactory.booleanLiteral(true), thenExpression, elseExpression);
2112 GatheringErrorListener errorListener = new GatheringErrorListener();
2113 ErrorReporter errorReporter =
2114 new ErrorReporter(errorListener, _dummySource());
2115 DartObjectImpl result = _evaluate(expression, errorReporter);
2116 expect(result, isNull);
2117 errorListener
2118 .assertErrorsWithCodes([CompileTimeErrorCode.INVALID_CONSTANT]);
2119 }
2120
2121 void test_visitConditionalExpression_nonConstantThen() {
2122 Expression thenExpression = AstFactory.identifier3("x");
2123 Expression elseExpression = AstFactory.integer(0);
2124 ConditionalExpression expression = AstFactory.conditionalExpression(
2125 AstFactory.booleanLiteral(true), thenExpression, elseExpression);
2126 GatheringErrorListener errorListener = new GatheringErrorListener();
2127 ErrorReporter errorReporter =
2128 new ErrorReporter(errorListener, _dummySource());
2129 DartObjectImpl result = _evaluate(expression, errorReporter);
2130 expect(result, isNull);
2131 errorListener
2132 .assertErrorsWithCodes([CompileTimeErrorCode.INVALID_CONSTANT]);
2133 }
2134
2135 void test_visitConditionalExpression_true() {
2136 Expression thenExpression = AstFactory.integer(1);
2137 Expression elseExpression = AstFactory.integer(0);
2138 ConditionalExpression expression = AstFactory.conditionalExpression(
2139 AstFactory.booleanLiteral(true), thenExpression, elseExpression);
2140 GatheringErrorListener errorListener = new GatheringErrorListener();
2141 ErrorReporter errorReporter =
2142 new ErrorReporter(errorListener, _dummySource());
2143 _assertValue(1, _evaluate(expression, errorReporter));
2144 errorListener.assertNoErrors();
2145 }
2146
2147 void test_visitSimpleIdentifier_className() {
2148 CompilationUnit compilationUnit = resolveSource('''
2149 const a = C;
2150 class C {}
2151 ''');
2152 DartObjectImpl result = _evaluateConstant(compilationUnit, 'a', null);
2153 expect(result.type, typeProvider.typeType);
2154 expect(result.toTypeValue().name, 'C');
2155 }
2156
2157 void test_visitSimpleIdentifier_dynamic() {
2158 CompilationUnit compilationUnit = resolveSource('''
2159 const a = dynamic;
2160 ''');
2161 DartObjectImpl result = _evaluateConstant(compilationUnit, 'a', null);
2162 expect(result.type, typeProvider.typeType);
2163 expect(result.toTypeValue(), typeProvider.dynamicType);
2164 }
2165
2166 void test_visitSimpleIdentifier_inEnvironment() {
2167 CompilationUnit compilationUnit = resolveSource(r'''
2168 const a = b;
2169 const b = 3;''');
2170 Map<String, DartObjectImpl> environment = new Map<String, DartObjectImpl>();
2171 DartObjectImpl six =
2172 new DartObjectImpl(typeProvider.intType, new IntState(6));
2173 environment["b"] = six;
2174 _assertValue(6, _evaluateConstant(compilationUnit, "a", environment));
2175 }
2176
2177 void test_visitSimpleIdentifier_notInEnvironment() {
2178 CompilationUnit compilationUnit = resolveSource(r'''
2179 const a = b;
2180 const b = 3;''');
2181 Map<String, DartObjectImpl> environment = new Map<String, DartObjectImpl>();
2182 DartObjectImpl six =
2183 new DartObjectImpl(typeProvider.intType, new IntState(6));
2184 environment["c"] = six;
2185 _assertValue(3, _evaluateConstant(compilationUnit, "a", environment));
2186 }
2187
2188 void test_visitSimpleIdentifier_withoutEnvironment() {
2189 CompilationUnit compilationUnit = resolveSource(r'''
2190 const a = b;
2191 const b = 3;''');
2192 _assertValue(3, _evaluateConstant(compilationUnit, "a", null));
2193 }
2194
2195 void _assertValue(int expectedValue, DartObjectImpl result) {
2196 expect(result, isNotNull);
2197 expect(result.type.name, "int");
2198 expect(result.toIntValue(), expectedValue);
2199 }
2200
2201 NonExistingSource _dummySource() {
2202 String path = '/test.dart';
2203 return new NonExistingSource(path, toUri(path), UriKind.FILE_URI);
2204 }
2205
2206 DartObjectImpl _evaluate(Expression expression, ErrorReporter errorReporter) {
2207 return expression.accept(new ConstantVisitor(
2208 new ConstantEvaluationEngine(
2209 new TestTypeProvider(), new DeclaredVariables(),
2210 typeSystem: new TypeSystemImpl()),
2211 errorReporter));
2212 }
2213
2214 DartObjectImpl _evaluateConstant(CompilationUnit compilationUnit, String name,
2215 Map<String, DartObjectImpl> lexicalEnvironment) {
2216 Source source = compilationUnit.element.source;
2217 Expression expression =
2218 findTopLevelConstantExpression(compilationUnit, name);
2219 GatheringErrorListener errorListener = new GatheringErrorListener();
2220 ErrorReporter errorReporter = new ErrorReporter(errorListener, source);
2221 DartObjectImpl result = expression.accept(new ConstantVisitor(
2222 new ConstantEvaluationEngine(typeProvider, new DeclaredVariables(),
2223 typeSystem: typeSystem),
2224 errorReporter,
2225 lexicalEnvironment: lexicalEnvironment));
2226 errorListener.assertNoErrors();
2227 return result;
2228 }
2229 }
2230
2231 @reflectiveTest
2232 class DartObjectImplTest extends EngineTestCase {
2233 TypeProvider _typeProvider = new TestTypeProvider();
2234
2235 void test_add_knownDouble_knownDouble() {
2236 _assertAdd(_doubleValue(3.0), _doubleValue(1.0), _doubleValue(2.0));
2237 }
2238
2239 void test_add_knownDouble_knownInt() {
2240 _assertAdd(_doubleValue(3.0), _doubleValue(1.0), _intValue(2));
2241 }
2242
2243 void test_add_knownDouble_unknownDouble() {
2244 _assertAdd(_doubleValue(null), _doubleValue(1.0), _doubleValue(null));
2245 }
2246
2247 void test_add_knownDouble_unknownInt() {
2248 _assertAdd(_doubleValue(null), _doubleValue(1.0), _intValue(null));
2249 }
2250
2251 void test_add_knownInt_knownInt() {
2252 _assertAdd(_intValue(3), _intValue(1), _intValue(2));
2253 }
2254
2255 void test_add_knownInt_knownString() {
2256 _assertAdd(null, _intValue(1), _stringValue("2"));
2257 }
2258
2259 void test_add_knownInt_unknownDouble() {
2260 _assertAdd(_doubleValue(null), _intValue(1), _doubleValue(null));
2261 }
2262
2263 void test_add_knownInt_unknownInt() {
2264 _assertAdd(_intValue(null), _intValue(1), _intValue(null));
2265 }
2266
2267 void test_add_knownString_knownInt() {
2268 _assertAdd(null, _stringValue("1"), _intValue(2));
2269 }
2270
2271 void test_add_knownString_knownString() {
2272 _assertAdd(_stringValue("ab"), _stringValue("a"), _stringValue("b"));
2273 }
2274
2275 void test_add_knownString_unknownString() {
2276 _assertAdd(_stringValue(null), _stringValue("a"), _stringValue(null));
2277 }
2278
2279 void test_add_unknownDouble_knownDouble() {
2280 _assertAdd(_doubleValue(null), _doubleValue(null), _doubleValue(2.0));
2281 }
2282
2283 void test_add_unknownDouble_knownInt() {
2284 _assertAdd(_doubleValue(null), _doubleValue(null), _intValue(2));
2285 }
2286
2287 void test_add_unknownInt_knownDouble() {
2288 _assertAdd(_doubleValue(null), _intValue(null), _doubleValue(2.0));
2289 }
2290
2291 void test_add_unknownInt_knownInt() {
2292 _assertAdd(_intValue(null), _intValue(null), _intValue(2));
2293 }
2294
2295 void test_add_unknownString_knownString() {
2296 _assertAdd(_stringValue(null), _stringValue(null), _stringValue("b"));
2297 }
2298
2299 void test_add_unknownString_unknownString() {
2300 _assertAdd(_stringValue(null), _stringValue(null), _stringValue(null));
2301 }
2302
2303 void test_bitAnd_knownInt_knownInt() {
2304 _assertBitAnd(_intValue(2), _intValue(6), _intValue(3));
2305 }
2306
2307 void test_bitAnd_knownInt_knownString() {
2308 _assertBitAnd(null, _intValue(6), _stringValue("3"));
2309 }
2310
2311 void test_bitAnd_knownInt_unknownInt() {
2312 _assertBitAnd(_intValue(null), _intValue(6), _intValue(null));
2313 }
2314
2315 void test_bitAnd_knownString_knownInt() {
2316 _assertBitAnd(null, _stringValue("6"), _intValue(3));
2317 }
2318
2319 void test_bitAnd_unknownInt_knownInt() {
2320 _assertBitAnd(_intValue(null), _intValue(null), _intValue(3));
2321 }
2322
2323 void test_bitAnd_unknownInt_unknownInt() {
2324 _assertBitAnd(_intValue(null), _intValue(null), _intValue(null));
2325 }
2326
2327 void test_bitNot_knownInt() {
2328 _assertBitNot(_intValue(-4), _intValue(3));
2329 }
2330
2331 void test_bitNot_knownString() {
2332 _assertBitNot(null, _stringValue("6"));
2333 }
2334
2335 void test_bitNot_unknownInt() {
2336 _assertBitNot(_intValue(null), _intValue(null));
2337 }
2338
2339 void test_bitOr_knownInt_knownInt() {
2340 _assertBitOr(_intValue(7), _intValue(6), _intValue(3));
2341 }
2342
2343 void test_bitOr_knownInt_knownString() {
2344 _assertBitOr(null, _intValue(6), _stringValue("3"));
2345 }
2346
2347 void test_bitOr_knownInt_unknownInt() {
2348 _assertBitOr(_intValue(null), _intValue(6), _intValue(null));
2349 }
2350
2351 void test_bitOr_knownString_knownInt() {
2352 _assertBitOr(null, _stringValue("6"), _intValue(3));
2353 }
2354
2355 void test_bitOr_unknownInt_knownInt() {
2356 _assertBitOr(_intValue(null), _intValue(null), _intValue(3));
2357 }
2358
2359 void test_bitOr_unknownInt_unknownInt() {
2360 _assertBitOr(_intValue(null), _intValue(null), _intValue(null));
2361 }
2362
2363 void test_bitXor_knownInt_knownInt() {
2364 _assertBitXor(_intValue(5), _intValue(6), _intValue(3));
2365 }
2366
2367 void test_bitXor_knownInt_knownString() {
2368 _assertBitXor(null, _intValue(6), _stringValue("3"));
2369 }
2370
2371 void test_bitXor_knownInt_unknownInt() {
2372 _assertBitXor(_intValue(null), _intValue(6), _intValue(null));
2373 }
2374
2375 void test_bitXor_knownString_knownInt() {
2376 _assertBitXor(null, _stringValue("6"), _intValue(3));
2377 }
2378
2379 void test_bitXor_unknownInt_knownInt() {
2380 _assertBitXor(_intValue(null), _intValue(null), _intValue(3));
2381 }
2382
2383 void test_bitXor_unknownInt_unknownInt() {
2384 _assertBitXor(_intValue(null), _intValue(null), _intValue(null));
2385 }
2386
2387 void test_concatenate_knownInt_knownString() {
2388 _assertConcatenate(null, _intValue(2), _stringValue("def"));
2389 }
2390
2391 void test_concatenate_knownString_knownInt() {
2392 _assertConcatenate(null, _stringValue("abc"), _intValue(3));
2393 }
2394
2395 void test_concatenate_knownString_knownString() {
2396 _assertConcatenate(
2397 _stringValue("abcdef"), _stringValue("abc"), _stringValue("def"));
2398 }
2399
2400 void test_concatenate_knownString_unknownString() {
2401 _assertConcatenate(
2402 _stringValue(null), _stringValue("abc"), _stringValue(null));
2403 }
2404
2405 void test_concatenate_unknownString_knownString() {
2406 _assertConcatenate(
2407 _stringValue(null), _stringValue(null), _stringValue("def"));
2408 }
2409
2410 void test_divide_knownDouble_knownDouble() {
2411 _assertDivide(_doubleValue(3.0), _doubleValue(6.0), _doubleValue(2.0));
2412 }
2413
2414 void test_divide_knownDouble_knownInt() {
2415 _assertDivide(_doubleValue(3.0), _doubleValue(6.0), _intValue(2));
2416 }
2417
2418 void test_divide_knownDouble_unknownDouble() {
2419 _assertDivide(_doubleValue(null), _doubleValue(6.0), _doubleValue(null));
2420 }
2421
2422 void test_divide_knownDouble_unknownInt() {
2423 _assertDivide(_doubleValue(null), _doubleValue(6.0), _intValue(null));
2424 }
2425
2426 void test_divide_knownInt_knownInt() {
2427 _assertDivide(_doubleValue(3.0), _intValue(6), _intValue(2));
2428 }
2429
2430 void test_divide_knownInt_knownString() {
2431 _assertDivide(null, _intValue(6), _stringValue("2"));
2432 }
2433
2434 void test_divide_knownInt_unknownDouble() {
2435 _assertDivide(_doubleValue(null), _intValue(6), _doubleValue(null));
2436 }
2437
2438 void test_divide_knownInt_unknownInt() {
2439 _assertDivide(_doubleValue(null), _intValue(6), _intValue(null));
2440 }
2441
2442 void test_divide_knownString_knownInt() {
2443 _assertDivide(null, _stringValue("6"), _intValue(2));
2444 }
2445
2446 void test_divide_unknownDouble_knownDouble() {
2447 _assertDivide(_doubleValue(null), _doubleValue(null), _doubleValue(2.0));
2448 }
2449
2450 void test_divide_unknownDouble_knownInt() {
2451 _assertDivide(_doubleValue(null), _doubleValue(null), _intValue(2));
2452 }
2453
2454 void test_divide_unknownInt_knownDouble() {
2455 _assertDivide(_doubleValue(null), _intValue(null), _doubleValue(2.0));
2456 }
2457
2458 void test_divide_unknownInt_knownInt() {
2459 _assertDivide(_doubleValue(null), _intValue(null), _intValue(2));
2460 }
2461
2462 void test_equalEqual_bool_false() {
2463 _assertEqualEqual(_boolValue(false), _boolValue(false), _boolValue(true));
2464 }
2465
2466 void test_equalEqual_bool_true() {
2467 _assertEqualEqual(_boolValue(true), _boolValue(true), _boolValue(true));
2468 }
2469
2470 void test_equalEqual_bool_unknown() {
2471 _assertEqualEqual(_boolValue(null), _boolValue(null), _boolValue(false));
2472 }
2473
2474 void test_equalEqual_double_false() {
2475 _assertEqualEqual(_boolValue(false), _doubleValue(2.0), _doubleValue(4.0));
2476 }
2477
2478 void test_equalEqual_double_true() {
2479 _assertEqualEqual(_boolValue(true), _doubleValue(2.0), _doubleValue(2.0));
2480 }
2481
2482 void test_equalEqual_double_unknown() {
2483 _assertEqualEqual(_boolValue(null), _doubleValue(1.0), _doubleValue(null));
2484 }
2485
2486 void test_equalEqual_int_false() {
2487 _assertEqualEqual(_boolValue(false), _intValue(-5), _intValue(5));
2488 }
2489
2490 void test_equalEqual_int_true() {
2491 _assertEqualEqual(_boolValue(true), _intValue(5), _intValue(5));
2492 }
2493
2494 void test_equalEqual_int_unknown() {
2495 _assertEqualEqual(_boolValue(null), _intValue(null), _intValue(3));
2496 }
2497
2498 void test_equalEqual_list_empty() {
2499 _assertEqualEqual(null, _listValue(), _listValue());
2500 }
2501
2502 void test_equalEqual_list_false() {
2503 _assertEqualEqual(null, _listValue(), _listValue());
2504 }
2505
2506 void test_equalEqual_map_empty() {
2507 _assertEqualEqual(null, _mapValue(), _mapValue());
2508 }
2509
2510 void test_equalEqual_map_false() {
2511 _assertEqualEqual(null, _mapValue(), _mapValue());
2512 }
2513
2514 void test_equalEqual_null() {
2515 _assertEqualEqual(_boolValue(true), _nullValue(), _nullValue());
2516 }
2517
2518 void test_equalEqual_string_false() {
2519 _assertEqualEqual(
2520 _boolValue(false), _stringValue("abc"), _stringValue("def"));
2521 }
2522
2523 void test_equalEqual_string_true() {
2524 _assertEqualEqual(
2525 _boolValue(true), _stringValue("abc"), _stringValue("abc"));
2526 }
2527
2528 void test_equalEqual_string_unknown() {
2529 _assertEqualEqual(
2530 _boolValue(null), _stringValue(null), _stringValue("def"));
2531 }
2532
2533 void test_equals_list_false_differentSizes() {
2534 expect(
2535 _listValue([_boolValue(true)]) ==
2536 _listValue([_boolValue(true), _boolValue(false)]),
2537 isFalse);
2538 }
2539
2540 void test_equals_list_false_sameSize() {
2541 expect(_listValue([_boolValue(true)]) == _listValue([_boolValue(false)]),
2542 isFalse);
2543 }
2544
2545 void test_equals_list_true_empty() {
2546 expect(_listValue(), _listValue());
2547 }
2548
2549 void test_equals_list_true_nonEmpty() {
2550 expect(_listValue([_boolValue(true)]), _listValue([_boolValue(true)]));
2551 }
2552
2553 void test_equals_map_true_empty() {
2554 expect(_mapValue(), _mapValue());
2555 }
2556
2557 void test_equals_symbol_false() {
2558 expect(_symbolValue("a") == _symbolValue("b"), isFalse);
2559 }
2560
2561 void test_equals_symbol_true() {
2562 expect(_symbolValue("a"), _symbolValue("a"));
2563 }
2564
2565 void test_getValue_bool_false() {
2566 expect(_boolValue(false).toBoolValue(), false);
2567 }
2568
2569 void test_getValue_bool_true() {
2570 expect(_boolValue(true).toBoolValue(), true);
2571 }
2572
2573 void test_getValue_bool_unknown() {
2574 expect(_boolValue(null).toBoolValue(), isNull);
2575 }
2576
2577 void test_getValue_double_known() {
2578 double value = 2.3;
2579 expect(_doubleValue(value).toDoubleValue(), value);
2580 }
2581
2582 void test_getValue_double_unknown() {
2583 expect(_doubleValue(null).toDoubleValue(), isNull);
2584 }
2585
2586 void test_getValue_int_known() {
2587 int value = 23;
2588 expect(_intValue(value).toIntValue(), value);
2589 }
2590
2591 void test_getValue_int_unknown() {
2592 expect(_intValue(null).toIntValue(), isNull);
2593 }
2594
2595 void test_getValue_list_empty() {
2596 Object result = _listValue().toListValue();
2597 _assertInstanceOfObjectArray(result);
2598 List<Object> array = result as List<Object>;
2599 expect(array, hasLength(0));
2600 }
2601
2602 void test_getValue_list_valid() {
2603 Object result = _listValue([_intValue(23)]).toListValue();
2604 _assertInstanceOfObjectArray(result);
2605 List<Object> array = result as List<Object>;
2606 expect(array, hasLength(1));
2607 }
2608
2609 void test_getValue_map_empty() {
2610 Object result = _mapValue().toMapValue();
2611 EngineTestCase.assertInstanceOf((obj) => obj is Map, Map, result);
2612 Map map = result as Map;
2613 expect(map, hasLength(0));
2614 }
2615
2616 void test_getValue_map_valid() {
2617 Object result =
2618 _mapValue([_stringValue("key"), _stringValue("value")]).toMapValue();
2619 EngineTestCase.assertInstanceOf((obj) => obj is Map, Map, result);
2620 Map map = result as Map;
2621 expect(map, hasLength(1));
2622 }
2623
2624 void test_getValue_null() {
2625 expect(_nullValue().isNull, isTrue);
2626 }
2627
2628 void test_getValue_string_known() {
2629 String value = "twenty-three";
2630 expect(_stringValue(value).toStringValue(), value);
2631 }
2632
2633 void test_getValue_string_unknown() {
2634 expect(_stringValue(null).toStringValue(), isNull);
2635 }
2636
2637 void test_greaterThan_knownDouble_knownDouble_false() {
2638 _assertGreaterThan(_boolValue(false), _doubleValue(1.0), _doubleValue(2.0));
2639 }
2640
2641 void test_greaterThan_knownDouble_knownDouble_true() {
2642 _assertGreaterThan(_boolValue(true), _doubleValue(2.0), _doubleValue(1.0));
2643 }
2644
2645 void test_greaterThan_knownDouble_knownInt_false() {
2646 _assertGreaterThan(_boolValue(false), _doubleValue(1.0), _intValue(2));
2647 }
2648
2649 void test_greaterThan_knownDouble_knownInt_true() {
2650 _assertGreaterThan(_boolValue(true), _doubleValue(2.0), _intValue(1));
2651 }
2652
2653 void test_greaterThan_knownDouble_unknownDouble() {
2654 _assertGreaterThan(_boolValue(null), _doubleValue(1.0), _doubleValue(null));
2655 }
2656
2657 void test_greaterThan_knownDouble_unknownInt() {
2658 _assertGreaterThan(_boolValue(null), _doubleValue(1.0), _intValue(null));
2659 }
2660
2661 void test_greaterThan_knownInt_knownInt_false() {
2662 _assertGreaterThan(_boolValue(false), _intValue(1), _intValue(2));
2663 }
2664
2665 void test_greaterThan_knownInt_knownInt_true() {
2666 _assertGreaterThan(_boolValue(true), _intValue(2), _intValue(1));
2667 }
2668
2669 void test_greaterThan_knownInt_knownString() {
2670 _assertGreaterThan(null, _intValue(1), _stringValue("2"));
2671 }
2672
2673 void test_greaterThan_knownInt_unknownDouble() {
2674 _assertGreaterThan(_boolValue(null), _intValue(1), _doubleValue(null));
2675 }
2676
2677 void test_greaterThan_knownInt_unknownInt() {
2678 _assertGreaterThan(_boolValue(null), _intValue(1), _intValue(null));
2679 }
2680
2681 void test_greaterThan_knownString_knownInt() {
2682 _assertGreaterThan(null, _stringValue("1"), _intValue(2));
2683 }
2684
2685 void test_greaterThan_unknownDouble_knownDouble() {
2686 _assertGreaterThan(_boolValue(null), _doubleValue(null), _doubleValue(2.0));
2687 }
2688
2689 void test_greaterThan_unknownDouble_knownInt() {
2690 _assertGreaterThan(_boolValue(null), _doubleValue(null), _intValue(2));
2691 }
2692
2693 void test_greaterThan_unknownInt_knownDouble() {
2694 _assertGreaterThan(_boolValue(null), _intValue(null), _doubleValue(2.0));
2695 }
2696
2697 void test_greaterThan_unknownInt_knownInt() {
2698 _assertGreaterThan(_boolValue(null), _intValue(null), _intValue(2));
2699 }
2700
2701 void test_greaterThanOrEqual_knownDouble_knownDouble_false() {
2702 _assertGreaterThanOrEqual(
2703 _boolValue(false), _doubleValue(1.0), _doubleValue(2.0));
2704 }
2705
2706 void test_greaterThanOrEqual_knownDouble_knownDouble_true() {
2707 _assertGreaterThanOrEqual(
2708 _boolValue(true), _doubleValue(2.0), _doubleValue(1.0));
2709 }
2710
2711 void test_greaterThanOrEqual_knownDouble_knownInt_false() {
2712 _assertGreaterThanOrEqual(
2713 _boolValue(false), _doubleValue(1.0), _intValue(2));
2714 }
2715
2716 void test_greaterThanOrEqual_knownDouble_knownInt_true() {
2717 _assertGreaterThanOrEqual(
2718 _boolValue(true), _doubleValue(2.0), _intValue(1));
2719 }
2720
2721 void test_greaterThanOrEqual_knownDouble_unknownDouble() {
2722 _assertGreaterThanOrEqual(
2723 _boolValue(null), _doubleValue(1.0), _doubleValue(null));
2724 }
2725
2726 void test_greaterThanOrEqual_knownDouble_unknownInt() {
2727 _assertGreaterThanOrEqual(
2728 _boolValue(null), _doubleValue(1.0), _intValue(null));
2729 }
2730
2731 void test_greaterThanOrEqual_knownInt_knownInt_false() {
2732 _assertGreaterThanOrEqual(_boolValue(false), _intValue(1), _intValue(2));
2733 }
2734
2735 void test_greaterThanOrEqual_knownInt_knownInt_true() {
2736 _assertGreaterThanOrEqual(_boolValue(true), _intValue(2), _intValue(2));
2737 }
2738
2739 void test_greaterThanOrEqual_knownInt_knownString() {
2740 _assertGreaterThanOrEqual(null, _intValue(1), _stringValue("2"));
2741 }
2742
2743 void test_greaterThanOrEqual_knownInt_unknownDouble() {
2744 _assertGreaterThanOrEqual(
2745 _boolValue(null), _intValue(1), _doubleValue(null));
2746 }
2747
2748 void test_greaterThanOrEqual_knownInt_unknownInt() {
2749 _assertGreaterThanOrEqual(_boolValue(null), _intValue(1), _intValue(null));
2750 }
2751
2752 void test_greaterThanOrEqual_knownString_knownInt() {
2753 _assertGreaterThanOrEqual(null, _stringValue("1"), _intValue(2));
2754 }
2755
2756 void test_greaterThanOrEqual_unknownDouble_knownDouble() {
2757 _assertGreaterThanOrEqual(
2758 _boolValue(null), _doubleValue(null), _doubleValue(2.0));
2759 }
2760
2761 void test_greaterThanOrEqual_unknownDouble_knownInt() {
2762 _assertGreaterThanOrEqual(
2763 _boolValue(null), _doubleValue(null), _intValue(2));
2764 }
2765
2766 void test_greaterThanOrEqual_unknownInt_knownDouble() {
2767 _assertGreaterThanOrEqual(
2768 _boolValue(null), _intValue(null), _doubleValue(2.0));
2769 }
2770
2771 void test_greaterThanOrEqual_unknownInt_knownInt() {
2772 _assertGreaterThanOrEqual(_boolValue(null), _intValue(null), _intValue(2));
2773 }
2774
2775 void test_hasKnownValue_bool_false() {
2776 expect(_boolValue(false).hasKnownValue, isTrue);
2777 }
2778
2779 void test_hasKnownValue_bool_true() {
2780 expect(_boolValue(true).hasKnownValue, isTrue);
2781 }
2782
2783 void test_hasKnownValue_bool_unknown() {
2784 expect(_boolValue(null).hasKnownValue, isFalse);
2785 }
2786
2787 void test_hasKnownValue_double_known() {
2788 expect(_doubleValue(2.3).hasKnownValue, isTrue);
2789 }
2790
2791 void test_hasKnownValue_double_unknown() {
2792 expect(_doubleValue(null).hasKnownValue, isFalse);
2793 }
2794
2795 void test_hasKnownValue_dynamic() {
2796 expect(_dynamicValue().hasKnownValue, isTrue);
2797 }
2798
2799 void test_hasKnownValue_int_known() {
2800 expect(_intValue(23).hasKnownValue, isTrue);
2801 }
2802
2803 void test_hasKnownValue_int_unknown() {
2804 expect(_intValue(null).hasKnownValue, isFalse);
2805 }
2806
2807 void test_hasKnownValue_list_empty() {
2808 expect(_listValue().hasKnownValue, isTrue);
2809 }
2810
2811 void test_hasKnownValue_list_invalidElement() {
2812 expect(_listValue([_dynamicValue]).hasKnownValue, isTrue);
2813 }
2814
2815 void test_hasKnownValue_list_valid() {
2816 expect(_listValue([_intValue(23)]).hasKnownValue, isTrue);
2817 }
2818
2819 void test_hasKnownValue_map_empty() {
2820 expect(_mapValue().hasKnownValue, isTrue);
2821 }
2822
2823 void test_hasKnownValue_map_invalidKey() {
2824 expect(_mapValue([_dynamicValue(), _stringValue("value")]).hasKnownValue,
2825 isTrue);
2826 }
2827
2828 void test_hasKnownValue_map_invalidValue() {
2829 expect(_mapValue([_stringValue("key"), _dynamicValue()]).hasKnownValue,
2830 isTrue);
2831 }
2832
2833 void test_hasKnownValue_map_valid() {
2834 expect(
2835 _mapValue([_stringValue("key"), _stringValue("value")]).hasKnownValue,
2836 isTrue);
2837 }
2838
2839 void test_hasKnownValue_null() {
2840 expect(_nullValue().hasKnownValue, isTrue);
2841 }
2842
2843 void test_hasKnownValue_num() {
2844 expect(_numValue().hasKnownValue, isFalse);
2845 }
2846
2847 void test_hasKnownValue_string_known() {
2848 expect(_stringValue("twenty-three").hasKnownValue, isTrue);
2849 }
2850
2851 void test_hasKnownValue_string_unknown() {
2852 expect(_stringValue(null).hasKnownValue, isFalse);
2853 }
2854
2855 void test_identical_bool_false() {
2856 _assertIdentical(_boolValue(false), _boolValue(false), _boolValue(true));
2857 }
2858
2859 void test_identical_bool_true() {
2860 _assertIdentical(_boolValue(true), _boolValue(true), _boolValue(true));
2861 }
2862
2863 void test_identical_bool_unknown() {
2864 _assertIdentical(_boolValue(null), _boolValue(null), _boolValue(false));
2865 }
2866
2867 void test_identical_double_false() {
2868 _assertIdentical(_boolValue(false), _doubleValue(2.0), _doubleValue(4.0));
2869 }
2870
2871 void test_identical_double_true() {
2872 _assertIdentical(_boolValue(true), _doubleValue(2.0), _doubleValue(2.0));
2873 }
2874
2875 void test_identical_double_unknown() {
2876 _assertIdentical(_boolValue(null), _doubleValue(1.0), _doubleValue(null));
2877 }
2878
2879 void test_identical_int_false() {
2880 _assertIdentical(_boolValue(false), _intValue(-5), _intValue(5));
2881 }
2882
2883 void test_identical_int_true() {
2884 _assertIdentical(_boolValue(true), _intValue(5), _intValue(5));
2885 }
2886
2887 void test_identical_int_unknown() {
2888 _assertIdentical(_boolValue(null), _intValue(null), _intValue(3));
2889 }
2890
2891 void test_identical_list_empty() {
2892 _assertIdentical(_boolValue(true), _listValue(), _listValue());
2893 }
2894
2895 void test_identical_list_false() {
2896 _assertIdentical(
2897 _boolValue(false), _listValue(), _listValue([_intValue(3)]));
2898 }
2899
2900 void test_identical_map_empty() {
2901 _assertIdentical(_boolValue(true), _mapValue(), _mapValue());
2902 }
2903
2904 void test_identical_map_false() {
2905 _assertIdentical(_boolValue(false), _mapValue(),
2906 _mapValue([_intValue(1), _intValue(2)]));
2907 }
2908
2909 void test_identical_null() {
2910 _assertIdentical(_boolValue(true), _nullValue(), _nullValue());
2911 }
2912
2913 void test_identical_string_false() {
2914 _assertIdentical(
2915 _boolValue(false), _stringValue("abc"), _stringValue("def"));
2916 }
2917
2918 void test_identical_string_true() {
2919 _assertIdentical(
2920 _boolValue(true), _stringValue("abc"), _stringValue("abc"));
2921 }
2922
2923 void test_identical_string_unknown() {
2924 _assertIdentical(_boolValue(null), _stringValue(null), _stringValue("def"));
2925 }
2926
2927 void test_integerDivide_knownDouble_knownDouble() {
2928 _assertIntegerDivide(_intValue(3), _doubleValue(6.0), _doubleValue(2.0));
2929 }
2930
2931 void test_integerDivide_knownDouble_knownInt() {
2932 _assertIntegerDivide(_intValue(3), _doubleValue(6.0), _intValue(2));
2933 }
2934
2935 void test_integerDivide_knownDouble_unknownDouble() {
2936 _assertIntegerDivide(
2937 _intValue(null), _doubleValue(6.0), _doubleValue(null));
2938 }
2939
2940 void test_integerDivide_knownDouble_unknownInt() {
2941 _assertIntegerDivide(_intValue(null), _doubleValue(6.0), _intValue(null));
2942 }
2943
2944 void test_integerDivide_knownInt_knownInt() {
2945 _assertIntegerDivide(_intValue(3), _intValue(6), _intValue(2));
2946 }
2947
2948 void test_integerDivide_knownInt_knownString() {
2949 _assertIntegerDivide(null, _intValue(6), _stringValue("2"));
2950 }
2951
2952 void test_integerDivide_knownInt_unknownDouble() {
2953 _assertIntegerDivide(_intValue(null), _intValue(6), _doubleValue(null));
2954 }
2955
2956 void test_integerDivide_knownInt_unknownInt() {
2957 _assertIntegerDivide(_intValue(null), _intValue(6), _intValue(null));
2958 }
2959
2960 void test_integerDivide_knownString_knownInt() {
2961 _assertIntegerDivide(null, _stringValue("6"), _intValue(2));
2962 }
2963
2964 void test_integerDivide_unknownDouble_knownDouble() {
2965 _assertIntegerDivide(
2966 _intValue(null), _doubleValue(null), _doubleValue(2.0));
2967 }
2968
2969 void test_integerDivide_unknownDouble_knownInt() {
2970 _assertIntegerDivide(_intValue(null), _doubleValue(null), _intValue(2));
2971 }
2972
2973 void test_integerDivide_unknownInt_knownDouble() {
2974 _assertIntegerDivide(_intValue(null), _intValue(null), _doubleValue(2.0));
2975 }
2976
2977 void test_integerDivide_unknownInt_knownInt() {
2978 _assertIntegerDivide(_intValue(null), _intValue(null), _intValue(2));
2979 }
2980
2981 void test_isBoolNumStringOrNull_bool_false() {
2982 expect(_boolValue(false).isBoolNumStringOrNull, isTrue);
2983 }
2984
2985 void test_isBoolNumStringOrNull_bool_true() {
2986 expect(_boolValue(true).isBoolNumStringOrNull, isTrue);
2987 }
2988
2989 void test_isBoolNumStringOrNull_bool_unknown() {
2990 expect(_boolValue(null).isBoolNumStringOrNull, isTrue);
2991 }
2992
2993 void test_isBoolNumStringOrNull_double_known() {
2994 expect(_doubleValue(2.3).isBoolNumStringOrNull, isTrue);
2995 }
2996
2997 void test_isBoolNumStringOrNull_double_unknown() {
2998 expect(_doubleValue(null).isBoolNumStringOrNull, isTrue);
2999 }
3000
3001 void test_isBoolNumStringOrNull_dynamic() {
3002 expect(_dynamicValue().isBoolNumStringOrNull, isTrue);
3003 }
3004
3005 void test_isBoolNumStringOrNull_int_known() {
3006 expect(_intValue(23).isBoolNumStringOrNull, isTrue);
3007 }
3008
3009 void test_isBoolNumStringOrNull_int_unknown() {
3010 expect(_intValue(null).isBoolNumStringOrNull, isTrue);
3011 }
3012
3013 void test_isBoolNumStringOrNull_list() {
3014 expect(_listValue().isBoolNumStringOrNull, isFalse);
3015 }
3016
3017 void test_isBoolNumStringOrNull_null() {
3018 expect(_nullValue().isBoolNumStringOrNull, isTrue);
3019 }
3020
3021 void test_isBoolNumStringOrNull_num() {
3022 expect(_numValue().isBoolNumStringOrNull, isTrue);
3023 }
3024
3025 void test_isBoolNumStringOrNull_string_known() {
3026 expect(_stringValue("twenty-three").isBoolNumStringOrNull, isTrue);
3027 }
3028
3029 void test_isBoolNumStringOrNull_string_unknown() {
3030 expect(_stringValue(null).isBoolNumStringOrNull, isTrue);
3031 }
3032
3033 void test_lessThan_knownDouble_knownDouble_false() {
3034 _assertLessThan(_boolValue(false), _doubleValue(2.0), _doubleValue(1.0));
3035 }
3036
3037 void test_lessThan_knownDouble_knownDouble_true() {
3038 _assertLessThan(_boolValue(true), _doubleValue(1.0), _doubleValue(2.0));
3039 }
3040
3041 void test_lessThan_knownDouble_knownInt_false() {
3042 _assertLessThan(_boolValue(false), _doubleValue(2.0), _intValue(1));
3043 }
3044
3045 void test_lessThan_knownDouble_knownInt_true() {
3046 _assertLessThan(_boolValue(true), _doubleValue(1.0), _intValue(2));
3047 }
3048
3049 void test_lessThan_knownDouble_unknownDouble() {
3050 _assertLessThan(_boolValue(null), _doubleValue(1.0), _doubleValue(null));
3051 }
3052
3053 void test_lessThan_knownDouble_unknownInt() {
3054 _assertLessThan(_boolValue(null), _doubleValue(1.0), _intValue(null));
3055 }
3056
3057 void test_lessThan_knownInt_knownInt_false() {
3058 _assertLessThan(_boolValue(false), _intValue(2), _intValue(1));
3059 }
3060
3061 void test_lessThan_knownInt_knownInt_true() {
3062 _assertLessThan(_boolValue(true), _intValue(1), _intValue(2));
3063 }
3064
3065 void test_lessThan_knownInt_knownString() {
3066 _assertLessThan(null, _intValue(1), _stringValue("2"));
3067 }
3068
3069 void test_lessThan_knownInt_unknownDouble() {
3070 _assertLessThan(_boolValue(null), _intValue(1), _doubleValue(null));
3071 }
3072
3073 void test_lessThan_knownInt_unknownInt() {
3074 _assertLessThan(_boolValue(null), _intValue(1), _intValue(null));
3075 }
3076
3077 void test_lessThan_knownString_knownInt() {
3078 _assertLessThan(null, _stringValue("1"), _intValue(2));
3079 }
3080
3081 void test_lessThan_unknownDouble_knownDouble() {
3082 _assertLessThan(_boolValue(null), _doubleValue(null), _doubleValue(2.0));
3083 }
3084
3085 void test_lessThan_unknownDouble_knownInt() {
3086 _assertLessThan(_boolValue(null), _doubleValue(null), _intValue(2));
3087 }
3088
3089 void test_lessThan_unknownInt_knownDouble() {
3090 _assertLessThan(_boolValue(null), _intValue(null), _doubleValue(2.0));
3091 }
3092
3093 void test_lessThan_unknownInt_knownInt() {
3094 _assertLessThan(_boolValue(null), _intValue(null), _intValue(2));
3095 }
3096
3097 void test_lessThanOrEqual_knownDouble_knownDouble_false() {
3098 _assertLessThanOrEqual(
3099 _boolValue(false), _doubleValue(2.0), _doubleValue(1.0));
3100 }
3101
3102 void test_lessThanOrEqual_knownDouble_knownDouble_true() {
3103 _assertLessThanOrEqual(
3104 _boolValue(true), _doubleValue(1.0), _doubleValue(2.0));
3105 }
3106
3107 void test_lessThanOrEqual_knownDouble_knownInt_false() {
3108 _assertLessThanOrEqual(_boolValue(false), _doubleValue(2.0), _intValue(1));
3109 }
3110
3111 void test_lessThanOrEqual_knownDouble_knownInt_true() {
3112 _assertLessThanOrEqual(_boolValue(true), _doubleValue(1.0), _intValue(2));
3113 }
3114
3115 void test_lessThanOrEqual_knownDouble_unknownDouble() {
3116 _assertLessThanOrEqual(
3117 _boolValue(null), _doubleValue(1.0), _doubleValue(null));
3118 }
3119
3120 void test_lessThanOrEqual_knownDouble_unknownInt() {
3121 _assertLessThanOrEqual(
3122 _boolValue(null), _doubleValue(1.0), _intValue(null));
3123 }
3124
3125 void test_lessThanOrEqual_knownInt_knownInt_false() {
3126 _assertLessThanOrEqual(_boolValue(false), _intValue(2), _intValue(1));
3127 }
3128
3129 void test_lessThanOrEqual_knownInt_knownInt_true() {
3130 _assertLessThanOrEqual(_boolValue(true), _intValue(1), _intValue(2));
3131 }
3132
3133 void test_lessThanOrEqual_knownInt_knownString() {
3134 _assertLessThanOrEqual(null, _intValue(1), _stringValue("2"));
3135 }
3136
3137 void test_lessThanOrEqual_knownInt_unknownDouble() {
3138 _assertLessThanOrEqual(_boolValue(null), _intValue(1), _doubleValue(null));
3139 }
3140
3141 void test_lessThanOrEqual_knownInt_unknownInt() {
3142 _assertLessThanOrEqual(_boolValue(null), _intValue(1), _intValue(null));
3143 }
3144
3145 void test_lessThanOrEqual_knownString_knownInt() {
3146 _assertLessThanOrEqual(null, _stringValue("1"), _intValue(2));
3147 }
3148
3149 void test_lessThanOrEqual_unknownDouble_knownDouble() {
3150 _assertLessThanOrEqual(
3151 _boolValue(null), _doubleValue(null), _doubleValue(2.0));
3152 }
3153
3154 void test_lessThanOrEqual_unknownDouble_knownInt() {
3155 _assertLessThanOrEqual(_boolValue(null), _doubleValue(null), _intValue(2));
3156 }
3157
3158 void test_lessThanOrEqual_unknownInt_knownDouble() {
3159 _assertLessThanOrEqual(
3160 _boolValue(null), _intValue(null), _doubleValue(2.0));
3161 }
3162
3163 void test_lessThanOrEqual_unknownInt_knownInt() {
3164 _assertLessThanOrEqual(_boolValue(null), _intValue(null), _intValue(2));
3165 }
3166
3167 void test_logicalAnd_false_false() {
3168 _assertLogicalAnd(_boolValue(false), _boolValue(false), _boolValue(false));
3169 }
3170
3171 void test_logicalAnd_false_null() {
3172 try {
3173 _assertLogicalAnd(_boolValue(false), _boolValue(false), _nullValue());
3174 fail("Expected EvaluationException");
3175 } on EvaluationException {}
3176 }
3177
3178 void test_logicalAnd_false_string() {
3179 try {
3180 _assertLogicalAnd(
3181 _boolValue(false), _boolValue(false), _stringValue("false"));
3182 fail("Expected EvaluationException");
3183 } on EvaluationException {}
3184 }
3185
3186 void test_logicalAnd_false_true() {
3187 _assertLogicalAnd(_boolValue(false), _boolValue(false), _boolValue(true));
3188 }
3189
3190 void test_logicalAnd_null_false() {
3191 try {
3192 _assertLogicalAnd(_boolValue(false), _nullValue(), _boolValue(false));
3193 fail("Expected EvaluationException");
3194 } on EvaluationException {}
3195 }
3196
3197 void test_logicalAnd_null_true() {
3198 try {
3199 _assertLogicalAnd(_boolValue(false), _nullValue(), _boolValue(true));
3200 fail("Expected EvaluationException");
3201 } on EvaluationException {}
3202 }
3203
3204 void test_logicalAnd_string_false() {
3205 try {
3206 _assertLogicalAnd(
3207 _boolValue(false), _stringValue("true"), _boolValue(false));
3208 fail("Expected EvaluationException");
3209 } on EvaluationException {}
3210 }
3211
3212 void test_logicalAnd_string_true() {
3213 try {
3214 _assertLogicalAnd(
3215 _boolValue(false), _stringValue("false"), _boolValue(true));
3216 fail("Expected EvaluationException");
3217 } on EvaluationException {}
3218 }
3219
3220 void test_logicalAnd_true_false() {
3221 _assertLogicalAnd(_boolValue(false), _boolValue(true), _boolValue(false));
3222 }
3223
3224 void test_logicalAnd_true_null() {
3225 _assertLogicalAnd(null, _boolValue(true), _nullValue());
3226 }
3227
3228 void test_logicalAnd_true_string() {
3229 try {
3230 _assertLogicalAnd(
3231 _boolValue(false), _boolValue(true), _stringValue("true"));
3232 fail("Expected EvaluationException");
3233 } on EvaluationException {}
3234 }
3235
3236 void test_logicalAnd_true_true() {
3237 _assertLogicalAnd(_boolValue(true), _boolValue(true), _boolValue(true));
3238 }
3239
3240 void test_logicalNot_false() {
3241 _assertLogicalNot(_boolValue(true), _boolValue(false));
3242 }
3243
3244 void test_logicalNot_null() {
3245 _assertLogicalNot(null, _nullValue());
3246 }
3247
3248 void test_logicalNot_string() {
3249 try {
3250 _assertLogicalNot(_boolValue(true), _stringValue(null));
3251 fail("Expected EvaluationException");
3252 } on EvaluationException {}
3253 }
3254
3255 void test_logicalNot_true() {
3256 _assertLogicalNot(_boolValue(false), _boolValue(true));
3257 }
3258
3259 void test_logicalNot_unknown() {
3260 _assertLogicalNot(_boolValue(null), _boolValue(null));
3261 }
3262
3263 void test_logicalOr_false_false() {
3264 _assertLogicalOr(_boolValue(false), _boolValue(false), _boolValue(false));
3265 }
3266
3267 void test_logicalOr_false_null() {
3268 _assertLogicalOr(null, _boolValue(false), _nullValue());
3269 }
3270
3271 void test_logicalOr_false_string() {
3272 try {
3273 _assertLogicalOr(
3274 _boolValue(false), _boolValue(false), _stringValue("false"));
3275 fail("Expected EvaluationException");
3276 } on EvaluationException {}
3277 }
3278
3279 void test_logicalOr_false_true() {
3280 _assertLogicalOr(_boolValue(true), _boolValue(false), _boolValue(true));
3281 }
3282
3283 void test_logicalOr_null_false() {
3284 try {
3285 _assertLogicalOr(_boolValue(false), _nullValue(), _boolValue(false));
3286 fail("Expected EvaluationException");
3287 } on EvaluationException {}
3288 }
3289
3290 void test_logicalOr_null_true() {
3291 try {
3292 _assertLogicalOr(_boolValue(true), _nullValue(), _boolValue(true));
3293 fail("Expected EvaluationException");
3294 } on EvaluationException {}
3295 }
3296
3297 void test_logicalOr_string_false() {
3298 try {
3299 _assertLogicalOr(
3300 _boolValue(false), _stringValue("true"), _boolValue(false));
3301 fail("Expected EvaluationException");
3302 } on EvaluationException {}
3303 }
3304
3305 void test_logicalOr_string_true() {
3306 try {
3307 _assertLogicalOr(
3308 _boolValue(true), _stringValue("false"), _boolValue(true));
3309 fail("Expected EvaluationException");
3310 } on EvaluationException {}
3311 }
3312
3313 void test_logicalOr_true_false() {
3314 _assertLogicalOr(_boolValue(true), _boolValue(true), _boolValue(false));
3315 }
3316
3317 void test_logicalOr_true_null() {
3318 try {
3319 _assertLogicalOr(_boolValue(true), _boolValue(true), _nullValue());
3320 fail("Expected EvaluationException");
3321 } on EvaluationException {}
3322 }
3323
3324 void test_logicalOr_true_string() {
3325 try {
3326 _assertLogicalOr(
3327 _boolValue(true), _boolValue(true), _stringValue("true"));
3328 fail("Expected EvaluationException");
3329 } on EvaluationException {}
3330 }
3331
3332 void test_logicalOr_true_true() {
3333 _assertLogicalOr(_boolValue(true), _boolValue(true), _boolValue(true));
3334 }
3335
3336 void test_minus_knownDouble_knownDouble() {
3337 _assertMinus(_doubleValue(1.0), _doubleValue(4.0), _doubleValue(3.0));
3338 }
3339
3340 void test_minus_knownDouble_knownInt() {
3341 _assertMinus(_doubleValue(1.0), _doubleValue(4.0), _intValue(3));
3342 }
3343
3344 void test_minus_knownDouble_unknownDouble() {
3345 _assertMinus(_doubleValue(null), _doubleValue(4.0), _doubleValue(null));
3346 }
3347
3348 void test_minus_knownDouble_unknownInt() {
3349 _assertMinus(_doubleValue(null), _doubleValue(4.0), _intValue(null));
3350 }
3351
3352 void test_minus_knownInt_knownInt() {
3353 _assertMinus(_intValue(1), _intValue(4), _intValue(3));
3354 }
3355
3356 void test_minus_knownInt_knownString() {
3357 _assertMinus(null, _intValue(4), _stringValue("3"));
3358 }
3359
3360 void test_minus_knownInt_unknownDouble() {
3361 _assertMinus(_doubleValue(null), _intValue(4), _doubleValue(null));
3362 }
3363
3364 void test_minus_knownInt_unknownInt() {
3365 _assertMinus(_intValue(null), _intValue(4), _intValue(null));
3366 }
3367
3368 void test_minus_knownString_knownInt() {
3369 _assertMinus(null, _stringValue("4"), _intValue(3));
3370 }
3371
3372 void test_minus_unknownDouble_knownDouble() {
3373 _assertMinus(_doubleValue(null), _doubleValue(null), _doubleValue(3.0));
3374 }
3375
3376 void test_minus_unknownDouble_knownInt() {
3377 _assertMinus(_doubleValue(null), _doubleValue(null), _intValue(3));
3378 }
3379
3380 void test_minus_unknownInt_knownDouble() {
3381 _assertMinus(_doubleValue(null), _intValue(null), _doubleValue(3.0));
3382 }
3383
3384 void test_minus_unknownInt_knownInt() {
3385 _assertMinus(_intValue(null), _intValue(null), _intValue(3));
3386 }
3387
3388 void test_negated_double_known() {
3389 _assertNegated(_doubleValue(2.0), _doubleValue(-2.0));
3390 }
3391
3392 void test_negated_double_unknown() {
3393 _assertNegated(_doubleValue(null), _doubleValue(null));
3394 }
3395
3396 void test_negated_int_known() {
3397 _assertNegated(_intValue(-3), _intValue(3));
3398 }
3399
3400 void test_negated_int_unknown() {
3401 _assertNegated(_intValue(null), _intValue(null));
3402 }
3403
3404 void test_negated_string() {
3405 _assertNegated(null, _stringValue(null));
3406 }
3407
3408 void test_notEqual_bool_false() {
3409 _assertNotEqual(_boolValue(false), _boolValue(true), _boolValue(true));
3410 }
3411
3412 void test_notEqual_bool_true() {
3413 _assertNotEqual(_boolValue(true), _boolValue(false), _boolValue(true));
3414 }
3415
3416 void test_notEqual_bool_unknown() {
3417 _assertNotEqual(_boolValue(null), _boolValue(null), _boolValue(false));
3418 }
3419
3420 void test_notEqual_double_false() {
3421 _assertNotEqual(_boolValue(false), _doubleValue(2.0), _doubleValue(2.0));
3422 }
3423
3424 void test_notEqual_double_true() {
3425 _assertNotEqual(_boolValue(true), _doubleValue(2.0), _doubleValue(4.0));
3426 }
3427
3428 void test_notEqual_double_unknown() {
3429 _assertNotEqual(_boolValue(null), _doubleValue(1.0), _doubleValue(null));
3430 }
3431
3432 void test_notEqual_int_false() {
3433 _assertNotEqual(_boolValue(false), _intValue(5), _intValue(5));
3434 }
3435
3436 void test_notEqual_int_true() {
3437 _assertNotEqual(_boolValue(true), _intValue(-5), _intValue(5));
3438 }
3439
3440 void test_notEqual_int_unknown() {
3441 _assertNotEqual(_boolValue(null), _intValue(null), _intValue(3));
3442 }
3443
3444 void test_notEqual_null() {
3445 _assertNotEqual(_boolValue(false), _nullValue(), _nullValue());
3446 }
3447
3448 void test_notEqual_string_false() {
3449 _assertNotEqual(
3450 _boolValue(false), _stringValue("abc"), _stringValue("abc"));
3451 }
3452
3453 void test_notEqual_string_true() {
3454 _assertNotEqual(_boolValue(true), _stringValue("abc"), _stringValue("def"));
3455 }
3456
3457 void test_notEqual_string_unknown() {
3458 _assertNotEqual(_boolValue(null), _stringValue(null), _stringValue("def"));
3459 }
3460
3461 void test_performToString_bool_false() {
3462 _assertPerformToString(_stringValue("false"), _boolValue(false));
3463 }
3464
3465 void test_performToString_bool_true() {
3466 _assertPerformToString(_stringValue("true"), _boolValue(true));
3467 }
3468
3469 void test_performToString_bool_unknown() {
3470 _assertPerformToString(_stringValue(null), _boolValue(null));
3471 }
3472
3473 void test_performToString_double_known() {
3474 _assertPerformToString(_stringValue("2.0"), _doubleValue(2.0));
3475 }
3476
3477 void test_performToString_double_unknown() {
3478 _assertPerformToString(_stringValue(null), _doubleValue(null));
3479 }
3480
3481 void test_performToString_int_known() {
3482 _assertPerformToString(_stringValue("5"), _intValue(5));
3483 }
3484
3485 void test_performToString_int_unknown() {
3486 _assertPerformToString(_stringValue(null), _intValue(null));
3487 }
3488
3489 void test_performToString_null() {
3490 _assertPerformToString(_stringValue("null"), _nullValue());
3491 }
3492
3493 void test_performToString_string_known() {
3494 _assertPerformToString(_stringValue("abc"), _stringValue("abc"));
3495 }
3496
3497 void test_performToString_string_unknown() {
3498 _assertPerformToString(_stringValue(null), _stringValue(null));
3499 }
3500
3501 void test_remainder_knownDouble_knownDouble() {
3502 _assertRemainder(_doubleValue(1.0), _doubleValue(7.0), _doubleValue(2.0));
3503 }
3504
3505 void test_remainder_knownDouble_knownInt() {
3506 _assertRemainder(_doubleValue(1.0), _doubleValue(7.0), _intValue(2));
3507 }
3508
3509 void test_remainder_knownDouble_unknownDouble() {
3510 _assertRemainder(_doubleValue(null), _doubleValue(7.0), _doubleValue(null));
3511 }
3512
3513 void test_remainder_knownDouble_unknownInt() {
3514 _assertRemainder(_doubleValue(null), _doubleValue(6.0), _intValue(null));
3515 }
3516
3517 void test_remainder_knownInt_knownInt() {
3518 _assertRemainder(_intValue(1), _intValue(7), _intValue(2));
3519 }
3520
3521 void test_remainder_knownInt_knownString() {
3522 _assertRemainder(null, _intValue(7), _stringValue("2"));
3523 }
3524
3525 void test_remainder_knownInt_unknownDouble() {
3526 _assertRemainder(_doubleValue(null), _intValue(7), _doubleValue(null));
3527 }
3528
3529 void test_remainder_knownInt_unknownInt() {
3530 _assertRemainder(_intValue(null), _intValue(7), _intValue(null));
3531 }
3532
3533 void test_remainder_knownString_knownInt() {
3534 _assertRemainder(null, _stringValue("7"), _intValue(2));
3535 }
3536
3537 void test_remainder_unknownDouble_knownDouble() {
3538 _assertRemainder(_doubleValue(null), _doubleValue(null), _doubleValue(2.0));
3539 }
3540
3541 void test_remainder_unknownDouble_knownInt() {
3542 _assertRemainder(_doubleValue(null), _doubleValue(null), _intValue(2));
3543 }
3544
3545 void test_remainder_unknownInt_knownDouble() {
3546 _assertRemainder(_doubleValue(null), _intValue(null), _doubleValue(2.0));
3547 }
3548
3549 void test_remainder_unknownInt_knownInt() {
3550 _assertRemainder(_intValue(null), _intValue(null), _intValue(2));
3551 }
3552
3553 void test_shiftLeft_knownInt_knownInt() {
3554 _assertShiftLeft(_intValue(48), _intValue(6), _intValue(3));
3555 }
3556
3557 void test_shiftLeft_knownInt_knownString() {
3558 _assertShiftLeft(null, _intValue(6), _stringValue(null));
3559 }
3560
3561 void test_shiftLeft_knownInt_tooLarge() {
3562 _assertShiftLeft(
3563 _intValue(null),
3564 _intValue(6),
3565 new DartObjectImpl(
3566 _typeProvider.intType, new IntState(LONG_MAX_VALUE)));
3567 }
3568
3569 void test_shiftLeft_knownInt_unknownInt() {
3570 _assertShiftLeft(_intValue(null), _intValue(6), _intValue(null));
3571 }
3572
3573 void test_shiftLeft_knownString_knownInt() {
3574 _assertShiftLeft(null, _stringValue(null), _intValue(3));
3575 }
3576
3577 void test_shiftLeft_unknownInt_knownInt() {
3578 _assertShiftLeft(_intValue(null), _intValue(null), _intValue(3));
3579 }
3580
3581 void test_shiftLeft_unknownInt_unknownInt() {
3582 _assertShiftLeft(_intValue(null), _intValue(null), _intValue(null));
3583 }
3584
3585 void test_shiftRight_knownInt_knownInt() {
3586 _assertShiftRight(_intValue(6), _intValue(48), _intValue(3));
3587 }
3588
3589 void test_shiftRight_knownInt_knownString() {
3590 _assertShiftRight(null, _intValue(48), _stringValue(null));
3591 }
3592
3593 void test_shiftRight_knownInt_tooLarge() {
3594 _assertShiftRight(
3595 _intValue(null),
3596 _intValue(48),
3597 new DartObjectImpl(
3598 _typeProvider.intType, new IntState(LONG_MAX_VALUE)));
3599 }
3600
3601 void test_shiftRight_knownInt_unknownInt() {
3602 _assertShiftRight(_intValue(null), _intValue(48), _intValue(null));
3603 }
3604
3605 void test_shiftRight_knownString_knownInt() {
3606 _assertShiftRight(null, _stringValue(null), _intValue(3));
3607 }
3608
3609 void test_shiftRight_unknownInt_knownInt() {
3610 _assertShiftRight(_intValue(null), _intValue(null), _intValue(3));
3611 }
3612
3613 void test_shiftRight_unknownInt_unknownInt() {
3614 _assertShiftRight(_intValue(null), _intValue(null), _intValue(null));
3615 }
3616
3617 void test_stringLength_int() {
3618 try {
3619 _assertStringLength(_intValue(null), _intValue(0));
3620 fail("Expected EvaluationException");
3621 } on EvaluationException {}
3622 }
3623
3624 void test_stringLength_knownString() {
3625 _assertStringLength(_intValue(3), _stringValue("abc"));
3626 }
3627
3628 void test_stringLength_unknownString() {
3629 _assertStringLength(_intValue(null), _stringValue(null));
3630 }
3631
3632 void test_times_knownDouble_knownDouble() {
3633 _assertTimes(_doubleValue(6.0), _doubleValue(2.0), _doubleValue(3.0));
3634 }
3635
3636 void test_times_knownDouble_knownInt() {
3637 _assertTimes(_doubleValue(6.0), _doubleValue(2.0), _intValue(3));
3638 }
3639
3640 void test_times_knownDouble_unknownDouble() {
3641 _assertTimes(_doubleValue(null), _doubleValue(2.0), _doubleValue(null));
3642 }
3643
3644 void test_times_knownDouble_unknownInt() {
3645 _assertTimes(_doubleValue(null), _doubleValue(2.0), _intValue(null));
3646 }
3647
3648 void test_times_knownInt_knownInt() {
3649 _assertTimes(_intValue(6), _intValue(2), _intValue(3));
3650 }
3651
3652 void test_times_knownInt_knownString() {
3653 _assertTimes(null, _intValue(2), _stringValue("3"));
3654 }
3655
3656 void test_times_knownInt_unknownDouble() {
3657 _assertTimes(_doubleValue(null), _intValue(2), _doubleValue(null));
3658 }
3659
3660 void test_times_knownInt_unknownInt() {
3661 _assertTimes(_intValue(null), _intValue(2), _intValue(null));
3662 }
3663
3664 void test_times_knownString_knownInt() {
3665 _assertTimes(null, _stringValue("2"), _intValue(3));
3666 }
3667
3668 void test_times_unknownDouble_knownDouble() {
3669 _assertTimes(_doubleValue(null), _doubleValue(null), _doubleValue(3.0));
3670 }
3671
3672 void test_times_unknownDouble_knownInt() {
3673 _assertTimes(_doubleValue(null), _doubleValue(null), _intValue(3));
3674 }
3675
3676 void test_times_unknownInt_knownDouble() {
3677 _assertTimes(_doubleValue(null), _intValue(null), _doubleValue(3.0));
3678 }
3679
3680 void test_times_unknownInt_knownInt() {
3681 _assertTimes(_intValue(null), _intValue(null), _intValue(3));
3682 }
3683
3684 /**
3685 * Assert that the result of adding the left and right operands is the expecte d value, or that the
3686 * operation throws an exception if the expected value is `null`.
3687 *
3688 * @param expected the expected result of the operation
3689 * @param leftOperand the left operand to the operation
3690 * @param rightOperand the left operand to the operation
3691 * @throws EvaluationException if the result is an exception when it should no t be
3692 */
3693 void _assertAdd(DartObjectImpl expected, DartObjectImpl leftOperand,
3694 DartObjectImpl rightOperand) {
3695 if (expected == null) {
3696 try {
3697 leftOperand.add(_typeProvider, rightOperand);
3698 fail("Expected an EvaluationException");
3699 } on EvaluationException {}
3700 } else {
3701 DartObjectImpl result = leftOperand.add(_typeProvider, rightOperand);
3702 expect(result, isNotNull);
3703 expect(result, expected);
3704 }
3705 }
3706
3707 /**
3708 * Assert that the result of bit-anding the left and right operands is the exp ected value, or that
3709 * the operation throws an exception if the expected value is `null`.
3710 *
3711 * @param expected the expected result of the operation
3712 * @param leftOperand the left operand to the operation
3713 * @param rightOperand the left operand to the operation
3714 * @throws EvaluationException if the result is an exception when it should no t be
3715 */
3716 void _assertBitAnd(DartObjectImpl expected, DartObjectImpl leftOperand,
3717 DartObjectImpl rightOperand) {
3718 if (expected == null) {
3719 try {
3720 leftOperand.bitAnd(_typeProvider, rightOperand);
3721 fail("Expected an EvaluationException");
3722 } on EvaluationException {}
3723 } else {
3724 DartObjectImpl result = leftOperand.bitAnd(_typeProvider, rightOperand);
3725 expect(result, isNotNull);
3726 expect(result, expected);
3727 }
3728 }
3729
3730 /**
3731 * Assert that the bit-not of the operand is the expected value, or that the o peration throws an
3732 * exception if the expected value is `null`.
3733 *
3734 * @param expected the expected result of the operation
3735 * @param operand the operand to the operation
3736 * @throws EvaluationException if the result is an exception when it should no t be
3737 */
3738 void _assertBitNot(DartObjectImpl expected, DartObjectImpl operand) {
3739 if (expected == null) {
3740 try {
3741 operand.bitNot(_typeProvider);
3742 fail("Expected an EvaluationException");
3743 } on EvaluationException {}
3744 } else {
3745 DartObjectImpl result = operand.bitNot(_typeProvider);
3746 expect(result, isNotNull);
3747 expect(result, expected);
3748 }
3749 }
3750
3751 /**
3752 * Assert that the result of bit-oring the left and right operands is the expe cted value, or that
3753 * the operation throws an exception if the expected value is `null`.
3754 *
3755 * @param expected the expected result of the operation
3756 * @param leftOperand the left operand to the operation
3757 * @param rightOperand the left operand to the operation
3758 * @throws EvaluationException if the result is an exception when it should no t be
3759 */
3760 void _assertBitOr(DartObjectImpl expected, DartObjectImpl leftOperand,
3761 DartObjectImpl rightOperand) {
3762 if (expected == null) {
3763 try {
3764 leftOperand.bitOr(_typeProvider, rightOperand);
3765 fail("Expected an EvaluationException");
3766 } on EvaluationException {}
3767 } else {
3768 DartObjectImpl result = leftOperand.bitOr(_typeProvider, rightOperand);
3769 expect(result, isNotNull);
3770 expect(result, expected);
3771 }
3772 }
3773
3774 /**
3775 * Assert that the result of bit-xoring the left and right operands is the exp ected value, or that
3776 * the operation throws an exception if the expected value is `null`.
3777 *
3778 * @param expected the expected result of the operation
3779 * @param leftOperand the left operand to the operation
3780 * @param rightOperand the left operand to the operation
3781 * @throws EvaluationException if the result is an exception when it should no t be
3782 */
3783 void _assertBitXor(DartObjectImpl expected, DartObjectImpl leftOperand,
3784 DartObjectImpl rightOperand) {
3785 if (expected == null) {
3786 try {
3787 leftOperand.bitXor(_typeProvider, rightOperand);
3788 fail("Expected an EvaluationException");
3789 } on EvaluationException {}
3790 } else {
3791 DartObjectImpl result = leftOperand.bitXor(_typeProvider, rightOperand);
3792 expect(result, isNotNull);
3793 expect(result, expected);
3794 }
3795 }
3796
3797 /**
3798 * Assert that the result of concatenating the left and right operands is the expected value, or
3799 * that the operation throws an exception if the expected value is `null`.
3800 *
3801 * @param expected the expected result of the operation
3802 * @param leftOperand the left operand to the operation
3803 * @param rightOperand the left operand to the operation
3804 * @throws EvaluationException if the result is an exception when it should no t be
3805 */
3806 void _assertConcatenate(DartObjectImpl expected, DartObjectImpl leftOperand,
3807 DartObjectImpl rightOperand) {
3808 if (expected == null) {
3809 try {
3810 leftOperand.concatenate(_typeProvider, rightOperand);
3811 fail("Expected an EvaluationException");
3812 } on EvaluationException {}
3813 } else {
3814 DartObjectImpl result =
3815 leftOperand.concatenate(_typeProvider, rightOperand);
3816 expect(result, isNotNull);
3817 expect(result, expected);
3818 }
3819 }
3820
3821 /**
3822 * Assert that the result of dividing the left and right operands is the expec ted value, or that
3823 * the operation throws an exception if the expected value is `null`.
3824 *
3825 * @param expected the expected result of the operation
3826 * @param leftOperand the left operand to the operation
3827 * @param rightOperand the left operand to the operation
3828 * @throws EvaluationException if the result is an exception when it should no t be
3829 */
3830 void _assertDivide(DartObjectImpl expected, DartObjectImpl leftOperand,
3831 DartObjectImpl rightOperand) {
3832 if (expected == null) {
3833 try {
3834 leftOperand.divide(_typeProvider, rightOperand);
3835 fail("Expected an EvaluationException");
3836 } on EvaluationException {}
3837 } else {
3838 DartObjectImpl result = leftOperand.divide(_typeProvider, rightOperand);
3839 expect(result, isNotNull);
3840 expect(result, expected);
3841 }
3842 }
3843
3844 /**
3845 * Assert that the result of comparing the left and right operands for equalit y is the expected
3846 * value, or that the operation throws an exception if the expected value is ` null`.
3847 *
3848 * @param expected the expected result of the operation
3849 * @param leftOperand the left operand to the operation
3850 * @param rightOperand the left operand to the operation
3851 * @throws EvaluationException if the result is an exception when it should no t be
3852 */
3853 void _assertEqualEqual(DartObjectImpl expected, DartObjectImpl leftOperand,
3854 DartObjectImpl rightOperand) {
3855 if (expected == null) {
3856 try {
3857 leftOperand.equalEqual(_typeProvider, rightOperand);
3858 fail("Expected an EvaluationException");
3859 } on EvaluationException {}
3860 } else {
3861 DartObjectImpl result =
3862 leftOperand.equalEqual(_typeProvider, rightOperand);
3863 expect(result, isNotNull);
3864 expect(result, expected);
3865 }
3866 }
3867
3868 /**
3869 * Assert that the result of comparing the left and right operands is the expe cted value, or that
3870 * the operation throws an exception if the expected value is `null`.
3871 *
3872 * @param expected the expected result of the operation
3873 * @param leftOperand the left operand to the operation
3874 * @param rightOperand the left operand to the operation
3875 * @throws EvaluationException if the result is an exception when it should no t be
3876 */
3877 void _assertGreaterThan(DartObjectImpl expected, DartObjectImpl leftOperand,
3878 DartObjectImpl rightOperand) {
3879 if (expected == null) {
3880 try {
3881 leftOperand.greaterThan(_typeProvider, rightOperand);
3882 fail("Expected an EvaluationException");
3883 } on EvaluationException {}
3884 } else {
3885 DartObjectImpl result =
3886 leftOperand.greaterThan(_typeProvider, rightOperand);
3887 expect(result, isNotNull);
3888 expect(result, expected);
3889 }
3890 }
3891
3892 /**
3893 * Assert that the result of comparing the left and right operands is the expe cted value, or that
3894 * the operation throws an exception if the expected value is `null`.
3895 *
3896 * @param expected the expected result of the operation
3897 * @param leftOperand the left operand to the operation
3898 * @param rightOperand the left operand to the operation
3899 * @throws EvaluationException if the result is an exception when it should no t be
3900 */
3901 void _assertGreaterThanOrEqual(DartObjectImpl expected,
3902 DartObjectImpl leftOperand, DartObjectImpl rightOperand) {
3903 if (expected == null) {
3904 try {
3905 leftOperand.greaterThanOrEqual(_typeProvider, rightOperand);
3906 fail("Expected an EvaluationException");
3907 } on EvaluationException {}
3908 } else {
3909 DartObjectImpl result =
3910 leftOperand.greaterThanOrEqual(_typeProvider, rightOperand);
3911 expect(result, isNotNull);
3912 expect(result, expected);
3913 }
3914 }
3915
3916 /**
3917 * Assert that the result of comparing the left and right operands using
3918 * identical() is the expected value.
3919 *
3920 * @param expected the expected result of the operation
3921 * @param leftOperand the left operand to the operation
3922 * @param rightOperand the left operand to the operation
3923 */
3924 void _assertIdentical(DartObjectImpl expected, DartObjectImpl leftOperand,
3925 DartObjectImpl rightOperand) {
3926 DartObjectImpl result =
3927 leftOperand.isIdentical(_typeProvider, rightOperand);
3928 expect(result, isNotNull);
3929 expect(result, expected);
3930 }
3931
3932 void _assertInstanceOfObjectArray(Object result) {
3933 // TODO(scheglov) implement
3934 }
3935
3936 /**
3937 * Assert that the result of dividing the left and right operands as integers is the expected
3938 * value, or that the operation throws an exception if the expected value is ` null`.
3939 *
3940 * @param expected the expected result of the operation
3941 * @param leftOperand the left operand to the operation
3942 * @param rightOperand the left operand to the operation
3943 * @throws EvaluationException if the result is an exception when it should no t be
3944 */
3945 void _assertIntegerDivide(DartObjectImpl expected, DartObjectImpl leftOperand,
3946 DartObjectImpl rightOperand) {
3947 if (expected == null) {
3948 try {
3949 leftOperand.integerDivide(_typeProvider, rightOperand);
3950 fail("Expected an EvaluationException");
3951 } on EvaluationException {}
3952 } else {
3953 DartObjectImpl result =
3954 leftOperand.integerDivide(_typeProvider, rightOperand);
3955 expect(result, isNotNull);
3956 expect(result, expected);
3957 }
3958 }
3959
3960 /**
3961 * Assert that the result of comparing the left and right operands is the expe cted value, or that
3962 * the operation throws an exception if the expected value is `null`.
3963 *
3964 * @param expected the expected result of the operation
3965 * @param leftOperand the left operand to the operation
3966 * @param rightOperand the left operand to the operation
3967 * @throws EvaluationException if the result is an exception when it should no t be
3968 */
3969 void _assertLessThan(DartObjectImpl expected, DartObjectImpl leftOperand,
3970 DartObjectImpl rightOperand) {
3971 if (expected == null) {
3972 try {
3973 leftOperand.lessThan(_typeProvider, rightOperand);
3974 fail("Expected an EvaluationException");
3975 } on EvaluationException {}
3976 } else {
3977 DartObjectImpl result = leftOperand.lessThan(_typeProvider, rightOperand);
3978 expect(result, isNotNull);
3979 expect(result, expected);
3980 }
3981 }
3982
3983 /**
3984 * Assert that the result of comparing the left and right operands is the expe cted value, or that
3985 * the operation throws an exception if the expected value is `null`.
3986 *
3987 * @param expected the expected result of the operation
3988 * @param leftOperand the left operand to the operation
3989 * @param rightOperand the left operand to the operation
3990 * @throws EvaluationException if the result is an exception when it should no t be
3991 */
3992 void _assertLessThanOrEqual(DartObjectImpl expected,
3993 DartObjectImpl leftOperand, DartObjectImpl rightOperand) {
3994 if (expected == null) {
3995 try {
3996 leftOperand.lessThanOrEqual(_typeProvider, rightOperand);
3997 fail("Expected an EvaluationException");
3998 } on EvaluationException {}
3999 } else {
4000 DartObjectImpl result =
4001 leftOperand.lessThanOrEqual(_typeProvider, rightOperand);
4002 expect(result, isNotNull);
4003 expect(result, expected);
4004 }
4005 }
4006
4007 /**
4008 * Assert that the result of logical-anding the left and right operands is the expected value, or
4009 * that the operation throws an exception if the expected value is `null`.
4010 *
4011 * @param expected the expected result of the operation
4012 * @param leftOperand the left operand to the operation
4013 * @param rightOperand the left operand to the operation
4014 * @throws EvaluationException if the result is an exception when it should no t be
4015 */
4016 void _assertLogicalAnd(DartObjectImpl expected, DartObjectImpl leftOperand,
4017 DartObjectImpl rightOperand) {
4018 if (expected == null) {
4019 try {
4020 leftOperand.logicalAnd(_typeProvider, rightOperand);
4021 fail("Expected an EvaluationException");
4022 } on EvaluationException {}
4023 } else {
4024 DartObjectImpl result =
4025 leftOperand.logicalAnd(_typeProvider, rightOperand);
4026 expect(result, isNotNull);
4027 expect(result, expected);
4028 }
4029 }
4030
4031 /**
4032 * Assert that the logical-not of the operand is the expected value, or that t he operation throws
4033 * an exception if the expected value is `null`.
4034 *
4035 * @param expected the expected result of the operation
4036 * @param operand the operand to the operation
4037 * @throws EvaluationException if the result is an exception when it should no t be
4038 */
4039 void _assertLogicalNot(DartObjectImpl expected, DartObjectImpl operand) {
4040 if (expected == null) {
4041 try {
4042 operand.logicalNot(_typeProvider);
4043 fail("Expected an EvaluationException");
4044 } on EvaluationException {}
4045 } else {
4046 DartObjectImpl result = operand.logicalNot(_typeProvider);
4047 expect(result, isNotNull);
4048 expect(result, expected);
4049 }
4050 }
4051
4052 /**
4053 * Assert that the result of logical-oring the left and right operands is the expected value, or
4054 * that the operation throws an exception if the expected value is `null`.
4055 *
4056 * @param expected the expected result of the operation
4057 * @param leftOperand the left operand to the operation
4058 * @param rightOperand the left operand to the operation
4059 * @throws EvaluationException if the result is an exception when it should no t be
4060 */
4061 void _assertLogicalOr(DartObjectImpl expected, DartObjectImpl leftOperand,
4062 DartObjectImpl rightOperand) {
4063 if (expected == null) {
4064 try {
4065 leftOperand.logicalOr(_typeProvider, rightOperand);
4066 fail("Expected an EvaluationException");
4067 } on EvaluationException {}
4068 } else {
4069 DartObjectImpl result =
4070 leftOperand.logicalOr(_typeProvider, rightOperand);
4071 expect(result, isNotNull);
4072 expect(result, expected);
4073 }
4074 }
4075
4076 /**
4077 * Assert that the result of subtracting the left and right operands is the ex pected value, or
4078 * that the operation throws an exception if the expected value is `null`.
4079 *
4080 * @param expected the expected result of the operation
4081 * @param leftOperand the left operand to the operation
4082 * @param rightOperand the left operand to the operation
4083 * @throws EvaluationException if the result is an exception when it should no t be
4084 */
4085 void _assertMinus(DartObjectImpl expected, DartObjectImpl leftOperand,
4086 DartObjectImpl rightOperand) {
4087 if (expected == null) {
4088 try {
4089 leftOperand.minus(_typeProvider, rightOperand);
4090 fail("Expected an EvaluationException");
4091 } on EvaluationException {}
4092 } else {
4093 DartObjectImpl result = leftOperand.minus(_typeProvider, rightOperand);
4094 expect(result, isNotNull);
4095 expect(result, expected);
4096 }
4097 }
4098
4099 /**
4100 * Assert that the negation of the operand is the expected value, or that the operation throws an
4101 * exception if the expected value is `null`.
4102 *
4103 * @param expected the expected result of the operation
4104 * @param operand the operand to the operation
4105 * @throws EvaluationException if the result is an exception when it should no t be
4106 */
4107 void _assertNegated(DartObjectImpl expected, DartObjectImpl operand) {
4108 if (expected == null) {
4109 try {
4110 operand.negated(_typeProvider);
4111 fail("Expected an EvaluationException");
4112 } on EvaluationException {}
4113 } else {
4114 DartObjectImpl result = operand.negated(_typeProvider);
4115 expect(result, isNotNull);
4116 expect(result, expected);
4117 }
4118 }
4119
4120 /**
4121 * Assert that the result of comparing the left and right operands for inequal ity is the expected
4122 * value, or that the operation throws an exception if the expected value is ` null`.
4123 *
4124 * @param expected the expected result of the operation
4125 * @param leftOperand the left operand to the operation
4126 * @param rightOperand the left operand to the operation
4127 * @throws EvaluationException if the result is an exception when it should no t be
4128 */
4129 void _assertNotEqual(DartObjectImpl expected, DartObjectImpl leftOperand,
4130 DartObjectImpl rightOperand) {
4131 if (expected == null) {
4132 try {
4133 leftOperand.notEqual(_typeProvider, rightOperand);
4134 fail("Expected an EvaluationException");
4135 } on EvaluationException {}
4136 } else {
4137 DartObjectImpl result = leftOperand.notEqual(_typeProvider, rightOperand);
4138 expect(result, isNotNull);
4139 expect(result, expected);
4140 }
4141 }
4142
4143 /**
4144 * Assert that converting the operand to a string is the expected value, or th at the operation
4145 * throws an exception if the expected value is `null`.
4146 *
4147 * @param expected the expected result of the operation
4148 * @param operand the operand to the operation
4149 * @throws EvaluationException if the result is an exception when it should no t be
4150 */
4151 void _assertPerformToString(DartObjectImpl expected, DartObjectImpl operand) {
4152 if (expected == null) {
4153 try {
4154 operand.performToString(_typeProvider);
4155 fail("Expected an EvaluationException");
4156 } on EvaluationException {}
4157 } else {
4158 DartObjectImpl result = operand.performToString(_typeProvider);
4159 expect(result, isNotNull);
4160 expect(result, expected);
4161 }
4162 }
4163
4164 /**
4165 * Assert that the result of taking the remainder of the left and right operan ds is the expected
4166 * value, or that the operation throws an exception if the expected value is ` null`.
4167 *
4168 * @param expected the expected result of the operation
4169 * @param leftOperand the left operand to the operation
4170 * @param rightOperand the left operand to the operation
4171 * @throws EvaluationException if the result is an exception when it should no t be
4172 */
4173 void _assertRemainder(DartObjectImpl expected, DartObjectImpl leftOperand,
4174 DartObjectImpl rightOperand) {
4175 if (expected == null) {
4176 try {
4177 leftOperand.remainder(_typeProvider, rightOperand);
4178 fail("Expected an EvaluationException");
4179 } on EvaluationException {}
4180 } else {
4181 DartObjectImpl result =
4182 leftOperand.remainder(_typeProvider, rightOperand);
4183 expect(result, isNotNull);
4184 expect(result, expected);
4185 }
4186 }
4187
4188 /**
4189 * Assert that the result of multiplying the left and right operands is the ex pected value, or
4190 * that the operation throws an exception if the expected value is `null`.
4191 *
4192 * @param expected the expected result of the operation
4193 * @param leftOperand the left operand to the operation
4194 * @param rightOperand the left operand to the operation
4195 * @throws EvaluationException if the result is an exception when it should no t be
4196 */
4197 void _assertShiftLeft(DartObjectImpl expected, DartObjectImpl leftOperand,
4198 DartObjectImpl rightOperand) {
4199 if (expected == null) {
4200 try {
4201 leftOperand.shiftLeft(_typeProvider, rightOperand);
4202 fail("Expected an EvaluationException");
4203 } on EvaluationException {}
4204 } else {
4205 DartObjectImpl result =
4206 leftOperand.shiftLeft(_typeProvider, rightOperand);
4207 expect(result, isNotNull);
4208 expect(result, expected);
4209 }
4210 }
4211
4212 /**
4213 * Assert that the result of multiplying the left and right operands is the ex pected value, or
4214 * that the operation throws an exception if the expected value is `null`.
4215 *
4216 * @param expected the expected result of the operation
4217 * @param leftOperand the left operand to the operation
4218 * @param rightOperand the right operand to the operation
4219 * @throws EvaluationException if the result is an exception when it should no t be
4220 */
4221 void _assertShiftRight(DartObjectImpl expected, DartObjectImpl leftOperand,
4222 DartObjectImpl rightOperand) {
4223 if (expected == null) {
4224 try {
4225 leftOperand.shiftRight(_typeProvider, rightOperand);
4226 fail("Expected an EvaluationException");
4227 } on EvaluationException {}
4228 } else {
4229 DartObjectImpl result =
4230 leftOperand.shiftRight(_typeProvider, rightOperand);
4231 expect(result, isNotNull);
4232 expect(result, expected);
4233 }
4234 }
4235
4236 /**
4237 * Assert that the length of the operand is the expected value, or that the op eration throws an
4238 * exception if the expected value is `null`.
4239 *
4240 * @param expected the expected result of the operation
4241 * @param operand the operand to the operation
4242 * @throws EvaluationException if the result is an exception when it should no t be
4243 */
4244 void _assertStringLength(DartObjectImpl expected, DartObjectImpl operand) {
4245 if (expected == null) {
4246 try {
4247 operand.stringLength(_typeProvider);
4248 fail("Expected an EvaluationException");
4249 } on EvaluationException {}
4250 } else {
4251 DartObjectImpl result = operand.stringLength(_typeProvider);
4252 expect(result, isNotNull);
4253 expect(result, expected);
4254 }
4255 }
4256
4257 /**
4258 * Assert that the result of multiplying the left and right operands is the ex pected value, or
4259 * that the operation throws an exception if the expected value is `null`.
4260 *
4261 * @param expected the expected result of the operation
4262 * @param leftOperand the left operand to the operation
4263 * @param rightOperand the left operand to the operation
4264 * @throws EvaluationException if the result is an exception when it should no t be
4265 */
4266 void _assertTimes(DartObjectImpl expected, DartObjectImpl leftOperand,
4267 DartObjectImpl rightOperand) {
4268 if (expected == null) {
4269 try {
4270 leftOperand.times(_typeProvider, rightOperand);
4271 fail("Expected an EvaluationException");
4272 } on EvaluationException {}
4273 } else {
4274 DartObjectImpl result = leftOperand.times(_typeProvider, rightOperand);
4275 expect(result, isNotNull);
4276 expect(result, expected);
4277 }
4278 }
4279
4280 DartObjectImpl _boolValue(bool value) {
4281 if (value == null) {
4282 return new DartObjectImpl(
4283 _typeProvider.boolType, BoolState.UNKNOWN_VALUE);
4284 } else if (identical(value, false)) {
4285 return new DartObjectImpl(_typeProvider.boolType, BoolState.FALSE_STATE);
4286 } else if (identical(value, true)) {
4287 return new DartObjectImpl(_typeProvider.boolType, BoolState.TRUE_STATE);
4288 }
4289 fail("Invalid boolean value used in test");
4290 return null;
4291 }
4292
4293 DartObjectImpl _doubleValue(double value) {
4294 if (value == null) {
4295 return new DartObjectImpl(
4296 _typeProvider.doubleType, DoubleState.UNKNOWN_VALUE);
4297 } else {
4298 return new DartObjectImpl(
4299 _typeProvider.doubleType, new DoubleState(value));
4300 }
4301 }
4302
4303 DartObjectImpl _dynamicValue() {
4304 return new DartObjectImpl(
4305 _typeProvider.nullType, DynamicState.DYNAMIC_STATE);
4306 }
4307
4308 DartObjectImpl _intValue(int value) {
4309 if (value == null) {
4310 return new DartObjectImpl(_typeProvider.intType, IntState.UNKNOWN_VALUE);
4311 } else {
4312 return new DartObjectImpl(_typeProvider.intType, new IntState(value));
4313 }
4314 }
4315
4316 DartObjectImpl _listValue(
4317 [List<DartObjectImpl> elements = DartObjectImpl.EMPTY_LIST]) {
4318 return new DartObjectImpl(_typeProvider.listType, new ListState(elements));
4319 }
4320
4321 DartObjectImpl _mapValue(
4322 [List<DartObjectImpl> keyElementPairs = DartObjectImpl.EMPTY_LIST]) {
4323 Map<DartObjectImpl, DartObjectImpl> map =
4324 new Map<DartObjectImpl, DartObjectImpl>();
4325 int count = keyElementPairs.length;
4326 for (int i = 0; i < count;) {
4327 map[keyElementPairs[i++]] = keyElementPairs[i++];
4328 }
4329 return new DartObjectImpl(_typeProvider.mapType, new MapState(map));
4330 }
4331
4332 DartObjectImpl _nullValue() {
4333 return new DartObjectImpl(_typeProvider.nullType, NullState.NULL_STATE);
4334 }
4335
4336 DartObjectImpl _numValue() {
4337 return new DartObjectImpl(_typeProvider.nullType, NumState.UNKNOWN_VALUE);
4338 }
4339
4340 DartObjectImpl _stringValue(String value) {
4341 if (value == null) {
4342 return new DartObjectImpl(
4343 _typeProvider.stringType, StringState.UNKNOWN_VALUE);
4344 } else {
4345 return new DartObjectImpl(
4346 _typeProvider.stringType, new StringState(value));
4347 }
4348 }
4349
4350 DartObjectImpl _symbolValue(String value) {
4351 return new DartObjectImpl(_typeProvider.symbolType, new SymbolState(value));
4352 }
4353 }
4354
4355 @reflectiveTest
4356 class DeclaredVariablesTest extends EngineTestCase {
4357 void test_getBool_false() {
4358 TestTypeProvider typeProvider = new TestTypeProvider();
4359 String variableName = "var";
4360 DeclaredVariables variables = new DeclaredVariables();
4361 variables.define(variableName, "false");
4362 DartObject object = variables.getBool(typeProvider, variableName);
4363 expect(object, isNotNull);
4364 expect(object.toBoolValue(), false);
4365 }
4366
4367 void test_getBool_invalid() {
4368 TestTypeProvider typeProvider = new TestTypeProvider();
4369 String variableName = "var";
4370 DeclaredVariables variables = new DeclaredVariables();
4371 variables.define(variableName, "not true");
4372 _assertNullDartObject(
4373 typeProvider, variables.getBool(typeProvider, variableName));
4374 }
4375
4376 void test_getBool_true() {
4377 TestTypeProvider typeProvider = new TestTypeProvider();
4378 String variableName = "var";
4379 DeclaredVariables variables = new DeclaredVariables();
4380 variables.define(variableName, "true");
4381 DartObject object = variables.getBool(typeProvider, variableName);
4382 expect(object, isNotNull);
4383 expect(object.toBoolValue(), true);
4384 }
4385
4386 void test_getBool_undefined() {
4387 TestTypeProvider typeProvider = new TestTypeProvider();
4388 String variableName = "var";
4389 DeclaredVariables variables = new DeclaredVariables();
4390 _assertUnknownDartObject(
4391 typeProvider.boolType, variables.getBool(typeProvider, variableName));
4392 }
4393
4394 void test_getInt_invalid() {
4395 TestTypeProvider typeProvider = new TestTypeProvider();
4396 String variableName = "var";
4397 DeclaredVariables variables = new DeclaredVariables();
4398 variables.define(variableName, "four score and seven years");
4399 _assertNullDartObject(
4400 typeProvider, variables.getInt(typeProvider, variableName));
4401 }
4402
4403 void test_getInt_undefined() {
4404 TestTypeProvider typeProvider = new TestTypeProvider();
4405 String variableName = "var";
4406 DeclaredVariables variables = new DeclaredVariables();
4407 _assertUnknownDartObject(
4408 typeProvider.intType, variables.getInt(typeProvider, variableName));
4409 }
4410
4411 void test_getInt_valid() {
4412 TestTypeProvider typeProvider = new TestTypeProvider();
4413 String variableName = "var";
4414 DeclaredVariables variables = new DeclaredVariables();
4415 variables.define(variableName, "23");
4416 DartObject object = variables.getInt(typeProvider, variableName);
4417 expect(object, isNotNull);
4418 expect(object.toIntValue(), 23);
4419 }
4420
4421 void test_getString_defined() {
4422 TestTypeProvider typeProvider = new TestTypeProvider();
4423 String variableName = "var";
4424 String value = "value";
4425 DeclaredVariables variables = new DeclaredVariables();
4426 variables.define(variableName, value);
4427 DartObject object = variables.getString(typeProvider, variableName);
4428 expect(object, isNotNull);
4429 expect(object.toStringValue(), value);
4430 }
4431
4432 void test_getString_undefined() {
4433 TestTypeProvider typeProvider = new TestTypeProvider();
4434 String variableName = "var";
4435 DeclaredVariables variables = new DeclaredVariables();
4436 _assertUnknownDartObject(typeProvider.stringType,
4437 variables.getString(typeProvider, variableName));
4438 }
4439
4440 void _assertNullDartObject(TestTypeProvider typeProvider, DartObject result) {
4441 expect(result.type, typeProvider.nullType);
4442 }
4443
4444 void _assertUnknownDartObject(
4445 ParameterizedType expectedType, DartObject result) {
4446 expect((result as DartObjectImpl).isUnknown, isTrue);
4447 expect(result.type, expectedType);
4448 }
4449 }
4450
4451 @reflectiveTest
4452 class ReferenceFinderTest {
4453 DirectedGraph<ConstantEvaluationTarget> _referenceGraph;
4454 VariableElement _head;
4455 Element _tail;
4456
4457 void setUp() {
4458 _referenceGraph = new DirectedGraph<ConstantEvaluationTarget>();
4459 _head = ElementFactory.topLevelVariableElement2("v1");
4460 }
4461
4462 void test_visitSimpleIdentifier_const() {
4463 _visitNode(_makeTailVariable("v2", true));
4464 _assertOneArc(_tail);
4465 }
4466
4467 void test_visitSuperConstructorInvocation_const() {
4468 _visitNode(_makeTailSuperConstructorInvocation("A", true));
4469 _assertOneArc(_tail);
4470 }
4471
4472 void test_visitSuperConstructorInvocation_nonConst() {
4473 _visitNode(_makeTailSuperConstructorInvocation("A", false));
4474 _assertOneArc(_tail);
4475 }
4476
4477 void test_visitSuperConstructorInvocation_unresolved() {
4478 SuperConstructorInvocation superConstructorInvocation =
4479 AstFactory.superConstructorInvocation();
4480 _visitNode(superConstructorInvocation);
4481 _assertNoArcs();
4482 }
4483
4484 void _assertNoArcs() {
4485 Set<ConstantEvaluationTarget> tails = _referenceGraph.getTails(_head);
4486 expect(tails, hasLength(0));
4487 }
4488
4489 void _assertOneArc(Element tail) {
4490 Set<ConstantEvaluationTarget> tails = _referenceGraph.getTails(_head);
4491 expect(tails, hasLength(1));
4492 expect(tails.first, same(tail));
4493 }
4494
4495 ReferenceFinder _createReferenceFinder(ConstantEvaluationTarget source) =>
4496 new ReferenceFinder((ConstantEvaluationTarget dependency) {
4497 _referenceGraph.addEdge(source, dependency);
4498 });
4499 SuperConstructorInvocation _makeTailSuperConstructorInvocation(
4500 String name, bool isConst) {
4501 List<ConstructorInitializer> initializers =
4502 new List<ConstructorInitializer>();
4503 ConstructorDeclaration constructorDeclaration =
4504 AstFactory.constructorDeclaration(AstFactory.identifier3(name), null,
4505 AstFactory.formalParameterList(), initializers);
4506 if (isConst) {
4507 constructorDeclaration.constKeyword = new KeywordToken(Keyword.CONST, 0);
4508 }
4509 ClassElementImpl classElement = ElementFactory.classElement2(name);
4510 SuperConstructorInvocation superConstructorInvocation =
4511 AstFactory.superConstructorInvocation();
4512 ConstructorElementImpl constructorElement =
4513 ElementFactory.constructorElement(classElement, name, isConst);
4514 _tail = constructorElement;
4515 superConstructorInvocation.staticElement = constructorElement;
4516 return superConstructorInvocation;
4517 }
4518
4519 SimpleIdentifier _makeTailVariable(String name, bool isConst) {
4520 VariableDeclaration variableDeclaration =
4521 AstFactory.variableDeclaration(name);
4522 ConstLocalVariableElementImpl variableElement =
4523 ElementFactory.constLocalVariableElement(name);
4524 _tail = variableElement;
4525 variableElement.const3 = isConst;
4526 AstFactory.variableDeclarationList2(
4527 isConst ? Keyword.CONST : Keyword.VAR, [variableDeclaration]);
4528 SimpleIdentifier identifier = AstFactory.identifier3(name);
4529 identifier.staticElement = variableElement;
4530 return identifier;
4531 }
4532
4533 void _visitNode(AstNode node) {
4534 node.accept(_createReferenceFinder(_head));
4535 }
4536 }
4537
4538 class _TestAnalysisContext extends TestAnalysisContext {
4539 @override
4540 InternalAnalysisContext getContextFor(Source source) => this;
4541 }
OLDNEW
« no previous file with comments | « pkg/analyzer/test/context/test_all.dart ('k') | pkg/analyzer/test/src/dart/constant/evaluation_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698