Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(449)

Side by Side Diff: pkg/scheduled_test/lib/src/descriptor/file.dart

Issue 12853005: Change the way Patterns work in scheduled_test/descriptor. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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' as io; 8 import 'dart:io' as 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 '../../../../../pkg/pathos/lib/path.dart' as path; 12 import '../../../../../pkg/pathos/lib/path.dart' as path;
13 13
14 import '../../descriptor.dart' as descriptor; 14 import '../../descriptor.dart' as descriptor;
15 import '../../scheduled_test.dart'; 15 import '../../scheduled_test.dart';
16 import '../utils.dart'; 16 import '../utils.dart';
17 import 'utils.dart';
18 17
19 /// A descriptor describing a single file. 18 /// A descriptor describing a single file.
20 class File extends descriptor.Entry { 19 class File extends descriptor.Entry {
21 /// Whether this descriptor describes a binary file. This is only used when 20 /// Whether this descriptor describes a binary file. This is only used when
22 /// displaying error messages. 21 /// displaying error messages.
23 final bool isBinary; 22 final bool isBinary;
24 23
25 /// The contents of the file, in bytes. 24 /// The contents of the file, in bytes.
26 final List<int> contents; 25 final List<int> contents;
27 26
28 /// The contents of the file as a String. Assumes UTF-8 encoding. 27 /// The contents of the file as a String. Assumes UTF-8 encoding.
29 String get textContents => new String.fromCharCodes(contents); 28 String get textContents => new String.fromCharCodes(contents);
30 29
31 File.binary(Pattern name, List<int> contents) 30 File.binary(String name, List<int> contents)
32 : this._(name, contents, true); 31 : this._(name, contents, true);
33 32
34 File(Pattern name, String contents) 33 File(String name, String contents)
35 : this._(name, encodeUtf8(contents), false); 34 : this._(name, encodeUtf8(contents), false);
36 35
37 File._(Pattern name, this.contents, this.isBinary) 36 File._(String name, this.contents, this.isBinary)
38 : super(name); 37 : super(name);
39 38
40 Future create([String parent]) => schedule(() { 39 Future create([String parent]) => schedule(() {
41 if (parent == null) parent = descriptor.defaultRoot; 40 if (parent == null) parent = descriptor.defaultRoot;
42 return new io.File(path.join(parent, stringName)).writeAsBytes(contents); 41 return new io.File(path.join(parent, name)).writeAsBytes(contents);
43 }, 'creating file $nameDescription'); 42 }, "creating file '$name'");
44 43
45 Future validate([String parent]) => schedule(() { 44 Future validate([String parent]) =>
45 schedule(() => validateNow(parent), "validating file '$name'");
46
47 Future validateNow([String parent]) {
46 if (parent == null) parent = descriptor.defaultRoot; 48 if (parent == null) parent = descriptor.defaultRoot;
47 var fullPath = entryMatchingPattern('File', parent, name); 49 var fullPath = path.join(parent, name);
50 if (!new io.File(fullPath).existsSync()) {
51 throw "File not found: '$fullPath'.";
52 }
53
48 return new io.File(fullPath).readAsBytes() 54 return new io.File(fullPath).readAsBytes()
49 .then((actualContents) { 55 .then((actualContents) {
50 if (orderedIterableEquals(contents, actualContents)) return; 56 if (orderedIterableEquals(contents, actualContents)) return;
51 if (isBinary) { 57 if (isBinary) {
52 // TODO(nweiz): show a hex dump here if the data is small enough. 58 // TODO(nweiz): show a hex dump here if the data is small enough.
53 throw "File $nameDescription didn't contain the expected binary " 59 throw "File '$name' didn't contain the expected binary data.";
54 "data.";
55 } 60 }
56 var description = nameDescription; 61 throw _textMismatchMessage(textContents,
57 if (name is! String) { 62 new String.fromCharCodes(actualContents));
58 description = "'${path.basename(fullPath)}' (matching $description)";
59 }
60 throw _textMismatchMessage(description, textContents,
61 new String.fromCharCodes(actualContents));;
62 }); 63 });
63 }, 'validating file $nameDescription'); 64 }
64 65
65 Stream<List<int>> read() => new Future.immediate(contents).asStream(); 66 Stream<List<int>> read() => new Future.immediate(contents).asStream();
66 67
67 String describe() { 68 String describe() => name;
68 if (name is String) return name; 69
69 return 'file matching $nameDescription'; 70 String _textMismatchMessage(String expected, String actual) {
71 final expectedLines = expected.split('\n');
72 final actualLines = actual.split('\n');
73
74 var results = [];
75
76 // Compare them line by line to see which ones match.
77 var length = math.max(expectedLines.length, actualLines.length);
78 for (var i = 0; i < length; i++) {
79 if (i >= actualLines.length) {
80 // Missing output.
81 results.add('? ${expectedLines[i]}');
82 } else if (i >= expectedLines.length) {
83 // Unexpected extra output.
84 results.add('X ${actualLines[i]}');
85 } else {
86 var expectedLine = expectedLines[i];
87 var actualLine = actualLines[i];
88
89 if (expectedLine != actualLine) {
90 // Mismatched lines.
91 results.add('X $actualLine');
92 } else {
93 // Matched lines.
94 results.add('| $actualLine');
95 }
96 }
97 }
98
99 return "File '$name' should contain:\n"
100 "${prefixLines(expected)}\n"
Bob Nystrom 2013/03/15 21:58:46 Indent +2.
nweiz 2013/03/15 23:17:21 Done.
101 "but actually contained:\n"
102 "${results.join('\n')}";
70 } 103 }
71 } 104 }
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 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698