OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 import 'package:path/path.dart' as p; |
| 6 import 'package:test/test.dart'; |
| 7 import 'package:watcher/src/path_set.dart'; |
| 8 |
| 9 import 'utils.dart'; |
| 10 |
| 11 Matcher containsPath(String path) => predicate((set) => |
| 12 set is PathSet && set.contains(path), |
| 13 'set contains "$path"'); |
| 14 |
| 15 Matcher containsDir(String path) => predicate((set) => |
| 16 set is PathSet && set.containsDir(path), |
| 17 'set contains directory "$path"'); |
| 18 |
| 19 void main() { |
| 20 var set; |
| 21 setUp(() => set = new PathSet("root")); |
| 22 |
| 23 group("adding a path", () { |
| 24 test("stores the path in the set", () { |
| 25 set.add("root/path/to/file"); |
| 26 expect(set, containsPath("root/path/to/file")); |
| 27 }); |
| 28 |
| 29 test("that's a subdir of another path keeps both in the set", () { |
| 30 set.add("root/path"); |
| 31 set.add("root/path/to/file"); |
| 32 expect(set, containsPath("root/path")); |
| 33 expect(set, containsPath("root/path/to/file")); |
| 34 }); |
| 35 |
| 36 test("that's not normalized normalizes the path before storing it", () { |
| 37 set.add("root/../root/path/to/../to/././file"); |
| 38 expect(set, containsPath("root/path/to/file")); |
| 39 }); |
| 40 |
| 41 test("that's absolute normalizes the path before storing it", () { |
| 42 set.add(p.absolute("root/path/to/file")); |
| 43 expect(set, containsPath("root/path/to/file")); |
| 44 }); |
| 45 |
| 46 test("that's not beneath the root throws an error", () { |
| 47 expect(() => set.add("path/to/file"), throwsArgumentError); |
| 48 }); |
| 49 }); |
| 50 |
| 51 group("removing a path", () { |
| 52 test("that's in the set removes and returns that path", () { |
| 53 set.add("root/path/to/file"); |
| 54 expect(set.remove("root/path/to/file"), |
| 55 unorderedEquals([p.normalize("root/path/to/file")])); |
| 56 expect(set, isNot(containsPath("root/path/to/file"))); |
| 57 }); |
| 58 |
| 59 test("that's not in the set returns an empty set", () { |
| 60 set.add("root/path/to/file"); |
| 61 expect(set.remove("root/path/to/nothing"), isEmpty); |
| 62 }); |
| 63 |
| 64 test("that's a directory removes and returns all files beneath it", () { |
| 65 set.add("root/outside"); |
| 66 set.add("root/path/to/one"); |
| 67 set.add("root/path/to/two"); |
| 68 set.add("root/path/to/sub/three"); |
| 69 |
| 70 expect(set.remove("root/path"), unorderedEquals([ |
| 71 "root/path/to/one", |
| 72 "root/path/to/two", |
| 73 "root/path/to/sub/three" |
| 74 ].map(p.normalize))); |
| 75 |
| 76 expect(set, containsPath("root/outside")); |
| 77 expect(set, isNot(containsPath("root/path/to/one"))); |
| 78 expect(set, isNot(containsPath("root/path/to/two"))); |
| 79 expect(set, isNot(containsPath("root/path/to/sub/three"))); |
| 80 }); |
| 81 |
| 82 test("that's a directory in the set removes and returns it and all files " |
| 83 "beneath it", () { |
| 84 set.add("root/path"); |
| 85 set.add("root/path/to/one"); |
| 86 set.add("root/path/to/two"); |
| 87 set.add("root/path/to/sub/three"); |
| 88 |
| 89 expect(set.remove("root/path"), unorderedEquals([ |
| 90 "root/path", |
| 91 "root/path/to/one", |
| 92 "root/path/to/two", |
| 93 "root/path/to/sub/three" |
| 94 ].map(p.normalize))); |
| 95 |
| 96 expect(set, isNot(containsPath("root/path"))); |
| 97 expect(set, isNot(containsPath("root/path/to/one"))); |
| 98 expect(set, isNot(containsPath("root/path/to/two"))); |
| 99 expect(set, isNot(containsPath("root/path/to/sub/three"))); |
| 100 }); |
| 101 |
| 102 test("that's not normalized removes and returns the normalized path", () { |
| 103 set.add("root/path/to/file"); |
| 104 expect(set.remove("root/../root/path/to/../to/./file"), |
| 105 unorderedEquals([p.normalize("root/path/to/file")])); |
| 106 }); |
| 107 |
| 108 test("that's absolute removes and returns the normalized path", () { |
| 109 set.add("root/path/to/file"); |
| 110 expect(set.remove(p.absolute("root/path/to/file")), |
| 111 unorderedEquals([p.normalize("root/path/to/file")])); |
| 112 }); |
| 113 |
| 114 test("that's not beneath the root throws an error", () { |
| 115 expect(() => set.remove("path/to/file"), throwsArgumentError); |
| 116 }); |
| 117 }); |
| 118 |
| 119 group("containsPath()", () { |
| 120 test("returns false for a non-existent path", () { |
| 121 set.add("root/path/to/file"); |
| 122 expect(set, isNot(containsPath("root/path/to/nothing"))); |
| 123 }); |
| 124 |
| 125 test("returns false for a directory that wasn't added explicitly", () { |
| 126 set.add("root/path/to/file"); |
| 127 expect(set, isNot(containsPath("root/path"))); |
| 128 }); |
| 129 |
| 130 test("returns true for a directory that was added explicitly", () { |
| 131 set.add("root/path"); |
| 132 set.add("root/path/to/file"); |
| 133 expect(set, containsPath("root/path")); |
| 134 }); |
| 135 |
| 136 test("with a non-normalized path normalizes the path before looking it up", |
| 137 () { |
| 138 set.add("root/path/to/file"); |
| 139 expect(set, containsPath("root/../root/path/to/../to/././file")); |
| 140 }); |
| 141 |
| 142 test("with an absolute path normalizes the path before looking it up", () { |
| 143 set.add("root/path/to/file"); |
| 144 expect(set, containsPath(p.absolute("root/path/to/file"))); |
| 145 }); |
| 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 }); |
| 151 |
| 152 group("containsDir()", () { |
| 153 test("returns true for a directory that was added implicitly", () { |
| 154 set.add("root/path/to/file"); |
| 155 expect(set, containsDir("root/path")); |
| 156 expect(set, containsDir("root/path/to")); |
| 157 }); |
| 158 |
| 159 test("returns true for a directory that was added explicitly", () { |
| 160 set.add("root/path"); |
| 161 set.add("root/path/to/file"); |
| 162 expect(set, containsDir("root/path")); |
| 163 }); |
| 164 |
| 165 test("returns false for a directory that wasn't added", () { |
| 166 expect(set, isNot(containsDir("root/nothing"))); |
| 167 }); |
| 168 |
| 169 test("returns false for a non-directory path that was added", () { |
| 170 set.add("root/path/to/file"); |
| 171 expect(set, isNot(containsDir("root/path/to/file"))); |
| 172 }); |
| 173 |
| 174 test("returns false for a directory that was added implicitly and then " |
| 175 "removed implicitly", () { |
| 176 set.add("root/path/to/file"); |
| 177 set.remove("root/path/to/file"); |
| 178 expect(set, isNot(containsDir("root/path"))); |
| 179 }); |
| 180 |
| 181 test("returns false for a directory that was added explicitly whose " |
| 182 "children were then removed", () { |
| 183 set.add("root/path"); |
| 184 set.add("root/path/to/file"); |
| 185 set.remove("root/path/to/file"); |
| 186 expect(set, isNot(containsDir("root/path"))); |
| 187 }); |
| 188 |
| 189 test("with a non-normalized path normalizes the path before looking it up", |
| 190 () { |
| 191 set.add("root/path/to/file"); |
| 192 expect(set, containsDir("root/../root/path/to/../to/.")); |
| 193 }); |
| 194 |
| 195 test("with an absolute path normalizes the path before looking it up", () { |
| 196 set.add("root/path/to/file"); |
| 197 expect(set, containsDir(p.absolute("root/path"))); |
| 198 }); |
| 199 }); |
| 200 |
| 201 group("toSet", () { |
| 202 test("returns paths added to the set", () { |
| 203 set.add("root/path"); |
| 204 set.add("root/path/to/one"); |
| 205 set.add("root/path/to/two"); |
| 206 |
| 207 expect(set.toSet(), unorderedEquals([ |
| 208 "root/path", |
| 209 "root/path/to/one", |
| 210 "root/path/to/two", |
| 211 ].map(p.normalize))); |
| 212 }); |
| 213 |
| 214 test("doesn't return paths removed from the set", () { |
| 215 set.add("root/path/to/one"); |
| 216 set.add("root/path/to/two"); |
| 217 set.remove("root/path/to/two"); |
| 218 |
| 219 expect(set.toSet(), unorderedEquals([p.normalize("root/path/to/one")])); |
| 220 }); |
| 221 }); |
| 222 |
| 223 group("clear", () { |
| 224 test("removes all paths from the set", () { |
| 225 set.add("root/path"); |
| 226 set.add("root/path/to/one"); |
| 227 set.add("root/path/to/two"); |
| 228 |
| 229 set.clear(); |
| 230 expect(set.toSet(), isEmpty); |
| 231 }); |
| 232 }); |
| 233 } |
OLD | NEW |