OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library descriptor.file; |
| 6 |
| 7 import 'dart:async'; |
| 8 import 'dart:io' as io; |
| 9 import 'dart:math' as math; |
| 10 import 'dart:utf'; |
| 11 |
| 12 import 'package:pathos/path.dart' as path; |
| 13 |
| 14 import '../../descriptor.dart' as descriptor; |
| 15 import '../../scheduled_test.dart'; |
| 16 import '../utils.dart'; |
| 17 import 'utils.dart'; |
| 18 |
| 19 /// A descriptor describing a single file. |
| 20 class File extends descriptor.Entry { |
| 21 /// Whether this descriptor describes a binary file. This is only used when |
| 22 /// displaying error messages. |
| 23 final bool isBinary; |
| 24 |
| 25 /// The contents of the file, in bytes. |
| 26 final List<int> contents; |
| 27 |
| 28 /// The contents of the file as a String. Assumes UTF-8 encoding. |
| 29 String get textContents => new String.fromCharCodes(contents); |
| 30 |
| 31 File.binary(Pattern name, List<int> contents) |
| 32 : this._(name, contents, true); |
| 33 |
| 34 File(Pattern name, String contents) |
| 35 : this._(name, encodeUtf8(contents), false); |
| 36 |
| 37 File._(Pattern name, this.contents, this.isBinary) |
| 38 : super(name); |
| 39 |
| 40 Future create([String parent]) => schedule(() { |
| 41 if (parent == null) parent = descriptor.defaultRoot; |
| 42 return new io.File(path.join(parent, stringName)).writeAsBytes(contents); |
| 43 }, 'creating file $nameDescription'); |
| 44 |
| 45 Future validate([String parent]) => schedule(() { |
| 46 if (parent == null) parent = descriptor.defaultRoot; |
| 47 var fullPath = entryMatchingPattern('File', parent, name); |
| 48 return new io.File(fullPath).readAsBytes() |
| 49 .then((actualContents) { |
| 50 if (orderedIterableEquals(contents, actualContents)) return; |
| 51 if (isBinary) { |
| 52 // TODO(nweiz): show a hex dump here if the data is small enough. |
| 53 throw "File $nameDescription didn't contain the expected binary " |
| 54 "data."; |
| 55 } |
| 56 var description = nameDescription; |
| 57 if (name is! String) { |
| 58 description = "'${path.basename(fullPath)}' (matching $description)"; |
| 59 } |
| 60 throw _textMismatchMessage(description, textContents, |
| 61 new String.fromCharCodes(actualContents));; |
| 62 }); |
| 63 }, 'validating file $nameDescription'); |
| 64 |
| 65 Stream<List<int>> read() => new Future.immediate(contents).asStream(); |
| 66 |
| 67 String describe() { |
| 68 if (name is String) return name; |
| 69 return 'file matching $nameDescription'; |
| 70 } |
| 71 } |
| 72 |
| 73 String _textMismatchMessage(String description, String expected, |
| 74 String actual) { |
| 75 final expectedLines = expected.split('\n'); |
| 76 final actualLines = actual.split('\n'); |
| 77 |
| 78 var results = []; |
| 79 |
| 80 // Compare them line by line to see which ones match. |
| 81 var length = math.max(expectedLines.length, actualLines.length); |
| 82 for (var i = 0; i < length; i++) { |
| 83 if (i >= actualLines.length) { |
| 84 // Missing output. |
| 85 results.add('? ${expectedLines[i]}'); |
| 86 } else if (i >= expectedLines.length) { |
| 87 // Unexpected extra output. |
| 88 results.add('X ${actualLines[i]}'); |
| 89 } else { |
| 90 var expectedLine = expectedLines[i]; |
| 91 var actualLine = actualLines[i]; |
| 92 |
| 93 if (expectedLine != actualLine) { |
| 94 // Mismatched lines. |
| 95 results.add('X $actualLine'); |
| 96 } else { |
| 97 // Matched lines. |
| 98 results.add('| $actualLine'); |
| 99 } |
| 100 } |
| 101 } |
| 102 |
| 103 return "File $description should contain:\n" |
| 104 "${prefixLines(expected)}\n" |
| 105 "but actually contained:\n" |
| 106 "${results.join('\n')}"; |
| 107 } |
OLD | NEW |