From bc991b25d711644f01ae1ad184675d7750a79774 Mon Sep 17 00:00:00 2001 From: Lenkomotive Date: Sun, 12 Nov 2023 16:15:13 +0100 Subject: [PATCH] PAINTROID - Implement JSON Serialization via Code Generation more tetsing --- lib/core/path_with_action_history.dart | 35 ++++++++++++ .../converter/path_action_converter.dart | 36 ------------- .../draw_path_command_serializer_test.dart | 54 ++++++++++++++++++- .../converter/paint_converter_test.dart | 43 +++++---------- .../converter/path_action_converter_test.dart | 20 ++++--- ...th_with_action_history_converter_test.dart | 48 +++++++++++++++++ 6 files changed, 159 insertions(+), 77 deletions(-) delete mode 100644 lib/io/src/serialization/serializer/converter/path_action_converter.dart create mode 100644 test/unit/io/serialization/converter/path_with_action_history_converter_test.dart diff --git a/lib/core/path_with_action_history.dart b/lib/core/path_with_action_history.dart index c0149a57..bad94022 100644 --- a/lib/core/path_with_action_history.dart +++ b/lib/core/path_with_action_history.dart @@ -1,5 +1,6 @@ import 'dart:ui'; +import 'package:collection/collection.dart'; import 'package:paintroid/io/src/json_serializer/converter/path_action_converter.dart'; import 'package:paintroid/io/src/json_serializer/converter/path_with_action_history_converter.dart'; @@ -35,6 +36,18 @@ class PathWithActionHistory extends Path { factory PathWithActionHistory.fromJson(Map json) { return const PathWithActionHistoryConverter().fromJson(json); } + + @override + bool operator ==(Object other) { + if (other is PathWithActionHistory) { + return const ListEquality().equals(actions, other.actions); + } + return false; + } + + @override + int get hashCode => const ListEquality().hash(actions); + } abstract class PathAction { @@ -46,6 +59,17 @@ class MoveToAction extends PathAction { final double y; const MoveToAction(this.x, this.y); + + @override + bool operator ==(Object other) { + if (other is MoveToAction) { + return x == other.x && y == other.y; + } + return false; + } + + @override + int get hashCode => Object.hash(x, y); } class LineToAction extends PathAction { @@ -53,6 +77,17 @@ class LineToAction extends PathAction { final double y; const LineToAction(this.x, this.y); + + @override + bool operator ==(Object other) { + if (other is LineToAction) { + return x == other.x && y == other.y; + } + return false; + } + + @override + int get hashCode => Object.hash(x, y); } class CloseAction extends PathAction { diff --git a/lib/io/src/serialization/serializer/converter/path_action_converter.dart b/lib/io/src/serialization/serializer/converter/path_action_converter.dart deleted file mode 100644 index 80f19c33..00000000 --- a/lib/io/src/serialization/serializer/converter/path_action_converter.dart +++ /dev/null @@ -1,36 +0,0 @@ -import 'package:freezed_annotation/freezed_annotation.dart'; - -import 'package:paintroid/core/path_with_action_history.dart'; - -class PathActionConverter - implements JsonConverter> { - const PathActionConverter(); - - @override - PathAction fromJson(Map json) { - switch (json['type'] as String) { - case 'MoveToAction': - return MoveToAction(json['x'] as double, json['y'] as double); - case 'LineToAction': - return LineToAction(json['x'] as double, json['y'] as double); - case 'CloseAction': - return const CloseAction(); - default: - // handle error - throw Exception('Unknown PathAction type: ${json['type']}'); - } - } - - @override - Map toJson(PathAction action) { - if (action is MoveToAction) { - return {'type': 'MoveToAction', 'x': action.x, 'y': action.y}; - } else if (action is LineToAction) { - return {'type': 'LineToAction', 'x': action.x, 'y': action.y}; - } else if (action is CloseAction) { - return {'type': 'CloseAction'}; - } else { - throw Exception('Unknown PathAction type'); - } - } -} diff --git a/test/unit/io/serialization/commands/draw_path_command_serializer_test.dart b/test/unit/io/serialization/commands/draw_path_command_serializer_test.dart index d18db867..b8773cc9 100644 --- a/test/unit/io/serialization/commands/draw_path_command_serializer_test.dart +++ b/test/unit/io/serialization/commands/draw_path_command_serializer_test.dart @@ -1,8 +1,60 @@ +import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; +import 'package:paintroid/command/command.dart'; +import 'package:paintroid/core/path_with_action_history.dart'; void main() { - test('Test...', () { + Paint createPaint() { + return Paint() + ..color = Colors.blue + ..strokeWidth = 5.0 + ..strokeCap = StrokeCap.round + ..isAntiAlias = true + ..style = PaintingStyle.fill + ..strokeJoin = StrokeJoin.bevel; + } + bool comparePaint(Paint paint1, Paint paint2) { + return paint1.color == paint2.color && + paint1.strokeWidth == paint2.strokeWidth && + paint1.strokeCap == paint2.strokeCap && + paint1.isAntiAlias == paint2.isAntiAlias && + paint1.style == paint2.style && + paint1.strokeJoin == paint2.strokeJoin; + } + + PathWithActionHistory createPathWithActionHistory(int numberOfActions) { + PathWithActionHistory pathWithActionHistory = PathWithActionHistory(); + for (int i = 0; i < numberOfActions; i++) { + pathWithActionHistory.moveTo(i.toDouble(), i.toDouble() + 1); + pathWithActionHistory.lineTo(i.toDouble() + 2, i.toDouble() + 3); + } + pathWithActionHistory.close(); + return pathWithActionHistory; + } + + test('Test DrawPathCommand serialization with one path', () { + PathWithActionHistory originalPath = createPathWithActionHistory(1); + Paint originalPaint = createPaint(); + DrawPathCommand command = DrawPathCommand(originalPath, originalPaint); + + var json = command.toJson(); + DrawPathCommand deserializedCommand = DrawPathCommand.fromJson(json); + + expect(comparePaint(originalPaint, deserializedCommand.paint), isTrue); + expect(originalPath, equals(deserializedCommand.path)); + }); + + test('Test DrawPathCommand serialization with multiple paths', () { + PathWithActionHistory originalPath = createPathWithActionHistory(5); + Paint originalPaint = createPaint(); + DrawPathCommand command = DrawPathCommand(originalPath, originalPaint); + + var json = command.toJson(); + DrawPathCommand deserializedCommand = DrawPathCommand.fromJson(json); + + expect(comparePaint(originalPaint, deserializedCommand.paint), isTrue); + expect(originalPath, equals(deserializedCommand.path)); }); } diff --git a/test/unit/io/serialization/converter/paint_converter_test.dart b/test/unit/io/serialization/converter/paint_converter_test.dart index 58ce2966..3fb56669 100644 --- a/test/unit/io/serialization/converter/paint_converter_test.dart +++ b/test/unit/io/serialization/converter/paint_converter_test.dart @@ -6,6 +6,17 @@ import 'package:paintroid/io/src/json_serializer/converter/paint_converter.dart' void main() { + PaintConverter converter = const PaintConverter(); + + bool comparePaint(Paint paint1, Paint paint2) { + return paint1.color == paint2.color && + paint1.strokeWidth == paint2.strokeWidth && + paint1.strokeCap == paint2.strokeCap && + paint1.isAntiAlias == paint2.isAntiAlias && + paint1.style == paint2.style && + paint1.strokeJoin == paint2.strokeJoin; + } + test('Test converter for Paint version 1 - Basic Paint', () { Paint originalPaint = Paint() ..color = Colors.blue @@ -15,18 +26,11 @@ void main() { ..style = PaintingStyle.fill ..strokeJoin = StrokeJoin.bevel; - PaintConverter converter = const PaintConverter(); - var json = converter.toJson(originalPaint); Paint deserializedPaint = converter.fromJson(json); - expect(deserializedPaint.color, equals(originalPaint.color)); - expect(deserializedPaint.strokeWidth, equals(originalPaint.strokeWidth)); - expect(deserializedPaint.strokeCap, equals(originalPaint.strokeCap)); - expect(deserializedPaint.isAntiAlias, equals(originalPaint.isAntiAlias)); - expect(deserializedPaint.style, equals(originalPaint.style)); - expect(deserializedPaint.strokeJoin, equals(originalPaint.strokeJoin)); + expect(comparePaint(deserializedPaint, originalPaint), isTrue); }); test('Test converter for Paint version 1 - Basic Paint', () { @@ -38,18 +42,11 @@ void main() { ..style = PaintingStyle.stroke ..strokeJoin = StrokeJoin.miter; - PaintConverter converter = const PaintConverter(); - var json = converter.toJson(originalPaint); Paint deserializedPaint = converter.fromJson(json); - expect(deserializedPaint.color, equals(originalPaint.color)); - expect(deserializedPaint.strokeWidth, equals(originalPaint.strokeWidth)); - expect(deserializedPaint.strokeCap, equals(originalPaint.strokeCap)); - expect(deserializedPaint.isAntiAlias, equals(originalPaint.isAntiAlias)); - expect(deserializedPaint.style, equals(originalPaint.style)); - expect(deserializedPaint.strokeJoin, equals(originalPaint.strokeJoin)); + expect(comparePaint(deserializedPaint, originalPaint), isTrue); }); test('Test converter for Paint version 1 - Basic Paint', () { @@ -61,27 +58,16 @@ void main() { ..style = PaintingStyle.stroke ..strokeJoin = StrokeJoin.round; - PaintConverter converter = const PaintConverter(); - var json = converter.toJson(originalPaint); Paint deserializedPaint = converter.fromJson(json); - expect(deserializedPaint.color, equals(originalPaint.color)); - expect(deserializedPaint.strokeWidth, equals(originalPaint.strokeWidth)); - expect(deserializedPaint.strokeCap, equals(originalPaint.strokeCap)); - expect(deserializedPaint.isAntiAlias, equals(originalPaint.isAntiAlias)); - expect(deserializedPaint.style, equals(originalPaint.style)); - expect(deserializedPaint.strokeJoin, equals(originalPaint.strokeJoin)); + expect(comparePaint(deserializedPaint, originalPaint), isTrue); }); - - test('Test converter for Paint version 1 - Paint with Custom Color', () { Paint originalPaint = Paint()..color = Colors.red; - PaintConverter converter = const PaintConverter(); - var json = converter.toJson(originalPaint); Paint deserializedPaint = converter.fromJson(json); @@ -89,4 +75,3 @@ void main() { expect(deserializedPaint.color, equals(originalPaint.color)); }); } - diff --git a/test/unit/io/serialization/converter/path_action_converter_test.dart b/test/unit/io/serialization/converter/path_action_converter_test.dart index 1b0eefb2..ad50e55c 100644 --- a/test/unit/io/serialization/converter/path_action_converter_test.dart +++ b/test/unit/io/serialization/converter/path_action_converter_test.dart @@ -18,24 +18,22 @@ void main() { expect(deserializedMoveToAction, isA()); deserializedMoveToAction as MoveToAction; - expect(deserializedMoveToAction.x, xExpected); - expect(deserializedMoveToAction.y, yExpected); + expect(moveToAction, equals(deserializedMoveToAction)); }); test('Test converter for LineToAction version 1', () { double xExpected = 1.0; double yExpected = 2.0; - LineToAction moveToAction = LineToAction(xExpected, yExpected); + LineToAction lineToAction = LineToAction(xExpected, yExpected); - var json = converter.toJson(moveToAction); + var json = converter.toJson(lineToAction); - PathAction deserializedMoveToAction = converter.fromJson(json); + PathAction deserializedLineToAction = converter.fromJson(json); - expect(deserializedMoveToAction, isA()); - deserializedMoveToAction as LineToAction; - expect(deserializedMoveToAction.x, xExpected); - expect(deserializedMoveToAction.y, yExpected); + expect(deserializedLineToAction, isA()); + deserializedLineToAction as LineToAction; + expect(lineToAction, equals(deserializedLineToAction)); }); test('Test converter for CloseAction version 1', () { @@ -43,9 +41,9 @@ void main() { var json = converter.toJson(closeAction); - PathAction deserializedMoveToAction = converter.fromJson(json); + PathAction deserializedCloseAction = converter.fromJson(json); - expect(deserializedMoveToAction, isA()); + expect(deserializedCloseAction, isA()); }); } diff --git a/test/unit/io/serialization/converter/path_with_action_history_converter_test.dart b/test/unit/io/serialization/converter/path_with_action_history_converter_test.dart new file mode 100644 index 00000000..c68163c1 --- /dev/null +++ b/test/unit/io/serialization/converter/path_with_action_history_converter_test.dart @@ -0,0 +1,48 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:paintroid/core/path_with_action_history.dart'; +import 'package:paintroid/io/src/json_serializer/converter/path_with_action_history_converter.dart'; + +void main() { + + const PathWithActionHistoryConverter converter = PathWithActionHistoryConverter(); + + PathWithActionHistory createPathWithActionHistory(int numberOfActions) { + PathWithActionHistory pathWithActionHistory = PathWithActionHistory(); + for (int i = 0; i < numberOfActions; i++) { + pathWithActionHistory.moveTo(i.toDouble(), i.toDouble() + 1); + pathWithActionHistory.lineTo(i.toDouble() + 2, i.toDouble() + 3); + } + pathWithActionHistory.close(); + return pathWithActionHistory; + } + + test('Test converter for PathWithActionHistory with one path', () { + PathWithActionHistory path = createPathWithActionHistory(1); + + var json = converter.toJson(path); + + PathWithActionHistory deserializedPath = converter.fromJson(json); + + expect(path, equals(deserializedPath)); + }); + + test('Test converter for PathWithActionHistory with two paths', () { + PathWithActionHistory path = createPathWithActionHistory(2); + + var json = converter.toJson(path); + + PathWithActionHistory deserializedPath = converter.fromJson(json); + + expect(path, equals(deserializedPath)); + }); + + test('Test converter for PathWithActionHistory with multiple paths', () { + PathWithActionHistory path = createPathWithActionHistory(10); + + var json = converter.toJson(path); + + PathWithActionHistory deserializedPath = converter.fromJson(json); + + expect(path, equals(deserializedPath)); + }); +}