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

Side by Side Diff: pkg/analyzer/test/resource_utils.dart

Issue 1777913003: extract resource path test utils (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 9 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
« no previous file with comments | « no previous file | pkg/analyzer/test/source/embedder_test.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) 2015, 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 analyzer.test.resource_utils;
6
7 import 'dart:core' hide Resource;
8
9 import 'package:analyzer/file_system/file_system.dart';
10 import 'package:analyzer/file_system/memory_file_system.dart';
11 import 'package:analyzer/src/util/absolute_path.dart';
12 import 'package:path/path.dart' as path;
13 import 'package:unittest/unittest.dart';
14
15
16 bool get isWindows => path.Style.platform == path.Style.windows;
17
18 /**
19 * Assert that the given path is posix and absolute.
20 */
21 void expectAbsolutePosixPath(String posixPath) {
22 expect(posixPath, startsWith('/'),
23 reason: 'Expected absolute posix path, but found $posixPath');
24 }
25
26 /**
27 * Assert that the given path is posix.
28 */
29 void expectPosixPath(String posixPath) {
30 expect(posixPath.indexOf('\\'), -1,
31 reason: 'Expected posix path, but found $posixPath');
32 }
33
34 /**
35 * Translate the given posixPath to a path appropriate for the
36 * platform on which the tests are executing.
37 */
38 String posixToOSPath(String posixPath) {
39 expectPosixPath(posixPath);
40 if (isWindows) {
41 String windowsPath = posixPath.replaceAll('/', '\\');
42 if (posixPath.startsWith('/')) {
43 return 'C:$windowsPath';
44 }
45 return windowsPath;
46 }
47 return posixPath;
48 }
49
50 /**
51 * Translate the given posixPath to a file URI appropriate for the
52 * platform on which the tests are executing.
53 */
54 String posixToOSFileUri(String posixPath) {
55 expectPosixPath(posixPath);
56 return isWindows ? 'file:///C:$posixPath' : 'file://$posixPath';
57 }
58
59 /**
60 * A convenience utility for setting up a test [MemoryResourceProvider].
61 * All supplied paths are assumed to be in [path.posix] format
62 * and are automatically translated to [path.context].
63 *
64 * This class intentionally does not implement [ResourceProvider]
65 * directly or indirectly so that it cannot be used as a resource provider.
66 * We do not want functionality under test to interact with a resource provider
67 * that automatically translates paths.
68 */
69 class TestPathTranslator {
70 final MemoryResourceProvider _provider;
71
72 TestPathTranslator(this._provider);
73
74 Resource getResource(String posixPath) =>
75 _provider.getResource(posixToOSPath(posixPath));
76
77 File newFile(String posixPath, String content) =>
78 _provider.newFile(posixToOSPath(posixPath), content);
79
80 Folder newFolder(String posixPath) =>
81 _provider.newFolder(posixToOSPath(posixPath));
82 }
83
84 /**
85 * A resource provider for testing that asserts that any supplied paths
86 * are appropriate for the OS platform on which the tests are running.
87 */
88 class TestResourceProvider implements ResourceProvider {
89 final ResourceProvider _provider;
90
91 TestResourceProvider(this._provider) {
92 expect(_provider.absolutePathContext.separator, isWindows ? '\\' : '/');
93 }
94
95 @override
96 AbsolutePathContext get absolutePathContext => _provider.absolutePathContext;
97
98 @override
99 File getFile(String path) => _provider.getFile(_assertPath(path));
100
101 @override
102 Folder getFolder(String path) => _provider.getFolder(_assertPath(path));
103
104 @override
105 Resource getResource(String path) => _provider.getResource(_assertPath(path));
106
107 @override
108 Folder getStateLocation(String pluginId) =>
109 _provider.getStateLocation(pluginId);
110
111 @override
112 path.Context get pathContext => _provider.pathContext;
113
114 /**
115 * Assert that the given path is valid for the OS platform on which the
116 * tests are running.
117 */
118 String _assertPath(String path) {
119 if (isWindows) {
120 if (path.contains('/')) {
121 fail('Expected windows path, but found: $path');
122 }
123 } else {
124 if (path.contains('\\')) {
125 fail('Expected posix path, but found: $path');
126 }
127 }
128 return path;
129 }
130 }
OLDNEW
« no previous file with comments | « no previous file | pkg/analyzer/test/source/embedder_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698