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