3
3
// BSD-style license that can be found in the LICENSE file.
4
4
5
5
import 'package:analyzer/dart/element/element.dart' ;
6
+ import 'package:analyzer/dart/element/nullability_suffix.dart' ;
6
7
import 'package:analyzer/dart/element/type.dart' ;
7
8
import 'package:analyzer/src/dart/element/type.dart' ;
8
9
import 'package:analyzer/src/dart/element/type_algebra.dart' ;
@@ -20,6 +21,7 @@ main() {
20
21
defineReflectiveTests (SubstituteFromPairsTest );
21
22
defineReflectiveTests (SubstituteFromUpperAndLowerBoundsTest );
22
23
defineReflectiveTests (SubstituteTest );
24
+ defineReflectiveTests (SubstituteWithNullabilityTest );
23
25
});
24
26
}
25
27
@@ -276,19 +278,57 @@ class SubstituteTest extends _Base {
276
278
var result = substitute (type, substitution);
277
279
expect (result, same (type));
278
280
}
281
+ }
279
282
280
- void _assertSubstitution (
281
- DartType type,
282
- Map <TypeParameterElement , DartType > substitution,
283
- String expected,
284
- ) {
285
- var result = substitute (type, substitution);
286
- assertElementTypeString (result, expected);
283
+ @reflectiveTest
284
+ class SubstituteWithNullabilityTest extends _Base {
285
+ SubstituteWithNullabilityTest () : super (useNnbd: true );
286
+
287
+ test_interface_none () async {
288
+ // class A<T> {}
289
+ var T = typeParameter ('T' );
290
+ var A = class_ (name: 'A' , typeParameters: [T ]);
291
+
292
+ var U = typeParameter ('U' );
293
+ var type = interfaceType (A ,
294
+ typeArguments: [typeParameterType (U )],
295
+ nullabilitySuffix: NullabilitySuffix .none);
296
+ _assertSubstitution (type, {U : intType}, 'A<int>' );
297
+ }
298
+
299
+ test_interface_question () async {
300
+ // class A<T> {}
301
+ var T = typeParameter ('T' );
302
+ var A = class_ (name: 'A' , typeParameters: [T ]);
303
+
304
+ var U = typeParameter ('U' );
305
+ var type = interfaceType (A ,
306
+ typeArguments: [typeParameterType (U )],
307
+ nullabilitySuffix: NullabilitySuffix .question);
308
+ _assertSubstitution (type, {U : intType}, 'A<int>?' );
309
+ }
310
+
311
+ test_interface_star () async {
312
+ // class A<T> {}
313
+ var T = typeParameter ('T' );
314
+ var A = class_ (name: 'A' , typeParameters: [T ]);
315
+
316
+ var U = typeParameter ('U' );
317
+ var type = interfaceType (A ,
318
+ typeArguments: [typeParameterType (U )],
319
+ nullabilitySuffix: NullabilitySuffix .star);
320
+ _assertSubstitution (type, {U : intType}, 'A<int>*' );
287
321
}
288
322
}
289
323
290
324
class _Base with ElementsTypesMixin {
291
- final typeProvider = TestTypeProvider ();
325
+ final TestTypeProvider typeProvider;
326
+
327
+ final bool useNnbd;
328
+
329
+ _Base ({this .useNnbd = false })
330
+ : typeProvider = TestTypeProvider (null , null ,
331
+ useNnbd ? NullabilitySuffix .none : NullabilitySuffix .question);
292
332
293
333
InterfaceType get boolType => typeProvider.boolType;
294
334
@@ -297,11 +337,20 @@ class _Base with ElementsTypesMixin {
297
337
InterfaceType get intType => typeProvider.intType;
298
338
299
339
/// Whether `DartType.toString()` with nullability should be asked.
300
- bool get typeToStringWithNullability => false ;
340
+ bool get typeToStringWithNullability => useNnbd ;
301
341
302
342
void assertElementTypeString (DartType type, String expected) {
303
343
TypeImpl typeImpl = type;
304
344
expect (typeImpl.toString (withNullability: typeToStringWithNullability),
305
345
expected);
306
346
}
347
+
348
+ void _assertSubstitution (
349
+ DartType type,
350
+ Map <TypeParameterElement , DartType > substitution,
351
+ String expected,
352
+ ) {
353
+ var result = substitute (type, substitution);
354
+ assertElementTypeString (result, expected);
355
+ }
307
356
}
0 commit comments