From 3f032b6f53772dab8da38b4737009c87d5ee6b09 Mon Sep 17 00:00:00 2001 From: Lucas Oliveira <62367544+tilucasoli@users.noreply.github.com> Date: Thu, 26 Sep 2024 17:51:04 -0300 Subject: [PATCH 1/2] Update shape_border_dto.dart --- .../attributes/border/shape_border_dto.dart | 156 ++++++++---------- 1 file changed, 73 insertions(+), 83 deletions(-) diff --git a/packages/mix/lib/src/attributes/border/shape_border_dto.dart b/packages/mix/lib/src/attributes/border/shape_border_dto.dart index d9c2ffe38..7976cf220 100644 --- a/packages/mix/lib/src/attributes/border/shape_border_dto.dart +++ b/packages/mix/lib/src/attributes/border/shape_border_dto.dart @@ -35,16 +35,6 @@ sealed class ShapeBorderDto extends Dto { : (side: null, borderRadius: null, boxShape: null); } - BorderSideDto? get _side => - this is OutlinedBorderDto ? (this as OutlinedBorderDto).side : null; - - BeveledRectangleBorderDto toBeveled(); - RoundedRectangleBorderDto toRoundedRectangle(); - ContinuousRectangleBorderDto toContinuous(); - CircleBorderDto toCircle(); - StadiumBorderDto toStadiumBorder(); - StarBorderDto toStar(); - LinearBorderDto toLinear(); @override ShapeBorderDto merge(covariant ShapeBorderDto? other); @@ -73,16 +63,9 @@ abstract class OutlinedBorderDto B extends OutlinedBorderDto>(A a, B b) { if (a.runtimeType == b.runtimeType) return a.merge(b) as B; - return switch (b) { - (BeveledRectangleBorderDto b) => a.toBeveled().merge(b) as B, - (RoundedRectangleBorderDto b) => a.toRoundedRectangle().merge(b) as B, - (ContinuousRectangleBorderDto b) => a.toContinuous().merge(b) as B, - (CircleBorderDto b) => a.toCircle().merge(b) as B, - (StadiumBorderDto b) => a.toStadiumBorder().merge(b) as B, - (StarBorderDto b) => a.toStar().merge(b) as B, - (LinearBorderDto b) => a.toLinear().merge(b) as B, - (OutlinedBorderDto b) => a.merge(b) as B, - }; + final adaptedA = b.adapt(a) as B; + + return adaptedA.merge(b) as B; } BoxShape? _toBoxShape() { @@ -98,67 +81,7 @@ abstract class OutlinedBorderDto @protected BorderRadiusGeometryDto? get borderRadiusGetter; - /// Tries to get borderRadius if available for [OutlineBorderDto] - - @override - BeveledRectangleBorderDto toBeveled() { - if (this is BeveledRectangleBorderDto) { - return this as BeveledRectangleBorderDto; - } - - return BeveledRectangleBorderDto( - borderRadius: borderRadiusGetter, - side: _side, - ); - } - - @override - ContinuousRectangleBorderDto toContinuous() { - return ContinuousRectangleBorderDto( - borderRadius: borderRadiusGetter, - side: _side, - ); - } - - @override - RoundedRectangleBorderDto toRoundedRectangle() { - if (this is RoundedRectangleBorderDto) { - return this as RoundedRectangleBorderDto; - } - - return RoundedRectangleBorderDto( - borderRadius: borderRadiusGetter, - side: _side, - ); - } - - @override - CircleBorderDto toCircle() { - if (this is CircleBorderDto) return this as CircleBorderDto; - - return CircleBorderDto(side: _side); - } - - @override - StadiumBorderDto toStadiumBorder() { - if (this is StadiumBorderDto) return this as StadiumBorderDto; - - return StadiumBorderDto(side: _side); - } - - @override - LinearBorderDto toLinear() { - if (this is LinearBorderDto) return this as LinearBorderDto; - - return LinearBorderDto(side: _side); - } - - @override - StarBorderDto toStar() { - if (this is StarBorderDto) return this as StarBorderDto; - - return StarBorderDto(side: _side); - } + OutlinedBorderDto adapt(OutlinedBorderDto other); @override OutlinedBorderDto merge(covariant OutlinedBorderDto? other); @@ -176,6 +99,16 @@ final class RoundedRectangleBorderDto const RoundedRectangleBorderDto({this.borderRadius, super.side}); + @override + RoundedRectangleBorderDto adapt(OutlinedBorderDto other) { + if (other is RoundedRectangleBorderDto) return other; + + return RoundedRectangleBorderDto( + borderRadius: other.borderRadiusGetter, + side: other.side, + ); + } + @override BorderRadiusGeometryDto? get borderRadiusGetter => borderRadius; @@ -191,6 +124,16 @@ final class BeveledRectangleBorderDto const BeveledRectangleBorderDto({this.borderRadius, super.side}); + @override + BeveledRectangleBorderDto adapt(OutlinedBorderDto other) { + if (other is BeveledRectangleBorderDto) return other; + + return BeveledRectangleBorderDto( + borderRadius: other.borderRadiusGetter, + side: other.side, + ); + } + @override BorderRadiusGeometryDto? get borderRadiusGetter => borderRadius; @@ -206,6 +149,18 @@ final class ContinuousRectangleBorderDto const ContinuousRectangleBorderDto({this.borderRadius, super.side}); + @override + ContinuousRectangleBorderDto adapt(OutlinedBorderDto other) { + if (other is ContinuousRectangleBorderDto) { + return other; + } + + return ContinuousRectangleBorderDto( + borderRadius: other.borderRadiusGetter, + side: other.side, + ); + } + @override BorderRadiusGeometryDto? get borderRadiusGetter => borderRadius; @override @@ -219,6 +174,16 @@ final class CircleBorderDto extends OutlinedBorderDto final double? eccentricity; const CircleBorderDto({super.side, this.eccentricity}); + + @override + CircleBorderDto adapt(OutlinedBorderDto other) { + if (other is CircleBorderDto) { + return other; + } + + return CircleBorderDto(side: other.side); + } + @override BorderRadiusGeometryDto? get borderRadiusGetter => null; @override @@ -244,6 +209,12 @@ final class StarBorderDto extends OutlinedBorderDto this.rotation, this.squash, }); + + @override + StarBorderDto adapt(OutlinedBorderDto other) { + return StarBorderDto(side: other.side); + } + @override BorderRadiusGeometryDto? get borderRadiusGetter => null; @override @@ -265,6 +236,16 @@ final class LinearBorderDto extends OutlinedBorderDto this.top, this.bottom, }); + + @override + LinearBorderDto adapt(OutlinedBorderDto other) { + if (other is LinearBorderDto) { + return other; + } + + return LinearBorderDto(side: other.side); + } + @override BorderRadiusGeometryDto? get borderRadiusGetter => null; @override @@ -288,6 +269,15 @@ final class StadiumBorderDto extends OutlinedBorderDto with _$StadiumBorderDto { const StadiumBorderDto({super.side}); + @override + StadiumBorderDto adapt(OutlinedBorderDto other) { + if (other is StadiumBorderDto) { + return other; + } + + return StadiumBorderDto(side: other.side); + } + @override BorderRadiusGeometryDto? get borderRadiusGetter => null; @override @@ -339,7 +329,7 @@ class ShapeBorderUtility late final star = StarBorderUtility(builder); - ShapeBorderUtility(super.builder); + late final shapeBuilder = builder; - T call(ShapeBorder value) => builder(value.toDto()); + ShapeBorderUtility(super.builder); } From 1ce90805316155f5dd03cdac9a4cefc8d7a9d296 Mon Sep 17 00:00:00 2001 From: Lucas Oliveira <62367544+tilucasoli@users.noreply.github.com> Date: Thu, 26 Sep 2024 18:18:07 -0300 Subject: [PATCH 2/2] Update shape_border_dto_test.dart --- .../border/shape_border_dto_test.dart | 179 ++++++++++++++++++ 1 file changed, 179 insertions(+) diff --git a/packages/mix/test/src/attributes/border/shape_border_dto_test.dart b/packages/mix/test/src/attributes/border/shape_border_dto_test.dart index e2b2b6e32..22ce2ce45 100644 --- a/packages/mix/test/src/attributes/border/shape_border_dto_test.dart +++ b/packages/mix/test/src/attributes/border/shape_border_dto_test.dart @@ -640,4 +640,183 @@ void main() { expect(OutlinedBorderDto.tryToMerge(dto1, dto2), equals(expectedResult)); }); }); + group('RoundedRectangleBorderDto', () { + test( + 'adapt method should return the same instance if input is RoundedRectangleBorderDto', + () { + const dtoA = RoundedRectangleBorderDto( + borderRadius: BorderRadiusDto(topLeft: Radius.circular(10)), + side: BorderSideDto(width: 2.0), + ); + const dtoB = RoundedRectangleBorderDto( + borderRadius: BorderRadiusDto(topLeft: Radius.circular(20)), + side: BorderSideDto(width: 4.0), + ); + expect(dtoA.adapt(dtoB), equals(dtoB)); + }); + + test( + 'adapt method should create a new instance from other OutlinedBorderDto', + () { + const dtoA = RoundedRectangleBorderDto(); + const dtoB = CircleBorderDto(side: BorderSideDto(width: 3.0)); + final result = dtoA.adapt(dtoB); + expect(result, isA()); + expect(result.side, equals(dtoB.side)); + expect(result.borderRadius, isNull); + }); + }); + + group('BeveledRectangleBorderDto', () { + test( + 'adapt method should return the same instance if input is BeveledRectangleBorderDto', + () { + const dtoA = BeveledRectangleBorderDto( + borderRadius: BorderRadiusDto(topLeft: Radius.circular(5)), + side: BorderSideDto(width: 1.5), + ); + const dtoB = BeveledRectangleBorderDto( + borderRadius: BorderRadiusDto(topLeft: Radius.circular(5)), + side: BorderSideDto(width: 3), + ); + expect(dtoA.adapt(dtoB), equals(dtoB)); + }); + + test( + 'adapt method should create a new instance from other OutlinedBorderDto', + () { + const dtoA = BeveledRectangleBorderDto(); + const dtoB = StadiumBorderDto(side: BorderSideDto(width: 2.5)); + final result = dtoA.adapt(dtoB); + expect(result, isA()); + expect(result.side, equals(dtoB.side)); + expect(result.borderRadius, isNull); + }); + }); + + group('ContinuousRectangleBorderDto', () { + test( + 'adapt method should return the same instance if input is ContinuousRectangleBorderDto', + () { + const dtoA = ContinuousRectangleBorderDto( + borderRadius: BorderRadiusDto(topLeft: Radius.circular(8)), + side: BorderSideDto(width: 1.2), + ); + const dtoB = ContinuousRectangleBorderDto( + borderRadius: BorderRadiusDto(topLeft: Radius.circular(16)), + side: BorderSideDto(width: 3), + ); + expect(dtoA.adapt(dtoB), equals(dtoB)); + }); + + test( + 'adapt method should create a new instance from other OutlinedBorderDto', + () { + const dtoA = ContinuousRectangleBorderDto(); + const dtoB = RoundedRectangleBorderDto( + side: BorderSideDto(width: 1.8), + borderRadius: BorderRadiusDto( + topLeft: Radius.circular(10), + ), + ); + final result = dtoA.adapt(dtoB); + expect(result, isA()); + expect(result.side, equals(dtoB.side)); + expect(result.borderRadius, equals(dtoB.borderRadius)); + }); + }); + + group('CircleBorderDto', () { + test( + 'adapt method should return the same instance if input is CircleBorderDto', + () { + const dtoA = + CircleBorderDto(side: BorderSideDto(width: 2.2), eccentricity: 0.5); + const dtoB = + CircleBorderDto(side: BorderSideDto(width: 22), eccentricity: 0.7); + expect(dtoA.adapt(dtoB), equals(dtoB)); + }); + + test( + 'adapt method should create a new instance from other OutlinedBorderDto', + () { + const dtoA = CircleBorderDto(); + const dtoB = BeveledRectangleBorderDto(side: BorderSideDto(width: 1.7)); + final result = dtoA.adapt(dtoB); + expect(result, isA()); + expect(result.side, equals(dtoB.side)); + expect(result.eccentricity, isNull); + }); + }); + + group('StarBorderDto', () { + test( + 'adapt method should create a new instance from other OutlinedBorderDto', + () { + const dtoA = StarBorderDto(); + const dtoB = ContinuousRectangleBorderDto( + side: BorderSideDto(width: 1.3), + ); + + final result = dtoA.adapt(dtoB); + expect(result, isA()); + expect(result.side, equals(dtoB.side)); + expect(result.points, isNull); + expect(result.innerRadiusRatio, isNull); + expect(result.pointRounding, isNull); + expect(result.valleyRounding, isNull); + expect(result.rotation, isNull); + expect(result.squash, isNull); + }); + }); + + group('LinearBorderDto', () { + test( + 'adapt method should return the same instance if input is LinearBorderDto', + () { + const dtoA = LinearBorderDto( + side: BorderSideDto(width: 1.9), + start: LinearBorderEdgeDto(size: 2.0, alignment: 0.5), + ); + const dtoB = LinearBorderDto( + side: BorderSideDto(width: 19), + start: LinearBorderEdgeDto(size: 20, alignment: 0.9), + ); + expect(dtoA.adapt(dtoB), equals(dtoB)); + }); + + test( + 'adapt method should create a new instance from other OutlinedBorderDto', + () { + const dtoA = LinearBorderDto(); + const dtoB = StadiumBorderDto(side: BorderSideDto(width: 2.1)); + final result = dtoA.adapt(dtoB); + expect(result, isA()); + expect(result.side, equals(dtoB.side)); + expect(result.start, isNull); + expect(result.end, isNull); + expect(result.top, isNull); + expect(result.bottom, isNull); + }); + }); + + group('StadiumBorderDto', () { + test( + 'adapt method should return the same instance if input is StadiumBorderDto', + () { + const dtoA = StadiumBorderDto(side: BorderSideDto(width: 1.6)); + const dtoB = StadiumBorderDto(side: BorderSideDto(width: 16)); + expect(dtoA.adapt(dtoB), equals(dtoB)); + }); + + test( + 'adapt method should create a new instance from other OutlinedBorderDto', + () { + const dtoA = StadiumBorderDto(); + const dtoB = CircleBorderDto(side: BorderSideDto(width: 2.3)); + final result = dtoA.adapt(dtoB); + expect(result, isA()); + expect(result.side, equals(dtoB.side)); + }); + }); }