Skip to content

Test cleanup #166

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 5 commits into from
May 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
134 changes: 91 additions & 43 deletions json_serializable/test/kitchen_sink_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,26 @@ import 'package:json_annotation/json_annotation.dart';
import 'package:json_serializable/src/constants.dart';
import 'package:yaml/yaml.dart';

import 'test_files/kitchen_sink.dart' as nullable
import 'kitchen_sink_test_files/kitchen_sink.dart' as nullable
show testFactory, testFromJson;
import 'test_files/kitchen_sink.non_nullable.checked.dart' as checked
import 'kitchen_sink_test_files/kitchen_sink.non_nullable.checked.dart'
as checked show testFactory, testFromJson;
import 'kitchen_sink_test_files/kitchen_sink.non_nullable.dart' as nn
show testFactory, testFromJson;
import 'test_files/kitchen_sink.non_nullable.dart' as nn
show testFactory, testFromJson;
import 'test_files/kitchen_sink.non_nullable.wrapped.dart' as nnwrapped
show testFactory, testFromJson;
import 'test_files/kitchen_sink.wrapped.dart' as wrapped
import 'kitchen_sink_test_files/kitchen_sink.non_nullable.wrapped.dart'
as nnwrapped show testFactory, testFromJson;
import 'kitchen_sink_test_files/kitchen_sink.wrapped.dart' as wrapped
show testFactory, testFromJson;

import 'test_files/kitchen_sink_interface.dart';
import 'kitchen_sink_test_files/kitchen_sink_interface.dart';
import 'test_utils.dart';

final _isACastError = const isInstanceOf<CastError>();
final _isATypeError = const isInstanceOf<TypeError>();

