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

Side by Side Diff: pkg/analyzer/test/src/util/absolute_path_test.dart

Issue 1511833004: Validate that root paths are absolute and normalized. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 5 years 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
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 test.analyzer.src.util.absolute_path; 5 library test.analyzer.src.util.absolute_path;
6 6
7 import 'package:analyzer/src/util/absolute_path.dart'; 7 import 'package:analyzer/src/util/absolute_path.dart';
8 import 'package:unittest/unittest.dart'; 8 import 'package:unittest/unittest.dart';
9 9
10 import '../../reflective_tests.dart'; 10 import '../../reflective_tests.dart';
11 import '../../utils.dart'; 11 import '../../utils.dart';
12 12
13 main() { 13 main() {
14 initializeTestEnvironment(); 14 initializeTestEnvironment();
15 runReflectiveTests(AbsolutePathContextPosixTest); 15 runReflectiveTests(AbsolutePathContextPosixTest);
16 runReflectiveTests(AbsolutePathContextWindowsTest); 16 runReflectiveTests(AbsolutePathContextWindowsTest);
17 } 17 }
18 18
19 @reflectiveTest 19 @reflectiveTest
20 class AbsolutePathContextPosixTest { 20 class AbsolutePathContextPosixTest {
21 AbsolutePathContext context = new AbsolutePathContext(r'/'); 21 AbsolutePathContext context = new AbsolutePathContext(false);
22 22
23 void test_append() { 23 void test_append() {
24 expect(context.append(r'/path/to', r'foo.dart'), r'/path/to/foo.dart'); 24 expect(context.append(r'/path/to', r'foo.dart'), r'/path/to/foo.dart');
25 } 25 }
26 26
27 void test_basename() { 27 void test_basename() {
28 expect(context.basename(r'/path/to/foo.dart'), r'foo.dart'); 28 expect(context.basename(r'/path/to/foo.dart'), r'foo.dart');
29 expect(context.basename(r'/path/to'), r'to'); 29 expect(context.basename(r'/path/to'), r'to');
30 expect(context.basename(r'/path'), r'path'); 30 expect(context.basename(r'/path'), r'path');
31 expect(context.basename(r'/'), r''); 31 expect(context.basename(r'/'), r'');
32 } 32 }
33 33
34 void test_dirname() { 34 void test_dirname() {
35 expect(context.dirname(r'/path/to/foo.dart'), r'/path/to'); 35 expect(context.dirname(r'/path/to/foo.dart'), r'/path/to');
36 expect(context.dirname(r'/path/to'), r'/path'); 36 expect(context.dirname(r'/path/to'), r'/path');
37 expect(context.dirname(r'/path'), r'/'); 37 expect(context.dirname(r'/path'), r'/');
38 expect(context.dirname(r'/'), r'/'); 38 expect(context.dirname(r'/'), r'/');
39 } 39 }
40 40
41 void test_isValid_absolute() {
42 expect(context.isValid(r'/foo/bar'), isTrue);
43 expect(context.isValid(r'/foo'), isTrue);
44 expect(context.isValid(r'/'), isTrue);
45 expect(context.isValid(r''), isFalse);
46 expect(context.isValid(r'foo/bar'), isFalse);
47 }
48
49 void test_isValid_normalized() {
50 expect(context.isValid(r'/foo/bar'), isTrue);
51 expect(context.isValid(r'/foo/..bar'), isTrue);
52 expect(context.isValid(r'/foo/.bar/baz'), isTrue);
53 expect(context.isValid(r'/foo/...'), isTrue);
54 expect(context.isValid(r'/foo/.../bar'), isTrue);
55 expect(context.isValid(r'/foo/.bar/.'), isFalse);
56 expect(context.isValid(r'/foo/bar/../baz'), isFalse);
57 expect(context.isValid(r'/foo/bar/..'), isFalse);
58 expect(context.isValid(r'/foo/./bar'), isFalse);
59 expect(context.isValid(r'/.'), isFalse);
60 }
61
41 void test_isWithin() { 62 void test_isWithin() {
42 expect(context.isWithin(r'/root/path', r'/root/path/a'), isTrue); 63 expect(context.isWithin(r'/root/path', r'/root/path/a'), isTrue);
43 expect(context.isWithin(r'/root/path', r'/root/other'), isFalse); 64 expect(context.isWithin(r'/root/path', r'/root/other'), isFalse);
44 expect(context.isWithin(r'/root/path', r'/root/path'), isFalse); 65 expect(context.isWithin(r'/root/path', r'/root/path'), isFalse);
45 } 66 }
46 67
47 void test_split() { 68 void test_split() {
48 expect(context.split(r'/path/to/foo'), [r'', r'path', r'to', r'foo']); 69 expect(context.split(r'/path/to/foo'), [r'', r'path', r'to', r'foo']);
49 expect(context.split(r'/path'), [r'', r'path']); 70 expect(context.split(r'/path'), [r'', r'path']);
50 } 71 }
51 72
52 void test_suffix() { 73 void test_suffix() {
53 expect(context.suffix(r'/root/path', r'/root/path/a/b.dart'), r'a/b.dart'); 74 expect(context.suffix(r'/root/path', r'/root/path/a/b.dart'), r'a/b.dart');
54 expect(context.suffix(r'/root/path', r'/root/other.dart'), isNull); 75 expect(context.suffix(r'/root/path', r'/root/other.dart'), isNull);
55 } 76 }
56 } 77 }
57 78
58 @reflectiveTest 79 @reflectiveTest
59 class AbsolutePathContextWindowsTest { 80 class AbsolutePathContextWindowsTest {
60 AbsolutePathContext context = new AbsolutePathContext(r'\'); 81 AbsolutePathContext context = new AbsolutePathContext(true);
61 82
62 void test_append() { 83 void test_append() {
63 expect(context.append(r'C:\path\to', r'foo.dart'), r'C:\path\to\foo.dart'); 84 expect(context.append(r'C:\path\to', r'foo.dart'), r'C:\path\to\foo.dart');
64 } 85 }
65 86
66 void test_basename() { 87 void test_basename() {
67 expect(context.basename(r'C:\path\to\foo.dart'), r'foo.dart'); 88 expect(context.basename(r'C:\path\to\foo.dart'), r'foo.dart');
68 expect(context.basename(r'C:\path\to'), r'to'); 89 expect(context.basename(r'C:\path\to'), r'to');
69 expect(context.basename(r'C:\path'), r'path'); 90 expect(context.basename(r'C:\path'), r'path');
70 expect(context.basename(r'C:\'), r''); 91 expect(context.basename(r'C:\'), r'');
71 } 92 }
72 93
73 void test_dirname() { 94 void test_dirname() {
74 expect(context.dirname(r'C:\path\to\foo.dart'), r'C:\path\to'); 95 expect(context.dirname(r'C:\path\to\foo.dart'), r'C:\path\to');
75 expect(context.dirname(r'C:\path\to'), r'C:\path'); 96 expect(context.dirname(r'C:\path\to'), r'C:\path');
76 expect(context.dirname(r'C:\path'), r'C:\'); 97 expect(context.dirname(r'C:\path'), r'C:\');
77 expect(context.dirname(r'C:\'), r'C:\'); 98 expect(context.dirname(r'C:\'), r'C:\');
78 } 99 }
79 100
101 void test_isValid_absolute() {
102 expect(context.isValid(r'C:\foo\bar'), isTrue);
103 expect(context.isValid(r'c:\foo\bar'), isTrue);
104 expect(context.isValid(r'D:\foo\bar'), isTrue);
105 expect(context.isValid(r'C:\foo'), isTrue);
106 expect(context.isValid(r'C:\'), isTrue);
107 expect(context.isValid(r''), isFalse);
108 expect(context.isValid(r'foo\bar'), isFalse);
109 }
110
111 void test_isValid_normalized() {
112 expect(context.isValid(r'C:\foo\bar'), isTrue);
113 expect(context.isValid(r'C:\foo\..bar'), isTrue);
114 expect(context.isValid(r'C:\foo\.bar\baz'), isTrue);
115 expect(context.isValid(r'C:\foo\...'), isTrue);
116 expect(context.isValid(r'C:\foo\...\bar'), isTrue);
117 expect(context.isValid(r'C:\foo\.bar\.'), isFalse);
118 expect(context.isValid(r'C:\foo\bar\..\baz'), isFalse);
119 expect(context.isValid(r'C:\foo\bar\..'), isFalse);
120 expect(context.isValid(r'C:\foo\.\bar'), isFalse);
121 expect(context.isValid(r'C:\.'), isFalse);
122 }
123
80 void test_isWithin() { 124 void test_isWithin() {
81 expect(context.isWithin(r'C:\root\path', r'C:\root\path\a'), isTrue); 125 expect(context.isWithin(r'C:\root\path', r'C:\root\path\a'), isTrue);
82 expect(context.isWithin(r'C:\root\path', r'C:\root\other'), isFalse); 126 expect(context.isWithin(r'C:\root\path', r'C:\root\other'), isFalse);
83 expect(context.isWithin(r'C:\root\path', r'C:\root\path'), isFalse); 127 expect(context.isWithin(r'C:\root\path', r'C:\root\path'), isFalse);
84 } 128 }
85 129
86 void test_split() { 130 void test_split() {
87 expect(context.split(r'C:\path\to\foo'), [r'C:', r'path', r'to', r'foo']); 131 expect(context.split(r'C:\path\to\foo'), [r'C:', r'path', r'to', r'foo']);
88 expect(context.split(r'C:\path'), [r'C:', r'path']); 132 expect(context.split(r'C:\path'), [r'C:', r'path']);
89 } 133 }
90 134
91 void test_suffix() { 135 void test_suffix() {
92 expect( 136 expect(
93 context.suffix(r'C:\root\path', r'C:\root\path\a\b.dart'), r'a\b.dart'); 137 context.suffix(r'C:\root\path', r'C:\root\path\a\b.dart'), r'a\b.dart');
94 expect(context.suffix(r'C:\root\path', r'C:\root\other.dart'), isNull); 138 expect(context.suffix(r'C:\root\path', r'C:\root\other.dart'), isNull);
95 } 139 }
96 } 140 }
OLDNEW
« pkg/analyzer/lib/src/util/absolute_path.dart ('K') | « pkg/analyzer/lib/src/util/absolute_path.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698