Skip to content

Better test - 2 commits #239

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 2 commits into from
Jun 15, 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
2 changes: 1 addition & 1 deletion json_serializable/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ dev_dependencies:
collection: ^1.14.0
dart_style: ^1.0.0
logging: ^0.11.3+1
test: ^0.12.3
test: ^0.12.42
yaml: ^2.1.13
4 changes: 2 additions & 2 deletions json_serializable/test/config_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import 'package:source_gen/source_gen.dart';
import 'package:test/test.dart';
import 'package:yaml/yaml.dart';

final _throwsCastError = throwsA(const isInstanceOf<CastError>());
import 'test_utils.dart';

void main() {
StreamSubscription sub;
Expand Down Expand Up @@ -90,7 +90,7 @@ void main() {
config[entry.key] = entry.value;

expect(() => jsonSerializable(new BuilderOptions(config)),
_throwsCastError);
throwsCastError);
});
}
});
Expand Down
4 changes: 2 additions & 2 deletions json_serializable/test/generic_files/generic_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ void main() {
expect(
() => new GenericClass<double, String>()
..fieldT = (true as dynamic) as double,
throwsA(isACastError));
throwsCastError);
});
test('with bad arguments', () {
expect(
() => new GenericClass<double, String>()
..fieldS = (5 as dynamic) as String,
throwsA(isACastError));
throwsCastError);
});
});
}
3 changes: 1 addition & 2 deletions json_serializable/test/integration/integration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,7 @@ void main() {
'ints': [3.14, 0],
};

expect(() => new Numbers.fromJson(value),
throwsA(const isInstanceOf<CastError>()));
expect(() => new Numbers.fromJson(value), throwsCastError);
});
});
}
35 changes: 14 additions & 21 deletions json_serializable/test/json_serializable_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,16 @@ import 'package:test/test.dart';

import 'analysis_utils.dart';
import 'test_file_utils.dart';
import 'test_utils.dart';

Matcher _throwsInvalidGenerationSourceError(messageMatcher, todoMatcher) =>
throwsA(allOf(
const isInstanceOf<InvalidGenerationSourceError>(),
new FeatureMatcher<InvalidGenerationSourceError>(
'message', (e) => e.message, messageMatcher),
new FeatureMatcher<InvalidGenerationSourceError>(
'todo', (e) => e.todo, todoMatcher),
new FeatureMatcher<InvalidGenerationSourceError>(
'element', (e) => e.element, isNotNull)));
throwsA(const TypeMatcher<InvalidGenerationSourceError>()
.having((e) => e.message, 'message', messageMatcher)
.having((e) => e.todo, 'todo', todoMatcher)
.having((e) => e.element, 'element', isNotNull));

Matcher _throwsUnsupportedError(matcher) =>
throwsA(const TypeMatcher<UnsupportedError>()
.having((e) => e.message, 'message', matcher));

final _formatter = new dart_style.DartFormatter();

Expand Down Expand Up @@ -374,21 +373,17 @@ abstract class _$OrderSerializerMixin {
test('fails if ignored field is referenced by ctor', () {
expect(
() => runForElementNamed('IgnoredFieldCtorClass'),
throwsA(new FeatureMatcher<UnsupportedError>(
'message',
(e) => e.message,
_throwsUnsupportedError(
'Cannot populate the required constructor argument: '
'ignoredTrueField. It is assigned to an ignored field.')));
'ignoredTrueField. It is assigned to an ignored field.'));
});

test('fails if private field is referenced by ctor', () {
expect(
() => runForElementNamed('PrivateFieldCtorClass'),
throwsA(new FeatureMatcher<UnsupportedError>(
'message',
(e) => e.message,
_throwsUnsupportedError(
'Cannot populate the required constructor argument: '
'_privateField. It is assigned to a private field.')));
'_privateField. It is assigned to a private field.'));
});
}

Expand Down Expand Up @@ -528,10 +523,8 @@ FromDynamicCollection _$FromDynamicCollectionFromJson(
test('missing default ctor with a factory', () async {
expect(
() => runForElementNamed('NoCtorClass'),
throwsA(new FeatureMatcher<UnsupportedError>(
'message',
(e) => e.message,
'The class `NoCtorClass` has no default constructor.')));
_throwsUnsupportedError(
'The class `NoCtorClass` has no default constructor.'));
});

test('generic classes', () async {
Expand Down
30 changes: 13 additions & 17 deletions json_serializable/test/kitchen_sink/kitchen_sink_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,15 @@ import 'kitchen_sink.wrapped.dart' as wrapped show testFactory, testFromJson;
import 'kitchen_sink_interface.dart';
import 'strict_keys_object.dart';

final _isATypeError = const isInstanceOf<TypeError>();
final _isATypeError = const TypeMatcher<TypeError>();

Matcher _isAUnrecognizedKeysEexception(expectedMessage) => allOf(
const isInstanceOf<UnrecognizedKeysException>(),
new FeatureMatcher<UnrecognizedKeysException>(
'message', (e) => e.message, expectedMessage));
Matcher _isAUnrecognizedKeysEexception(expectedMessage) =>
const TypeMatcher<UnrecognizedKeysException>()
.having((e) => e.message, 'message', expectedMessage);

Matcher _isMissingKeyException(expectedMessage) => allOf(
const isInstanceOf<MissingRequiredKeysException>(),
new FeatureMatcher<MissingRequiredKeysException>(
'message', (e) => e.message, expectedMessage));
Matcher _isMissingKeyException(expectedMessage) =>
const TypeMatcher<MissingRequiredKeysException>()
.having((e) => e.message, 'message', expectedMessage);

void main() {
test('valid values covers all keys', () {
Expand Down Expand Up @@ -239,12 +237,10 @@ void _testBadValue(bool isChecked, String key, Object badValue,
}
}

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 _checkedMatcher(String expectedKey) =>
const TypeMatcher<CheckedFromJsonException>()
.having((e) => e.className, 'className', 'KitchenSink')
.having((e) => e.key, 'key', expectedKey);

Matcher _getMatcher(bool checked, String expectedKey, bool checkedAssignment) {
Matcher innerMatcher;
Expand All @@ -258,7 +254,7 @@ Matcher _getMatcher(bool checked, String expectedKey, bool checkedAssignment) {
innerMatcher = _checkedMatcher(expectedKey);
} else {
innerMatcher = anyOf(
isACastError,
isCastError,
_isATypeError,
_isAUnrecognizedKeysEexception(
'Unrecognized keys: [invalid_key]; supported keys: [value, custom_field]'));
Expand All @@ -276,7 +272,7 @@ Matcher _getMatcher(bool checked, String expectedKey, bool checkedAssignment) {
break;
case 'intIterable':
case 'datetime-iterable':
innerMatcher = isACastError;
innerMatcher = isCastError;
break;
default:
throw new StateError('Not expected! - $expectedKey');
Expand Down
14 changes: 2 additions & 12 deletions json_serializable/test/test_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,8 @@ import 'dart:convert';

import 'package:test/test.dart';

final isACastError = const isInstanceOf<CastError>();

// TODO(kevmoo) add this to pkg/matcher – is nice!
class FeatureMatcher<T> extends CustomMatcher {
final dynamic Function(T value) _feature;

FeatureMatcher(String name, this._feature, matcher)
: super('`$name`', '`$name`', matcher);

@override
Object featureValueOf(covariant T actual) => _feature(actual);
}
final isCastError = const TypeMatcher<CastError>();
final throwsCastError = throwsA(isCastError);

T roundTripObject<T>(T object, T factory(Map<String, dynamic> json)) {
var data = loudEncode(object);
Expand Down