|
| 1 | +// Copyright (c) 2019, 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:nnbd_migration/src/decorated_type.dart'; |
| 6 | +import 'package:nnbd_migration/src/nullability_node.dart'; |
| 7 | +import 'package:test/test.dart'; |
| 8 | +import 'package:test_reflective_loader/test_reflective_loader.dart'; |
| 9 | + |
| 10 | +import 'abstract_single_unit.dart'; |
| 11 | + |
| 12 | +main() { |
| 13 | + defineReflectiveSuite(() { |
| 14 | + defineReflectiveTests(DecoratedTypeTest); |
| 15 | + }); |
| 16 | +} |
| 17 | + |
| 18 | +@reflectiveTest |
| 19 | +class DecoratedTypeTest extends AbstractSingleUnitTest { |
| 20 | + final _graph = NullabilityGraph(); |
| 21 | + |
| 22 | + NullabilityNode get always => _graph.always; |
| 23 | + |
| 24 | + @override |
| 25 | + void setUp() { |
| 26 | + NullabilityNode.clearDebugNames(); |
| 27 | + super.setUp(); |
| 28 | + } |
| 29 | + |
| 30 | + test_toString_named_parameter() async { |
| 31 | + await resolveTestUnit('''dynamic f({int x}) {}'''); |
| 32 | + var type = findElement.function('f').type; |
| 33 | + var decoratedType = DecoratedType(type, always, |
| 34 | + namedParameters: { |
| 35 | + 'x': DecoratedType(type.namedParameterTypes['x'], _node(1)) |
| 36 | + }, |
| 37 | + returnType: DecoratedType(type.returnType, always)); |
| 38 | + expect(decoratedType.toString(), 'dynamic Function({x: int?(type(1))})?'); |
| 39 | + } |
| 40 | + |
| 41 | + test_toString_normal_and_named_parameter() async { |
| 42 | + await resolveTestUnit('''dynamic f(int x, {int y}) {}'''); |
| 43 | + var type = findElement.function('f').type; |
| 44 | + var decoratedType = DecoratedType(type, always, |
| 45 | + positionalParameters: [ |
| 46 | + DecoratedType(type.normalParameterTypes[0], _node(1)) |
| 47 | + ], |
| 48 | + namedParameters: { |
| 49 | + 'y': DecoratedType(type.namedParameterTypes['y'], _node(2)) |
| 50 | + }, |
| 51 | + returnType: DecoratedType(type.returnType, always)); |
| 52 | + expect(decoratedType.toString(), |
| 53 | + 'dynamic Function(int?(type(1)), {y: int?(type(2))})?'); |
| 54 | + } |
| 55 | + |
| 56 | + test_toString_normal_and_optional_parameter() async { |
| 57 | + await resolveTestUnit('''dynamic f(int x, [int y]) {}'''); |
| 58 | + var type = findElement.function('f').type; |
| 59 | + var decoratedType = DecoratedType(type, always, |
| 60 | + positionalParameters: [ |
| 61 | + DecoratedType(type.normalParameterTypes[0], _node(1)), |
| 62 | + DecoratedType(type.optionalParameterTypes[0], _node(2)) |
| 63 | + ], |
| 64 | + returnType: DecoratedType(type.returnType, always)); |
| 65 | + expect(decoratedType.toString(), |
| 66 | + 'dynamic Function(int?(type(1)), [int?(type(2))])?'); |
| 67 | + } |
| 68 | + |
| 69 | + test_toString_normal_parameter() async { |
| 70 | + await resolveTestUnit('''dynamic f(int x) {}'''); |
| 71 | + var type = findElement.function('f').type; |
| 72 | + var decoratedType = DecoratedType(type, always, |
| 73 | + positionalParameters: [ |
| 74 | + DecoratedType(type.normalParameterTypes[0], _node(1)) |
| 75 | + ], |
| 76 | + returnType: DecoratedType(type.returnType, always)); |
| 77 | + expect(decoratedType.toString(), 'dynamic Function(int?(type(1)))?'); |
| 78 | + } |
| 79 | + |
| 80 | + test_toString_optional_parameter() async { |
| 81 | + await resolveTestUnit('''dynamic f([int x]) {}'''); |
| 82 | + var type = findElement.function('f').type; |
| 83 | + var decoratedType = DecoratedType(type, always, |
| 84 | + positionalParameters: [ |
| 85 | + DecoratedType(type.optionalParameterTypes[0], _node(1)) |
| 86 | + ], |
| 87 | + returnType: DecoratedType(type.returnType, always)); |
| 88 | + expect(decoratedType.toString(), 'dynamic Function([int?(type(1))])?'); |
| 89 | + } |
| 90 | + |
| 91 | + NullabilityNode _node(int offset) => |
| 92 | + NullabilityNode.forTypeAnnotation(offset); |
| 93 | +} |
0 commit comments