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

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: 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
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', () {
Brian Wilkerson 2014/05/25 15:29:58 It would be good to also test a non-existent file.
scheglov 2014/05/25 16:16:38 Done.
129 Source source;
130
131 setUp(() {
132 File file = provider.newFile('/foo/test.dart', 'library test;');
133 source = file.createSource(UriKind.FILE_URI);
134 });
135
136 test('contents', () {
137 TimestampedData<String> contents = source.contents;
138 expect(contents.data, 'library test;');
139 });
140
141 test('encoding', () {
142 expect(source.encoding, 'f/foo/test.dart');
143 });
144
145 test('exists', () {
146 expect(source.exists(), isTrue);
147 });
148
149 test('fullName', () {
150 expect(source.fullName, '/foo/test.dart');
151 });
152
153 test('shortName', () {
154 expect(source.shortName, 'test.dart');
155 });
156
157 test('resolveRelative', () {
158 var relative = source.resolveRelative(new Uri.file('bar/baz.dart'));
159 expect(relative.fullName, '/foo/bar/baz.dart');
160 });
161 });
162 });
163
164 group('PhysicalResourceProvider', () {
165 io.Directory tempDirectory;
166 String tempPath;
167
168 setUp(() {
169 tempDirectory = io.Directory.systemTemp.createTempSync('test_resource');
170 tempPath = tempDirectory.absolute.path;
171 });
172
173 tearDown(() {
174 tempDirectory.deleteSync(recursive: true);
175 });
176
177 group('File', () {
178 String path;
179 File file;
180
181 setUp(() {
182 path = join(tempPath, 'file.txt');
183 file = PhysicalResourceProvider.INSTANCE.getResource(path);
184 });
185
186 test('createSource', () {
187 new io.File(path).writeAsStringSync('contents');
188 var source = file.createSource(UriKind.FILE_URI);
189 expect(source.uriKind, UriKind.FILE_URI);
190 expect(source.exists(), isTrue);
191 expect(source.contents.data, 'contents');
192 });
193
194 group('exists', () {
195 test('false', () {
196 expect(file.exists, isFalse);
197 });
198
199 test('true', () {
200 new io.File(path).writeAsStringSync('contents');
201 expect(file.exists, isTrue);
202 });
203 });
204
205 test('fullName', () {
206 expect(file.fullName, path);
207 });
208
209 test('hashCode', () {
210 file.hashCode;
211 });
212
213 test('shortName', () {
214 expect(file.shortName, 'file.txt');
215 });
216
217 test('toString', () {
218 expect(file.toString(), path);
219 });
220 });
221
222 group('Folder', () {
223 String path;
224 Folder folder;
225
226 setUp(() {
227 path = join(tempPath, 'folder');
228 new io.Directory(path).createSync();
229 folder = PhysicalResourceProvider.INSTANCE.getResource(path);
230 });
231
232 group('getChild', () {
233 test('does not exist', () {
234 var child = folder.getChild('no-such-resource');
235 expect(child, isNotNull);
236 expect(child.exists, isFalse);
237 });
238
239 test('file', () {
240 new io.File(join(path, 'myFile')).createSync();
241 var child = folder.getChild('myFile');
242 expect(child, _isFile);
243 expect(child.exists, isTrue);
244 });
245
246 test('folder', () {
247 new io.Directory(join(path, 'myFolder')).createSync();
248 var child = folder.getChild('myFolder');
249 expect(child, _isFolder);
250 expect(child.exists, isTrue);
251 });
252 });
253
254 test('getChildren', () {
255 // create 2 files and 1 folder
256 new io.File(join(path, 'a.txt')).createSync();
257 new io.Directory(join(path, 'bFolder')).createSync();
258 new io.File(join(path, 'c.txt')).createSync();
259 // prepare 3 children
260 List<Resource> children = folder.getChildren();
261 expect(children, hasLength(3));
262 children.sort((a, b) => a.shortName.compareTo(b.shortName));
263 // check that each child exists
264 children.forEach((child) {
265 expect(child.exists, true);
266 });
267 // check names
268 expect(children[0].shortName, 'a.txt');
269 expect(children[1].shortName, 'bFolder');
270 expect(children[2].shortName, 'c.txt');
271 // check types
272 expect(children[0], _isFile);
273 expect(children[1], _isFolder);
274 expect(children[2], _isFile);
275 });
276 });
277 });
278 }
279
280 var _isFile = new isInstanceOf<File>();
281 var _isFolder = new isInstanceOf<Folder>();
OLDNEW
« pkg/analysis_server/lib/src/resource.dart ('K') | « pkg/analysis_server/pubspec.yaml ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698