| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library analyzer.test.resource_utils; | 5 library analyzer.test.resource_utils; |
| 6 | 6 |
| 7 import 'dart:core' hide Resource; | 7 import 'dart:core' hide Resource; |
| 8 | 8 |
| 9 import 'package:analyzer/file_system/file_system.dart'; | 9 import 'package:analyzer/file_system/file_system.dart'; |
| 10 import 'package:analyzer/file_system/memory_file_system.dart'; | 10 import 'package:analyzer/file_system/memory_file_system.dart'; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 | 25 |
| 26 /** | 26 /** |
| 27 * Assert that the given path is posix. | 27 * Assert that the given path is posix. |
| 28 */ | 28 */ |
| 29 void expectPosixPath(String posixPath) { | 29 void expectPosixPath(String posixPath) { |
| 30 expect(posixPath.indexOf('\\'), -1, | 30 expect(posixPath.indexOf('\\'), -1, |
| 31 reason: 'Expected posix path, but found $posixPath'); | 31 reason: 'Expected posix path, but found $posixPath'); |
| 32 } | 32 } |
| 33 | 33 |
| 34 /** | 34 /** |
| 35 * Translate the given posixPath to a file URI appropriate for the |
| 36 * platform on which the tests are executing. |
| 37 */ |
| 38 String posixToOSFileUri(String posixPath) { |
| 39 expectPosixPath(posixPath); |
| 40 return isWindows ? 'file:///C:$posixPath' : 'file://$posixPath'; |
| 41 } |
| 42 |
| 43 /** |
| 35 * Translate the given posixPath to a path appropriate for the | 44 * Translate the given posixPath to a path appropriate for the |
| 36 * platform on which the tests are executing. | 45 * platform on which the tests are executing. |
| 37 */ | 46 */ |
| 38 String posixToOSPath(String posixPath) { | 47 String posixToOSPath(String posixPath) { |
| 39 expectPosixPath(posixPath); | 48 expectPosixPath(posixPath); |
| 40 if (isWindows) { | 49 if (isWindows) { |
| 41 String windowsPath = posixPath.replaceAll('/', '\\'); | 50 String windowsPath = posixPath.replaceAll('/', '\\'); |
| 42 if (posixPath.startsWith('/')) { | 51 if (posixPath.startsWith('/')) { |
| 43 return 'C:$windowsPath'; | 52 return 'C:$windowsPath'; |
| 44 } | 53 } |
| 45 return windowsPath; | 54 return windowsPath; |
| 46 } | 55 } |
| 47 return posixPath; | 56 return posixPath; |
| 48 } | 57 } |
| 49 | 58 |
| 50 /** | 59 /** |
| 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]. | 60 * A convenience utility for setting up a test [MemoryResourceProvider]. |
| 61 * All supplied paths are assumed to be in [path.posix] format | 61 * All supplied paths are assumed to be in [path.posix] format |
| 62 * and are automatically translated to [path.context]. | 62 * and are automatically translated to [path.context]. |
| 63 * | 63 * |
| 64 * This class intentionally does not implement [ResourceProvider] | 64 * This class intentionally does not implement [ResourceProvider] |
| 65 * directly or indirectly so that it cannot be used as a resource provider. | 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 | 66 * We do not want functionality under test to interact with a resource provider |
| 67 * that automatically translates paths. | 67 * that automatically translates paths. |
| 68 */ | 68 */ |
| 69 class TestPathTranslator { | 69 class TestPathTranslator { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 89 final ResourceProvider _provider; | 89 final ResourceProvider _provider; |
| 90 | 90 |
| 91 TestResourceProvider(this._provider) { | 91 TestResourceProvider(this._provider) { |
| 92 expect(_provider.absolutePathContext.separator, isWindows ? '\\' : '/'); | 92 expect(_provider.absolutePathContext.separator, isWindows ? '\\' : '/'); |
| 93 } | 93 } |
| 94 | 94 |
| 95 @override | 95 @override |
| 96 AbsolutePathContext get absolutePathContext => _provider.absolutePathContext; | 96 AbsolutePathContext get absolutePathContext => _provider.absolutePathContext; |
| 97 | 97 |
| 98 @override | 98 @override |
| 99 path.Context get pathContext => _provider.pathContext; |
| 100 |
| 101 @override |
| 99 File getFile(String path) => _provider.getFile(_assertPath(path)); | 102 File getFile(String path) => _provider.getFile(_assertPath(path)); |
| 100 | 103 |
| 101 @override | 104 @override |
| 102 Folder getFolder(String path) => _provider.getFolder(_assertPath(path)); | 105 Folder getFolder(String path) => _provider.getFolder(_assertPath(path)); |
| 103 | 106 |
| 104 @override | 107 @override |
| 105 Resource getResource(String path) => _provider.getResource(_assertPath(path)); | 108 Resource getResource(String path) => _provider.getResource(_assertPath(path)); |
| 106 | 109 |
| 107 @override | 110 @override |
| 108 Folder getStateLocation(String pluginId) => | 111 Folder getStateLocation(String pluginId) => |
| 109 _provider.getStateLocation(pluginId); | 112 _provider.getStateLocation(pluginId); |
| 110 | 113 |
| 111 @override | |
| 112 path.Context get pathContext => _provider.pathContext; | |
| 113 | |
| 114 /** | 114 /** |
| 115 * Assert that the given path is valid for the OS platform on which the | 115 * Assert that the given path is valid for the OS platform on which the |
| 116 * tests are running. | 116 * tests are running. |
| 117 */ | 117 */ |
| 118 String _assertPath(String path) { | 118 String _assertPath(String path) { |
| 119 if (isWindows) { | 119 if (isWindows) { |
| 120 if (path.contains('/')) { | 120 if (path.contains('/')) { |
| 121 fail('Expected windows path, but found: $path'); | 121 fail('Expected windows path, but found: $path'); |
| 122 } | 122 } |
| 123 } else { | 123 } else { |
| 124 if (path.contains('\\')) { | 124 if (path.contains('\\')) { |
| 125 fail('Expected posix path, but found: $path'); | 125 fail('Expected posix path, but found: $path'); |
| 126 } | 126 } |
| 127 } | 127 } |
| 128 return path; | 128 return path; |
| 129 } | 129 } |
| 130 } | 130 } |
| OLD | NEW |