| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 import 'package:path/path.dart' as p; | 5 import 'package:path/path.dart' as p; |
| 6 import 'package:test/test.dart'; | 6 import 'package:test/test.dart'; |
| 7 import 'package:watcher/src/path_set.dart'; | 7 import 'package:watcher/src/path_set.dart'; |
| 8 | 8 |
| 9 import 'utils.dart'; | |
| 10 | |
| 11 Matcher containsPath(String path) => predicate((set) => | 9 Matcher containsPath(String path) => predicate((set) => |
| 12 set is PathSet && set.contains(path), | 10 set is PathSet && set.contains(path), |
| 13 'set contains "$path"'); | 11 'set contains "$path"'); |
| 14 | 12 |
| 15 Matcher containsDir(String path) => predicate((set) => | 13 Matcher containsDir(String path) => predicate((set) => |
| 16 set is PathSet && set.containsDir(path), | 14 set is PathSet && set.containsDir(path), |
| 17 'set contains directory "$path"'); | 15 'set contains directory "$path"'); |
| 18 | 16 |
| 19 void main() { | 17 void main() { |
| 20 var set; | 18 var set; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 35 | 33 |
| 36 test("that's not normalized normalizes the path before storing it", () { | 34 test("that's not normalized normalizes the path before storing it", () { |
| 37 set.add("root/../root/path/to/../to/././file"); | 35 set.add("root/../root/path/to/../to/././file"); |
| 38 expect(set, containsPath("root/path/to/file")); | 36 expect(set, containsPath("root/path/to/file")); |
| 39 }); | 37 }); |
| 40 | 38 |
| 41 test("that's absolute normalizes the path before storing it", () { | 39 test("that's absolute normalizes the path before storing it", () { |
| 42 set.add(p.absolute("root/path/to/file")); | 40 set.add(p.absolute("root/path/to/file")); |
| 43 expect(set, containsPath("root/path/to/file")); | 41 expect(set, containsPath("root/path/to/file")); |
| 44 }); | 42 }); |
| 45 | |
| 46 test("that's not beneath the root throws an error", () { | |
| 47 expect(() => set.add("path/to/file"), throwsArgumentError); | |
| 48 }); | |
| 49 }); | 43 }); |
| 50 | 44 |
| 51 group("removing a path", () { | 45 group("removing a path", () { |
| 52 test("that's in the set removes and returns that path", () { | 46 test("that's in the set removes and returns that path", () { |
| 53 set.add("root/path/to/file"); | 47 set.add("root/path/to/file"); |
| 54 expect(set.remove("root/path/to/file"), | 48 expect(set.remove("root/path/to/file"), |
| 55 unorderedEquals([p.normalize("root/path/to/file")])); | 49 unorderedEquals([p.normalize("root/path/to/file")])); |
| 56 expect(set, isNot(containsPath("root/path/to/file"))); | 50 expect(set, isNot(containsPath("root/path/to/file"))); |
| 57 }); | 51 }); |
| 58 | 52 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 71 "root/path/to/one", | 65 "root/path/to/one", |
| 72 "root/path/to/two", | 66 "root/path/to/two", |
| 73 "root/path/to/sub/three" | 67 "root/path/to/sub/three" |
| 74 ].map(p.normalize))); | 68 ].map(p.normalize))); |
| 75 | 69 |
| 76 expect(set, containsPath("root/outside")); | 70 expect(set, containsPath("root/outside")); |
| 77 expect(set, isNot(containsPath("root/path/to/one"))); | 71 expect(set, isNot(containsPath("root/path/to/one"))); |
| 78 expect(set, isNot(containsPath("root/path/to/two"))); | 72 expect(set, isNot(containsPath("root/path/to/two"))); |
| 79 expect(set, isNot(containsPath("root/path/to/sub/three"))); | 73 expect(set, isNot(containsPath("root/path/to/sub/three"))); |
| 80 }); | 74 }); |
| 81 | 75 |
| 82 test("that's a directory in the set removes and returns it and all files " | 76 test("that's a directory in the set removes and returns it and all files " |
| 83 "beneath it", () { | 77 "beneath it", () { |
| 84 set.add("root/path"); | 78 set.add("root/path"); |
| 85 set.add("root/path/to/one"); | 79 set.add("root/path/to/one"); |
| 86 set.add("root/path/to/two"); | 80 set.add("root/path/to/two"); |
| 87 set.add("root/path/to/sub/three"); | 81 set.add("root/path/to/sub/three"); |
| 88 | 82 |
| 89 expect(set.remove("root/path"), unorderedEquals([ | 83 expect(set.remove("root/path"), unorderedEquals([ |
| 90 "root/path", | 84 "root/path", |
| 91 "root/path/to/one", | 85 "root/path/to/one", |
| (...skipping 11 matching lines...) Expand all Loading... |
| 103 set.add("root/path/to/file"); | 97 set.add("root/path/to/file"); |
| 104 expect(set.remove("root/../root/path/to/../to/./file"), | 98 expect(set.remove("root/../root/path/to/../to/./file"), |
| 105 unorderedEquals([p.normalize("root/path/to/file")])); | 99 unorderedEquals([p.normalize("root/path/to/file")])); |
| 106 }); | 100 }); |
| 107 | 101 |
| 108 test("that's absolute removes and returns the normalized path", () { | 102 test("that's absolute removes and returns the normalized path", () { |
| 109 set.add("root/path/to/file"); | 103 set.add("root/path/to/file"); |
| 110 expect(set.remove(p.absolute("root/path/to/file")), | 104 expect(set.remove(p.absolute("root/path/to/file")), |
| 111 unorderedEquals([p.normalize("root/path/to/file")])); | 105 unorderedEquals([p.normalize("root/path/to/file")])); |
| 112 }); | 106 }); |
| 113 | |
| 114 test("that's not beneath the root throws an error", () { | |
| 115 expect(() => set.remove("path/to/file"), throwsArgumentError); | |
| 116 }); | |
| 117 }); | 107 }); |
| 118 | 108 |
| 119 group("containsPath()", () { | 109 group("containsPath()", () { |
| 120 test("returns false for a non-existent path", () { | 110 test("returns false for a non-existent path", () { |
| 121 set.add("root/path/to/file"); | 111 set.add("root/path/to/file"); |
| 122 expect(set, isNot(containsPath("root/path/to/nothing"))); | 112 expect(set, isNot(containsPath("root/path/to/nothing"))); |
| 123 }); | 113 }); |
| 124 | 114 |
| 125 test("returns false for a directory that wasn't added explicitly", () { | 115 test("returns false for a directory that wasn't added explicitly", () { |
| 126 set.add("root/path/to/file"); | 116 set.add("root/path/to/file"); |
| 127 expect(set, isNot(containsPath("root/path"))); | 117 expect(set, isNot(containsPath("root/path"))); |
| 128 }); | 118 }); |
| 129 | 119 |
| 130 test("returns true for a directory that was added explicitly", () { | 120 test("returns true for a directory that was added explicitly", () { |
| 131 set.add("root/path"); | 121 set.add("root/path"); |
| 132 set.add("root/path/to/file"); | 122 set.add("root/path/to/file"); |
| 133 expect(set, containsPath("root/path")); | 123 expect(set, containsPath("root/path")); |
| 134 }); | 124 }); |
| 135 | 125 |
| 136 test("with a non-normalized path normalizes the path before looking it up", | 126 test("with a non-normalized path normalizes the path before looking it up", |
| 137 () { | 127 () { |
| 138 set.add("root/path/to/file"); | 128 set.add("root/path/to/file"); |
| 139 expect(set, containsPath("root/../root/path/to/../to/././file")); | 129 expect(set, containsPath("root/../root/path/to/../to/././file")); |
| 140 }); | 130 }); |
| 141 | 131 |
| 142 test("with an absolute path normalizes the path before looking it up", () { | 132 test("with an absolute path normalizes the path before looking it up", () { |
| 143 set.add("root/path/to/file"); | 133 set.add("root/path/to/file"); |
| 144 expect(set, containsPath(p.absolute("root/path/to/file"))); | 134 expect(set, containsPath(p.absolute("root/path/to/file"))); |
| 145 }); | 135 }); |
| 146 | |
| 147 test("with a path that's not beneath the root throws an error", () { | |
| 148 expect(() => set.contains("path/to/file"), throwsArgumentError); | |
| 149 }); | |
| 150 }); | 136 }); |
| 151 | 137 |
| 152 group("containsDir()", () { | 138 group("containsDir()", () { |
| 153 test("returns true for a directory that was added implicitly", () { | 139 test("returns true for a directory that was added implicitly", () { |
| 154 set.add("root/path/to/file"); | 140 set.add("root/path/to/file"); |
| 155 expect(set, containsDir("root/path")); | 141 expect(set, containsDir("root/path")); |
| 156 expect(set, containsDir("root/path/to")); | 142 expect(set, containsDir("root/path/to")); |
| 157 }); | 143 }); |
| 158 | 144 |
| 159 test("returns true for a directory that was added explicitly", () { | 145 test("returns true for a directory that was added explicitly", () { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 set.add("root/path/to/file"); | 177 set.add("root/path/to/file"); |
| 192 expect(set, containsDir("root/../root/path/to/../to/.")); | 178 expect(set, containsDir("root/../root/path/to/../to/.")); |
| 193 }); | 179 }); |
| 194 | 180 |
| 195 test("with an absolute path normalizes the path before looking it up", () { | 181 test("with an absolute path normalizes the path before looking it up", () { |
| 196 set.add("root/path/to/file"); | 182 set.add("root/path/to/file"); |
| 197 expect(set, containsDir(p.absolute("root/path"))); | 183 expect(set, containsDir(p.absolute("root/path"))); |
| 198 }); | 184 }); |
| 199 }); | 185 }); |
| 200 | 186 |
| 201 group("toSet", () { | 187 group("paths", () { |
| 202 test("returns paths added to the set", () { | 188 test("returns paths added to the set", () { |
| 203 set.add("root/path"); | 189 set.add("root/path"); |
| 204 set.add("root/path/to/one"); | 190 set.add("root/path/to/one"); |
| 205 set.add("root/path/to/two"); | 191 set.add("root/path/to/two"); |
| 206 | 192 |
| 207 expect(set.toSet(), unorderedEquals([ | 193 expect(set.paths, unorderedEquals([ |
| 208 "root/path", | 194 "root/path", |
| 209 "root/path/to/one", | 195 "root/path/to/one", |
| 210 "root/path/to/two", | 196 "root/path/to/two", |
| 211 ].map(p.normalize))); | 197 ].map(p.normalize))); |
| 212 }); | 198 }); |
| 213 | 199 |
| 214 test("doesn't return paths removed from the set", () { | 200 test("doesn't return paths removed from the set", () { |
| 215 set.add("root/path/to/one"); | 201 set.add("root/path/to/one"); |
| 216 set.add("root/path/to/two"); | 202 set.add("root/path/to/two"); |
| 217 set.remove("root/path/to/two"); | 203 set.remove("root/path/to/two"); |
| 218 | 204 |
| 219 expect(set.toSet(), unorderedEquals([p.normalize("root/path/to/one")])); | 205 expect(set.paths, unorderedEquals([p.normalize("root/path/to/one")])); |
| 220 }); | 206 }); |
| 221 }); | 207 }); |
| 222 | 208 |
| 223 group("clear", () { | 209 group("clear", () { |
| 224 test("removes all paths from the set", () { | 210 test("removes all paths from the set", () { |
| 225 set.add("root/path"); | 211 set.add("root/path"); |
| 226 set.add("root/path/to/one"); | 212 set.add("root/path/to/one"); |
| 227 set.add("root/path/to/two"); | 213 set.add("root/path/to/two"); |
| 228 | 214 |
| 229 set.clear(); | 215 set.clear(); |
| 230 expect(set.toSet(), isEmpty); | 216 expect(set.paths, isEmpty); |
| 231 }); | 217 }); |
| 232 }); | 218 }); |
| 233 } | 219 } |
| OLD | NEW |