| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library descriptor.file; | 5 library descriptor.file; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:io'; | 8 import 'dart:io'; |
| 9 import 'dart:math' as math; | 9 import 'dart:math' as math; |
| 10 import 'dart:utf'; | 10 import 'dart:utf'; |
| 11 | 11 |
| 12 import 'package:pathos/path.dart' as path; | 12 import 'package:pathos/path.dart' as path; |
| 13 | 13 |
| 14 import '../../descriptor.dart'; | 14 import '../../descriptor.dart'; |
| 15 import '../../scheduled_test.dart'; | 15 import '../../scheduled_test.dart'; |
| 16 import '../utils.dart'; | 16 import '../utils.dart'; |
| 17 | 17 |
| 18 /// A descriptor describing a single file. | 18 /// A descriptor describing a single file. |
| 19 class FileDescriptor extends Descriptor { | 19 class FileDescriptor extends Descriptor { |
| 20 /// Whether this descriptor describes a binary file. This is only used when | |
| 21 /// displaying error messages. | |
| 22 final bool isBinary; | |
| 23 | |
| 24 /// The contents of the file, in bytes. | 20 /// The contents of the file, in bytes. |
| 25 final List<int> contents; | 21 final List<int> contents; |
| 26 | 22 |
| 27 /// The contents of the file as a String. Assumes UTF-8 encoding. | 23 /// The contents of the file as a String. Assumes UTF-8 encoding. |
| 28 String get textContents => new String.fromCharCodes(contents); | 24 String get textContents => new String.fromCharCodes(contents); |
| 29 | 25 |
| 30 FileDescriptor.binary(String name, List<int> contents) | 26 /// Creates a new text [FileDescriptor] with [name] that matches its String |
| 31 : this._(name, contents, true); | 27 /// contents against [matcher]. If the file is created, it's considered to be |
| 28 /// empty. |
| 29 factory FileDescriptor.matcher(String name, Matcher matcher) => |
| 30 new _MatcherFileDescriptor(name, matcher, isBinary: false); |
| 32 | 31 |
| 33 FileDescriptor(String name, String contents) | 32 /// Creates a new binary [FileDescriptor] with [name] that matches its binary |
| 34 : this._(name, encodeUtf8(contents), false); | 33 /// contents against [matcher]. If the file is created, it's considered to be |
| 34 /// empty. |
| 35 factory FileDescriptor.binaryMatcher(String name, Matcher matcher) => |
| 36 new _MatcherFileDescriptor(name, matcher, isBinary: true); |
| 35 | 37 |
| 36 FileDescriptor._(String name, this.contents, this.isBinary) | 38 /// Creates a new binary [FileDescriptor] descriptor with [name] and |
| 39 /// [contents]. |
| 40 factory FileDescriptor.binary(String name, List<int> contents) => |
| 41 new _BinaryFileDescriptor(name, contents); |
| 42 |
| 43 /// Creates a new text [FileDescriptor] with [name] and [contents]. |
| 44 factory FileDescriptor(String name, String contents) => |
| 45 new _StringFileDescriptor(name, contents); |
| 46 |
| 47 FileDescriptor._(String name, this.contents) |
| 37 : super(name); | 48 : super(name); |
| 38 | 49 |
| 39 Future create([String parent]) => schedule(() { | 50 Future create([String parent]) => schedule(() { |
| 40 if (parent == null) parent = defaultRoot; | 51 if (parent == null) parent = defaultRoot; |
| 41 return new File(path.join(parent, name)).writeAsBytes(contents); | 52 return new File(path.join(parent, name)).writeAsBytes(contents); |
| 42 }, "creating file '$name'"); | 53 }, "creating file '$name'"); |
| 43 | 54 |
| 44 Future validate([String parent]) => | 55 Future validate([String parent]) => |
| 45 schedule(() => validateNow(parent), "validating file '$name'"); | 56 schedule(() => validateNow(parent), "validating file '$name'"); |
| 46 | 57 |
| 47 Future validateNow([String parent]) { | 58 Future validateNow([String parent]) { |
| 48 if (parent == null) parent = defaultRoot; | 59 if (parent == null) parent = defaultRoot; |
| 49 var fullPath = path.join(parent, name); | 60 var fullPath = path.join(parent, name); |
| 50 if (!new File(fullPath).existsSync()) { | 61 if (!new File(fullPath).existsSync()) { |
| 51 throw "File not found: '$fullPath'."; | 62 throw "File not found: '$fullPath'."; |
| 52 } | 63 } |
| 53 | 64 |
| 54 return new File(fullPath).readAsBytes() | 65 return new File(fullPath).readAsBytes().then(_validateNow); |
| 55 .then((actualContents) { | |
| 56 if (orderedIterableEquals(contents, actualContents)) return; | |
| 57 if (isBinary) { | |
| 58 // TODO(nweiz): show a hex dump here if the data is small enough. | |
| 59 throw "File '$name' didn't contain the expected binary data."; | |
| 60 } | |
| 61 throw _textMismatchMessage(textContents, | |
| 62 new String.fromCharCodes(actualContents)); | |
| 63 }); | |
| 64 } | 66 } |
| 67 |
| 68 // TODO(nweiz): rather than setting up an inheritance chain, just store a |
| 69 // Matcher for validation. This would require better error messages from the |
| 70 // matcher library, though. |
| 71 /// A function that throws an error if [binaryContents] doesn't match the |
| 72 /// expected contents of the descriptor. |
| 73 void _validateNow(List<int> binaryContents); |
| 65 | 74 |
| 66 Stream<List<int>> read() => new Future.value(contents).asStream(); | 75 Stream<List<int>> read() => new Future.value(contents).asStream(); |
| 67 | 76 |
| 68 String describe() => name; | 77 String describe() => name; |
| 78 } |
| 79 |
| 80 class _BinaryFileDescriptor extends FileDescriptor { |
| 81 _BinaryFileDescriptor(String name, List<int> contents) |
| 82 : super._(name, contents); |
| 83 |
| 84 Future _validateNow(List<int> actualContents) { |
| 85 if (orderedIterableEquals(contents, actualContents)) return; |
| 86 // TODO(nweiz): show a hex dump here if the data is small enough. |
| 87 throw "File '$name' didn't contain the expected binary data."; |
| 88 } |
| 89 } |
| 90 |
| 91 class _StringFileDescriptor extends FileDescriptor { |
| 92 _StringFileDescriptor(String name, String contents) |
| 93 : super._(name, encodeUtf8(contents)); |
| 94 |
| 95 Future _validateNow(List<int> actualContents) { |
| 96 if (orderedIterableEquals(contents, actualContents)) return; |
| 97 throw _textMismatchMessage(textContents, |
| 98 new String.fromCharCodes(actualContents)); |
| 99 } |
| 69 | 100 |
| 70 String _textMismatchMessage(String expected, String actual) { | 101 String _textMismatchMessage(String expected, String actual) { |
| 71 final expectedLines = expected.split('\n'); | 102 final expectedLines = expected.split('\n'); |
| 72 final actualLines = actual.split('\n'); | 103 final actualLines = actual.split('\n'); |
| 73 | 104 |
| 74 var results = []; | 105 var results = []; |
| 75 | 106 |
| 76 // Compare them line by line to see which ones match. | 107 // Compare them line by line to see which ones match. |
| 77 var length = math.max(expectedLines.length, actualLines.length); | 108 var length = math.max(expectedLines.length, actualLines.length); |
| 78 for (var i = 0; i < length; i++) { | 109 for (var i = 0; i < length; i++) { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 95 } | 126 } |
| 96 } | 127 } |
| 97 } | 128 } |
| 98 | 129 |
| 99 return "File '$name' should contain:\n" | 130 return "File '$name' should contain:\n" |
| 100 "${prefixLines(expected)}\n" | 131 "${prefixLines(expected)}\n" |
| 101 "but actually contained:\n" | 132 "but actually contained:\n" |
| 102 "${results.join('\n')}"; | 133 "${results.join('\n')}"; |
| 103 } | 134 } |
| 104 } | 135 } |
| 136 |
| 137 class _MatcherFileDescriptor extends FileDescriptor { |
| 138 final Matcher _matcher; |
| 139 final bool _isBinary; |
| 140 |
| 141 _MatcherFileDescriptor(String name, this._matcher, {bool isBinary}) |
| 142 : _isBinary = isBinary == true ? true : false, |
| 143 super._(name, <int>[]); |
| 144 |
| 145 void _validateNow(List<int> actualContents) => |
| 146 expect( |
| 147 _isBinary ? actualContents : new String.fromCharCodes(actualContents), |
| 148 _matcher); |
| 149 } |
| OLD | NEW |