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:utf'; | |
10 | |
11 import 'package:path/path.dart' as path; | |
12 | |
13 import '../../descriptor.dart' as descriptor; | |
14 import '../../scheduled_test.dart'; | |
15 import '../utils.dart'; | |
16 import 'utils.dart'; | |
17 | |
18 /// A descriptor describing a single file. | |
19 class File extends descriptor.Entry { | |
20 /// Whether this descriptor describes a binary file. This is only used when | |
21 /// displaying error messages. | |
22 final bool binary; | |
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 File.bytes(Pattern name, List<int> contents) | |
Bob Nystrom
2013/02/22 17:58:21
bytes -> binary
The rest of the package seems to
nweiz
2013/02/22 20:57:20
Done.
| |
31 : this._(name, contents, true); | |
32 | |
33 File(Pattern name, String contents) | |
34 : this._(name, encodeUtf8(contents), false); | |
35 | |
36 File._(Pattern name, this.contents, this.binary) | |
37 : super(name); | |
38 | |
39 Future create([String parent]) => schedule(() { | |
40 if (parent == null) parent = descriptor.defaultRoot; | |
41 return new io.File(path.join(parent, stringName)).writeAsBytes(contents); | |
42 }, 'creating file $nameDescription'); | |
43 | |
44 Future validate([String parent]) => schedule(() { | |
45 if (parent == null) parent = descriptor.defaultRoot; | |
46 var fullPath = entryMatchingPattern(parent, name); | |
47 return new io.File(fullPath).readAsBytes() | |
48 .then((actualContents) { | |
49 if (orderedIterableEquals(contents, actualContents)) return; | |
50 if (binary) { | |
51 throw "File $nameDescription didn't contain the expected binary " | |
52 "data."; | |
Bob Nystrom
2013/02/22 17:58:21
If the data is pretty small, it might be nice to s
nweiz
2013/02/22 20:57:20
I'll add a TODO.
| |
53 } | |
54 var description = nameDescription; | |
55 if (name is! String) { | |
56 description = "'${path.basename(fullPath)}' (matching $description)"; | |
57 } | |
58 throw "File $description should contain:\n" | |
59 "${prefixLines(textContents)}\n" | |
60 "but actually contained:\n" | |
61 "${prefixLines(new String.fromCharCodes(actualContents))}"; | |
Bob Nystrom
2013/02/22 17:58:21
Consider stealing the comparison code in test_pub
nweiz
2013/02/22 20:57:20
Done.
| |
62 }); | |
63 }, 'validating file $nameDescription'); | |
64 | |
65 Stream<List<int>> load([String pathToLoad]) { | |
66 if (pathToLoad != null) { | |
67 return errorStream("Can't load '$pathToLoad' from within " | |
68 "$nameDescription: not a directory."); | |
69 } | |
70 | |
71 return new Future.immediate(contents).asStream(); | |
72 } | |
73 | |
74 String describe() { | |
75 if (name is String) return name; | |
76 return 'file matching $nameDescription'; | |
77 } | |
78 } | |
OLD | NEW |