Chromium Code Reviews| Index: pkg/scheduled_test/lib/src/descriptor/file.dart |
| diff --git a/pkg/scheduled_test/lib/src/descriptor/file.dart b/pkg/scheduled_test/lib/src/descriptor/file.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..98a002b1486dac758741029b7a8f52758bc70811 |
| --- /dev/null |
| +++ b/pkg/scheduled_test/lib/src/descriptor/file.dart |
| @@ -0,0 +1,78 @@ |
| +// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +library descriptor.file; |
| + |
| +import 'dart:async'; |
| +import 'dart:io' as io; |
| +import 'dart:utf'; |
| + |
| +import 'package:path/path.dart' as path; |
| + |
| +import '../../descriptor.dart' as descriptor; |
| +import '../../scheduled_test.dart'; |
| +import '../utils.dart'; |
| +import 'utils.dart'; |
| + |
| +/// A descriptor describing a single file. |
| +class File extends descriptor.Entry { |
| + /// Whether this descriptor describes a binary file. This is only used when |
| + /// displaying error messages. |
| + final bool binary; |
| + |
| + /// The contents of the file, in bytes. |
| + final List<int> contents; |
| + |
| + /// The contents of the file as a String. Assumes UTF-8 encoding. |
| + String get textContents => new String.fromCharCodes(contents); |
| + |
| + 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.
|
| + : this._(name, contents, true); |
| + |
| + File(Pattern name, String contents) |
| + : this._(name, encodeUtf8(contents), false); |
| + |
| + File._(Pattern name, this.contents, this.binary) |
| + : super(name); |
| + |
| + Future create([String parent]) => schedule(() { |
| + if (parent == null) parent = descriptor.defaultRoot; |
| + return new io.File(path.join(parent, stringName)).writeAsBytes(contents); |
| + }, 'creating file $nameDescription'); |
| + |
| + Future validate([String parent]) => schedule(() { |
| + if (parent == null) parent = descriptor.defaultRoot; |
| + var fullPath = entryMatchingPattern(parent, name); |
| + return new io.File(fullPath).readAsBytes() |
| + .then((actualContents) { |
| + if (orderedIterableEquals(contents, actualContents)) return; |
| + if (binary) { |
| + throw "File $nameDescription didn't contain the expected binary " |
| + "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.
|
| + } |
| + var description = nameDescription; |
| + if (name is! String) { |
| + description = "'${path.basename(fullPath)}' (matching $description)"; |
| + } |
| + throw "File $description should contain:\n" |
| + "${prefixLines(textContents)}\n" |
| + "but actually contained:\n" |
| + "${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.
|
| + }); |
| + }, 'validating file $nameDescription'); |
| + |
| + Stream<List<int>> load([String pathToLoad]) { |
| + if (pathToLoad != null) { |
| + return errorStream("Can't load '$pathToLoad' from within " |
| + "$nameDescription: not a directory."); |
| + } |
| + |
| + return new Future.immediate(contents).asStream(); |
| + } |
| + |
| + String describe() { |
| + if (name is String) return name; |
| + return 'file matching $nameDescription'; |
| + } |
| +} |