Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(383)

Side by Side Diff: pkg/analysis_server/test/resource_test.dart

Issue 299403004: Resource/File/Folder interfaces and implementations for dart:io and memory. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fixes for review comments Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « pkg/analysis_server/pubspec.yaml ('k') | pkg/analysis_server/test/test_all.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2014, 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 library test.resource;
6
7 import 'dart:io' as io;
8
9 import 'package:analysis_server/src/resource.dart';
10 import 'package:analyzer/src/generated/engine.dart' show TimestampedData;
11 import 'package:analyzer/src/generated/source_io.dart';
12 import 'package:path/path.dart';
13 import 'package:unittest/unittest.dart';
14
15 main() {
16 groupSep = ' | ';
17
18 group('MemoryResourceProvider', () {
19 MemoryResourceProvider provider;
20
21 setUp(() {
22 provider = new MemoryResourceProvider();
23 });
24
25 group('File', () {
26 group('==', () {
27 test('false', () {
28 File fileA = provider.getResource('/fileA.txt');
29 File fileB = provider.getResource('/fileB.txt');
30 expect(fileA == new Object(), isFalse);
31 expect(fileA == fileB, isFalse);
32 });
33
34 test('true', () {
35 File file = provider.getResource('/file.txt');
36 expect(file == file, isTrue);
37 });
38 });
39
40 group('exists', () {
41 test('false', () {
42 File file = provider.getResource('/file.txt');
43 expect(file, isNotNull);
44 expect(file.exists, isFalse);
45 });
46
47 test('true', () {
48 provider.newFile('/foo/file.txt', 'qwerty');
49 File file = provider.getResource('/foo/file.txt');
50 expect(file, isNotNull);
51 expect(file.exists, isTrue);
52 });
53 });
54
55 test('fullName', () {
56 File file = provider.getResource('/foo/bar/file.txt');
57 expect(file.fullName, '/foo/bar/file.txt');
58 });
59
60 test('hashCode', () {
61 File file = provider.getResource('/foo/bar/file.txt');
62 file.hashCode;
63 });
64
65 test('shortName', () {
66 File file = provider.getResource('/foo/bar/file.txt');
67 expect(file.shortName, 'file.txt');
68 });
69
70 test('toString', () {
71 File file = provider.getResource('/foo/bar/file.txt');
72 expect(file.toString(), '/foo/bar/file.txt');
73 });
74 });
75
76 group('Folder', () {
77 Folder folder;
78
79 setUp(() {
80 folder = provider.newFolder('/foo/bar');
81 });
82
83 group('getChild', () {
84 test('does not exist', () {
85 File file = folder.getChild('file.txt');
86 expect(file, isNotNull);
87 expect(file.exists, isFalse);
88 });
89
90 test('file', () {
91 provider.newFile('/foo/bar/file.txt', 'content');
92 File child = folder.getChild('file.txt');
93 expect(child, isNotNull);
94 expect(child.exists, isTrue);
95 });
96
97 test('folder', () {
98 provider.newFolder('/foo/bar/baz');
99 Folder child = folder.getChild('baz');
100 expect(child, isNotNull);
101 expect(child.exists, isTrue);
102 });
103 });
104
105 test('getChildren', () {
106 provider.newFile('/foo/bar/a.txt', 'aaa');
107 provider.newFolder('/foo/bar/bFolder');
108 provider.newFile('/foo/bar/c.txt', 'ccc');
109 // prepare 3 children
110 List<Resource> children = folder.getChildren();
111 expect(children, hasLength(3));
112 children.sort((a, b) => a.shortName.compareTo(b.shortName));
113 // check that each child exists
114 children.forEach((child) {
115 expect(child.exists, true);
116 });
117 // check names
118 expect(children[0].shortName, 'a.txt');
119 expect(children[1].shortName, 'bFolder');
120 expect(children[2].shortName, 'c.txt');
121 // check types
122 expect(children[0], _isFile);
123 expect(children[1], _isFolder);
124 expect(children[2], _isFile);
125 });
126 });
127
128 group('_MemoryFileSource', () {
129 Source source;
130
131 group('existent', () {
132 setUp(() {
133 File file = provider.newFile('/foo/test.dart', 'library test;');
134 source = file.createSource(UriKind.FILE_URI);
135 });
136
137 test('contents', () {
138 TimestampedData<String> contents = source.contents;
139 expect(contents.data, 'library test;');
140 });
141
142 test('encoding', () {
143 expect(source.encoding, 'f/foo/test.dart');
144 });
145
146 test('exists', () {
147 expect(source.exists(), isTrue);
148 });
149
150 test('fullName', () {
151 expect(source.fullName, '/foo/test.dart');
152 });
153
154 test('shortName', () {
155 expect(source.shortName, 'test.dart');
156 });
157
158 test('resolveRelative', () {
159 var relative = source.resolveRelative(new Uri.file('bar/baz.dart'));
160 expect(relative.fullName, '/foo/bar/baz.dart');
161 });
162 });
163
164 group('non-existent', () {
165 setUp(() {
166 File file = provider.getResource('/foo/test.dart');
167 source = file.createSource(UriKind.FILE_URI);
168 });
169
170 test('contents', () {
171 expect(
172 () {
173 source.contents;
174 },
175 throwsA(_isMemoryResourceException)
176 );
177 });
178
179 test('encoding', () {
180 expect(source.encoding, 'f/foo/test.dart');
181 });
182
183 test('exists', () {
184 expect(source.exists(), isFalse);
185 });
186
187 test('fullName', () {
188 expect(source.fullName, '/foo/test.dart');
189 });
190
191 test('shortName', () {
192 expect(source.shortName, 'test.dart');
193 });
194
195 test('resolveRelative', () {
196 var relative = source.resolveRelative(new Uri.file('bar/baz.dart'));
197 expect(relative.fullName, '/foo/bar/baz.dart');
198 });
199 });
200 });
201 });
202
203 group('PhysicalResourceProvider', () {
204 io.Directory tempDirectory;
205 String tempPath;
206
207 setUp(() {
208 tempDirectory = io.Directory.systemTemp.createTempSync('test_resource');
209 tempPath = tempDirectory.absolute.path;
210 });
211
212 tearDown(() {
213 tempDirectory.deleteSync(recursive: true);
214 });
215
216 group('File', () {
217 String path;
218 File file;
219
220 setUp(() {
221 path = join(tempPath, 'file.txt');
222 file = PhysicalResourceProvider.INSTANCE.getResource(path);
223 });
224
225 test('createSource', () {
226 new io.File(path).writeAsStringSync('contents');
227 var source = file.createSource(UriKind.FILE_URI);
228 expect(source.uriKind, UriKind.FILE_URI);
229 expect(source.exists(), isTrue);
230 expect(source.contents.data, 'contents');
231 });
232
233 group('exists', () {
234 test('false', () {
235 expect(file.exists, isFalse);
236 });
237
238 test('true', () {
239 new io.File(path).writeAsStringSync('contents');
240 expect(file.exists, isTrue);
241 });
242 });
243
244 test('fullName', () {
245 expect(file.fullName, path);
246 });
247
248 test('hashCode', () {
249 file.hashCode;
250 });
251
252 test('shortName', () {
253 expect(file.shortName, 'file.txt');
254 });
255
256 test('toString', () {
257 expect(file.toString(), path);
258 });
259 });
260
261 group('Folder', () {
262 String path;
263 Folder folder;
264
265 setUp(() {
266 path = join(tempPath, 'folder');
267 new io.Directory(path).createSync();
268 folder = PhysicalResourceProvider.INSTANCE.getResource(path);
269 });
270
271 group('getChild', () {
272 test('does not exist', () {
273 var child = folder.getChild('no-such-resource');
274 expect(child, isNotNull);
275 expect(child.exists, isFalse);
276 });
277
278 test('file', () {
279 new io.File(join(path, 'myFile')).createSync();
280 var child = folder.getChild('myFile');
281 expect(child, _isFile);
282 expect(child.exists, isTrue);
283 });
284
285 test('folder', () {
286 new io.Directory(join(path, 'myFolder')).createSync();
287 var child = folder.getChild('myFolder');
288 expect(child, _isFolder);
289 expect(child.exists, isTrue);
290 });
291 });
292
293 test('getChildren', () {
294 // create 2 files and 1 folder
295 new io.File(join(path, 'a.txt')).createSync();
296 new io.Directory(join(path, 'bFolder')).createSync();
297 new io.File(join(path, 'c.txt')).createSync();
298 // prepare 3 children
299 List<Resource> children = folder.getChildren();
300 expect(children, hasLength(3));
301 children.sort((a, b) => a.shortName.compareTo(b.shortName));
302 // check that each child exists
303 children.forEach((child) {
304 expect(child.exists, true);
305 });
306 // check names
307 expect(children[0].shortName, 'a.txt');
308 expect(children[1].shortName, 'bFolder');
309 expect(children[2].shortName, 'c.txt');
310 // check types
311 expect(children[0], _isFile);
312 expect(children[1], _isFolder);
313 expect(children[2], _isFile);
314 });
315 });
316 });
317 }
318
319 var _isFile = new isInstanceOf<File>();
320 var _isFolder = new isInstanceOf<Folder>();
321 var _isMemoryResourceException = new isInstanceOf<MemoryResourceException>();
OLDNEW
« no previous file with comments | « pkg/analysis_server/pubspec.yaml ('k') | pkg/analysis_server/test/test_all.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698