Skip to content

Commit 4178ab4

Browse files
committed
test: Cleanup kitchen_sink Classes and generation
1 parent f67aa8f commit 4178ab4

8 files changed

+69
-43
lines changed

json_serializable/test/kitchen_sink_test.dart

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,19 @@ import 'package:test/test.dart';
66

77
import 'package:json_serializable/src/utils.dart';
88

9-
import 'test_files/kitchen_sink.dart';
9+
import 'test_files/kitchen_sink.dart' as nullable;
1010
import 'test_files/kitchen_sink.non_nullable.dart' as nn;
11+
import 'test_files/kitchen_sink_interface.dart';
1112
import 'test_utils.dart';
1213

1314
void main() {
1415
group('KitchenSink', () {
1516
roundTripItem(KitchenSink p) {
16-
roundTripObject(p, (json) => new KitchenSink.fromJson(json));
17+
roundTripObject(p, (json) => new nullable.KitchenSink.fromJson(json));
1718
}
1819

1920
test('Fields with `!includeIfNull` should not be included when null', () {
20-
var item = new KitchenSink();
21+
var item = new nullable.KitchenSink();
2122

2223
var expectedDefaultKeys = _expectedOrder.toSet()
2324
..removeAll(_excludeIfNullKeys);
@@ -33,15 +34,15 @@ void main() {
3334

3435
test('list and map of DateTime', () {
3536
var now = new DateTime.now();
36-
var item = new KitchenSink(dateTimeIterable: <DateTime>[now])
37+
var item = new nullable.KitchenSink(dateTimeIterable: <DateTime>[now])
3738
..dateTimeList = <DateTime>[now, null]
3839
..stringDateTimeMap = <String, DateTime>{'value': now, 'null': null};
3940

4041
roundTripItem(item);
4142
});
4243

4344
test('complex nested type', () {
44-
var item = new KitchenSink()
45+
var item = new nullable.KitchenSink()
4546
..crazyComplex = [
4647
null,
4748
{},
@@ -69,13 +70,13 @@ void main() {
6970
Iterable<Object> objectIterable,
7071
Iterable<int> intIterable,
7172
Iterable<DateTime> dateTimeIterable}) =>
72-
new KitchenSink(
73+
new nullable.KitchenSink(
7374
iterable: iterable,
7475
dynamicIterable: dynamicIterable,
7576
objectIterable: objectIterable,
7677
intIterable: intIterable,
7778
dateTimeIterable: dateTimeIterable),
78-
(j) => new KitchenSink.fromJson(j));
79+
(j) => new nullable.KitchenSink.fromJson(j));
7980
});
8081

8182
group('KitchenSink - non-nullable', () {

json_serializable/test/test_files/json_test_example.non_nullable.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// **************************************************************************
1010

1111
// ignore_for_file: annotate_overrides, hash_and_equals
12-
library json_serializable.test.json_test_example_non_nullable;
12+
library json_serializable.test.json_test_example.non_nullable;
1313

1414
import 'dart:collection';
1515

json_serializable/test/test_files/json_test_example.non_nullable.g.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
// GENERATED CODE - DO NOT MODIFY BY HAND
66

7-
part of json_serializable.test.json_test_example_non_nullable;
7+
part of json_serializable.test.json_test_example.non_nullable;
88

99
// **************************************************************************
1010
// Generator: JsonSerializableGenerator

json_serializable/test/test_files/kitchen_sink.dart

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@ library json_serializable.test.kitchen_sink;
77

88
import 'package:json_annotation/json_annotation.dart';
99

10-
import 'test_files_util.dart';
10+
import 'kitchen_sink_interface.dart' as k;
1111

1212
part 'kitchen_sink.g.dart';
1313

1414
List<T> _defaultList<T>() => null;
1515
Map _defaultMap() => null;
1616

1717
@JsonSerializable()
18-
class KitchenSink extends Object with _$KitchenSinkSerializerMixin {
18+
class KitchenSink extends Object
19+
with _$KitchenSinkSerializerMixin
20+
implements k.KitchenSink {
1921
// To ensure static members are not considered for serialization.
2022
static const answer = 42;
2123
static final reason = 42;
@@ -80,5 +82,5 @@ class KitchenSink extends Object with _$KitchenSinkSerializerMixin {
8082
@JsonKey(name: r'$string')
8183
String string;
8284

83-
bool operator ==(Object other) => sinkEquals(this, other);
85+
bool operator ==(Object other) => k.sinkEquals(this, other);
8486
}

json_serializable/test/test_files/kitchen_sink.non_nullable.dart

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@
99
// **************************************************************************
1010

1111
// ignore_for_file: annotate_overrides, hash_and_equals
12-
library json_serializable.test.kitchen_sink_non_nullable;
12+
library json_serializable.test.kitchen_sink.non_nullable;
1313

1414
import 'package:json_annotation/json_annotation.dart';
1515

16-
import 'kitchen_sink.dart' as k;
17-
import 'test_files_util.dart';
16+
import 'kitchen_sink_interface.dart' as k;
1817

1918
part 'kitchen_sink.non_nullable.g.dart';
2019

@@ -89,5 +88,5 @@ class KitchenSink extends Object
8988
@JsonKey(name: r'$string')
9089
String string;
9190

92-
bool operator ==(Object other) => sinkEquals(this, other);
91+
bool operator ==(Object other) => k.sinkEquals(this, other);
9392
}

json_serializable/test/test_files/kitchen_sink.non_nullable.g.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
// GENERATED CODE - DO NOT MODIFY BY HAND
66

7-
part of json_serializable.test.kitchen_sink_non_nullable;
7+
part of json_serializable.test.kitchen_sink.non_nullable;
88

99
// **************************************************************************
1010
// Generator: JsonSerializableGenerator

json_serializable/test/test_files/test_files_util.dart renamed to json_serializable/test/test_files/kitchen_sink_interface.dart

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,38 @@
1-
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
1+
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:collection/collection.dart';
66

7-
import 'kitchen_sink.dart';
7+
abstract class KitchenSink {
8+
DateTime dateTime;
9+
10+
Iterable get iterable;
11+
Iterable<dynamic> get dynamicIterable;
12+
Iterable<Object> get objectIterable;
13+
Iterable<int> get intIterable;
14+
15+
Iterable<DateTime> get dateTimeIterable;
16+
17+
List list;
18+
List<dynamic> dynamicList;
19+
List<Object> objectList;
20+
List<int> intList;
21+
List<DateTime> dateTimeList;
22+
23+
Map map;
24+
Map<String, String> stringStringMap;
25+
Map<String, int> stringIntMap;
26+
Map<String, DateTime> stringDateTimeMap;
27+
28+
List<Map<String, Map<String, List<List<DateTime>>>>> crazyComplex;
29+
30+
Map<String, bool> val;
31+
bool writeNotNull;
32+
String string;
33+
34+
Map<String, dynamic> toJson();
35+
}
836

937
//TODO(kevmoo) - finish this...
1038
bool sinkEquals(KitchenSink a, Object other) =>

json_serializable/tool/build_actions.dart

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,13 @@ class _NonNullableGenerator extends Generator {
5050
final path = buildStep.inputId.path;
5151
final baseName = p.basenameWithoutExtension(path);
5252

53-
// 1) - source file
54-
final f1Content = await buildStep.readAsString(buildStep.inputId);
55-
final f1LibraryDirective =
56-
'library ${buildStep.inputId.package}.test.$baseName';
57-
final f1PartDirective = "part '${baseName}.g.dart";
58-
53+
final content = await buildStep.readAsString(buildStep.inputId);
5954
var replacements = <_Replacement>[
6055
new _Replacement(_copyrightContent, ''),
61-
new _Replacement(f1LibraryDirective,
62-
'library ${buildStep.inputId.package}.test.${baseName}_non_nullable'),
56+
new _Replacement('library ${buildStep.inputId.package}.test.$baseName',
57+
'library ${buildStep.inputId.package}.test.${baseName}.non_nullable'),
6358
new _Replacement(
64-
f1PartDirective,
59+
"part '${baseName}.g.dart",
6560
"part '${baseName}.non_nullable.g.dart",
6661
),
6762
new _Replacement(
@@ -70,30 +65,16 @@ class _NonNullableGenerator extends Generator {
7065

7166
if (baseName == 'kitchen_sink') {
7267
replacements.addAll([
73-
new _Replacement("import 'test_files_util.dart';",
74-
"import 'kitchen_sink.dart' as k;\nimport 'test_files_util.dart';"),
7568
new _Replacement('List<T> _defaultList<T>() => null;',
7669
'List<T> _defaultList<T>() => <T>[];'),
7770
new _Replacement(
7871
'Map _defaultMap() => null;', 'Map _defaultMap() => {};'),
79-
new _Replacement(r'with _$KitchenSinkSerializerMixin {',
80-
r'with _$KitchenSinkSerializerMixin implements k.KitchenSink {'),
8172
new _Replacement('DateTime dateTime;',
8273
'DateTime dateTime = new DateTime(1981, 6, 5);')
8374
]);
8475
}
8576

86-
var f2Content = f1Content;
87-
88-
for (var r in replacements) {
89-
if (!f2Content.contains(r.existing)) {
90-
throw new StateError(
91-
'Input string did not contain `${r.existing}` as expected.');
92-
}
93-
f2Content = f2Content.replaceAll(r.existing, r.replacement);
94-
}
95-
96-
return f2Content;
77+
return _Replacement.generate(content, replacements);
9778
}
9879
}
9980

@@ -102,4 +83,19 @@ class _Replacement {
10283
final String replacement;
10384

10485
_Replacement(this.existing, this.replacement);
86+
87+
static String generate(
88+
String inputContent, Iterable<_Replacement> replacements) {
89+
var outputContent = inputContent;
90+
91+
for (var r in replacements) {
92+
if (!outputContent.contains(r.existing)) {
93+
throw new StateError(
94+
'Input string did not contain `${r.existing}` as expected.');
95+
}
96+
outputContent = outputContent.replaceAll(r.existing, r.replacement);
97+
}
98+
99+
return outputContent;
100+
}
105101
}

0 commit comments

Comments
 (0)