Skip to content

Move test Dart code for json_serializable_test to a dart file #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 5 additions & 150 deletions json_serializable/test/json_serializable_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ library json_serializable.test.json_generator_test;
// TODO(kevmoo): test all flavors of `nullable` - class, fields, etc

import 'dart:async';
import 'dart:io';

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

Future<CompilationUnit> _getCompilationUnitForString(String projectPath) async {
var source = new StringSource(_testSource, 'test content');
var filePath = p.join(
getPackagePath(), 'test', 'src', 'json_serializable_test_input.dart');
var source =
new StringSource(new File(filePath).readAsStringSync(), 'test content');

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

CompilationUnit _compUnit;

const _testSource = r'''
import 'package:json_annotation/json_annotation.dart';

@JsonSerializable()
const theAnswer = 42;

@JsonSerializable()
void annotatedMethod() => null;

@JsonSerializable()
class Person {
String firstName, lastName;
@JsonKey(name: 'h')
int height;
DateTime dateOfBirth;
dynamic dynamicType;
var varType;
List<int> listOfInts;
}

@JsonSerializable()
class Order {
final String firstName, lastName;
int height;
DateTime dateOfBirth;

Order(this.height, String firstName, [this.lastName]);
}

@JsonSerializable()
class FinalFields {
final int a;
int get b => 4;

FinalFields(this.a);
}

@JsonSerializable()
class FromJsonOptionalParameters {
final ChildWithFromJson child;

FromJsonOptionalParameters(this.child);
}

class ChildWithFromJson {
ChildWithFromJson.fromJson(json, {initValue: false}) {}
}

@JsonSerializable()
class ParentObject {
int number;
String str;
ChildObject child;
}

@JsonSerializable()
class ChildObject {
int number;
String str;

factory ChildObject.fromJson(json) => null;
}

@JsonSerializable()
class ParentObjectWithChildren {
int number;
String str;
List<ChildObject> children;
}

@JsonSerializable()
class ParentObjectWithDynamicChildren {
int number;
String str;
List<dynamic> children;
}

@JsonSerializable()
class UnknownCtorParamType {
int number;

UnknownCtorParamType(Bob number) : this.number = number;
}

@JsonSerializable()
class UnknownFieldType {
Bob number;
}

@JsonSerializable(createFactory: false)
class NoSerializeFieldType {
Stopwatch watch;
}

@JsonSerializable(createToJson: false)
class NoDeserializeFieldType {
Stopwatch watch;
}

@JsonSerializable(createFactory: false)
class NoSerializeBadKey {
Map<int, DateTime> intDateTimeMap;
}

@JsonSerializable(createToJson: false)
class NoDeserializeBadKey {
Map<int, DateTime> intDateTimeMap;
}

@JsonSerializable(createFactory: false)
class IncludeIfNullAll {
@JsonKey(includeIfNull: true)
int number;
String str;
}

@JsonSerializable(createFactory: false, includeIfNull: false)
class IncludeIfNullOverride {
@JsonKey(includeIfNull: true)
int number;
String str;
}

// https://github.com/dart-lang/json_serializable/issues/7 regression
@JsonSerializable()
class NoCtorClass {
final int member;

factory TestDoneEvent.fromJson(Map<String, dynamic> json) => null;
}

@JsonSerializable(createFactory: false)
class KeyDupesField {
@JsonKey(name: 'str')
int number;

String str;
}

@JsonSerializable(createFactory: false)
class DupeKeys {
@JsonKey(name: 'a')
int number;

@JsonKey(name: 'a')
String str;
}
''';
149 changes: 149 additions & 0 deletions json_serializable/test/src/json_serializable_test_input.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import 'package:json_annotation/json_annotation.dart';

@JsonSerializable()
const theAnswer = 42;

@JsonSerializable()
void annotatedMethod() => null;

@JsonSerializable()
class Person {
String firstName, lastName;
@JsonKey(name: 'h')
int height;
DateTime dateOfBirth;
dynamic dynamicType;
var varType;
List<int> listOfInts;
}

@JsonSerializable()
class Order {
final String firstName, lastName;
int height;
DateTime dateOfBirth;

Order(this.height, String firstName, [this.lastName])
: this.firstName = firstName;
}

@JsonSerializable()
class FinalFields {
final int a;
int get b => 4;

FinalFields(this.a);
}

@JsonSerializable()
class FromJsonOptionalParameters {
final ChildWithFromJson child;

FromJsonOptionalParameters(this.child);
}

class ChildWithFromJson {
ChildWithFromJson.fromJson(json, {initValue: false});
}

@JsonSerializable()
class ParentObject {
int number;
String str;
ChildObject child;
}

@JsonSerializable()
class ChildObject {
int number;
String str;

factory ChildObject.fromJson(json) => null;
}

@JsonSerializable()
class ParentObjectWithChildren {
int number;
String str;
List<ChildObject> children;
}

@JsonSerializable()
class ParentObjectWithDynamicChildren {
int number;
String str;
List<dynamic> children;
}

@JsonSerializable()
class UnknownCtorParamType {
int number;

// ignore: undefined_class, field_initializer_not_assignable
UnknownCtorParamType(Bob number) : this.number = number;
}

@JsonSerializable()
class UnknownFieldType {
// ignore: undefined_class
Bob number;
}

@JsonSerializable(createFactory: false)
class NoSerializeFieldType {
Stopwatch watch;
}

@JsonSerializable(createToJson: false)
class NoDeserializeFieldType {
Stopwatch watch;
}

@JsonSerializable(createFactory: false)
class NoSerializeBadKey {
Map<int, DateTime> intDateTimeMap;
}

@JsonSerializable(createToJson: false)
class NoDeserializeBadKey {
Map<int, DateTime> intDateTimeMap;
}

@JsonSerializable(createFactory: false)
class IncludeIfNullAll {
@JsonKey(includeIfNull: true)
int number;
String str;
}

@JsonSerializable(createFactory: false, includeIfNull: false)
class IncludeIfNullOverride {
@JsonKey(includeIfNull: true)
int number;
String str;
}

// https://github.com/dart-lang/json_serializable/issues/7 regression
@JsonSerializable()
class NoCtorClass {
final int member;

factory NoCtorClass.fromJson(Map<String, dynamic> json) => null;
}

@JsonSerializable(createFactory: false)
class KeyDupesField {
@JsonKey(name: 'str')
int number;

String str;
}

@JsonSerializable(createFactory: false)
class DupeKeys {
@JsonKey(name: 'a')
int number;

@JsonKey(name: 'a')
String str;
}