Skip to content

Commit 42effa6

Browse files
authored
Migrate many custom matchers to TypeMatcher (#1668)
1 parent ea2419f commit 42effa6

File tree

5 files changed

+30
-101
lines changed

5 files changed

+30
-101
lines changed

pkgs/file/pubspec.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,7 @@ dev_dependencies:
1515
file_testing: ^3.0.0
1616
lints: ^2.0.1
1717
test: ^1.23.1
18+
19+
dependency_overrides:
20+
file_testing:
21+
path: ../file_testing

pkgs/file_testing/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 3.1.0-wip
2+
3+
* Changed the type of several matchers to `TypeMatcher` which allows cascading
4+
their usage with `.having` and similar.
5+
16
## 3.0.2
27

38
* Require Dart 3.1.
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1 @@
1-
include: package:lints/recommended.yaml
2-
3-
analyzer:
4-
errors:
5-
# Allow having TODOs in the code
6-
todo: ignore
1+
include: package:dart_flutter_team_lints/analysis_options.yaml

pkgs/file_testing/lib/src/testing/core_matchers.dart

Lines changed: 17 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,36 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
// ignore_for_file: comment_references
6+
57
import 'dart:io';
68

79
import 'package:test/test.dart';
810

911
import 'internal.dart';
1012

1113
/// Matcher that successfully matches against any instance of [Directory].
12-
const Matcher isDirectory = TypeMatcher<Directory>();
14+
const isDirectory = TypeMatcher<Directory>();
1315

1416
/// Matcher that successfully matches against any instance of [File].
15-
const Matcher isFile = TypeMatcher<File>();
17+
const isFile = TypeMatcher<File>();
1618

1719
/// Matcher that successfully matches against any instance of [Link].
18-
const Matcher isLink = TypeMatcher<Link>();
20+
const isLink = TypeMatcher<Link>();
1921

2022
/// Matcher that successfully matches against any instance of
2123
/// [FileSystemEntity].
22-
const Matcher isFileSystemEntity = TypeMatcher<FileSystemEntity>();
24+
const isFileSystemEntity = TypeMatcher<FileSystemEntity>();
2325

2426
/// Matcher that successfully matches against any instance of [FileStat].
25-
const Matcher isFileStat = TypeMatcher<FileStat>();
27+
const isFileStat = TypeMatcher<FileStat>();
2628

2729
/// Returns a [Matcher] that matches [path] against an entity's path.
2830
///
2931
/// [path] may be a String, a predicate function, or a [Matcher]. If it is
3032
/// a String, it will be wrapped in an equality matcher.
31-
Matcher hasPath(dynamic path) => _HasPath(path);
33+
TypeMatcher<FileSystemEntity> hasPath(dynamic path) =>
34+
isFileSystemEntity.having((e) => e.path, 'path', path);
3235

3336
/// Returns a [Matcher] that successfully matches against an instance of
3437
/// [FileSystemException].
@@ -39,7 +42,8 @@ Matcher hasPath(dynamic path) => _HasPath(path);
3942
/// [osErrorCode] may be an `int`, a predicate function, or a [Matcher]. If it
4043
/// is an `int`, it will be wrapped in an equality matcher.
4144
Matcher isFileSystemException([dynamic osErrorCode]) =>
42-
_FileSystemException(osErrorCode);
45+
const TypeMatcher<FileSystemException>().having((e) => e.osError?.errorCode,
46+
'osError.errorCode', _fileExceptionWrapMatcher(osErrorCode));
4347

4448
/// Returns a matcher that successfully matches against a future or function
4549
/// that throws a [FileSystemException].
@@ -67,89 +71,10 @@ void expectFileSystemException(dynamic osErrorCode, void Function() callback) {
6771

6872
/// Matcher that successfully matches against a [FileSystemEntity] that
6973
/// exists ([FileSystemEntity.existsSync] returns true).
70-
const Matcher exists = _Exists();
71-
72-
class _FileSystemException extends Matcher {
73-
_FileSystemException(dynamic osErrorCode)
74-
: _matcher = _wrapMatcher(osErrorCode);
75-
76-
final Matcher? _matcher;
77-
78-
static Matcher? _wrapMatcher(dynamic osErrorCode) {
79-
if (osErrorCode == null) {
80-
return null;
81-
}
82-
return ignoreOsErrorCodes ? anything : wrapMatcher(osErrorCode);
83-
}
84-
85-
@override
86-
bool matches(dynamic item, Map<dynamic, dynamic> matchState) {
87-
if (item is FileSystemException) {
88-
return _matcher == null ||
89-
_matcher!.matches(item.osError?.errorCode, matchState);
90-
}
91-
return false;
92-
}
93-
94-
@override
95-
Description describe(Description desc) {
96-
if (_matcher == null) {
97-
return desc.add('FileSystemException');
98-
} else {
99-
desc.add('FileSystemException with osError.errorCode: ');
100-
return _matcher!.describe(desc);
101-
}
102-
}
103-
}
104-
105-
class _HasPath extends Matcher {
106-
_HasPath(dynamic path) : _matcher = wrapMatcher(path);
107-
108-
final Matcher _matcher;
74+
final TypeMatcher<FileSystemEntity> exists =
75+
isFileSystemEntity.having((e) => e.existsSync(), 'existsSync', true);
10976

110-
@override
111-
bool matches(dynamic item, Map<dynamic, dynamic> matchState) =>
112-
_matcher.matches(item.path, matchState);
113-
114-
@override
115-
Description describe(Description desc) {
116-
desc.add('has path: ');
117-
return _matcher.describe(desc);
118-
}
119-
120-
@override
121-
Description describeMismatch(
122-
dynamic item,
123-
Description desc,
124-
Map<dynamic, dynamic> matchState,
125-
bool verbose,
126-
) {
127-
desc.add('has path: \'${item.path}\'').add('\n Which: ');
128-
final Description pathDesc = StringDescription();
129-
_matcher.describeMismatch(item.path, pathDesc, matchState, verbose);
130-
desc.add(pathDesc.toString());
131-
return desc;
132-
}
133-
}
134-
135-
class _Exists extends Matcher {
136-
const _Exists();
137-
138-
@override
139-
bool matches(dynamic item, Map<dynamic, dynamic> matchState) =>
140-
item is FileSystemEntity && item.existsSync();
141-
142-
@override
143-
Description describe(Description description) =>
144-
description.add('a file system entity that exists');
145-
146-
@override
147-
Description describeMismatch(
148-
dynamic item,
149-
Description description,
150-
Map<dynamic, dynamic> matchState,
151-
bool verbose,
152-
) {
153-
return description.add('does not exist');
154-
}
155-
}
77+
Matcher? _fileExceptionWrapMatcher(dynamic osErrorCode) =>
78+
(osErrorCode == null || ignoreOsErrorCodes)
79+
? anything
80+
: wrapMatcher(osErrorCode);

pkgs/file_testing/pubspec.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: file_testing
2-
version: 3.0.2
2+
version: 3.1.0-wip
33
description: Testing utilities for package:file.
44
repository: https://github.com/dart-lang/tools/tree/main/pkgs/file_testing
55
issue_tracker: https://github.com/dart-lang/tools/issues?q=is%3Aissue+is%3Aopen+label%3Apackage%3Afile_testing
@@ -10,5 +10,5 @@ environment:
1010
dependencies:
1111
test: ^1.23.1
1212

13-
dev_dependencies:
14-
lints: ^5.0.0
13+
dev_dependencies:
14+
dart_flutter_team_lints: ^3.0.0

0 commit comments

Comments
 (0)