Skip to content

Commit

Permalink
PAINTROID - Implement JSON Serialization via Code Generation
Browse files Browse the repository at this point in the history
more tetsing
  • Loading branch information
Lenkomotive committed Nov 12, 2023
1 parent 7724d2b commit bc991b2
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 77 deletions.
35 changes: 35 additions & 0 deletions lib/core/path_with_action_history.dart
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -35,6 +36,18 @@ class PathWithActionHistory extends Path {
factory PathWithActionHistory.fromJson(Map<String, dynamic> json) {
return const PathWithActionHistoryConverter().fromJson(json);
}

@override
bool operator ==(Object other) {
if (other is PathWithActionHistory) {
return const ListEquality<PathAction>().equals(actions, other.actions);
}
return false;
}

@override
int get hashCode => const ListEquality<PathAction>().hash(actions);

}

abstract class PathAction {
Expand All @@ -46,13 +59,35 @@ 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 {
final double x;
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 {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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));
});
}
43 changes: 14 additions & 29 deletions test/unit/io/serialization/converter/paint_converter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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', () {
Expand All @@ -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', () {
Expand All @@ -61,32 +58,20 @@ 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);

expect(deserializedPaint.color, equals(originalPaint.color));
});
}

Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,32 @@ void main() {

expect(deserializedMoveToAction, isA<MoveToAction>());
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<LineToAction>());
deserializedMoveToAction as LineToAction;
expect(deserializedMoveToAction.x, xExpected);
expect(deserializedMoveToAction.y, yExpected);
expect(deserializedLineToAction, isA<LineToAction>());
deserializedLineToAction as LineToAction;
expect(lineToAction, equals(deserializedLineToAction));
});

test('Test converter for CloseAction version 1', () {
CloseAction closeAction = const CloseAction();

var json = converter.toJson(closeAction);

PathAction deserializedMoveToAction = converter.fromJson(json);
PathAction deserializedCloseAction = converter.fromJson(json);

expect(deserializedMoveToAction, isA<CloseAction>());
expect(deserializedCloseAction, isA<CloseAction>());
});
}

Original file line number Diff line number Diff line change
@@ -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));
});
}

0 comments on commit bc991b2

Please sign in to comment.