2
2
// for details. All rights reserved. Use of this source code is governed by a
3
3
// BSD-style license that can be found in the LICENSE file.
4
4
5
+ // ignore_for_file: comment_references
6
+
5
7
import 'dart:io' ;
6
8
7
9
import 'package:test/test.dart' ;
8
10
9
11
import 'internal.dart' ;
10
12
11
13
/// Matcher that successfully matches against any instance of [Directory] .
12
- const Matcher isDirectory = TypeMatcher <Directory >();
14
+ const isDirectory = TypeMatcher <Directory >();
13
15
14
16
/// Matcher that successfully matches against any instance of [File] .
15
- const Matcher isFile = TypeMatcher <File >();
17
+ const isFile = TypeMatcher <File >();
16
18
17
19
/// Matcher that successfully matches against any instance of [Link] .
18
- const Matcher isLink = TypeMatcher <Link >();
20
+ const isLink = TypeMatcher <Link >();
19
21
20
22
/// Matcher that successfully matches against any instance of
21
23
/// [FileSystemEntity] .
22
- const Matcher isFileSystemEntity = TypeMatcher <FileSystemEntity >();
24
+ const isFileSystemEntity = TypeMatcher <FileSystemEntity >();
23
25
24
26
/// Matcher that successfully matches against any instance of [FileStat] .
25
- const Matcher isFileStat = TypeMatcher <FileStat >();
27
+ const isFileStat = TypeMatcher <FileStat >();
26
28
27
29
/// Returns a [Matcher] that matches [path] against an entity's path.
28
30
///
29
31
/// [path] may be a String, a predicate function, or a [Matcher] . If it is
30
32
/// 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);
32
35
33
36
/// Returns a [Matcher] that successfully matches against an instance of
34
37
/// [FileSystemException] .
@@ -39,7 +42,8 @@ Matcher hasPath(dynamic path) => _HasPath(path);
39
42
/// [osErrorCode] may be an `int` , a predicate function, or a [Matcher] . If it
40
43
/// is an `int` , it will be wrapped in an equality matcher.
41
44
Matcher isFileSystemException ([dynamic osErrorCode]) =>
42
- _FileSystemException (osErrorCode);
45
+ const TypeMatcher <FileSystemException >().having ((e) => e.osError? .errorCode,
46
+ 'osError.errorCode' , _fileExceptionWrapMatcher (osErrorCode));
43
47
44
48
/// Returns a matcher that successfully matches against a future or function
45
49
/// that throws a [FileSystemException] .
@@ -67,89 +71,10 @@ void expectFileSystemException(dynamic osErrorCode, void Function() callback) {
67
71
68
72
/// Matcher that successfully matches against a [FileSystemEntity] that
69
73
/// 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 );
109
76
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);
0 commit comments