|
| 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