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 "package:expect/expect.dart"; | |
7 import "dart:io"; | |
8 import "dart:isolate"; | |
9 | |
10 | |
11 class FutureExpect { | |
12 static Future isTrue(Future<bool> result) => | |
13 result.then((value) => Expect.isTrue(value)); | |
14 static Future isFalse(Future<bool> result) => | |
15 result.then((value) => Expect.isFalse(value)); | |
16 static Future equals(expected, Future result) => | |
17 result.then((value) => Expect.equals(expected, value)); | |
18 static Future listEquals(expected, Future result) => | |
19 result.then((value) => Expect.listEquals(expected, value)); | |
20 static Future throws(Future result) => | |
21 result.then((value) { | |
22 throw new ExpectException( | |
23 "FutureExpect.throws received $value instead of an exception"); | |
24 }, onError: (_) => null); | |
25 } | |
26 | |
27 | |
28 Future testFileExistsCreate() => | |
Søren Gjesse
2013/05/03 07:13:36
Add a receive port to keep the test alive until th
Søren Gjesse
2013/05/03 07:13:36
Normally we don't use the => syntax for long metho
Bill Hesse
2013/05/03 08:44:06
This is done in the main method, which calls this.
Bill Hesse
2013/05/03 08:44:06
In this case, it was possible for all of them, sin
| |
29 new Directory('').createTemp().then((temp) { | |
30 var x = '${temp.path}${Platform.pathSeparator}x'; | |
31 var y = '${temp.path}${Platform.pathSeparator}y'; | |
32 return new Link(y).create(x) | |
33 .then((link) => Expect.equals(y, link.path)) | |
Bill Hesse
2013/05/02 14:02:42
The indentation has tried to follow the rule "Foll
| |
34 .then((_) => FutureExpect.isFalse(new File(y).exists())) | |
Bill Hesse
2013/05/02 14:02:42
Note that this also could be written
.then((_) =>
| |
35 .then((_) => FutureExpect.isFalse(new File(x).exists())) | |
36 .then((_) => FutureExpect.isTrue(FileSystemEntity.isLink(y))) | |
37 .then((_) => FutureExpect.isFalse(FileSystemEntity.isLink(x))) | |
38 .then((_) => FutureExpect.equals(FileSystemEntityType.NOT_FOUND, | |
39 FileSystemEntity.type(y))) | |
40 .then((_) => FutureExpect.equals(FileSystemEntityType.NOT_FOUND, | |
41 FileSystemEntity.type(x))) | |
42 .then((_) => FutureExpect.equals(FileSystemEntityType.LINK, | |
43 FileSystemEntity.type(y, | |
44 followLinks: false))) | |
45 .then((_) => FutureExpect.equals(FileSystemEntityType.NOT_FOUND, | |
46 FileSystemEntity.type(x, | |
47 followLinks: false))) | |
48 .then((_) => FutureExpect.equals(x, new Link(y).target())) | |
49 .then((_) => new File(y).create()) | |
Søren Gjesse
2013/05/03 07:13:36
Shouldn't this be creating the file x - or am I mi
Bill Hesse
2013/05/03 08:44:06
This is testing that file creation through a link
| |
50 .then((yFile) => Expect.equals(y, yFile.path)) | |
51 .then((_) => FutureExpect.isTrue(new File(y).exists())) | |
52 .then((_) => FutureExpect.isTrue(new File(x).exists())) | |
53 .then((_) => FutureExpect.isTrue(FileSystemEntity.isLink(y))) | |
54 .then((_) => FutureExpect.isFalse(FileSystemEntity.isLink(x))) | |
55 .then((_) => FutureExpect.isTrue(FileSystemEntity.isFile(y))) | |
56 .then((_) => FutureExpect.isTrue(FileSystemEntity.isFile(x))) | |
57 .then((_) => FutureExpect.equals(FileSystemEntityType.FILE, | |
58 FileSystemEntity.type(y))) | |
59 .then((_) => FutureExpect.equals(FileSystemEntityType.FILE, | |
60 FileSystemEntity.type(x))) | |
61 .then((_) => FutureExpect.equals(FileSystemEntityType.LINK, | |
62 FileSystemEntity.type(y, | |
63 followLinks: false))) | |
64 .then((_) => FutureExpect.equals(FileSystemEntityType.FILE, | |
65 FileSystemEntity.type(x, | |
66 followLinks: false))) | |
67 .then((_) => FutureExpect.equals(x, new Link(y).target())) | |
68 .then((_) => new File(x).delete()) | |
69 .then((xDeletedFile) => Expect.equals(x, xDeletedFile.path)) | |
70 .then((_) => new Directory(x).create()) | |
71 .then((xCreatedDirectory) => Expect.equals(x, xCreatedDirectory.path)) | |
72 .then((_) => FutureExpect.isTrue(FileSystemEntity.isLink(y))) | |
73 .then((_) => FutureExpect.isFalse(FileSystemEntity.isLink(x))) | |
74 .then((_) => FutureExpect.isTrue(FileSystemEntity.isDirectory(y))) | |
75 .then((_) => FutureExpect.isTrue(FileSystemEntity.isDirectory(x))) | |
76 .then((_) => FutureExpect.equals(FileSystemEntityType.DIRECTORY, | |
77 FileSystemEntity.type(y))) | |
78 .then((_) => FutureExpect.equals(FileSystemEntityType.DIRECTORY, | |
79 FileSystemEntity.type(x))) | |
80 .then((_) => FutureExpect.equals(FileSystemEntityType.LINK, | |
81 FileSystemEntity.type(y, | |
82 followLinks: false))) | |
83 .then((_) => FutureExpect.equals(FileSystemEntityType.DIRECTORY, | |
84 FileSystemEntity.type(x, | |
85 followLinks: false))) | |
86 .then((_) => FutureExpect.equals(x, new Link(y).target())) | |
87 .then((_) => new Link(y).delete()) | |
88 .then((_) => FutureExpect.isFalse(FileSystemEntity.isLink(y))) | |
89 .then((_) => FutureExpect.isFalse(FileSystemEntity.isLink(x))) | |
90 .then((_) => FutureExpect.equals(FileSystemEntityType.NOT_FOUND, | |
91 FileSystemEntity.type(y))) | |
92 .then((_) => FutureExpect.equals(FileSystemEntityType.DIRECTORY, | |
93 FileSystemEntity.type(x))) | |
94 .then((_) => FutureExpect.throws(new Link(y).target())) | |
95 .then((_) => temp.delete(recursive: true)); | |
96 }); | |
97 | |
98 | |
99 Future testFileDelete() => | |
100 new Directory('').createTemp().then((temp) { | |
101 var x = '${temp.path}${Platform.pathSeparator}x'; | |
102 var y = '${temp.path}${Platform.pathSeparator}y'; | |
103 return new File(x).create() | |
104 .then((_) => new Link(y).create(x)) | |
105 .then((_) => FutureExpect.isTrue(new File(x).exists())) | |
106 .then((_) => FutureExpect.isTrue(new File(y).exists())) | |
107 .then((_) => new File(y).delete()) | |
108 .then((_) => FutureExpect.isTrue(new File(x).exists())) | |
109 .then((_) => FutureExpect.isFalse(new File(y).exists())) | |
110 .then((_) => new Link(y).create(x)) | |
111 .then((_) => FutureExpect.isTrue(new File(x).exists())) | |
112 .then((_) => FutureExpect.isTrue(new File(y).exists())) | |
113 .then((_) => new File(y).delete()) | |
114 .then((_) => FutureExpect.isTrue(new File(x).exists())) | |
115 .then((_) => FutureExpect.isFalse(new File(y).exists())) | |
116 .then((_) => temp.delete(recursive: true)); | |
117 }); | |
118 | |
119 | |
120 Future testFileWriteRead() => | |
121 new Directory('').createTemp().then((temp) { | |
122 var x = '${temp.path}${Platform.pathSeparator}x'; | |
123 var y = '${temp.path}${Platform.pathSeparator}y'; | |
124 var data = "asdf".codeUnits; | |
125 return new File(x).create() | |
126 .then((_) => new Link(y).create(x)) | |
127 .then((_) => | |
128 (new File(y).openWrite(mode: FileMode.WRITE)..add(data)).close()) | |
129 .then((_) => FutureExpect.listEquals(data, new File(y).readAsBytes())) | |
130 .then((_) => FutureExpect.listEquals(data, new File(x).readAsBytes())) | |
131 .then((_) => temp.delete(recursive: true)); | |
132 }); | |
133 | |
134 | |
135 Future testDirectoryExistsCreate() => | |
136 new Directory('').createTemp().then((temp) { | |
137 var x = '${temp.path}${Platform.pathSeparator}x'; | |
138 var y = '${temp.path}${Platform.pathSeparator}y'; | |
139 return new Link(y).create(x) | |
140 .then((_) => FutureExpect.isFalse(new Directory(x).exists())) | |
141 .then((_) => FutureExpect.isFalse(new Directory(y).exists())) | |
142 .then((_) => FutureExpect.throws(new Directory(y).create())) | |
143 .then((_) => temp.delete(recursive: true)); | |
144 }); | |
145 | |
146 | |
147 | |
148 Future testDirectoryDelete() => | |
149 new Directory('').createTemp().then((temp) => | |
150 new Directory('').createTemp().then((temp2) { | |
151 var y = '${temp.path}${Platform.pathSeparator}y'; | |
152 var x = '${temp2.path}${Platform.pathSeparator}x'; | |
153 var link = new Directory(y); | |
154 return new File(x).create() | |
155 .then((_) => new Link(y).create(temp2.path)) | |
156 .then((_) => FutureExpect.isTrue(link.exists())) | |
157 .then((_) => FutureExpect.isTrue(temp2.exists())) | |
158 .then((_) => link.delete()) | |
159 .then((_) => FutureExpect.isFalse(link.exists())) | |
160 .then((_) => FutureExpect.isTrue(temp2.exists())) | |
161 .then((_) => new Link(y).create(temp2.path)) | |
162 .then((_) => FutureExpect.isTrue(link.exists())) | |
163 .then((_) => temp.delete(recursive: true)) | |
164 .then((_) => FutureExpect.isFalse(link.exists())) | |
165 .then((_) => FutureExpect.isFalse(temp.exists())) | |
166 .then((_) => FutureExpect.isTrue(temp2.exists())) | |
167 .then((_) => FutureExpect.isTrue(new File(x).exists())) | |
168 .then((_) => temp2.delete(recursive: true)); | |
169 })); | |
170 | |
171 | |
172 testDirectoryListing() => | |
Søren Gjesse
2013/05/03 07:13:36
Please add explicit return type
| |
173 new Directory('').createTemp().then((temp) => | |
174 new Directory('').createTemp().then((temp2) { | |
175 var sep = Platform.pathSeparator; | |
176 var y = '${temp.path}${sep}y'; | |
177 var x = '${temp2.path}${sep}x'; | |
178 return new File(x).create() | |
179 .then((_) => new Link(y).create(temp2.path)) | |
180 .then((_) => temp.list(recursive: true).singleWhere( | |
181 (entry) => entry is File)) | |
182 .then((file) => Expect.isTrue(file.path.endsWith('$y${sep}x'))) | |
183 .then((_) => temp.list(recursive: true).singleWhere( | |
184 (entry) => entry is Directory)) | |
185 .then((dir) => Expect.isTrue(dir.path.endsWith('y'))) | |
186 .then((_) => temp.delete(recursive: true)) | |
187 .then((_) => temp2.delete(recursive: true)); | |
188 })); | |
189 | |
190 | |
191 testDirectoryListingBrokenLink() => | |
192 new Directory('').createTemp().then((temp) { | |
193 var x = '${temp.path}${Platform.pathSeparator}x'; | |
194 var link = '${temp.path}${Platform.pathSeparator}link'; | |
195 var doesNotExist = 'this_thing_does_not_exist'; | |
196 bool sawFile = false; | |
197 bool sawLink = false; | |
198 return new File(x).create() | |
199 .then((_) => new Link(link).create(doesNotExist)) | |
200 .then((_) => temp.list(recursive: true).forEach( | |
201 (entity) { | |
202 if (entity is File) { | |
203 Expect.isFalse(sawFile); | |
204 sawFile = true; | |
205 Expect.isTrue(entity.path.endsWith(x)); | |
206 } else { | |
207 Expect.isTrue(entity is Link); | |
208 Expect.isFalse(sawLink); | |
209 sawLink = true; | |
210 Expect.isTrue(entity.path.endsWith(link)); | |
211 } | |
212 return true; | |
213 })) | |
214 .then((_) => temp.delete(recursive: true)); | |
215 }); | |
216 | |
217 | |
218 main() { | |
219 ReceivePort keepAlive = new ReceivePort(); | |
220 testFileExistsCreate() | |
221 .then((_) => testFileDelete()) | |
222 .then((_) => testFileWriteRead()) | |
223 .then((_) => testDirectoryExistsCreate()) | |
224 .then((_) => testDirectoryDelete()) | |
225 .then((_) => testDirectoryListing()) | |
226 .then((_) => testDirectoryListingBrokenLink()) | |
227 .then((_) => keepAlive.close()); | |
228 } | |
OLD | NEW |