|
| 1 | +// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:analyzer/dart/analysis/features.dart'; |
| 6 | +import 'package:analyzer/dart/element/type.dart'; |
| 7 | +import 'package:analyzer/dart/element/type_provider.dart'; |
| 8 | +import 'package:analyzer/src/dart/element/type.dart'; |
| 9 | +import 'package:analyzer/src/generated/resolver.dart' show TypeSystemImpl; |
| 10 | +import 'package:test/test.dart'; |
| 11 | +import 'package:test_reflective_loader/test_reflective_loader.dart'; |
| 12 | + |
| 13 | +import '../../../generated/elements_types_mixin.dart'; |
| 14 | +import '../../../generated/test_analysis_context.dart'; |
| 15 | + |
| 16 | +main() { |
| 17 | + defineReflectiveSuite(() { |
| 18 | + defineReflectiveTests(FactorTypeTest); |
| 19 | + }); |
| 20 | +} |
| 21 | + |
| 22 | +@reflectiveTest |
| 23 | +class FactorTypeTest with ElementsTypesMixin { |
| 24 | + @override |
| 25 | + TypeProvider typeProvider; |
| 26 | + |
| 27 | + TypeSystemImpl typeSystem; |
| 28 | + |
| 29 | + FeatureSet get testFeatureSet { |
| 30 | + return FeatureSet.forTesting( |
| 31 | + additionalFeatures: [Feature.non_nullable], |
| 32 | + ); |
| 33 | + } |
| 34 | + |
| 35 | + void setUp() { |
| 36 | + var analysisContext = TestAnalysisContext( |
| 37 | + featureSet: testFeatureSet, |
| 38 | + ); |
| 39 | + typeProvider = analysisContext.typeProviderNonNullableByDefault; |
| 40 | + typeSystem = analysisContext.typeSystemNonNullableByDefault; |
| 41 | + } |
| 42 | + |
| 43 | + void test_futureOr() { |
| 44 | + _check(futureOrNone(intNone), intNone, 'Future<int>'); |
| 45 | + _check(futureOrNone(intNone), futureNone(intNone), 'int'); |
| 46 | + |
| 47 | + _check(futureOrNone(intQuestion), intNone, 'FutureOr<int?>'); |
| 48 | + _check(futureOrNone(intQuestion), futureNone(intNone), 'FutureOr<int?>'); |
| 49 | + _check(futureOrNone(intQuestion), intQuestion, 'Future<int?>'); |
| 50 | + _check(futureOrNone(intQuestion), futureNone(intQuestion), 'int?'); |
| 51 | + _check(futureOrNone(intQuestion), intStar, 'Future<int?>'); |
| 52 | + _check(futureOrNone(intQuestion), futureNone(intStar), 'int?'); |
| 53 | + |
| 54 | + _check(futureOrNone(intNone), numNone, 'Future<int>'); |
| 55 | + _check(futureOrNone(intNone), futureNone(numNone), 'int'); |
| 56 | + } |
| 57 | + |
| 58 | + void test_object() { |
| 59 | + _check(objectNone, objectNone, 'Never'); |
| 60 | + _check(objectNone, objectQuestion, 'Never'); |
| 61 | + _check(objectNone, objectStar, 'Never'); |
| 62 | + |
| 63 | + _check(objectNone, intNone, 'Object'); |
| 64 | + _check(objectNone, intQuestion, 'Object'); |
| 65 | + _check(objectNone, intStar, 'Object'); |
| 66 | + |
| 67 | + _check(objectQuestion, objectNone, 'Never?'); |
| 68 | + _check(objectQuestion, objectQuestion, 'Never'); |
| 69 | + _check(objectQuestion, objectStar, 'Never'); |
| 70 | + |
| 71 | + _check(objectQuestion, intNone, 'Object?'); |
| 72 | + _check(objectQuestion, intQuestion, 'Object'); |
| 73 | + _check(objectQuestion, intStar, 'Object'); |
| 74 | + } |
| 75 | + |
| 76 | + test_subtype() { |
| 77 | + _check(intNone, intNone, 'Never'); |
| 78 | + _check(intNone, intQuestion, 'Never'); |
| 79 | + _check(intNone, intStar, 'Never'); |
| 80 | + |
| 81 | + _check(intQuestion, intNone, 'Never?'); |
| 82 | + _check(intQuestion, intQuestion, 'Never'); |
| 83 | + _check(intQuestion, intStar, 'Never'); |
| 84 | + |
| 85 | + _check(intStar, intNone, 'Never'); |
| 86 | + _check(intStar, intQuestion, 'Never'); |
| 87 | + _check(intStar, intStar, 'Never'); |
| 88 | + |
| 89 | + _check(intNone, numNone, 'Never'); |
| 90 | + _check(intNone, numQuestion, 'Never'); |
| 91 | + _check(intNone, numStar, 'Never'); |
| 92 | + |
| 93 | + _check(intQuestion, numNone, 'Never?'); |
| 94 | + _check(intQuestion, numQuestion, 'Never'); |
| 95 | + _check(intQuestion, numStar, 'Never'); |
| 96 | + |
| 97 | + _check(intStar, numNone, 'Never'); |
| 98 | + _check(intStar, numQuestion, 'Never'); |
| 99 | + _check(intStar, numStar, 'Never'); |
| 100 | + |
| 101 | + _check(intNone, nullNone, 'int'); |
| 102 | + _check(intQuestion, nullNone, 'int'); |
| 103 | + _check(intStar, nullNone, 'int'); |
| 104 | + |
| 105 | + _check(intNone, stringNone, 'int'); |
| 106 | + _check(intQuestion, stringNone, 'int?'); |
| 107 | + _check(intStar, stringNone, 'int*'); |
| 108 | + |
| 109 | + _check(intNone, stringQuestion, 'int'); |
| 110 | + _check(intQuestion, stringQuestion, 'int'); |
| 111 | + _check(intStar, stringQuestion, 'int'); |
| 112 | + |
| 113 | + _check(intNone, stringStar, 'int'); |
| 114 | + _check(intQuestion, stringStar, 'int'); |
| 115 | + _check(intStar, stringStar, 'int'); |
| 116 | + } |
| 117 | + |
| 118 | + void _check(DartType T, DartType S, String expectedStr) { |
| 119 | + var result = typeSystem.factor(T, S); |
| 120 | + var resultStr = _typeString(result); |
| 121 | + |
| 122 | + expect(resultStr, expectedStr); |
| 123 | + } |
| 124 | + |
| 125 | + String _typeString(TypeImpl type) { |
| 126 | + return type.getDisplayString(withNullability: true); |
| 127 | + } |
| 128 | +} |
0 commit comments