| 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:io'; | |
| 6 | |
| 7 import 'package:path/path.dart' as path; | |
| 8 import 'package:scheduled_test/descriptor.dart' as d; | |
| 9 import 'package:scheduled_test/scheduled_test.dart'; | |
| 10 | |
| 11 import 'package:metatest/metatest.dart'; | |
| 12 import 'utils.dart'; | |
| 13 | |
| 14 void main() => initTests(_test); | |
| 15 | |
| 16 void _test(message) { | |
| 17 initMetatest(message); | |
| 18 | |
| 19 setUpTimeout(); | |
| 20 | |
| 21 expectTestsPass('file().create() creates a file', () { | |
| 22 test('test', () { | |
| 23 scheduleSandbox(); | |
| 24 | |
| 25 d.file('name.txt', 'contents').create(); | |
| 26 | |
| 27 schedule(() { | |
| 28 expect(new File(path.join(sandbox, 'name.txt')).readAsString(), | |
| 29 completion(equals('contents'))); | |
| 30 }); | |
| 31 }); | |
| 32 }); | |
| 33 | |
| 34 expectTestsPass('file().create() overwrites an existing file', () { | |
| 35 test('test', () { | |
| 36 scheduleSandbox(); | |
| 37 | |
| 38 d.file('name.txt', 'contents1').create(); | |
| 39 | |
| 40 d.file('name.txt', 'contents2').create(); | |
| 41 | |
| 42 schedule(() { | |
| 43 expect(new File(path.join(sandbox, 'name.txt')).readAsString(), | |
| 44 completion(equals('contents2'))); | |
| 45 }); | |
| 46 }); | |
| 47 }); | |
| 48 | |
| 49 expectTestsPass('file().validate() completes successfully if the filesystem ' | |
| 50 'matches the descriptor', () { | |
| 51 test('test', () { | |
| 52 scheduleSandbox(); | |
| 53 | |
| 54 schedule(() { | |
| 55 return new File(path.join(sandbox, 'name.txt')) | |
| 56 .writeAsString('contents'); | |
| 57 }); | |
| 58 | |
| 59 d.file('name.txt', 'contents').validate(); | |
| 60 }); | |
| 61 }); | |
| 62 | |
| 63 expectTestsPass("file().validate() fails if there's a file with the wrong " | |
| 64 "contents", () { | |
| 65 var errors; | |
| 66 test('test 1', () { | |
| 67 scheduleSandbox(); | |
| 68 | |
| 69 currentSchedule.onException.schedule(() { | |
| 70 errors = currentSchedule.errors; | |
| 71 }); | |
| 72 | |
| 73 schedule(() { | |
| 74 return new File(path.join(sandbox, 'name.txt')) | |
| 75 .writeAsString('wrongtents'); | |
| 76 }); | |
| 77 | |
| 78 d.file('name.txt', 'contents').validate(); | |
| 79 }); | |
| 80 | |
| 81 test('test 2', () { | |
| 82 expect(errors.single, new isInstanceOf<ScheduleError>()); | |
| 83 expect(errors.single.error.toString(), equals( | |
| 84 "File 'name.txt' should contain:\n" | |
| 85 "| contents\n" | |
| 86 "but actually contained:\n" | |
| 87 "X wrongtents")); | |
| 88 }); | |
| 89 }, passing: ['test 2']); | |
| 90 | |
| 91 expectTestsPass("file().validate() fails if there's no file", () { | |
| 92 var errors; | |
| 93 test('test 1', () { | |
| 94 scheduleSandbox(); | |
| 95 | |
| 96 currentSchedule.onException.schedule(() { | |
| 97 errors = currentSchedule.errors; | |
| 98 }); | |
| 99 | |
| 100 d.file('name.txt', 'contents').validate(); | |
| 101 }); | |
| 102 | |
| 103 test('test 2', () { | |
| 104 expect(errors.single, new isInstanceOf<ScheduleError>()); | |
| 105 expect(errors.single.error.toString(), | |
| 106 matches(r"^File not found: '[^']+[\\/]name\.txt'\.$")); | |
| 107 }); | |
| 108 }, passing: ['test 2']); | |
| 109 | |
| 110 expectTestsPass("file().read() returns the contents of the file as a stream", | |
| 111 () { | |
| 112 test('test', () { | |
| 113 expect(byteStreamToString(d.file('name.txt', 'contents').read()), | |
| 114 completion(equals('contents'))); | |
| 115 }); | |
| 116 }); | |
| 117 | |
| 118 expectTestsPass("file().describe() returns the filename", () { | |
| 119 test('test', () { | |
| 120 expect(d.file('name.txt', 'contents').describe(), equals('name.txt')); | |
| 121 }); | |
| 122 }); | |
| 123 | |
| 124 expectTestsPass('binaryFile().create() creates a file', () { | |
| 125 test('test', () { | |
| 126 scheduleSandbox(); | |
| 127 | |
| 128 d.binaryFile('name.bin', [1, 2, 3, 4, 5]).create(); | |
| 129 | |
| 130 schedule(() { | |
| 131 expect(new File(path.join(sandbox, 'name.bin')).readAsBytes(), | |
| 132 completion(equals([1, 2, 3, 4, 5]))); | |
| 133 }); | |
| 134 }); | |
| 135 }); | |
| 136 | |
| 137 expectTestsPass('binaryFile().validate() completes successfully if the ' | |
| 138 'filesystem matches the descriptor', () { | |
| 139 test('test', () { | |
| 140 scheduleSandbox(); | |
| 141 | |
| 142 schedule(() { | |
| 143 return new File(path.join(sandbox, 'name.bin')) | |
| 144 .writeAsBytes([1, 2, 3, 4, 5]); | |
| 145 }); | |
| 146 | |
| 147 d.binaryFile('name.bin', [1, 2, 3, 4, 5]).validate(); | |
| 148 }); | |
| 149 }); | |
| 150 | |
| 151 expectTestsPass("binaryFile().validate() fails if there's a file with the " | |
| 152 "wrong contents", () { | |
| 153 var errors; | |
| 154 test('test 1', () { | |
| 155 scheduleSandbox(); | |
| 156 | |
| 157 currentSchedule.onException.schedule(() { | |
| 158 errors = currentSchedule.errors; | |
| 159 }); | |
| 160 | |
| 161 schedule(() { | |
| 162 return new File(path.join(sandbox, 'name.bin')) | |
| 163 .writeAsBytes([2, 4, 6, 8, 10]); | |
| 164 }); | |
| 165 | |
| 166 d.binaryFile('name.bin', [1, 2, 3, 4, 5]).validate(); | |
| 167 }); | |
| 168 | |
| 169 test('test 2', () { | |
| 170 expect(errors.single, new isInstanceOf<ScheduleError>()); | |
| 171 expect(errors.single.error.toString(), equals( | |
| 172 "File 'name.bin' didn't contain the expected binary data.")); | |
| 173 }); | |
| 174 }, passing: ['test 2']); | |
| 175 | |
| 176 expectTestsPass('matcherFile().create() creates an empty file', () { | |
| 177 test('test', () { | |
| 178 scheduleSandbox(); | |
| 179 | |
| 180 d.matcherFile('name.txt', isNot(isEmpty)).create(); | |
| 181 | |
| 182 schedule(() { | |
| 183 expect(new File(path.join(sandbox, 'name.txt')).readAsString(), | |
| 184 completion(equals(''))); | |
| 185 }); | |
| 186 }); | |
| 187 }); | |
| 188 | |
| 189 expectTestsPass('matcherFile().validate() completes successfully if the ' | |
| 190 'string contents of the file matches the matcher', () { | |
| 191 test('test', () { | |
| 192 scheduleSandbox(); | |
| 193 | |
| 194 schedule(() { | |
| 195 return new File(path.join(sandbox, 'name.txt')) | |
| 196 .writeAsString('barfoobaz'); | |
| 197 }); | |
| 198 | |
| 199 d.matcherFile('name.txt', contains('foo')).validate(); | |
| 200 }); | |
| 201 }); | |
| 202 | |
| 203 expectTestsPass("matcherFile().validate() fails if the string contents of " | |
| 204 "the file doesn't match the matcher", () { | |
| 205 var errors; | |
| 206 test('test 1', () { | |
| 207 scheduleSandbox(); | |
| 208 | |
| 209 currentSchedule.onException.schedule(() { | |
| 210 errors = currentSchedule.errors; | |
| 211 }); | |
| 212 | |
| 213 schedule(() { | |
| 214 return new File(path.join(sandbox, 'name.txt')) | |
| 215 .writeAsString('barfoobaz'); | |
| 216 }); | |
| 217 | |
| 218 d.matcherFile('name.txt', contains('baaz')).validate(); | |
| 219 }); | |
| 220 | |
| 221 test('test 2', () { | |
| 222 expect(errors.single, new isInstanceOf<ScheduleError>()); | |
| 223 expect(errors.single.error.toString(), equals( | |
| 224 "Expected: contains 'baaz'\n" | |
| 225 " Actual: 'barfoobaz'\n")); | |
| 226 }); | |
| 227 }, passing: ['test 2']); | |
| 228 | |
| 229 expectTestsPass('binaryMatcherFile().validate() completes successfully if ' | |
| 230 'the string contents of the file matches the matcher', () { | |
| 231 test('test', () { | |
| 232 scheduleSandbox(); | |
| 233 | |
| 234 schedule(() { | |
| 235 return new File(path.join(sandbox, 'name.txt')) | |
| 236 .writeAsString('barfoobaz'); | |
| 237 }); | |
| 238 | |
| 239 d.binaryMatcherFile('name.txt', contains(111)).validate(); | |
| 240 }); | |
| 241 }); | |
| 242 | |
| 243 expectTestsPass("binaryMatcherFile().validate() fails if the string contents " | |
| 244 "of the file doesn't match the matcher", () { | |
| 245 var errors; | |
| 246 test('test 1', () { | |
| 247 scheduleSandbox(); | |
| 248 | |
| 249 currentSchedule.onException.schedule(() { | |
| 250 errors = currentSchedule.errors; | |
| 251 }); | |
| 252 | |
| 253 schedule(() { | |
| 254 return new File(path.join(sandbox, 'name.txt')) | |
| 255 .writeAsString('barfoobaz'); | |
| 256 }); | |
| 257 | |
| 258 d.binaryMatcherFile('name.txt', contains(12)).validate(); | |
| 259 }); | |
| 260 | |
| 261 test('test 2', () { | |
| 262 expect(errors.single, new isInstanceOf<ScheduleError>()); | |
| 263 expect(errors.single.error.toString(), equals( | |
| 264 "Expected: contains <12>\n" | |
| 265 " Actual: [98, 97, 114, 102, 111, 111, 98, 97, 122]\n")); | |
| 266 }); | |
| 267 }, passing: ['test 2']); | |
| 268 } | |
| OLD | NEW |