void main() {
test('valid values covers all keys', () {
expect(_invalidValues.keys, orderedEquals(_validValues.keys));
expect(_invalidValueTypes.keys, orderedEquals(_validValues.keys));
});

group('nullable', () {
Expand Down Expand Up @@ -73,7 +73,7 @@ void _nonNullableTests(KitchenSinkCtor ctor, KitchenSink fromJson(Map json),

test('with empty json fails deserialization', () {
if (isChecked) {
expect(() => fromJson({}), _checkedMatcher(true, 'intIterable'));
expect(() => fromJson({}), throwsA(_checkedMatcher('intIterable')));
} else {
expect(() => fromJson({}), throwsNoSuchMethodError);
}
Expand Down Expand Up @@ -197,37 +197,75 @@ void _sharedTests(KitchenSinkCtor ctor, KitchenSink fromJson(Map json),
});

group('a bad value for', () {
for (var invalidEntry in _invalidValues.entries) {
final expectedKeyValue =
const ['intIterable', 'datetime-iterable'].contains(invalidEntry.key)
? null
: invalidEntry.key;
final matcher = _checkedMatcher(isChecked, expectedKeyValue);

for (var isJson in [true, false]) {
test('`${invalidEntry.key}` fails - ${isJson ? 'json' : 'yaml'}', () {
var copy = new Map.from(_validValues);
copy[invalidEntry.key] = invalidEntry.value;

if (!isJson) {
copy = loadYaml(loudEncode(copy)) as YamlMap;
}

expect(() => fromJson(copy), matcher);
});
}
for (var e in _invalidValueTypes.entries) {
_testBadValue(isChecked, e.key, e.value, fromJson, false);
}
for (var e in _invalidCheckedValues.entries) {
_testBadValue(isChecked, e.key, e.value, fromJson, true);
}
});
}

Matcher _checkedMatcher(bool checked, String expectedKey) => throwsA(checked
? allOf(
const isInstanceOf<CheckedFromJsonException>(),
new FeatureMatcher<CheckedFromJsonException>(
'className', (e) => e.className, 'KitchenSink'),
new FeatureMatcher<CheckedFromJsonException>(
'key', (e) => e.key, expectedKey))
: anyOf(_isACastError, _isATypeError, isArgumentError));
void _testBadValue(bool isChecked, String key, Object badValue,
KitchenSink fromJson(Map json), bool checkedAssignment) {
final matcher = _getMatcher(isChecked, key, checkedAssignment);

for (var isJson in [true, false]) {
test('`$key` fails with value `$badValue`- ${isJson ? 'json' : 'yaml'}',
() {
var copy = new Map.from(_validValues);
copy[key] = badValue;

if (!isJson) {
copy = loadYaml(loudEncode(copy)) as YamlMap;
}

expect(() => fromJson(copy), matcher);
});
}
}

Matcher _checkedMatcher(String expectedKey) => allOf(
const isInstanceOf<CheckedFromJsonException>(),
new FeatureMatcher<CheckedFromJsonException>(
'className', (e) => e.className, 'KitchenSink'),
new FeatureMatcher<CheckedFromJsonException>(
'key', (e) => e.key, expectedKey));

Matcher _getMatcher(bool checked, String expectedKey, bool checkedAssignment) {
Matcher innerMatcher;

if (checked) {
if (checkedAssignment &&
const ['intIterable', 'datetime-iterable', 'validatedPropertyNo42']
.contains(expectedKey)) {
expectedKey = null;
}

innerMatcher = _checkedMatcher(expectedKey);
} else {
innerMatcher = anyOf(_isACastError, _isATypeError);

if (checkedAssignment) {
switch (expectedKey) {
case 'validatedPropertyNo42':
innerMatcher = isStateError;
break;
case 'no-42':
innerMatcher = isArgumentError;
break;
case 'intIterable':
case 'datetime-iterable':
innerMatcher = _isACastError;
break;
default:
throw new StateError('Not expected! - $expectedKey');
}
}
}

return throwsA(innerMatcher);
}

final _validValues = const {
'no-42': 0,
Expand All @@ -250,17 +288,18 @@ final _validValues = const {
toJsonMapVarName: const {},
toJsonMapHelperName: null,
r'$string': null,
'simpleObject': const {'value': 42}
'simpleObject': const {'value': 42},
'validatedPropertyNo42': 0
};

final _invalidValues = const {
'no-42': 42,
final _invalidValueTypes = const {
'no-42': true,
'dateTime': true,
'iterable': true,
'dynamicIterable': true,
'objectIterable': true,
'intIterable': const [true],
'datetime-iterable': const [true],
'intIterable': true,
'datetime-iterable': true,
'list': true,
'dynamicList': true,
'objectList': true,
Expand All @@ -274,7 +313,16 @@ final _invalidValues = const {
toJsonMapVarName: const {'key': 42},
toJsonMapHelperName: 42,
r'$string': true,
'simpleObject': 42
'simpleObject': 42,
'validatedPropertyNo42': true
};

/// Invalid values that are found after the property set or ctor call
final _invalidCheckedValues = const {
'no-42': 42,
'validatedPropertyNo42': 42,
'intIterable': const [true],
'datetime-iterable': const [true],
};

final _excludeIfNullKeys = const [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,15 @@ class KitchenSink extends Object

SimpleObject simpleObject = _defaultSimpleObject();

int _validatedPropertyNo42;
int get validatedPropertyNo42 => _validatedPropertyNo42;

set validatedPropertyNo42(int value) {
if (value == 42) {
throw new StateError('Cannot be 42!');
}
_validatedPropertyNo42 = value;
}

bool operator ==(Object other) => k.sinkEquals(this, other);
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ KitchenSink _$KitchenSinkFromJson(Map json) => new KitchenSink(
..val = json['val'] == null ? null : new Map<String, bool>.from(json['val'] as Map)
..writeNotNull = json['writeNotNull'] as bool
..string = json[r'$string'] as String
..simpleObject = json['simpleObject'] == null ? null : new SimpleObject.fromJson(json['simpleObject'] as Map);
..simpleObject = json['simpleObject'] == null ? null : new SimpleObject.fromJson(json['simpleObject'] as Map)
..validatedPropertyNo42 = json['validatedPropertyNo42'] as int;

abstract class _$KitchenSinkSerializerMixin {
int get ctorValidatedNo42;
Expand All @@ -70,6 +71,7 @@ abstract class _$KitchenSinkSerializerMixin {
bool get writeNotNull;
String get string;
SimpleObject get simpleObject;
int get validatedPropertyNo42;
Map<String, dynamic> toJson() {
var val = <String, dynamic>{
'no-42': ctorValidatedNo42,
Expand Down Expand Up @@ -115,6 +117,7 @@ abstract class _$KitchenSinkSerializerMixin {
val['writeNotNull'] = this.writeNotNull;
val[r'$string'] = string;
val['simpleObject'] = simpleObject;
val['validatedPropertyNo42'] = validatedPropertyNo42;
return val;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,15 @@ class KitchenSink extends Object

SimpleObject simpleObject = _defaultSimpleObject();

int _validatedPropertyNo42;
int get validatedPropertyNo42 => _validatedPropertyNo42;

set validatedPropertyNo42(int value) {
if (value == 42) {
throw new StateError('Cannot be 42!');
}
_validatedPropertyNo42 = value;
}

bool operator ==(Object other) => k.sinkEquals(this, other);
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ KitchenSink _$KitchenSinkFromJson(Map json) => $checkedNew(
..val = $checkedConvert(json, 'val', (v) => new Map<String, bool>.from(v as Map))
..writeNotNull = $checkedConvert(json, 'writeNotNull', (v) => v as bool)
..string = $checkedConvert(json, r'$string', (v) => v as String)
..simpleObject = $checkedConvert(json, 'simpleObject', (v) => new SimpleObject.fromJson(v as Map)),
..simpleObject = $checkedConvert(json, 'simpleObject', (v) => new SimpleObject.fromJson(v as Map))
..validatedPropertyNo42 = $checkedConvert(json, 'validatedPropertyNo42', (v) => v as int),
fieldKeyMap: const {
'ctorValidatedNo42': 'no-42',
'dateTimeIterable': 'datetime-iterable',
Expand Down Expand Up @@ -87,6 +88,7 @@ abstract class _$KitchenSinkSerializerMixin {
bool get writeNotNull;
String get string;
SimpleObject get simpleObject;
int get validatedPropertyNo42;
Map<String, dynamic> toJson() => <String, dynamic>{
'no-42': ctorValidatedNo42,
'dateTime': dateTime.toIso8601String(),
Expand Down Expand Up @@ -118,6 +120,7 @@ abstract class _$KitchenSinkSerializerMixin {
'val': val,
'writeNotNull': writeNotNull,
r'$string': string,
'simpleObject': simpleObject
'simpleObject': simpleObject,
'validatedPropertyNo42': validatedPropertyNo42
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,15 @@ class KitchenSink extends Object

SimpleObject simpleObject = _defaultSimpleObject();

int _validatedPropertyNo42;
int get validatedPropertyNo42 => _validatedPropertyNo42;

set validatedPropertyNo42(int value) {
if (value == 42) {
throw new StateError('Cannot be 42!');
}
_validatedPropertyNo42 = value;
}

bool operator ==(Object other) => k.sinkEquals(this, other);
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ KitchenSink _$KitchenSinkFromJson(Map json) => new KitchenSink(
..val = new Map<String, bool>.from(json['val'] as Map)
..writeNotNull = json['writeNotNull'] as bool
..string = json[r'$string'] as String
..simpleObject = new SimpleObject.fromJson(json['simpleObject'] as Map);
..simpleObject = new SimpleObject.fromJson(json['simpleObject'] as Map)
..validatedPropertyNo42 = json['validatedPropertyNo42'] as int;

abstract class _$KitchenSinkSerializerMixin {
int get ctorValidatedNo42;
Expand All @@ -65,6 +66,7 @@ abstract class _$KitchenSinkSerializerMixin {
bool get writeNotNull;
String get string;
SimpleObject get simpleObject;
int get validatedPropertyNo42;
Map<String, dynamic> toJson() => <String, dynamic>{
'no-42': ctorValidatedNo42,
'dateTime': dateTime.toIso8601String(),
Expand Down Expand Up @@ -96,6 +98,7 @@ abstract class _$KitchenSinkSerializerMixin {
'val': val,
'writeNotNull': writeNotNull,
r'$string': string,
'simpleObject': simpleObject
'simpleObject': simpleObject,
'validatedPropertyNo42': validatedPropertyNo42
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,15 @@ class KitchenSink extends Object

SimpleObject simpleObject = _defaultSimpleObject();

int _validatedPropertyNo42;
int get validatedPropertyNo42 => _validatedPropertyNo42;

set validatedPropertyNo42(int value) {
if (value == 42) {
throw new StateError('Cannot be 42!');
}
_validatedPropertyNo42 = value;
}

bool operator ==(Object other) => k.sinkEquals(this, other);
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ KitchenSink _$KitchenSinkFromJson(Map json) => new KitchenSink(
..val = new Map<String, bool>.from(json['val'] as Map)
..writeNotNull = json['writeNotNull'] as bool
..string = json[r'$string'] as String
..simpleObject = new SimpleObject.fromJson(json['simpleObject'] as Map);
..simpleObject = new SimpleObject.fromJson(json['simpleObject'] as Map)
..validatedPropertyNo42 = json['validatedPropertyNo42'] as int;

abstract class _$KitchenSinkSerializerMixin {
int get ctorValidatedNo42;
Expand All @@ -65,6 +66,7 @@ abstract class _$KitchenSinkSerializerMixin {
bool get writeNotNull;
String get string;
SimpleObject get simpleObject;
int get validatedPropertyNo42;
Map<String, dynamic> toJson() => new _$KitchenSinkJsonMapWrapper(this);
}

Expand Down Expand Up @@ -94,7 +96,8 @@ class _$KitchenSinkJsonMapWrapper extends $JsonMapWrapper {
'val',
'writeNotNull',
r'$string',
'simpleObject'
'simpleObject',
'validatedPropertyNo42'
];

@override
Expand Down Expand Up @@ -154,6 +157,8 @@ class _$KitchenSinkJsonMapWrapper extends $JsonMapWrapper {
return _v.string;
case 'simpleObject':
return _v.simpleObject;
case 'validatedPropertyNo42':
return _v.validatedPropertyNo42;
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,15 @@ class KitchenSink extends Object

SimpleObject simpleObject = _defaultSimpleObject();

int _validatedPropertyNo42;
int get validatedPropertyNo42 => _validatedPropertyNo42;

set validatedPropertyNo42(int value) {
if (value == 42) {
throw new StateError('Cannot be 42!');
}
_validatedPropertyNo42 = value;
}

bool operator ==(Object other) => k.sinkEquals(this, other);
}
Loading