Skip to content

Commit a298768

Browse files
committed
Move test Dart code for json_serializable_test to a dart file
Makes it a lot easier to maintain this content Using //ignore comments for intentionally invalid code
1 parent 32c1b2c commit a298768

File tree

2 files changed

+154
-150
lines changed

2 files changed

+154
-150
lines changed

json_serializable/test/json_serializable_test.dart

Lines changed: 5 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ library json_serializable.test.json_generator_test;
88
// TODO(kevmoo): test all flavors of `nullable` - class, fields, etc
99

1010
import 'dart:async';
11+
import 'dart:io';
1112

1213
import 'package:analyzer/dart/ast/ast.dart';
1314
import 'package:analyzer/src/string_source.dart';
@@ -278,7 +279,10 @@ abstract class _$OrderSerializerMixin {
278279
final _formatter = new dart_style.DartFormatter();
279280

280281
Future<CompilationUnit> _getCompilationUnitForString(String projectPath) async {
281-
var source = new StringSource(_testSource, 'test content');
282+
var filePath = p.join(
283+
getPackagePath(), 'test', 'src', 'json_serializable_test_input.dart');
284+
var source =
285+
new StringSource(new File(filePath).readAsStringSync(), 'test content');
282286

283287
var foundFiles = await getDartFiles(projectPath,
284288
searchList: [p.join(getPackagePath(), 'test', 'test_files')]);
@@ -290,152 +294,3 @@ Future<CompilationUnit> _getCompilationUnitForString(String projectPath) async {
290294
}
291295

292296
CompilationUnit _compUnit;
293-
294-
const _testSource = r'''
295-
import 'package:json_annotation/json_annotation.dart';
296-
297-
@JsonSerializable()
298-
const theAnswer = 42;
299-
300-
@JsonSerializable()
301-
void annotatedMethod() => null;
302-
303-
@JsonSerializable()
304-
class Person {
305-
String firstName, lastName;
306-
@JsonKey(name: 'h')
307-
int height;
308-
DateTime dateOfBirth;
309-
dynamic dynamicType;
310-
var varType;
311-
List<int> listOfInts;
312-
}
313-
314-
@JsonSerializable()
315-
class Order {
316-
final String firstName, lastName;
317-
int height;
318-
DateTime dateOfBirth;
319-
320-
Order(this.height, String firstName, [this.lastName]);
321-
}
322-
323-
@JsonSerializable()
324-
class FinalFields {
325-
final int a;
326-
int get b => 4;
327-
328-
FinalFields(this.a);
329-
}
330-
331-
@JsonSerializable()
332-
class FromJsonOptionalParameters {
333-
final ChildWithFromJson child;
334-
335-
FromJsonOptionalParameters(this.child);
336-
}
337-
338-
class ChildWithFromJson {
339-
ChildWithFromJson.fromJson(json, {initValue: false}) {}
340-
}
341-
342-
@JsonSerializable()
343-
class ParentObject {
344-
int number;
345-
String str;
346-
ChildObject child;
347-
}
348-
349-
@JsonSerializable()
350-
class ChildObject {
351-
int number;
352-
String str;
353-
354-
factory ChildObject.fromJson(json) => null;
355-
}
356-
357-
@JsonSerializable()
358-
class ParentObjectWithChildren {
359-
int number;
360-
String str;
361-
List<ChildObject> children;
362-
}
363-
364-
@JsonSerializable()
365-
class ParentObjectWithDynamicChildren {
366-
int number;
367-
String str;
368-
List<dynamic> children;
369-
}
370-
371-
@JsonSerializable()
372-
class UnknownCtorParamType {
373-
int number;
374-
375-
UnknownCtorParamType(Bob number) : this.number = number;
376-
}
377-
378-
@JsonSerializable()
379-
class UnknownFieldType {
380-
Bob number;
381-
}
382-
383-
@JsonSerializable(createFactory: false)
384-
class NoSerializeFieldType {
385-
Stopwatch watch;
386-
}
387-
388-
@JsonSerializable(createToJson: false)
389-
class NoDeserializeFieldType {
390-
Stopwatch watch;
391-
}
392-
393-
@JsonSerializable(createFactory: false)
394-
class NoSerializeBadKey {
395-
Map<int, DateTime> intDateTimeMap;
396-
}
397-
398-
@JsonSerializable(createToJson: false)
399-
class NoDeserializeBadKey {
400-
Map<int, DateTime> intDateTimeMap;
401-
}
402-
403-
@JsonSerializable(createFactory: false)
404-
class IncludeIfNullAll {
405-
@JsonKey(includeIfNull: true)
406-
int number;
407-
String str;
408-
}
409-
410-
@JsonSerializable(createFactory: false, includeIfNull: false)
411-
class IncludeIfNullOverride {
412-
@JsonKey(includeIfNull: true)
413-
int number;
414-
String str;
415-
}
416-
417-
// https://github.com/dart-lang/json_serializable/issues/7 regression
418-
@JsonSerializable()
419-
class NoCtorClass {
420-
final int member;
421-
422-
factory TestDoneEvent.fromJson(Map<String, dynamic> json) => null;
423-
}
424-
425-
@JsonSerializable(createFactory: false)
426-
class KeyDupesField {
427-
@JsonKey(name: 'str')
428-
int number;
429-
430-
String str;
431-
}
432-
433-
@JsonSerializable(createFactory: false)
434-
class DupeKeys {
435-
@JsonKey(name: 'a')
436-
int number;
437-
438-
@JsonKey(name: 'a')
439-
String str;
440-
}
441-
''';
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
import 'package:json_annotation/json_annotation.dart';
2+
3+
@JsonSerializable()
4+
const theAnswer = 42;
5+
6+
@JsonSerializable()
7+
void annotatedMethod() => null;
8+
9+
@JsonSerializable()
10+
class Person {
11+
String firstName, lastName;
12+
@JsonKey(name: 'h')
13+
int height;
14+
DateTime dateOfBirth;
15+
dynamic dynamicType;
16+
var varType;
17+
List<int> listOfInts;
18+
}
19+
20+
@JsonSerializable()
21+
class Order {
22+
final String firstName, lastName;
23+
int height;
24+
DateTime dateOfBirth;
25+
26+
Order(this.height, String firstName, [this.lastName])
27+
: this.firstName = firstName;
28+
}
29+
30+
@JsonSerializable()
31+
class FinalFields {
32+
final int a;
33+
int get b => 4;
34+
35+
FinalFields(this.a);
36+
}
37+
38+
@JsonSerializable()
39+
class FromJsonOptionalParameters {
40+
final ChildWithFromJson child;
41+
42+
FromJsonOptionalParameters(this.child);
43+
}
44+
45+
class ChildWithFromJson {
46+
ChildWithFromJson.fromJson(json, {initValue: false});
47+
}
48+
49+
@JsonSerializable()
50+
class ParentObject {
51+
int number;
52+
String str;
53+
ChildObject child;
54+
}
55+
56+
@JsonSerializable()
57+
class ChildObject {
58+
int number;
59+
String str;
60+
61+
factory ChildObject.fromJson(json) => null;
62+
}
63+
64+
@JsonSerializable()
65+
class ParentObjectWithChildren {
66+
int number;
67+
String str;
68+
List<ChildObject> children;
69+
}
70+
71+
@JsonSerializable()
72+
class ParentObjectWithDynamicChildren {
73+
int number;
74+
String str;
75+
List<dynamic> children;
76+
}
77+
78+
@JsonSerializable()
79+
class UnknownCtorParamType {
80+
int number;
81+
82+
// ignore: undefined_class, field_initializer_not_assignable
83+
UnknownCtorParamType(Bob number) : this.number = number;
84+
}
85+
86+
@JsonSerializable()
87+
class UnknownFieldType {
88+
// ignore: undefined_class
89+
Bob number;
90+
}
91+
92+
@JsonSerializable(createFactory: false)
93+
class NoSerializeFieldType {
94+
Stopwatch watch;
95+
}
96+
97+
@JsonSerializable(createToJson: false)
98+
class NoDeserializeFieldType {
99+
Stopwatch watch;
100+
}
101+
102+
@JsonSerializable(createFactory: false)
103+
class NoSerializeBadKey {
104+
Map<int, DateTime> intDateTimeMap;
105+
}
106+
107+
@JsonSerializable(createToJson: false)
108+
class NoDeserializeBadKey {
109+
Map<int, DateTime> intDateTimeMap;
110+
}
111+
112+
@JsonSerializable(createFactory: false)
113+
class IncludeIfNullAll {
114+
@JsonKey(includeIfNull: true)
115+
int number;
116+
String str;
117+
}
118+
119+
@JsonSerializable(createFactory: false, includeIfNull: false)
120+
class IncludeIfNullOverride {
121+
@JsonKey(includeIfNull: true)
122+
int number;
123+
String str;
124+
}
125+
126+
// https://github.com/dart-lang/json_serializable/issues/7 regression
127+
@JsonSerializable()
128+
class NoCtorClass {
129+
final int member;
130+
131+
factory NoCtorClass.fromJson(Map<String, dynamic> json) => null;
132+
}
133+
134+
@JsonSerializable(createFactory: false)
135+
class KeyDupesField {
136+
@JsonKey(name: 'str')
137+
int number;
138+
139+
String str;
140+
}
141+
142+
@JsonSerializable(createFactory: false)
143+
class DupeKeys {
144+
@JsonKey(name: 'a')
145+
int number;
146+
147+
@JsonKey(name: 'a')
148+
String str;
149+
}

0 commit comments

Comments
 (0)