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

Side by Side Diff: pkg/front_end/test/physical_file_system_test.dart

Issue 2471283002: Add implementations of the front end FileSystem API. (Closed)
Patch Set: Created 4 years, 1 month 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
OLDNEW
(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.physical_file_system_test;
7
8 import 'dart:convert';
9 import 'dart:io' as io;
10
11 import 'package:front_end/file_system.dart';
12 import 'package:front_end/physical_file_system.dart';
13 import 'package:path/path.dart' as p;
14 import 'package:test/test.dart';
15 import 'package:test_reflective_loader/test_reflective_loader.dart';
16
17 main() {
18 defineReflectiveSuite(() {
19 defineReflectiveTests(PhysicalFileSystemTest);
20 defineReflectiveTests(FileTest);
21 });
22 }
23
24 @reflectiveTest
25 class FileTest extends _BaseTest {
26 String path;
27 FileSystemEntity file;
28
29 setUp() {
30 super.setUp();
31 path = p.join(tempPath, 'file.txt');
32 file = PhysicalFileSystem.instance.entityForPath(path);
33 }
34
35 test_equals_differentPaths() {
36 expect(
37 file ==
38 PhysicalFileSystem.instance
39 .entityForPath(p.join(tempPath, 'file2.txt')),
40 isFalse);
41 }
42
43 test_equals_samePath() {
44 expect(
45 file ==
46 PhysicalFileSystem.instance
47 .entityForPath(p.join(tempPath, 'file.txt')),
48 isTrue);
49 }
50
51 test_hashCode_samePath() {
52 expect(
53 file.hashCode,
54 PhysicalFileSystem.instance
55 .entityForPath(p.join(tempPath, 'file.txt'))
56 .hashCode);
57 }
58
59 test_path() {
60 expect(file.path, path);
61 }
62
63 test_readAsBytes_badUtf8() async {
64 // A file containing invalid UTF-8 can still be read as raw bytes.
65 List<int> bytes = [0xc0, 0x40]; // Invalid UTF-8
66 new io.File(path).writeAsBytesSync(bytes);
67 expect(await file.readAsBytes(), bytes);
68 }
69
70 test_readAsBytes_doesNotExist() {
71 expect(file.readAsBytes(), throwsException);
72 }
73
74 test_readAsBytes_exists() async {
75 var s = 'contents';
76 new io.File(path).writeAsStringSync(s);
77 expect(await file.readAsBytes(), UTF8.encode(s));
78 }
79
80 test_readAsString_badUtf8() {
81 new io.File(path).writeAsBytesSync([0xc0, 0x40]); // Invalid UTF-8
82 expect(file.readAsString(), throwsException);
83 }
84
85 test_readAsString_doesNotExist() {
86 expect(file.readAsString(), throwsException);
87 }
88
89 test_readAsString_exists() async {
90 var s = 'contents';
91 new io.File(path).writeAsStringSync(s);
92 expect(await file.readAsString(), s);
93 }
94
95 test_readAsString_utf8() async {
96 var bytes = [0xe2, 0x82, 0xac]; // Unicode € symbol (in UTF-8)
97 new io.File(path).writeAsBytesSync(bytes);
98 expect(await file.readAsString(), '\u20ac');
99 }
100 }
101
102 @reflectiveTest
103 class PhysicalFileSystemTest extends _BaseTest {
104 Uri tempUri;
105
106 setUp() {
107 super.setUp();
108 tempUri = new Uri.directory(tempPath);
109 }
110
111 test_entityForPath() {
112 var path = p.join(tempPath, 'file.txt');
113 expect(PhysicalFileSystem.instance.entityForPath(path).path, path);
114 }
115
116 test_entityForPath_absolutize() {
117 expect(PhysicalFileSystem.instance.entityForPath('file.txt').path,
118 new io.File('file.txt').absolute.path);
119 }
120
121 test_entityForPath_normalize_dot() {
122 expect(
123 PhysicalFileSystem.instance
124 .entityForPath(p.join(tempPath, '.', 'file.txt'))
125 .path,
126 p.join(tempPath, 'file.txt'));
127 }
128
129 test_entityForPath_normalize_dotDot() {
130 expect(
131 PhysicalFileSystem.instance
132 .entityForPath(p.join(tempPath, 'foo', '..', 'file.txt'))
133 .path,
134 p.join(tempPath, 'file.txt'));
135 }
136
137 test_entityForUri() {
138 expect(
139 PhysicalFileSystem.instance
140 .entityForUri(Uri.parse('$tempUri/file.txt'))
141 .path,
142 p.join(tempPath, 'file.txt'));
143 }
144
145 test_entityForUri_bareUri_absolute() {
146 expect(
147 () => PhysicalFileSystem.instance.entityForUri(Uri.parse('/file.txt')),
148 throwsA(new isInstanceOf<Error>()));
149 }
150
151 test_entityForUri_bareUri_relative() {
152 expect(
153 () => PhysicalFileSystem.instance.entityForUri(Uri.parse('file.txt')),
154 throwsA(new isInstanceOf<Error>()));
155 }
156
157 test_entityForUri_nonFileUri() {
158 expect(
159 () => PhysicalFileSystem.instance
160 .entityForUri(Uri.parse('package:foo/bar.dart')),
161 throwsA(new isInstanceOf<Error>()));
162 }
163
164 test_entityForUri_normalize_dot() {
165 expect(
166 PhysicalFileSystem.instance
167 .entityForUri(Uri.parse('$tempUri/./file.txt'))
168 .path,
169 p.join(tempPath, 'file.txt'));
170 }
171
172 test_entityForUri_normalize_dotDot() {
173 expect(
174 PhysicalFileSystem.instance
175 .entityForUri(Uri.parse('$tempUri/foo/../file.txt'))
176 .path,
177 p.join(tempPath, 'file.txt'));
178 }
179 }
180
181 class _BaseTest {
182 io.Directory tempDirectory;
183 String tempPath;
184
185 setUp() {
186 tempDirectory = io.Directory.systemTemp.createTempSync('test_file_system');
187 tempPath = tempDirectory.absolute.path;
188 }
189
190 tearDown() {
191 tempDirectory.deleteSync(recursive: true);
192 }
193 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698