202 lines
5.5 KiB
Dart
202 lines
5.5 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:jadb/models/kanjivg/kanjivg_entry.dart';
|
|
import 'package:jadb/models/kanjivg/kanjivg_path.dart';
|
|
import 'package:jadb/models/kanjivg/kanjivg_path_group.dart';
|
|
import 'package:jadb/models/kanjivg/kanjivg_path_group_position.dart';
|
|
import 'package:jadb/models/kanjivg/kanjivg_radical.dart';
|
|
import 'package:test/test.dart';
|
|
|
|
Object? _roundTripJson(Object? value) => jsonDecode(jsonEncode(value));
|
|
|
|
Map<String, dynamic> _roundTripMap(Object? json) =>
|
|
Map<String, dynamic>.from(_roundTripJson(json) as Map);
|
|
|
|
void main() {
|
|
group('KanjiVG model serialization', () {
|
|
test('KanjiVGPathGroupPosition roundtrips all values', () {
|
|
for (final value in KanjiVGPathGroupPosition.values) {
|
|
expect(
|
|
KanjiVGPathGroupPosition.fromJson(_roundTripJson(value.toJson())),
|
|
equals(value),
|
|
);
|
|
}
|
|
});
|
|
|
|
test('KanjiVGPathGroupPosition parses SVG aliases', () {
|
|
expect(
|
|
KanjiVGPathGroupPosition.fromString('⿵A'),
|
|
equals(KanjiVGPathGroupPosition.upperA),
|
|
);
|
|
expect(
|
|
KanjiVGPathGroupPosition.fromString('⿵B'),
|
|
equals(KanjiVGPathGroupPosition.upperB),
|
|
);
|
|
expect(
|
|
KanjiVGPathGroupPosition.fromString('⿶1'),
|
|
equals(KanjiVGPathGroupPosition.lower1),
|
|
);
|
|
expect(
|
|
KanjiVGPathGroupPosition.fromString('⿶2'),
|
|
equals(KanjiVGPathGroupPosition.lower2),
|
|
);
|
|
expect(
|
|
KanjiVGPathGroupPosition.fromString('left'),
|
|
equals(KanjiVGPathGroupPosition.left),
|
|
);
|
|
});
|
|
|
|
test('KanjiVGRadical roundtrips all values', () {
|
|
for (final value in KanjiVGRadical.values) {
|
|
expect(
|
|
KanjiVGRadical.fromJson(_roundTripJson(value.toJson())),
|
|
equals(value),
|
|
);
|
|
}
|
|
});
|
|
|
|
test('KanjiVGPath roundtrips via JSON', () {
|
|
final path = KanjiVGPath(
|
|
pathId: 1,
|
|
type: '㇐',
|
|
svgPath: 'M12.5,18c2.1,0.4,6.1,0.6,8.1,0.4',
|
|
labelX: 12.5,
|
|
labelY: 18.0,
|
|
);
|
|
|
|
final restored = KanjiVGPath.fromJson(_roundTripMap(path.toJson()));
|
|
|
|
expect(restored, equals(path));
|
|
});
|
|
|
|
test('KanjiVGPath roundtrips required label coordinates', () {
|
|
final path = KanjiVGPath(
|
|
pathId: 2,
|
|
type: '㇒',
|
|
svgPath: 'M18,12c0.5,1,1,2,1.5,3',
|
|
labelX: 9.5,
|
|
labelY: 14.0,
|
|
);
|
|
|
|
final restored = KanjiVGPath.fromJson(_roundTripMap(path.toJson()));
|
|
|
|
expect(restored, equals(path));
|
|
});
|
|
|
|
test('KanjiVGPathGroup roundtrips nested tree data', () {
|
|
final group = KanjiVGPathGroup(
|
|
groupId: 0,
|
|
element: '休',
|
|
position: KanjiVGPathGroupPosition.left,
|
|
paths: [
|
|
KanjiVGPath(
|
|
pathId: 1,
|
|
type: '㇐',
|
|
svgPath: 'M10,10c1,0,2,0,3,0',
|
|
labelX: 7.0,
|
|
labelY: 9.0,
|
|
),
|
|
],
|
|
children: [
|
|
KanjiVGPathGroup(
|
|
groupId: 1,
|
|
element: '人',
|
|
radical: KanjiVGRadical.general,
|
|
part: 1,
|
|
paths: [
|
|
KanjiVGPath(
|
|
pathId: 2,
|
|
type: '㇒',
|
|
svgPath: 'M12,8c0.5,1,1,2,1.5,3',
|
|
labelX: 11.0,
|
|
labelY: 6.5,
|
|
),
|
|
],
|
|
),
|
|
KanjiVGPathGroup(
|
|
groupId: 2,
|
|
element: '木',
|
|
original: '木',
|
|
position: KanjiVGPathGroupPosition.right,
|
|
paths: [
|
|
KanjiVGPath(
|
|
pathId: 3,
|
|
type: '㇑',
|
|
svgPath: 'M18,9c0,2,0,4,0,6',
|
|
labelX: 19.0,
|
|
labelY: 7.0,
|
|
),
|
|
],
|
|
),
|
|
],
|
|
);
|
|
|
|
final restored = KanjiVGPathGroup.fromJson(_roundTripMap(group.toJson()));
|
|
|
|
expect(restored, equals(group));
|
|
});
|
|
|
|
test('KanjiVGEntry roundtrips populated data', () {
|
|
final entry = KanjiVGEntry(
|
|
character: '休',
|
|
paths: [
|
|
KanjiVGPath(
|
|
pathId: 1,
|
|
type: '㇒',
|
|
svgPath: 'M18,12c0.5,1,1,2,1.5,3',
|
|
labelX: 12.0,
|
|
labelY: 10.0,
|
|
),
|
|
KanjiVGPath(
|
|
pathId: 2,
|
|
type: '㇐',
|
|
svgPath: 'M30,24c2,0,6,0,8,0',
|
|
labelX: 28.0,
|
|
labelY: 21.0,
|
|
),
|
|
],
|
|
pathGroups: [
|
|
KanjiVGPathGroup(
|
|
groupId: 0,
|
|
element: '休',
|
|
children: [
|
|
KanjiVGPathGroup(
|
|
groupId: 1,
|
|
element: '人',
|
|
radical: KanjiVGRadical.general,
|
|
paths: [
|
|
KanjiVGPath(
|
|
pathId: 1,
|
|
type: '㇒',
|
|
svgPath: 'M18,12c0.5,1,1,2,1.5,3',
|
|
labelX: 12.0,
|
|
labelY: 10.0,
|
|
),
|
|
],
|
|
),
|
|
KanjiVGPathGroup(
|
|
groupId: 2,
|
|
element: '木',
|
|
position: KanjiVGPathGroupPosition.right,
|
|
paths: [
|
|
KanjiVGPath(
|
|
pathId: 2,
|
|
type: '㇐',
|
|
svgPath: 'M30,24c2,0,6,0,8,0',
|
|
labelX: 28.0,
|
|
labelY: 21.0,
|
|
),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
],
|
|
);
|
|
|
|
final restored = KanjiVGEntry.fromJson(_roundTripMap(entry.toJson()));
|
|
|
|
expect(restored, equals(entry));
|
|
});
|
|
});
|
|
}
|