Skip to content
This repository has been archived by the owner on Nov 1, 2024. It is now read-only.

Commit

Permalink
remove the regular dependency on package:matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
devoncarew committed May 6, 2024
1 parent fcfe489 commit c914991
Show file tree
Hide file tree
Showing 22 changed files with 51 additions and 106 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
1 change: 0 additions & 1 deletion lib/code_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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:
Expand All @@ -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
4 changes: 0 additions & 4 deletions test/allocator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
18 changes: 0 additions & 18 deletions test/common.dart

This file was deleted.

4 changes: 1 addition & 3 deletions test/const_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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([]),
Expand Down
4 changes: 1 addition & 3 deletions test/directive_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions test/e2e/injection_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
8 changes: 5 additions & 3 deletions test/matcher_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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:<final x=1;>
Which: is different.
Expected: final y=2;
Actual: final x=1;
Expected: final y = 2;
Actual: final x = 1;
^
Differ at offset 6
''');
Expand Down
55 changes: 24 additions & 31 deletions lib/src/matchers.dart → test/matchers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,43 @@
// 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';

import 'base.dart';
import 'emitter.dart';
final DartFormatter _dartfmt = DartFormatter();

/// Encodes [spec] as Dart source code.
String _dart(Spec spec, DartEmitter emitter) =>
EqualsDart._format(spec.accept<StringSink>(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();
}

/// 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());
/// with dart_style.
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;
/// Encodes [spec] as Dart source code.
String _dart(Spec spec, DartEmitter emitter) =>
_EqualsDart._format(spec.accept<StringSink>(emitter).toString());

static String _format(String source) {
try {
return format(source).trim();
} catch (_) {
// Ignored on purpose, probably not exactly valid Dart code.
return collapseWhitespace(source).trim();
}
}
/// Implementation detail of using the [equalsDart] matcher.
class _EqualsDart extends Matcher {
static String _format(String source) => _dartFormatAttempt(source).trim();

final DartEmitter _emitter;
final String _expectedSource;

const EqualsDart._(this._expectedSource, this._emitter);
const _EqualsDart._(this._expectedSource, this._emitter);

@override
Description describe(Description description) =>
Expand Down
4 changes: 1 addition & 3 deletions test/specs/class_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
4 changes: 1 addition & 3 deletions test/specs/code/expression_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
});
Expand Down
4 changes: 1 addition & 3 deletions test/specs/code/statement_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down
4 changes: 1 addition & 3 deletions test/specs/enum_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions test/specs/extension_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions test/specs/extension_type_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions test/specs/field_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
4 changes: 1 addition & 3 deletions test/specs/library_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
4 changes: 1 addition & 3 deletions test/specs/method_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
4 changes: 1 addition & 3 deletions test/specs/mixin_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
4 changes: 1 addition & 3 deletions test/specs/record_type_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
4 changes: 1 addition & 3 deletions test/specs/type_reference_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c914991

Please sign in to comment.