Skip to content

Commit c77bf7c

Browse files
committed
test: add integration tests
1 parent 5a87138 commit c77bf7c

File tree

4 files changed

+285
-14
lines changed

4 files changed

+285
-14
lines changed

json_serializable/test/integration/integration_test.dart

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import 'json_enum_example.dart';
1313
import 'json_keys_example.dart' as js_keys;
1414
import 'json_test_common.dart' show Category, Platform, StatusCode;
1515
import 'json_test_example.dart';
16+
import 'sealed_class_examples.dart';
1617

1718
Matcher _throwsArgumentError(Object matcher) =>
1819
throwsA(isArgumentError.having((e) => e.message, 'message', matcher));
@@ -465,6 +466,58 @@ void main() {
465466
});
466467
});
467468

469+
group('SimpleSealedBase', () {
470+
void roundTripSimpleSealed(SimpleSealedBase p) {
471+
roundTripObject(p, SimpleSealedBase.fromJson);
472+
}
473+
474+
test('SimpleSealedImplOne', () {
475+
roundTripSimpleSealed(
476+
SimpleSealedImplOne(
477+
testFieldOne: 'test field one',
478+
commonField: 'common field',
479+
),
480+
);
481+
});
482+
483+
test('SimpleSealedImplTwo', () {
484+
roundTripSimpleSealed(
485+
SimpleSealedImplTwo(
486+
testFieldTwo: 'test field two',
487+
commonField: 'common field',
488+
),
489+
);
490+
});
491+
});
492+
493+
group('SealedWithDiscriminatorAndRenameBase', () {
494+
void roundTripSimpleSealedWithDiscriminatorAndRename(
495+
SealedWithDiscriminatorAndRenameBase p) {
496+
roundTripObject(
497+
p,
498+
SealedWithDiscriminatorAndRenameBase.fromJson,
499+
);
500+
}
501+
502+
test('SealedWithDiscriminatorAndRenameImplOne', () {
503+
roundTripSimpleSealedWithDiscriminatorAndRename(
504+
SealedWithDiscriminatorAndRenameImplOne(
505+
42,
506+
42,
507+
),
508+
);
509+
});
510+
511+
test('SealedWithDiscriminatorAndRenameImplTwo', () {
512+
roundTripSimpleSealedWithDiscriminatorAndRename(
513+
SealedWithDiscriminatorAndRenameImplTwo(
514+
42,
515+
42,
516+
),
517+
);
518+
});
519+
});
520+
468521
test('Issue1226Regression', () {
469522
final instance = Issue1226Regression(durationType: null);
470523
expect(instance.toJson(), isEmpty);
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import 'package:json_annotation/json_annotation.dart';
2+
3+
part 'sealed_class_examples.g.dart';
4+
5+
@JsonSerializable()
6+
sealed class SimpleSealedBase {
7+
final String commonField;
8+
9+
SimpleSealedBase({required this.commonField});
10+
11+
factory SimpleSealedBase.fromJson(Map<String, dynamic> json) =>
12+
_$SimpleSealedBaseFromJson(json);
13+
14+
Map<String, dynamic> toJson() => _$SimpleSealedBaseToJson(this);
15+
}
16+
17+
@JsonSerializable()
18+
class SimpleSealedImplOne extends SimpleSealedBase {
19+
final String testFieldOne;
20+
21+
SimpleSealedImplOne({
22+
required this.testFieldOne,
23+
required super.commonField,
24+
});
25+
26+
@override
27+
bool operator ==(Object other) =>
28+
other is SimpleSealedImplOne &&
29+
testFieldOne == other.testFieldOne &&
30+
commonField == other.commonField;
31+
32+
@override
33+
int get hashCode => identityHashCode(this);
34+
}
35+
36+
@JsonSerializable()
37+
class SimpleSealedImplTwo extends SimpleSealedBase {
38+
final String testFieldTwo;
39+
40+
SimpleSealedImplTwo({
41+
required this.testFieldTwo,
42+
required super.commonField,
43+
});
44+
45+
@override
46+
bool operator ==(Object other) =>
47+
other is SimpleSealedImplTwo &&
48+
testFieldTwo == other.testFieldTwo &&
49+
commonField == other.commonField;
50+
51+
@override
52+
int get hashCode => identityHashCode(this);
53+
}
54+
55+
@JsonSerializable(
56+
unionDiscriminator: 'custom_discriminator',
57+
unionRename: UnionRename.snake,
58+
)
59+
sealed class SealedWithDiscriminatorAndRenameBase {
60+
final int common;
61+
62+
SealedWithDiscriminatorAndRenameBase(this.common);
63+
64+
factory SealedWithDiscriminatorAndRenameBase.fromJson(
65+
Map<String, dynamic> json) =>
66+
_$SealedWithDiscriminatorAndRenameBaseFromJson(json);
67+
68+
Map<String, dynamic> toJson() =>
69+
_$SealedWithDiscriminatorAndRenameBaseToJson(this);
70+
}
71+
72+
@JsonSerializable()
73+
class SealedWithDiscriminatorAndRenameImplOne
74+
extends SealedWithDiscriminatorAndRenameBase {
75+
final int testOne;
76+
77+
SealedWithDiscriminatorAndRenameImplOne(
78+
this.testOne,
79+
super.common,
80+
);
81+
82+
@override
83+
bool operator ==(Object other) =>
84+
other is SealedWithDiscriminatorAndRenameImplOne &&
85+
testOne == other.testOne &&
86+
common == other.common;
87+
88+
@override
89+
int get hashCode => identityHashCode(this);
90+
}
91+
92+
@JsonSerializable()
93+
class SealedWithDiscriminatorAndRenameImplTwo
94+
extends SealedWithDiscriminatorAndRenameBase {
95+
final int testTwo;
96+
97+
SealedWithDiscriminatorAndRenameImplTwo(
98+
this.testTwo,
99+
super.common,
100+
);
101+
102+
@override
103+
bool operator ==(Object other) =>
104+
other is SealedWithDiscriminatorAndRenameImplTwo &&
105+
testTwo == other.testTwo &&
106+
common == other.common;
107+
108+
@override
109+
int get hashCode => identityHashCode(this);
110+
}
111+
112+
// TODO(@O-Hannonen): Add more examples for round trip cases, add at least:
113+
// - Nested sealed classes
114+
// - Sealed classes with itself as instance field (nullable?)
115+
// - Subclasses that implement multiple sealed classes

json_serializable/test/integration/sealed_class_examples.g.dart

Lines changed: 117 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

json_serializable/test/src/sealed_test_input.dart

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -763,17 +763,3 @@ class SubFourMultipleImpl
763763

764764
SubFourMultipleImpl(this.subFourField);
765765
}
766-
767-
768-
769-
770-
///
771-
///
772-
/// Happy cases:
773-
/// - Simple with two subclasses
774-
/// - Simple with changed discriminator
775-
/// - Simple with union rename
776-
/// - Simple with changed discriminator and union rename
777-
/// - Complex nested
778-
/// - Complex many implementations
779-
///

0 commit comments

Comments
 (0)