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 import 'dart:async'; |
| 6 import 'dart:io'; |
| 7 |
| 8 import 'package:pathos/path.dart' as path; |
| 9 import 'package:scheduled_test/descriptor.dart' as d; |
| 10 import 'package:scheduled_test/scheduled_test.dart'; |
| 11 |
| 12 import '../metatest.dart'; |
| 13 import 'utils.dart'; |
| 14 |
| 15 void main() { |
| 16 setUpTimeout(); |
| 17 |
| 18 expectTestsPass('file().create() creates a file', () { |
| 19 test('test', () { |
| 20 scheduleSandbox(); |
| 21 |
| 22 d.file('name.txt', 'contents').create(); |
| 23 |
| 24 schedule(() { |
| 25 expect(new File(path.join(sandbox, 'name.txt')).readAsString(), |
| 26 completion(equals('contents'))); |
| 27 }); |
| 28 }); |
| 29 }); |
| 30 |
| 31 expectTestsPass('file().create() overwrites an existing file', () { |
| 32 test('test', () { |
| 33 scheduleSandbox(); |
| 34 |
| 35 d.file('name.txt', 'contents1').create(); |
| 36 |
| 37 d.file('name.txt', 'contents2').create(); |
| 38 |
| 39 schedule(() { |
| 40 expect(new File(path.join(sandbox, 'name.txt')).readAsString(), |
| 41 completion(equals('contents2'))); |
| 42 }); |
| 43 }); |
| 44 }); |
| 45 |
| 46 expectTestsPass('file().validate() completes successfully if the filesystem ' |
| 47 'matches the descriptor', () { |
| 48 test('test', () { |
| 49 scheduleSandbox(); |
| 50 |
| 51 schedule(() { |
| 52 return new File(path.join(sandbox, 'name.txt')) |
| 53 .writeAsString('contents'); |
| 54 }); |
| 55 |
| 56 d.file('name.txt', 'contents').validate(); |
| 57 }); |
| 58 }); |
| 59 |
| 60 expectTestsPass("file().validate() fails if there's a file with the wrong " |
| 61 "contents", () { |
| 62 var errors; |
| 63 test('test 1', () { |
| 64 scheduleSandbox(); |
| 65 |
| 66 currentSchedule.onException.schedule(() { |
| 67 errors = currentSchedule.errors; |
| 68 }); |
| 69 |
| 70 schedule(() { |
| 71 return new File(path.join(sandbox, 'name.txt')) |
| 72 .writeAsString('wrongtents'); |
| 73 }); |
| 74 |
| 75 d.file('name.txt', 'contents').validate(); |
| 76 }); |
| 77 |
| 78 test('test 2', () { |
| 79 expect(errors, everyElement(new isInstanceOf<ScheduleError>())); |
| 80 expect(errors.map((e) => e.error), equals([ |
| 81 "File 'name.txt' should contain:\n" |
| 82 "| contents\n" |
| 83 "but actually contained:\n" |
| 84 "X wrongtents" |
| 85 ]), verbose: true); |
| 86 }); |
| 87 }, passing: ['test 2']); |
| 88 |
| 89 expectTestsPass("file().validate() fails if there's no file", () { |
| 90 var errors; |
| 91 test('test 1', () { |
| 92 scheduleSandbox(); |
| 93 |
| 94 currentSchedule.onException.schedule(() { |
| 95 errors = currentSchedule.errors; |
| 96 }); |
| 97 |
| 98 d.file('name.txt', 'contents').validate(); |
| 99 }); |
| 100 |
| 101 test('test 2', () { |
| 102 expect(errors, everyElement(new isInstanceOf<ScheduleError>())); |
| 103 expect(errors.length, equals(1)); |
| 104 expect(errors.first.error, |
| 105 matches(r"^File not found: '[^']+[\\/]name\.txt'\.$")); |
| 106 }); |
| 107 }, passing: ['test 2']); |
| 108 |
| 109 expectTestsPass("file().read() returns the contents of the file as a stream", |
| 110 () { |
| 111 test('test', () { |
| 112 expect(byteStreamToString(d.file('name.txt', 'contents').read()), |
| 113 completion(equals('contents'))); |
| 114 }); |
| 115 }); |
| 116 |
| 117 expectTestsPass("file().load() throws an error", () { |
| 118 test('test', () { |
| 119 expect(d.file('name.txt', 'contents').load('path').toList(), |
| 120 throwsA(equals("Can't load 'path' from within 'name.txt': not a " |
| 121 "directory."))); |
| 122 }); |
| 123 }); |
| 124 |
| 125 expectTestsPass("file().describe() returns the filename", () { |
| 126 test('test', () { |
| 127 expect(d.file('name.txt', 'contents').describe(), equals('name.txt')); |
| 128 }); |
| 129 }); |
| 130 |
| 131 expectTestsPass('binaryFile().create() creates a file', () { |
| 132 test('test', () { |
| 133 scheduleSandbox(); |
| 134 |
| 135 d.binaryFile('name.bin', [1, 2, 3, 4, 5]).create(); |
| 136 |
| 137 schedule(() { |
| 138 expect(new File(path.join(sandbox, 'name.bin')).readAsBytes(), |
| 139 completion(equals([1, 2, 3, 4, 5]))); |
| 140 }); |
| 141 }); |
| 142 }); |
| 143 |
| 144 expectTestsPass('binaryFile().validate() completes successfully if the ' |
| 145 'filesystem matches the descriptor', () { |
| 146 test('test', () { |
| 147 scheduleSandbox(); |
| 148 |
| 149 schedule(() { |
| 150 return new File(path.join(sandbox, 'name.bin')) |
| 151 .writeAsBytes([1, 2, 3, 4, 5]); |
| 152 }); |
| 153 |
| 154 d.binaryFile('name.bin', [1, 2, 3, 4, 5]).validate(); |
| 155 }); |
| 156 }); |
| 157 |
| 158 expectTestsPass("binaryFile().validate() fails if there's a file with the " |
| 159 "wrong contents", () { |
| 160 var errors; |
| 161 test('test 1', () { |
| 162 scheduleSandbox(); |
| 163 |
| 164 currentSchedule.onException.schedule(() { |
| 165 errors = currentSchedule.errors; |
| 166 }); |
| 167 |
| 168 schedule(() { |
| 169 return new File(path.join(sandbox, 'name.bin')) |
| 170 .writeAsBytes([2, 4, 6, 8, 10]); |
| 171 }); |
| 172 |
| 173 d.binaryFile('name.bin', [1, 2, 3, 4, 5]).validate(); |
| 174 }); |
| 175 |
| 176 test('test 2', () { |
| 177 expect(errors, everyElement(new isInstanceOf<ScheduleError>())); |
| 178 expect(errors.map((e) => e.error), equals([ |
| 179 "File 'name.bin' didn't contain the expected binary data." |
| 180 ]), verbose: true); |
| 181 }); |
| 182 }, passing: ['test 2']); |
| 183 } |
OLD | NEW |