Chromium Code Reviews| Index: pkg/scheduled_test/lib/src/descriptor/file_descriptor.dart |
| diff --git a/pkg/scheduled_test/lib/src/descriptor/file_descriptor.dart b/pkg/scheduled_test/lib/src/descriptor/file_descriptor.dart |
| index f56f720c7fdfdbe440ebb5b6ec2772a8b611df3d..3f6b1c7b47bddc9d33fa7106dcc3b32aea0d70d9 100644 |
| --- a/pkg/scheduled_test/lib/src/descriptor/file_descriptor.dart |
| +++ b/pkg/scheduled_test/lib/src/descriptor/file_descriptor.dart |
| @@ -17,23 +17,25 @@ import '../utils.dart'; |
| /// A descriptor describing a single file. |
| class FileDescriptor extends Descriptor { |
| - /// Whether this descriptor describes a binary file. This is only used when |
| - /// displaying error messages. |
| - final bool isBinary; |
| - |
| /// The contents of the file, in bytes. |
| final List<int> contents; |
| /// The contents of the file as a String. Assumes UTF-8 encoding. |
| String get textContents => new String.fromCharCodes(contents); |
| - FileDescriptor.binary(String name, List<int> contents) |
| - : this._(name, contents, true); |
| + factory FileDescriptor.matcher(String name, Matcher matcher) => |
|
Andrei Mouravski
2013/05/09 00:08:14
How about some doc comments here?
nweiz
2013/05/09 00:15:12
Done.
|
| + new _MatcherFileDescriptor(name, matcher, isBinary: false); |
| + |
| + factory FileDescriptor.binaryMatcher(String name, Matcher matcher) => |
| + new _MatcherFileDescriptor(name, matcher, isBinary: true); |
| - FileDescriptor(String name, String contents) |
| - : this._(name, encodeUtf8(contents), false); |
| + factory FileDescriptor.binary(String name, List<int> contents) => |
| + new _BinaryFileDescriptor(name, contents); |
| - FileDescriptor._(String name, this.contents, this.isBinary) |
| + factory FileDescriptor(String name, String contents) => |
| + new _StringFileDescriptor(name, contents); |
| + |
| + FileDescriptor._(String name, this.contents) |
| : super(name); |
| Future create([String parent]) => schedule(() { |
| @@ -51,21 +53,41 @@ class FileDescriptor extends Descriptor { |
| throw "File not found: '$fullPath'."; |
| } |
| - return new File(fullPath).readAsBytes() |
| - .then((actualContents) { |
| - if (orderedIterableEquals(contents, actualContents)) return; |
| - if (isBinary) { |
| - // TODO(nweiz): show a hex dump here if the data is small enough. |
| - throw "File '$name' didn't contain the expected binary data."; |
| - } |
| - throw _textMismatchMessage(textContents, |
| - new String.fromCharCodes(actualContents)); |
| - }); |
| + return new File(fullPath).readAsBytes().then(_validateNow); |
| } |
| + |
| + // TODO(nweiz): rather than setting up an inheritance chain, just store a |
| + // Matcher for validation. This would require better error messages from the |
| + // matcher library, though. |
| + /// A function that throws an error if [binaryContents] doesn't match the |
| + /// expected contents of the descriptor. |
| + void _validateNow(List<int> binaryContents); |
| Stream<List<int>> read() => new Future.value(contents).asStream(); |
| String describe() => name; |
| +} |
| + |
| +class _BinaryFileDescriptor extends FileDescriptor { |
| + _BinaryFileDescriptor(String name, List<int> contents) |
| + : super._(name, contents); |
| + |
| + Future _validateNow(List<int> actualContents) { |
| + if (orderedIterableEquals(contents, actualContents)) return; |
| + // TODO(nweiz): show a hex dump here if the data is small enough. |
| + throw "File '$name' didn't contain the expected binary data."; |
| + } |
| +} |
| + |
| +class _StringFileDescriptor extends FileDescriptor { |
| + _StringFileDescriptor(String name, String contents) |
| + : super._(name, encodeUtf8(contents)); |
| + |
| + Future _validateNow(List<int> actualContents) { |
| + if (orderedIterableEquals(contents, actualContents)) return; |
| + throw _textMismatchMessage(textContents, |
| + new String.fromCharCodes(actualContents)); |
| + } |
| String _textMismatchMessage(String expected, String actual) { |
| final expectedLines = expected.split('\n'); |
| @@ -102,3 +124,17 @@ class FileDescriptor extends Descriptor { |
| "${results.join('\n')}"; |
| } |
| } |
| + |
| +class _MatcherFileDescriptor extends FileDescriptor { |
| + final Matcher _matcher; |
| + final bool _isBinary; |
| + |
| + _MatcherFileDescriptor(String name, this._matcher, {bool isBinary}) |
| + : _isBinary = isBinary == true ? true : false, |
| + super._(name, <int>[]); |
| + |
| + void _validateNow(List<int> actualContents) => |
| + expect( |
| + _isBinary ? actualContents : new String.fromCharCodes(actualContents), |
| + _matcher); |
| +} |