diff --git a/CHANGELOG.md b/CHANGELOG.md index 3c89c79f..3b9de42b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,7 @@ -## 4.10.1-wip +## 5.0.0-wip + +* Remove the regular dependency on `package:matcher`. +* Remove the `EqualsDart` class and `equalsDart()` top-level function. ## 4.10.0 diff --git a/lib/code_builder.dart b/lib/code_builder.dart index 9cd15b95..66570f6a 100644 --- a/lib/code_builder.dart +++ b/lib/code_builder.dart @@ -5,7 +5,6 @@ export 'src/allocator.dart' show Allocator; export 'src/base.dart' show Spec, lazySpec; export 'src/emitter.dart' show DartEmitter; -export 'src/matchers.dart' show EqualsDart, equalsDart; export 'src/specs/class.dart' show Class, ClassBuilder, ClassModifier; export 'src/specs/code.dart' show Block, BlockBuilder, Code, ScopedCode, StaticCode, lazyCode; diff --git a/lib/src/matchers.dart b/lib/src/matchers.dart deleted file mode 100644 index 0cdf7399..00000000 --- a/lib/src/matchers.dart +++ /dev/null @@ -1,73 +0,0 @@ -// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. - -import 'package:matcher/matcher.dart'; - -import 'base.dart'; -import 'emitter.dart'; - -/// Encodes [spec] as Dart source code. -String _dart(Spec spec, DartEmitter emitter) => - EqualsDart._format(spec.accept(emitter).toString()); - -/// Returns a matcher for [Spec] objects that emit code matching [source]. -/// -/// Both [source] and the result emitted from the compared [Spec] are formatted -/// with [EqualsDart.format]. A plain [DartEmitter] is used by default and may -/// be overridden with [emitter]. -Matcher equalsDart( - String source, [ - DartEmitter? emitter, -]) => - EqualsDart._(EqualsDart._format(source), emitter ?? DartEmitter()); - -/// Implementation detail of using the [equalsDart] matcher. -/// -/// See [EqualsDart.format] to specify the default source code formatter. -class EqualsDart extends Matcher { - /// May override to provide a function to format Dart on [equalsDart]. - /// - /// By default, uses [collapseWhitespace], but it is recommended to instead - /// use `dart_style` (dartfmt) where possible. See `test/common.dart` for an - /// example. - static String Function(String) format = collapseWhitespace; - - static String _format(String source) { - try { - return format(source).trim(); - } catch (_) { - // Ignored on purpose, probably not exactly valid Dart code. - return collapseWhitespace(source).trim(); - } - } - - final DartEmitter _emitter; - final String _expectedSource; - - const EqualsDart._(this._expectedSource, this._emitter); - - @override - Description describe(Description description) => - description.add(_expectedSource); - - @override - Description describeMismatch( - covariant Spec item, - Description mismatchDescription, - Map matchState, - bool verbose, - ) { - final actualSource = _dart(item, _emitter); - return equals(_expectedSource).describeMismatch( - actualSource, - mismatchDescription, - matchState, - verbose, - ); - } - - @override - bool matches(covariant Spec item, Object? matchState) => - _dart(item, _emitter) == _expectedSource; -} diff --git a/pubspec.yaml b/pubspec.yaml index edaf04a2..5c9c73e7 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: code_builder -version: 4.10.1-wip +version: 5.0.0-wip description: A fluent, builder-based library for generating valid Dart code. repository: https://github.com/dart-lang/code_builder @@ -10,7 +10,6 @@ dependencies: built_collection: ^5.0.0 built_value: ^8.0.0 collection: ^1.15.0 - matcher: ^0.12.10 meta: ^1.3.0 dev_dependencies: @@ -19,5 +18,6 @@ dev_dependencies: built_value_generator: ^8.0.0 dart_flutter_team_lints: ^2.0.0 dart_style: ^2.3.4 + matcher: ^0.12.10 source_gen: ^1.0.0 - test: ^1.16.0 + test: ^1.25.0 diff --git a/test/allocator_test.dart b/test/allocator_test.dart index 09f135c5..07bce485 100644 --- a/test/allocator_test.dart +++ b/test/allocator_test.dart @@ -5,11 +5,7 @@ import 'package:code_builder/code_builder.dart'; import 'package:test/test.dart'; -import 'common.dart'; - void main() { - useDartfmt(); - group('Allocator', () { Allocator allocator; diff --git a/test/common.dart b/test/common.dart deleted file mode 100644 index 9bfed545..00000000 --- a/test/common.dart +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. - -import 'package:code_builder/code_builder.dart'; -import 'package:dart_style/dart_style.dart'; - -final DartFormatter _dartfmt = DartFormatter(); -String _format(String source) { - try { - return _dartfmt.format(source); - } on FormatterException catch (_) { - return _dartfmt.formatStatement(source); - } -} - -/// Should be invoked in `main()` of every test in `test/**_test.dart`. -void useDartfmt() => EqualsDart.format = _format; diff --git a/test/const_test.dart b/test/const_test.dart index c4d69c61..0fa455e2 100644 --- a/test/const_test.dart +++ b/test/const_test.dart @@ -5,11 +5,9 @@ import 'package:code_builder/code_builder.dart'; import 'package:test/test.dart'; -import 'common.dart'; +import 'matchers.dart'; void main() { - useDartfmt(); - final constMap = literalConstMap({ 'list': literalConstList([]), 'duration': refer('Duration').constInstance([]), diff --git a/test/directive_test.dart b/test/directive_test.dart index 0aee6ae5..d95f9045 100644 --- a/test/directive_test.dart +++ b/test/directive_test.dart @@ -5,11 +5,9 @@ import 'package:code_builder/code_builder.dart'; import 'package:test/test.dart'; -import 'common.dart'; +import 'matchers.dart'; void main() { - useDartfmt(); - final $LinkedHashMap = refer('LinkedHashMap', 'dart:collection'); final library = Library((b) => b diff --git a/test/e2e/injection_test.dart b/test/e2e/injection_test.dart index 129f02cd..480a8d54 100644 --- a/test/e2e/injection_test.dart +++ b/test/e2e/injection_test.dart @@ -5,11 +5,9 @@ import 'package:code_builder/code_builder.dart'; import 'package:test/test.dart'; -import '../common.dart'; +import '../matchers.dart'; void main() { - useDartfmt(); - test('should generate a complex generated file', () { // Imports from an existing Dart library. final $App = refer('App', 'package:app/app.dart'); diff --git a/test/matcher_test.dart b/test/matcher_test.dart index c9475a84..2820b4f2 100644 --- a/test/matcher_test.dart +++ b/test/matcher_test.dart @@ -5,15 +5,17 @@ import 'package:code_builder/code_builder.dart'; import 'package:test/test.dart'; +import 'matchers.dart'; + void main() { test('describes mismatches', () { const actual = Code('final x=1;'); equalsDart('final y=2;').expectMismatch(actual, ''' - Expected: final y=2; + Expected: final y = 2; Actual: StaticCode: Which: is different. - Expected: final y=2; - Actual: final x=1; + Expected: final y = 2; + Actual: final x = 1; ^ Differ at offset 6 '''); diff --git a/test/matchers.dart b/test/matchers.dart new file mode 100644 index 00000000..7f1f80bb --- /dev/null +++ b/test/matchers.dart @@ -0,0 +1,64 @@ +// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import 'package:code_builder/code_builder.dart'; +import 'package:dart_style/dart_style.dart'; +import 'package:matcher/matcher.dart'; + +final DartFormatter _dartfmt = DartFormatter(); + +/// Returns a matcher for [Spec] objects that emit code matching [source]. +/// +/// Both [source] and the result emitted from the compared [Spec] are formatted +/// with dart_style. +Matcher equalsDart(String source, [DartEmitter? emitter]) => + _EqualsDart._(_EqualsDart._format(source), emitter ?? DartEmitter()); + +String _emitDart(Spec spec, DartEmitter emitter) => + _EqualsDart._format(spec.accept(emitter).toString()); + +String _dartFormatAttempt(String source) { + try { + return _dartfmt.format(source); + } on FormatterException catch (_) {} + + try { + return _dartfmt.formatStatement(source); + } on FormatterException catch (_) {} + + return collapseWhitespace(source).trim(); +} + +class _EqualsDart extends Matcher { + static String _format(String source) => _dartFormatAttempt(source).trim(); + + final DartEmitter _emitter; + final String _expectedSource; + + const _EqualsDart._(this._expectedSource, this._emitter); + + @override + Description describe(Description description) => + description.add(_expectedSource); + + @override + Description describeMismatch( + covariant Spec item, + Description mismatchDescription, + Map matchState, + bool verbose, + ) { + final actualSource = _emitDart(item, _emitter); + return equals(_expectedSource).describeMismatch( + actualSource, + mismatchDescription, + matchState, + verbose, + ); + } + + @override + bool matches(covariant Spec item, Object? matchState) => + _emitDart(item, _emitter) == _expectedSource; +} diff --git a/test/specs/class_test.dart b/test/specs/class_test.dart index 562bca4c..a6b55a6e 100644 --- a/test/specs/class_test.dart +++ b/test/specs/class_test.dart @@ -5,11 +5,9 @@ import 'package:code_builder/code_builder.dart'; import 'package:test/test.dart'; -import '../common.dart'; +import '../matchers.dart'; void main() { - useDartfmt(); - test('should create a class', () { expect( Class((b) => b..name = 'Foo'), diff --git a/test/specs/code/expression_test.dart b/test/specs/code/expression_test.dart index 4ce9ebaf..b8142195 100644 --- a/test/specs/code/expression_test.dart +++ b/test/specs/code/expression_test.dart @@ -5,11 +5,9 @@ import 'package:code_builder/code_builder.dart'; import 'package:test/test.dart'; -import '../../common.dart'; +import '../../matchers.dart'; void main() { - useDartfmt(); - test('should emit a simple expression', () { expect(literalNull, equalsDart('null')); }); diff --git a/test/specs/code/statement_test.dart b/test/specs/code/statement_test.dart index cd3d53af..2db32e50 100644 --- a/test/specs/code/statement_test.dart +++ b/test/specs/code/statement_test.dart @@ -5,11 +5,9 @@ import 'package:code_builder/code_builder.dart'; import 'package:test/test.dart'; -import '../../common.dart'; +import '../../matchers.dart'; void main() { - useDartfmt(); - test('should emit a block of code', () { expect( Block.of([ diff --git a/test/specs/enum_test.dart b/test/specs/enum_test.dart index 5a0b19a2..f6a335c2 100644 --- a/test/specs/enum_test.dart +++ b/test/specs/enum_test.dart @@ -5,11 +5,9 @@ import 'package:code_builder/code_builder.dart'; import 'package:test/test.dart'; -import '../common.dart'; +import '../matchers.dart'; void main() { - useDartfmt(); - test('should create an enum', () { expect( Enum((b) => b diff --git a/test/specs/extension_test.dart b/test/specs/extension_test.dart index b45fa658..c88e2fef 100644 --- a/test/specs/extension_test.dart +++ b/test/specs/extension_test.dart @@ -5,11 +5,9 @@ import 'package:code_builder/code_builder.dart'; import 'package:test/test.dart'; -import '../common.dart'; +import '../matchers.dart'; void main() { - useDartfmt(); - test('should create an extension', () { expect( Extension((b) => b diff --git a/test/specs/extension_type_test.dart b/test/specs/extension_type_test.dart index cc510468..4097bb7b 100644 --- a/test/specs/extension_type_test.dart +++ b/test/specs/extension_type_test.dart @@ -5,11 +5,9 @@ import 'package:code_builder/code_builder.dart'; import 'package:test/test.dart'; -import '../common.dart'; +import '../matchers.dart'; void main() { - useDartfmt(); - test('minimum extension type', () { expect( ExtensionType((b) => b diff --git a/test/specs/field_test.dart b/test/specs/field_test.dart index 3d53687b..9b0c2c08 100644 --- a/test/specs/field_test.dart +++ b/test/specs/field_test.dart @@ -5,11 +5,9 @@ import 'package:code_builder/code_builder.dart'; import 'package:test/test.dart'; -import '../common.dart'; +import '../matchers.dart'; void main() { - useDartfmt(); - test('should create a field', () { expect( Field((b) => b..name = 'foo'), diff --git a/test/specs/library_test.dart b/test/specs/library_test.dart index 8ea4c581..8d9445c1 100644 --- a/test/specs/library_test.dart +++ b/test/specs/library_test.dart @@ -5,11 +5,9 @@ import 'package:code_builder/code_builder.dart'; import 'package:test/test.dart'; -import '../common.dart'; +import '../matchers.dart'; void main() { - useDartfmt(); - group('File', () { final $LinkedHashMap = refer('LinkedHashMap', 'dart:collection'); diff --git a/test/specs/method_test.dart b/test/specs/method_test.dart index 5621bc52..ff78a127 100644 --- a/test/specs/method_test.dart +++ b/test/specs/method_test.dart @@ -5,11 +5,9 @@ import 'package:code_builder/code_builder.dart'; import 'package:test/test.dart'; -import '../common.dart'; +import '../matchers.dart'; void main() { - useDartfmt(); - test('should create a method', () { expect( Method((b) => b..name = 'foo'), diff --git a/test/specs/mixin_test.dart b/test/specs/mixin_test.dart index e167d02a..04009915 100644 --- a/test/specs/mixin_test.dart +++ b/test/specs/mixin_test.dart @@ -5,11 +5,9 @@ import 'package:code_builder/code_builder.dart'; import 'package:test/test.dart'; -import '../common.dart'; +import '../matchers.dart'; void main() { - useDartfmt(); - test('should create a mixin', () { expect( Mixin((b) => b..name = 'Foo'), diff --git a/test/specs/record_type_test.dart b/test/specs/record_type_test.dart index 0084c507..3febec19 100644 --- a/test/specs/record_type_test.dart +++ b/test/specs/record_type_test.dart @@ -5,13 +5,11 @@ import 'package:code_builder/code_builder.dart'; import 'package:test/test.dart'; -import '../common.dart'; +import '../matchers.dart'; void main() { late DartEmitter emitter; - useDartfmt(); - setUp(() => emitter = DartEmitter.scoped(useNullSafetySyntax: true)); final intRef = TypeReference((b) => b.symbol = 'int'); diff --git a/test/specs/type_reference_test.dart b/test/specs/type_reference_test.dart index 35365cbf..565bbfba 100644 --- a/test/specs/type_reference_test.dart +++ b/test/specs/type_reference_test.dart @@ -5,11 +5,9 @@ import 'package:code_builder/code_builder.dart'; import 'package:test/test.dart'; -import '../common.dart'; +import '../matchers.dart'; void main() { - useDartfmt(); - test('should create a nullable type in a pre-Null Safety library', () { expect( TypeReference((b) => b