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

Side by Side Diff: pkg/scheduled_test/lib/src/descriptor/file_descriptor.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: Code review changes. 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
(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';
9 import 'dart:math' as math;
10 import 'dart:utf';
11
12 import '../../../../../pkg/pathos/lib/path.dart' as path;
13
14 import '../../descriptor.dart';
15 import '../../scheduled_test.dart';
16 import '../utils.dart';
17
18 /// A descriptor describing a single file.
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.
25 final List<int> contents;
26
27 /// The contents of the file as a String. Assumes UTF-8 encoding.
28 String get textContents => new String.fromCharCodes(contents);
29
30 FileDescriptor.binary(String name, List<int> contents)
31 : this._(name, contents, true);
32
33 FileDescriptor(String name, String contents)
34 : this._(name, encodeUtf8(contents), false);
35
36 FileDescriptor._(String name, this.contents, this.isBinary)
37 : super(name);
38
39 Future create([String parent]) => schedule(() {
40 if (parent == null) parent = defaultRoot;
41 return new File(path.join(parent, name)).writeAsBytes(contents);
42 }, "creating file '$name'");
43
44 Future validate([String parent]) =>
45 schedule(() => validateNow(parent), "validating file '$name'");
46
47 Future validateNow([String parent]) {
48 if (parent == null) parent = defaultRoot;
49 var fullPath = path.join(parent, name);
50 if (!new File(fullPath).existsSync()) {
51 throw "File not found: '$fullPath'.";
52 }
53
54 return new File(fullPath).readAsBytes()
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 }
65
66 Stream<List<int>> read() => new Future.immediate(contents).asStream();
67
68 String describe() => name;
69
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"
101 "but actually contained:\n"
102 "${results.join('\n')}";
103 }
104 }
OLDNEW
« no previous file with comments | « pkg/scheduled_test/lib/src/descriptor/file.dart ('k') | pkg/scheduled_test/lib/src/descriptor/nothing.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698