OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2016, 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 // SharedOptions=--supermixin | |
5 | |
6 library front_end.test.memory_file_system_test; | |
7 | |
8 import 'dart:convert'; | |
9 import 'dart:io' as io; | |
10 | |
11 import 'package:front_end/memory_file_system.dart'; | |
12 import 'package:path/path.dart' as pathos; | |
13 import 'package:test/test.dart'; | |
14 import 'package:test_reflective_loader/test_reflective_loader.dart'; | |
Siggi Cherem (dart-lang)
2016/11/03 00:46:50
I've never used this framework, we normally just u
Paul Berry
2016/11/03 15:16:41
This framework adds a lot of functionality which h
| |
15 | |
16 main() { | |
17 defineReflectiveSuite(() { | |
18 defineReflectiveTests(MemoryFileSystemTestNative); | |
19 defineReflectiveTests(MemoryFileSystemTestPosix); | |
20 defineReflectiveTests(MemoryFileSystemTestWindows); | |
21 defineReflectiveTests(FileTest); | |
22 }); | |
23 } | |
24 | |
25 @reflectiveTest | |
26 class FileTest extends _BaseTestNative { | |
27 String path; | |
28 MemoryFileSystemEntity file; | |
29 | |
30 setUp() { | |
31 super.setUp(); | |
32 path = join(tempPath, 'file.txt'); | |
33 file = fileSystem.entityForPath(path); | |
34 } | |
35 | |
36 test_equals_differentPaths() { | |
37 expect( | |
38 file == fileSystem.entityForPath(join(tempPath, 'file2.txt')), isFalse); | |
39 } | |
40 | |
41 test_equals_samePath() { | |
42 expect( | |
43 file == fileSystem.entityForPath(join(tempPath, 'file.txt')), isTrue); | |
44 } | |
45 | |
46 test_hashCode_samePath() { | |
47 expect(file.hashCode, | |
48 fileSystem.entityForPath(join(tempPath, 'file.txt')).hashCode); | |
49 } | |
50 | |
51 test_path() { | |
52 expect(file.path, path); | |
53 } | |
54 | |
55 test_readAsBytes_badUtf8() async { | |
56 // A file containing invalid UTF-8 can still be read as raw bytes. | |
57 List<int> bytes = [0xc0, 0x40]; // Invalid UTF-8 | |
58 file.writeAsBytesSync(bytes); | |
59 expect(await file.readAsBytes(), bytes); | |
60 } | |
61 | |
62 test_readAsBytes_doesNotExist() { | |
63 expect(file.readAsBytes(), throwsException); | |
64 } | |
65 | |
66 test_readAsBytes_exists() async { | |
67 var s = 'contents'; | |
68 file.writeAsStringSync(s); | |
69 expect(await file.readAsBytes(), UTF8.encode(s)); | |
70 } | |
71 | |
72 test_readAsString_badUtf8() { | |
73 file.writeAsBytesSync([0xc0, 0x40]); // Invalid UTF-8 | |
74 expect(file.readAsString(), throwsException); | |
75 } | |
76 | |
77 test_readAsString_doesNotExist() { | |
78 expect(file.readAsString(), throwsException); | |
79 } | |
80 | |
81 test_readAsString_exists() async { | |
82 var s = 'contents'; | |
83 file.writeAsStringSync(s); | |
84 expect(await file.readAsString(), s); | |
85 } | |
86 | |
87 test_readAsString_utf8() async { | |
88 file.writeAsBytesSync([0xe2, 0x82, 0xac]); // Unicode € symbol, in UTF-8 | |
89 expect(await file.readAsString(), '\u20ac'); | |
90 } | |
91 | |
92 test_writeAsBytesSync_modifyAfterRead() async { | |
93 file.writeAsBytesSync([1]); | |
94 (await file.readAsBytes())[0] = 2; | |
95 expect(await file.readAsBytes(), [1]); | |
96 } | |
97 | |
98 test_writeAsBytesSync_modifyAfterWrite() async { | |
99 var bytes = [1]; | |
100 file.writeAsBytesSync(bytes); | |
101 bytes[0] = 2; | |
102 expect(await file.readAsBytes(), [1]); | |
103 } | |
104 | |
105 test_writeAsBytesSync_overwrite() async { | |
106 file.writeAsBytesSync([1]); | |
107 file.writeAsBytesSync([2]); | |
108 expect(await file.readAsBytes(), [2]); | |
109 } | |
110 | |
111 test_writeAsStringSync_overwrite() async { | |
112 file.writeAsStringSync('first'); | |
113 file.writeAsStringSync('second'); | |
114 expect(await file.readAsString(), 'second'); | |
115 } | |
116 | |
117 test_writeAsStringSync_utf8() async { | |
118 file.writeAsStringSync('\u20ac'); // Unicode € symbol | |
119 expect(await file.readAsBytes(), [0xe2, 0x82, 0xac]); | |
120 } | |
121 } | |
122 | |
123 abstract class MemoryFileSystemTestMixin extends _BaseTest { | |
124 Uri tempUri; | |
125 | |
126 setUp() { | |
127 super.setUp(); | |
128 tempUri = fileSystem.context.toUri(tempPath); | |
129 } | |
130 | |
131 test_entityForPath() { | |
132 var path = join(tempPath, 'file.txt'); | |
133 expect(fileSystem.entityForPath(path).path, path); | |
134 } | |
135 | |
136 test_entityForPath_absolutize() { | |
137 expect(fileSystem.entityForPath('file.txt').path, | |
138 join(fileSystem.currentDirectory, 'file.txt')); | |
139 } | |
140 | |
141 test_entityForPath_normalize_dot() { | |
142 expect(fileSystem.entityForPath(join(tempPath, '.', 'file.txt')).path, | |
143 join(tempPath, 'file.txt')); | |
144 } | |
145 | |
146 test_entityForPath_normalize_dotDot() { | |
147 expect( | |
148 fileSystem.entityForPath(join(tempPath, 'foo', '..', 'file.txt')).path, | |
149 join(tempPath, 'file.txt')); | |
150 } | |
151 | |
152 test_entityForUri() { | |
153 expect(fileSystem.entityForUri(Uri.parse('$tempUri/file.txt')).path, | |
154 join(tempPath, 'file.txt')); | |
155 } | |
156 | |
157 test_entityForUri_bareUri_absolute() { | |
158 expect(() => fileSystem.entityForUri(Uri.parse('/file.txt')), | |
159 throwsA(new isInstanceOf<Error>())); | |
scheglov
2016/11/03 02:15:24
Maybe just test for the specific isArgumentError t
Paul Berry
2016/11/03 15:16:41
The interface contract is that we simply throw an
| |
160 } | |
161 | |
162 test_entityForUri_bareUri_relative() { | |
163 expect(() => fileSystem.entityForUri(Uri.parse('file.txt')), | |
164 throwsA(new isInstanceOf<Error>())); | |
165 } | |
166 | |
167 test_entityForUri_nonFileUri() { | |
168 expect(() => fileSystem.entityForUri(Uri.parse('package:foo/bar.dart')), | |
169 throwsA(new isInstanceOf<Error>())); | |
170 } | |
171 | |
172 test_entityForUri_normalize_dot() { | |
173 expect(fileSystem.entityForUri(Uri.parse('$tempUri/./file.txt')).path, | |
174 join(tempPath, 'file.txt')); | |
175 } | |
176 | |
177 test_entityForUri_normalize_dotDot() { | |
178 expect(fileSystem.entityForUri(Uri.parse('$tempUri/foo/../file.txt')).path, | |
179 join(tempPath, 'file.txt')); | |
180 } | |
181 } | |
182 | |
183 @reflectiveTest | |
184 class MemoryFileSystemTestNative extends _BaseTestNative | |
185 with MemoryFileSystemTestMixin {} | |
186 | |
187 @reflectiveTest | |
188 class MemoryFileSystemTestPosix extends _BaseTestPosix | |
189 with MemoryFileSystemTestMixin {} | |
190 | |
191 @reflectiveTest | |
192 class MemoryFileSystemTestWindows extends _BaseTestWindows | |
193 with MemoryFileSystemTestMixin {} | |
194 | |
195 abstract class _BaseTest { | |
196 MemoryFileSystem get fileSystem; | |
197 String get tempPath; | |
198 String join(String path1, String path2, [String path3, String path4]); | |
199 void setUp(); | |
200 } | |
201 | |
202 class _BaseTestNative extends _BaseTest { | |
203 MemoryFileSystem fileSystem; | |
204 String tempPath; | |
205 | |
206 String join(String path1, String path2, [String path3, String path4]) => | |
207 pathos.join(path1, path2, path3, path4); | |
208 | |
209 setUp() { | |
210 tempPath = pathos.join(io.Directory.systemTemp.path, 'test_file_system'); | |
211 fileSystem = | |
212 new MemoryFileSystem(pathos.context, io.Directory.current.path); | |
213 } | |
214 } | |
215 | |
216 class _BaseTestPosix extends _BaseTest { | |
217 MemoryFileSystem fileSystem; | |
218 String tempPath; | |
219 | |
220 String join(String path1, String path2, [String path3, String path4]) => | |
221 pathos.posix.join(path1, path2, path3, path4); | |
222 | |
223 void setUp() { | |
224 tempPath = '/test_file_system'; | |
225 fileSystem = new MemoryFileSystem(pathos.posix, '/cwd'); | |
226 } | |
227 } | |
228 | |
229 class _BaseTestWindows extends _BaseTest { | |
230 MemoryFileSystem fileSystem; | |
231 String tempPath; | |
232 | |
233 String join(String path1, String path2, [String path3, String path4]) => | |
234 pathos.windows.join(path1, path2, path3, path4); | |
235 | |
236 void setUp() { | |
237 tempPath = r'c:\test_file_system'; | |
238 fileSystem = new MemoryFileSystem(pathos.windows, r'c:\cwd'); | |
239 } | |
240 } | |
OLD | NEW |