diff --git a/packages/auto_mappr/CHANGELOG.md b/packages/auto_mappr/CHANGELOG.md index 15f226fd..e2aa4395 100644 --- a/packages/auto_mappr/CHANGELOG.md +++ b/packages/auto_mappr/CHANGELOG.md @@ -1,5 +1,9 @@ [//]: # (## Unreleased) +## 2.13.0 +- Added handling for classes with dynamic nested types. +- Added warning when mapping dynamic types without defined type converter. [#252](https://github.com/netglade/auto_mappr/pull/252) + ## 2.12.0 - Removed (unused) dependency on `get_it` package. diff --git a/packages/auto_mappr/dcm_global.yaml b/packages/auto_mappr/dcm_global.yaml new file mode 100644 index 00000000..72d19122 --- /dev/null +++ b/packages/auto_mappr/dcm_global.yaml @@ -0,0 +1 @@ +version: "1.34.1" diff --git a/packages/auto_mappr/lib/src/builder/assignments/nested_object_mixin.dart b/packages/auto_mappr/lib/src/builder/assignments/nested_object_mixin.dart index 34394466..9c26d85e 100644 --- a/packages/auto_mappr/lib/src/builder/assignments/nested_object_mixin.dart +++ b/packages/auto_mappr/lib/src/builder/assignments/nested_object_mixin.dart @@ -26,8 +26,9 @@ mixin NestedObjectMixin on AssignmentBuilderBase { final fieldMapping = mapping.tryGetFieldMapping(assignment.targetName); // Source and target is the same. - if (source.isSame(target)) { - final shouldIgnoreNull = fieldMapping?.ignoreNull ?? + if (source.isSame(target) || source.isDynamic || target.isDynamic) { + final shouldIgnoreNull = + fieldMapping?.ignoreNull ?? mapping.ignoreFieldNull ?? mapperConfig.mapprOptions.ignoreNullableSourceField ?? false; @@ -40,10 +41,7 @@ mixin NestedObjectMixin on AssignmentBuilderBase { return sourceOnModel; } - final nestedMapping = mapperConfig.findMapping( - source: source, - target: target, - ); + final nestedMapping = mapperConfig.findMapping(source: source, target: target); // Type converters. final typeConvertersBuilder = TypeConverterBuilder( @@ -96,10 +94,7 @@ mixin NestedObjectMixin on AssignmentBuilderBase { // name: 'test', // ) // : _map_NestedDto_To_Nested(model.name), - return sourceOnModel.equalTo(literalNull).conditional( - fieldMapping!.whenNullExpression!, - convertCallExpression, - ); + return sourceOnModel.equalTo(literalNull).conditional(fieldMapping!.whenNullExpression!, convertCallExpression); } // Generates code like: @@ -130,14 +125,8 @@ mixin NestedObjectMixin on AssignmentBuilderBase { // Otherwise use non-nullable. final convertMethod = refer( useNullableMethod - ? MethodBuilderBase.constructNullableConvertMethodName( - source: source, - target: target, - ) - : MethodBuilderBase.constructConvertMethodName( - source: source, - target: target, - ), + ? MethodBuilderBase.constructNullableConvertMethodName(source: source, target: target) + : MethodBuilderBase.constructConvertMethodName(source: source, target: target), ); if (useNullableMethod) { @@ -150,10 +139,7 @@ mixin NestedObjectMixin on AssignmentBuilderBase { [convertMethodArgument], {}, includeGenericTypes - ? [ - EmitterHelper.current.typeRefer(type: source), - EmitterHelper.current.typeRefer(type: target), - ] + ? [EmitterHelper.current.typeRefer(type: source), EmitterHelper.current.typeRefer(type: target)] : [], ); } diff --git a/packages/auto_mappr/lib/src/builder/value_assignment_builder.dart b/packages/auto_mappr/lib/src/builder/value_assignment_builder.dart index 2c057b71..62ffa160 100644 --- a/packages/auto_mappr/lib/src/builder/value_assignment_builder.dart +++ b/packages/auto_mappr/lib/src/builder/value_assignment_builder.dart @@ -2,6 +2,7 @@ import 'package:auto_mappr/src/builder/assignments/assignments.dart'; import 'package:auto_mappr/src/extensions/dart_type_extension.dart'; import 'package:auto_mappr/src/helpers/emitter_helper.dart'; import 'package:auto_mappr/src/models/models.dart'; +import 'package:build/build.dart'; import 'package:code_builder/code_builder.dart'; /// Decides how values are assigned. @@ -121,6 +122,12 @@ class ValueAssignmentBuilder { .nullChecked; } + if (assignment.sourceType!.isDynamic && !assignment.targetType.isDynamic) { + log.warning("Casting dynamic source field '$assignment' when mapping '$mapping'. Consider providing a type converter or a custom mapping to avoid runtime casts."); + + return rightSide.asA(refer(assignment.targetType.getDisplayString())); + } + return rightSide; } } diff --git a/packages/auto_mappr/lib/src/extensions/dart_type_extension.dart b/packages/auto_mappr/lib/src/extensions/dart_type_extension.dart index e31d05f6..ce11e638 100644 --- a/packages/auto_mappr/lib/src/extensions/dart_type_extension.dart +++ b/packages/auto_mappr/lib/src/extensions/dart_type_extension.dart @@ -22,6 +22,10 @@ extension DartTypeExtension on DartType { return !isNullable; } + bool get isDynamic { + return this is DynamicType; + } + /// Is special variant of integer list. /// /// See `[Uint8List], [Uint16List], [Uint32List], [Uint64List]`. diff --git a/packages/auto_mappr/lib/src/models/type_converter.dart b/packages/auto_mappr/lib/src/models/type_converter.dart index cfa8897a..871a6ccc 100644 --- a/packages/auto_mappr/lib/src/models/type_converter.dart +++ b/packages/auto_mappr/lib/src/models/type_converter.dart @@ -11,11 +11,7 @@ class TypeConverter extends Equatable { @override List get props => [source, target, converter]; - const TypeConverter({ - required this.source, - required this.target, - required this.converter, - }); + const TypeConverter({required this.source, required this.target, required this.converter}); @override String toString() { @@ -25,18 +21,12 @@ class TypeConverter extends Equatable { return 'typeConverter $sourceX -> $targetX'; } - bool canBeUsed({ - required DartType mappingSource, - required DartType mappingTarget, - }) { + bool canBeUsed({required DartType mappingSource, required DartType mappingTarget}) { return _isConverterSubtype(source, mappingSource, _ConversionRole.source) && _isConverterSubtype(target, mappingTarget, _ConversionRole.target); } - bool canBeUsedNullable({ - required DartType mappingSource, - required DartType mappingTarget, - }) { + bool canBeUsedNullable({required DartType mappingSource, required DartType mappingTarget}) { // ignore: avoid-inverted-boolean-checks, this is better if (!(mappingSource.isNullable && mappingTarget.isNullable)) return false; @@ -51,7 +41,13 @@ class TypeConverter extends Equatable { } bool _isConverterSubtype(DartType converterType, DartType fieldType, _ConversionRole role) { - // Same type. + // Both types are dynamic, allow. + if (converterType.isDynamic && fieldType.isDynamic) return true; + + // One of the types is dynamic, deny. + if (converterType.isDynamic != fieldType.isDynamic) return false; + + // Same type, allow. if (converterType == fieldType) return true; // A TypeConverter with a non-nullable source converterType cannot handle a nullable source field @@ -73,7 +69,4 @@ class TypeConverter extends Equatable { } } -enum _ConversionRole { - source, - target, -} +enum _ConversionRole { source, target } diff --git a/packages/auto_mappr/pubspec.yaml b/packages/auto_mappr/pubspec.yaml index 98ae86fb..fad8a6db 100644 --- a/packages/auto_mappr/pubspec.yaml +++ b/packages/auto_mappr/pubspec.yaml @@ -1,6 +1,6 @@ name: auto_mappr description: Code generation for mapping between different objects with ease. -version: 2.12.0 +version: 2.13.0 repository: https://github.com/netglade/auto_mappr issue_tracker: https://github.com/netglade/auto_mappr/issues screenshots: diff --git a/packages/auto_mappr/test/integration/dynamic_test.dart b/packages/auto_mappr/test/integration/dynamic_test.dart new file mode 100644 index 00000000..456a0bca --- /dev/null +++ b/packages/auto_mappr/test/integration/dynamic_test.dart @@ -0,0 +1,100 @@ +import 'package:test/test.dart'; + +import 'fixture/dynamic.dart' as fixture; + +void main() { + late final fixture.Mappr mappr; + + setUpAll(() { + mappr = const fixture.Mappr(); + }); + + group('using converter', () { + group('from dynamic to int', () { + test('string to int', () { + const source = fixture.DynamicDto(value: '5'); + final converted = mappr.convert(source); + + expect(converted.value, equals(5)); + }); + + test('int to int', () { + const source = fixture.DynamicDto(value: 5); + final converted = mappr.convert(source); + + expect(converted.value, equals(5)); + }); + + test('unknown type to 0', () { + const source = fixture.DynamicDto(); + final converted = mappr.convert(source); + + expect(converted.value, equals(0)); + }); + }); + + group('from int to dynamic', () { + test('to int', () { + const source = fixture.Int(value: 20); + final converted = mappr.convert(source); + + expect(converted, isA()); + expect(converted.value, isA()); + expect(converted.value, equals(20)); + }); + + test('to string', () { + const source = fixture.Int(value: 60); + final converted = mappr.convert(source); + + expect(converted, isA()); + expect(converted.value, isA()); + expect(converted.value, equals('60')); + }); + }); + + test('from dynamic to dynamic', () { + const source = fixture.DynamicDto(value: 'test'); + final converted = mappr.convert(source); + + expect(converted, isA()); + expect(converted.value, equals('test')); + }); + + test('from iterable dynamic', () { + const source = [fixture.DynamicDto(value: 5), fixture.DynamicDto(value: 1)]; + final converted = mappr.convertIterable(source); + + expect(converted, isA>()); + expect(converted.length, equals(2)); + }); + }); + + group('without converter', () { + test('from dynamic to int', () { + const source = fixture.DynamicNoConverter(value: 5); + final converted = mappr.convert(source); + + expect(converted, isA()); + expect(converted.value, isA()); + expect(converted.value, equals(5)); + }); + + test('from int to dynamic', () { + const source = fixture.IntNoConverter(value: 5); + final converted = mappr.convert(source); + + expect(converted, isA()); + expect(converted.value, isA()); + expect(converted.value, equals(5)); + }); + + test('from iterable dynamic', () { + const source = [fixture.DynamicNoConverter(value: 5), fixture.DynamicNoConverter(value: 1)]; + final converted = mappr.convertIterable(source); + + expect(converted, isA>()); + expect(converted.length, equals(2)); + }); + }); +} diff --git a/packages/auto_mappr/test/integration/fixture/dynamic.dart b/packages/auto_mappr/test/integration/fixture/dynamic.dart new file mode 100644 index 00000000..3e587318 --- /dev/null +++ b/packages/auto_mappr/test/integration/fixture/dynamic.dart @@ -0,0 +1,59 @@ +// ignore_for_file: avoid-dynamic + +import 'package:auto_mappr_annotation/auto_mappr_annotation.dart'; + +import 'dynamic.auto_mappr.dart'; + +@AutoMappr([ + MapType(converters: [DynamicConverter.dynamicToIntConverter]), + MapType(converters: [DynamicConverter.intToDynamicConverter]), + MapType(), + MapType(), + MapType(), +]) +class Mappr extends $Mappr { + const Mappr(); +} + +class Dynamic { + final dynamic value; + + const Dynamic({this.value}); +} + +class DynamicDto { + final dynamic value; + + const DynamicDto({this.value}); +} + +class Int { + final int value; + + const Int({required this.value}); +} + +class DynamicNoConverter { + final dynamic value; + + const DynamicNoConverter({this.value}); +} + +class IntNoConverter { + final int value; + + const IntNoConverter({required this.value}); +} + +abstract final class DynamicConverter { + static const intToDynamicConverter = TypeConverter(intToDynamic); + static const dynamicToIntConverter = TypeConverter(dynamicToInt); + + static dynamic intToDynamic(int source) => source < 50 ? source : source.toString(); + + static int dynamicToInt(dynamic source) => switch (source) { + int() => source, + String() => int.parse(source), + _ => 0, + }; +} diff --git a/packages/auto_mappr/test/integration/fixture/rename.dart b/packages/auto_mappr/test/integration/fixture/rename.dart index 9d1c815c..11ddc958 100644 --- a/packages/auto_mappr/test/integration/fixture/rename.dart +++ b/packages/auto_mappr/test/integration/fixture/rename.dart @@ -323,9 +323,7 @@ class NoConstructorWithLateDto { // ignore: must_be_immutable, ok in tests class NoConstructorWithLate with EquatableMixin { - // ignore: avoid-unassigned-late-fields, will be set using Mappr late int alpha; - // ignore: avoid-unassigned-late-fields, will be set using Mappr late String beta; @override diff --git a/packages/auto_mappr/test/integration/fixture/type_converters.dart b/packages/auto_mappr/test/integration/fixture/type_converters.dart index a881f372..809b9cbe 100644 --- a/packages/auto_mappr/test/integration/fixture/type_converters.dart +++ b/packages/auto_mappr/test/integration/fixture/type_converters.dart @@ -8,12 +8,8 @@ import 'type_converters/module_alpha.dart'; @AutoMappr( [ - MapType( - converters: [TypeConverter(Mappr.objectToString)], - ), - MapType( - converters: [TypeConverter>(Mappr.intToValueInt)], - ), + MapType(converters: [TypeConverter(Mappr.objectToString)]), + MapType(converters: [TypeConverter>(Mappr.intToValueInt)]), MapType(), MapType(), MapType(), @@ -42,6 +38,7 @@ class Mappr extends $Mappr { static Value objectToValueObject2(Object source) { if (source is int) { + // ignore: avoid-inferrable-type-arguments, ok here return Value(source); } @@ -83,11 +80,7 @@ class NormalFieldDto { final String xString; final bool normalBool; - const NormalFieldDto({ - required this.xInt, - required this.xString, - required this.normalBool, - }); + const NormalFieldDto({required this.xInt, required this.xString, required this.normalBool}); } class NormalField with EquatableMixin { @@ -108,11 +101,7 @@ class InListDto { final String xString; final bool normalBool; - const InListDto({ - required this.xInt, - required this.xString, - required this.normalBool, - }); + const InListDto({required this.xInt, required this.xString, required this.normalBool}); } class InList with EquatableMixin { diff --git a/packages/auto_mappr/test/integration/generics_test.dart b/packages/auto_mappr/test/integration/generics_test.dart index 089f6f83..27c95b89 100644 --- a/packages/auto_mappr/test/integration/generics_test.dart +++ b/packages/auto_mappr/test/integration/generics_test.dart @@ -14,244 +14,162 @@ void main() { const dto = fixture.With(first: 18, second: 789); final converted = mappr.convert, fixture.With>(dto); - expect( - converted, - equals(const fixture.With(first: 18, second: 789)), - ); + expect(converted, equals(const fixture.With(first: 18, second: 789))); }); test('With -> With', () { const dto = fixture.With(first: 'Some string', second: 123123); final converted = mappr.convert, fixture.With>(dto); - expect( - converted, - equals(const fixture.With(first: 'Some string', second: 123123)), - ); + expect(converted, equals(const fixture.With(first: 'Some string', second: 123123))); }); test('With> -> With>', () { const dto = fixture.With>( first: 69, - second: fixture.AlphaDto( - fixture.With(first: 741, second: 852.666), - 123, - ), + second: fixture.AlphaDto(fixture.With(first: 741, second: 852.666), 123), + ); + final converted = mappr.convert>, fixture.With>>( + dto, ); - final converted = - mappr.convert>, fixture.With>>(dto); expect( converted, equals( const fixture.With>( first: 69, - second: fixture.Alpha( - fixture.With(first: 741, second: 852.666), - 123, - ), + second: fixture.Alpha(fixture.With(first: 741, second: 852.666), 123), ), ), ); }); test('With -> Without', () { - const dto = fixture.With(first: 'test abc', second: 111222333); + const dto = fixture.With(first: 'test abc', second: 111222333); final converted = mappr.convert, fixture.Without>(dto); - expect( - converted, - equals(const fixture.Without(first: 'test abc', second: 111222333)), - ); + expect(converted, equals(const fixture.Without(first: 'test abc', second: 111222333))); }); test('With -> With', () { - const dto = fixture.With(first: 'test abc 2', second: 741852963); + const dto = fixture.With(first: 'test abc 2', second: 741852963); final converted = mappr.convert, fixture.With>(dto); - expect( - converted, - equals(const fixture.With(first: 'test abc 2', second: 741852963)), - ); + expect(converted, equals(const fixture.With(first: 'test abc 2', second: 741852963))); }); test('Without -> With', () { const dto = fixture.Without(first: 'alpha test', second: 111000); final converted = mappr.convert>(dto); - expect( - converted, - equals(const fixture.With(first: 'alpha test', second: 111000)), - ); + expect(converted, equals(const fixture.With(first: 'alpha test', second: 111000))); }); }); group('nested', () { test('AlphaDto -> Alpha', () { - const dto = fixture.AlphaDto(fixture.With(first: 1, second: 2), 420); + const dto = fixture.AlphaDto(fixture.With(first: 1, second: 2), 420); final converted = mappr.convert, fixture.Alpha>(dto); - expect( - converted, - equals(const fixture.Alpha(fixture.With(first: 1, second: 2), 420)), - ); + expect(converted, equals(const fixture.Alpha(fixture.With(first: 1, second: 2), 420))); }); test('AlphaDto -> Alpha', () { - const dto = fixture.AlphaDto(fixture.With(first: 7771, second: 1472), 42); + const dto = fixture.AlphaDto(fixture.With(first: 7771, second: 1472), 42); final converted = mappr.convert, fixture.Alpha>(dto); - expect( - converted, - equals(const fixture.Alpha(fixture.With(first: 7771, second: 1472), 42)), - ); + expect(converted, equals(const fixture.Alpha(fixture.With(first: 7771, second: 1472), 42))); }); test('AlphaDto -> Alpha', () { - const dto = fixture.AlphaDto(fixture.With(first: 'test a', second: 'test b'), 42); + const dto = fixture.AlphaDto(fixture.With(first: 'test a', second: 'test b'), 42); final converted = mappr.convert, fixture.Alpha>(dto); - expect( - converted, - equals(const fixture.Alpha(fixture.With(first: 'test a', second: 'test b'), 42)), - ); + expect(converted, equals(const fixture.Alpha(fixture.With(first: 'test a', second: 'test b'), 42))); }); }); group('collections', () { - test( - 'ListHolder> -> ListHolder>', - () { - const dto = fixture.ListHolder>( - [ - fixture.With>( + test('ListHolder> -> ListHolder>', () { + const dto = fixture.ListHolder>([ + fixture.With>( + first: 13, + second: fixture.AlphaDto(fixture.With(first: 11.7, second: 12.99), 130), + ), + fixture.With>( + first: 28, + second: fixture.AlphaDto(fixture.With(first: 77, second: 99.99), 20), + ), + ]); + final converted = mappr + .convert>, fixture.ListHolder>>(dto); + + expect( + converted, + equals( + const fixture.ListHolder>([ + fixture.With>( first: 13, - second: fixture.AlphaDto(fixture.With(first: 11.7, second: 12.99), 130), + second: fixture.Alpha(fixture.With(first: 11.7, second: 12.99), 130), ), - fixture.With>( + fixture.With>( first: 28, - second: fixture.AlphaDto(fixture.With(first: 77, second: 99.99), 20), - ), - ], - ); - final converted = mappr - .convert>, fixture.ListHolder>>(dto); - - expect( - converted, - equals( - const fixture.ListHolder>( - [ - fixture.With>( - first: 13, - second: fixture.Alpha(fixture.With(first: 11.7, second: 12.99), 130), - ), - fixture.With>( - first: 28, - second: fixture.Alpha(fixture.With(first: 77, second: 99.99), 20), - ), - ], + second: fixture.Alpha(fixture.With(first: 77, second: 99.99), 20), ), - ), - ); - }, - ); + ]), + ), + ); + }); test('SetHolder -> SetHolder', () { - final dto = fixture.SetHolder( - { - const fixture.With( - first: 'alpha x1', - second: 'alpha x2', - ), - const fixture.With( - first: 'alpha x3', - second: 'alpha x4', - ), - }, - ); + final dto = fixture.SetHolder({ + const fixture.With(first: 'alpha x1', second: 'alpha x2'), + const fixture.With(first: 'alpha x3', second: 'alpha x4'), + }); final converted = mappr.convert, fixture.SetHolder>(dto); expect( converted, equals( - fixture.SetHolder( - { - const fixture.With( - first: 'alpha x1', - second: 'alpha x2', - ), - const fixture.With( - first: 'alpha x3', - second: 'alpha x4', - ), - }, - ), + fixture.SetHolder({ + const fixture.With(first: 'alpha x1', second: 'alpha x2'), + const fixture.With(first: 'alpha x3', second: 'alpha x4'), + }), ), ); }); test('IterableHolder -> IterableHolder', () { - final dto = fixture.IterableHolder( - { - const fixture.With(first: 'beta y1', second: 4201), - const fixture.With(first: 'beta y2', second: 4202), - }, - ); + final dto = fixture.IterableHolder({ + const fixture.With(first: 'beta y1', second: 4201), + const fixture.With(first: 'beta y2', second: 4202), + }); final converted = mappr.convert, fixture.IterableHolder>(dto); expect( converted, equals( - fixture.IterableHolder( - { - const fixture.With(first: 'beta y1', second: 4201), - const fixture.With(first: 'beta y2', second: 4202), - }, - ), + fixture.IterableHolder({ + const fixture.With(first: 'beta y1', second: 4201), + const fixture.With(first: 'beta y2', second: 4202), + }), ), ); }); - test( - 'MapHolder -> MapHolder', - () { - const dto = fixture.MapHolder( - { - 'hello test 1': fixture.With( - first: 999, - second: true, - ), - 'hello test 2': fixture.With( - first: 888, - second: true, - ), - 'hello test 3': fixture.With( - first: 777, - second: false, - ), - }, - ); - final converted = - mappr.convert, fixture.MapHolder>(dto); + test('MapHolder -> MapHolder', () { + const dto = fixture.MapHolder({ + 'hello test 1': fixture.With(first: 999, second: true), + 'hello test 2': fixture.With(first: 888, second: true), + 'hello test 3': fixture.With(first: 777, second: false), + }); + final converted = mappr.convert, fixture.MapHolder>(dto); - expect( - converted, - equals( - const fixture.MapHolder( - { - 'hello test 1': fixture.With( - first: 999, - second: true, - ), - 'hello test 2': fixture.With( - first: 888, - second: true, - ), - 'hello test 3': fixture.With( - first: 777, - second: false, - ), - }, - ), - ), - ); - }, - ); + expect( + converted, + equals( + const fixture.MapHolder({ + 'hello test 1': fixture.With(first: 999, second: true), + 'hello test 2': fixture.With(first: 888, second: true), + 'hello test 3': fixture.With(first: 777, second: false), + }), + ), + ); + }); }); } diff --git a/packages/auto_mappr/test/integration/import_alias_test.dart b/packages/auto_mappr/test/integration/import_alias_test.dart index 6ac08af1..2fd991e5 100644 --- a/packages/auto_mappr/test/integration/import_alias_test.dart +++ b/packages/auto_mappr/test/integration/import_alias_test.dart @@ -11,73 +11,70 @@ void main() { mappr = const fixture.Mappr(); }); - group( - 'Mapping between objects with the same name from different libraries', - () { - test('UserDto to User works', () { - const dto = fixture.UserDto(name: 'John Wick', age: 42); - final converted = mappr.convert(dto); - - expect(converted, equals(const fixture.User(name: 'John Wick', age: 42))); - }); + group('Mapping between objects with the same name from different libraries', () { + test('UserDto to User works', () { + const dto = fixture.UserDto(name: 'John Wick', age: 42); + final converted = mappr.convert(dto); - test('UserDto to a1.User works', () { - const dto = fixture.UserDto(name: 'John Wick', age: 42); - final converted = mappr.convert(dto); + expect(converted, equals(const fixture.User(name: 'John Wick', age: 42))); + }); - expect(converted, equals(const fixture_a1.User(name: 'John Wick', age: 42))); - }); + test('UserDto to a1.User works', () { + const dto = fixture.UserDto(name: 'John Wick', age: 42); + final converted = mappr.convert(dto); - test('UserDto to a2.User works', () { - const dto = fixture.UserDto(name: 'John Wick', age: 42); - final converted = mappr.convert(dto); + expect(converted, equals(const fixture_a1.User(name: 'John Wick', age: 42))); + }); - expect(converted, equals(const fixture_a2.User(name: 'John Wick', age: 42))); - }); + test('UserDto to a2.User works', () { + const dto = fixture.UserDto(name: 'John Wick', age: 42); + final converted = mappr.convert(dto); - test('a1.UserDto to User works', () { - const dto = fixture_a1.UserDto(name: 'John Wick', age: 42); - final converted = mappr.convert(dto); + expect(converted, equals(const fixture_a2.User(name: 'John Wick', age: 42))); + }); - expect(converted, equals(const fixture.User(name: 'John Wick', age: 42))); - }); + test('a1.UserDto to User works', () { + const dto = fixture_a1.UserDto(name: 'John Wick', age: 42); + final converted = mappr.convert(dto); - test('a1.UserDto to a1.User works', () { - const dto = fixture_a1.UserDto(name: 'John Wick', age: 42); - final converted = mappr.convert(dto); + expect(converted, equals(const fixture.User(name: 'John Wick', age: 42))); + }); - expect(converted, equals(const fixture_a1.User(name: 'John Wick', age: 42))); - }); + test('a1.UserDto to a1.User works', () { + const dto = fixture_a1.UserDto(name: 'John Wick', age: 42); + final converted = mappr.convert(dto); + + expect(converted, equals(const fixture_a1.User(name: 'John Wick', age: 42))); + }); - test('a1.UserDto to a2.User works', () { - const dto = fixture_a1.UserDto(name: 'John Wick', age: 42); - final converted = mappr.convert(dto); + test('a1.UserDto to a2.User works', () { + const dto = fixture_a1.UserDto(name: 'John Wick', age: 42); + final converted = mappr.convert(dto); - expect(converted, equals(const fixture_a2.User(name: 'John Wick', age: 42))); - }); + expect(converted, equals(const fixture_a2.User(name: 'John Wick', age: 42))); + }); - test('a2.UserDto to User works', () { - const dto = fixture_a2.UserDto(name: 'John Wick', age: 42); - final converted = mappr.convert(dto); + test('a2.UserDto to User works', () { + const dto = fixture_a2.UserDto(name: 'John Wick', age: 42); + final converted = mappr.convert(dto); - expect(converted, equals(const fixture.User(name: 'John Wick', age: 42))); - }); + expect(converted, equals(const fixture.User(name: 'John Wick', age: 42))); + }); - test('a2.UserDto to a1.User works', () { - const dto = fixture_a2.UserDto(name: 'John Wick', age: 42); - final converted = mappr.convert(dto); + test('a2.UserDto to a1.User works', () { + const dto = fixture_a2.UserDto(name: 'John Wick', age: 42); + final converted = mappr.convert(dto); - expect(converted, equals(const fixture_a1.User(name: 'John Wick', age: 42))); - }); + expect(converted, equals(const fixture_a1.User(name: 'John Wick', age: 42))); + }); - test('a2.UserDto to a2.User works', () { - const dto = fixture_a2.UserDto(name: 'John Wick', age: 42); - final converted = mappr.convert(dto); + test('a2.UserDto to a2.User works', () { + const dto = fixture_a2.UserDto(name: 'John Wick', age: 42); + final converted = mappr.convert(dto); - expect(converted, equals(const fixture_a2.User(name: 'John Wick', age: 42))); - }); - }, - ); + expect(converted, equals(const fixture_a2.User(name: 'John Wick', age: 42))); + }); + }); group('Aliasing import with exports inside works correctly', () { test('XxxDto to Xxx works', () { @@ -97,12 +94,15 @@ void main() { group('generics', () { test('Holder to a2.Holder works', () { - const dto = fixture.Holder( + const dto = fixture.Holder( first: fixture_a1.UserDto(name: 'John Wick', age: 42), second: fixture_a2.UserDto(name: 'Obi-wan Kenobi', age: 69), ); - final converted = mappr.convert, - fixture_a2.Holder>(dto); + final converted = mappr + .convert< + fixture.Holder, + fixture_a2.Holder + >(dto); expect( converted, @@ -118,7 +118,7 @@ void main() { group('iterables', () { test('list works', () { - const dto = fixture.ListHolder([ + const dto = fixture.ListHolder([ fixture.UserDto(name: 'John Wick', age: 42), fixture.UserDto(name: 'Obi-wan Kenobi', age: 69), ]); @@ -136,7 +136,7 @@ void main() { }); test('map works', () { - const dto = fixture.MapHolder({ + const dto = fixture.MapHolder({ 'alpha': fixture.UserDto(name: 'John Wick', age: 42), 'beta': fixture.UserDto(name: 'Obi-wan Kenobi', age: 69), }); diff --git a/packages/auto_mappr/test/integration/type_converters_test.dart b/packages/auto_mappr/test/integration/type_converters_test.dart index 1f79f984..62cd486a 100644 --- a/packages/auto_mappr/test/integration/type_converters_test.dart +++ b/packages/auto_mappr/test/integration/type_converters_test.dart @@ -34,11 +34,7 @@ void main() { expect( converted, equals( - const fixture.InList( - [fixture.Value(789), fixture.Value(5), fixture.Value(1)], - fixture.Value('Dunno'), - false, - ), + const fixture.InList([fixture.Value(789), fixture.Value(5), fixture.Value(1)], fixture.Value('Dunno'), false), ), ); });