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

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

Issue 15074003: Add the ability to match the contents of a file descriptor against a Matcher. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 7 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'; 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 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.
31 : this._(name, contents, true); 27 new _MatcherFileDescriptor(name, matcher, isBinary: false);
32 28
33 FileDescriptor(String name, String contents) 29 factory FileDescriptor.binaryMatcher(String name, Matcher matcher) =>
34 : this._(name, encodeUtf8(contents), false); 30 new _MatcherFileDescriptor(name, matcher, isBinary: true);
35 31
36 FileDescriptor._(String name, this.contents, this.isBinary) 32 factory FileDescriptor.binary(String name, List<int> contents) =>
33 new _BinaryFileDescriptor(name, contents);
34
35 factory FileDescriptor(String name, String contents) =>
36 new _StringFileDescriptor(name, contents);
37
38 FileDescriptor._(String name, this.contents)
37 : super(name); 39 : super(name);
38 40
39 Future create([String parent]) => schedule(() { 41 Future create([String parent]) => schedule(() {
40 if (parent == null) parent = defaultRoot; 42 if (parent == null) parent = defaultRoot;
41 return new File(path.join(parent, name)).writeAsBytes(contents); 43 return new File(path.join(parent, name)).writeAsBytes(contents);
42 }, "creating file '$name'"); 44 }, "creating file '$name'");
43 45
44 Future validate([String parent]) => 46 Future validate([String parent]) =>
45 schedule(() => validateNow(parent), "validating file '$name'"); 47 schedule(() => validateNow(parent), "validating file '$name'");
46 48
47 Future validateNow([String parent]) { 49 Future validateNow([String parent]) {
48 if (parent == null) parent = defaultRoot; 50 if (parent == null) parent = defaultRoot;
49 var fullPath = path.join(parent, name); 51 var fullPath = path.join(parent, name);
50 if (!new File(fullPath).existsSync()) { 52 if (!new File(fullPath).existsSync()) {
51 throw "File not found: '$fullPath'."; 53 throw "File not found: '$fullPath'.";
52 } 54 }
53 55
54 return new File(fullPath).readAsBytes() 56 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 } 57 }
58
59 // TODO(nweiz): rather than setting up an inheritance chain, just store a
60 // Matcher for validation. This would require better error messages from the
61 // matcher library, though.
62 /// A function that throws an error if [binaryContents] doesn't match the
63 /// expected contents of the descriptor.
64 void _validateNow(List<int> binaryContents);
65 65
66 Stream<List<int>> read() => new Future.value(contents).asStream(); 66 Stream<List<int>> read() => new Future.value(contents).asStream();
67 67
68 String describe() => name; 68 String describe() => name;
69 }
70
71 class _BinaryFileDescriptor extends FileDescriptor {
72 _BinaryFileDescriptor(String name, List<int> contents)
73 : super._(name, contents);
74
75 Future _validateNow(List<int> actualContents) {
76 if (orderedIterableEquals(contents, actualContents)) return;
77 // TODO(nweiz): show a hex dump here if the data is small enough.
78 throw "File '$name' didn't contain the expected binary data.";
79 }
80 }
81
82 class _StringFileDescriptor extends FileDescriptor {
83 _StringFileDescriptor(String name, String contents)
84 : super._(name, encodeUtf8(contents));
85
86 Future _validateNow(List<int> actualContents) {
87 if (orderedIterableEquals(contents, actualContents)) return;
88 throw _textMismatchMessage(textContents,
89 new String.fromCharCodes(actualContents));
90 }
69 91
70 String _textMismatchMessage(String expected, String actual) { 92 String _textMismatchMessage(String expected, String actual) {
71 final expectedLines = expected.split('\n'); 93 final expectedLines = expected.split('\n');
72 final actualLines = actual.split('\n'); 94 final actualLines = actual.split('\n');
73 95
74 var results = []; 96 var results = [];
75 97
76 // Compare them line by line to see which ones match. 98 // Compare them line by line to see which ones match.
77 var length = math.max(expectedLines.length, actualLines.length); 99 var length = math.max(expectedLines.length, actualLines.length);
78 for (var i = 0; i < length; i++) { 100 for (var i = 0; i < length; i++) {
(...skipping 16 matching lines...) Expand all
95 } 117 }
96 } 118 }
97 } 119 }
98 120
99 return "File '$name' should contain:\n" 121 return "File '$name' should contain:\n"
100 "${prefixLines(expected)}\n" 122 "${prefixLines(expected)}\n"
101 "but actually contained:\n" 123 "but actually contained:\n"
102 "${results.join('\n')}"; 124 "${results.join('\n')}";
103 } 125 }
104 } 126 }
127
128 class _MatcherFileDescriptor extends FileDescriptor {
129 final Matcher _matcher;
130 final bool _isBinary;
131
132 _MatcherFileDescriptor(String name, this._matcher, {bool isBinary})
133 : _isBinary = isBinary == true ? true : false,
134 super._(name, <int>[]);
135
136 void _validateNow(List<int> actualContents) =>
137 expect(
138 _isBinary ? actualContents : new String.fromCharCodes(actualContents),
139 _matcher);
140 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698