Skip to content

Commit 2c60bb3

Browse files
committed
Test configuration
1 parent aecf2e5 commit 2c60bb3

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

json_serializable/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@ dev_dependencies:
2121
build_test: ">=0.9.0 <0.11.0"
2222
collection: ^1.14.0
2323
dart_style: ^1.0.0
24+
logging: ^0.11.3+1
2425
test: ^0.12.3
2526
yaml: ^2.1.13
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'dart:async';
6+
7+
import 'package:build/build.dart';
8+
import 'package:json_serializable/builder.dart';
9+
import 'package:logging/logging.dart';
10+
import 'package:source_gen/source_gen.dart';
11+
import 'package:test/test.dart';
12+
13+
final _throwsCastError = throwsA(const isInstanceOf<CastError>());
14+
15+
void main() {
16+
StreamSubscription sub;
17+
final records = <LogRecord>[];
18+
19+
setUpAll(() {
20+
sub = log.onRecord.listen(records.add);
21+
});
22+
23+
tearDownAll(() async {
24+
await sub.cancel();
25+
});
26+
27+
setUp(records.clear);
28+
29+
test('empty', () async {
30+
var builder = jsonSerializable(BuilderOptions.empty) as PartBuilder;
31+
expect(builder, isNotNull);
32+
expect(records, isEmpty);
33+
});
34+
35+
test('valid config', () async {
36+
var builder =
37+
jsonSerializable(const BuilderOptions(_validConfig)) as PartBuilder;
38+
expect(builder, isNotNull);
39+
40+
expect(records, isEmpty);
41+
});
42+
43+
test('unsupported configuration', () async {
44+
var builder =
45+
jsonSerializable(const BuilderOptions(const {'unsupported': 'config'}))
46+
as PartBuilder;
47+
expect(builder, isNotNull);
48+
49+
expect(records.single.message,
50+
'These options were ignored: `{unsupported: config}`.');
51+
});
52+
53+
group('invalid config', () {
54+
for (var entry in _invalidConfig.entries) {
55+
test(entry.key, () {
56+
var config = new Map<String, dynamic>.from(_validConfig);
57+
config[entry.key] = entry.value;
58+
59+
expect(() => jsonSerializable(new BuilderOptions(config)),
60+
_throwsCastError);
61+
});
62+
}
63+
});
64+
}
65+
66+
const _validConfig = const {
67+
'header': 'header',
68+
'use_wrappers': true,
69+
'any_map': true
70+
};
71+
72+
const _invalidConfig = const {
73+
'header': true,
74+
'use_wrappers': 42,
75+
'any_map': 42
76+
};

0 commit comments

Comments
 (0)