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 library barback.test.asset_set_test; |
| 6 |
| 7 import 'package:barback/barback.dart'; |
| 8 import 'package:unittest/unittest.dart'; |
| 9 |
| 10 import 'utils.dart'; |
| 11 |
| 12 main() { |
| 13 initConfig(); |
| 14 |
| 15 var fooId = new AssetId.parse("app|foo.txt"); |
| 16 var barId = new AssetId.parse("app|bar.txt"); |
| 17 var bazId = new AssetId.parse("app|baz.txt"); |
| 18 |
| 19 group(".from()", () { |
| 20 test("creates a set from an iterable", () { |
| 21 var set = new AssetSet.from([ |
| 22 new Asset.fromString(fooId, "foo"), |
| 23 new Asset.fromString(barId, "bar") |
| 24 ]); |
| 25 |
| 26 expect(set.containsId(fooId), isTrue); |
| 27 expect(set.containsId(barId), isTrue); |
| 28 expect(set.containsId(bazId), isFalse); |
| 29 }); |
| 30 }); |
| 31 |
| 32 group("[] operator", () { |
| 33 test("gets an asset with the given ID", () { |
| 34 var set = new AssetSet(); |
| 35 var foo = new Asset.fromString(fooId, "foo"); |
| 36 set.add(foo); |
| 37 |
| 38 expect(set[fooId], equals(foo)); |
| 39 }); |
| 40 |
| 41 test("returns null if no asset with the ID is in the set", () { |
| 42 var set = new AssetSet(); |
| 43 expect(set[fooId], isNull); |
| 44 }); |
| 45 }); |
| 46 |
| 47 group(".add()", () { |
| 48 test("adds the asset to the set", () { |
| 49 var set = new AssetSet(); |
| 50 var foo = new Asset.fromString(fooId, "foo"); |
| 51 set.add(foo); |
| 52 expect(set.contains(foo), isTrue); |
| 53 }); |
| 54 |
| 55 test("replaces a previously added asset with that ID", () { |
| 56 var set = new AssetSet(); |
| 57 set.add(new Asset.fromString(fooId, "before")); |
| 58 set.add(new Asset.fromString(fooId, "after")); |
| 59 expect(set[fooId].readAsString(), completion(equals("after"))); |
| 60 }); |
| 61 |
| 62 test("returns the added item", () { |
| 63 var set = new AssetSet(); |
| 64 var foo = new Asset.fromString(fooId, "foo"); |
| 65 expect(set.add(foo), equals(foo)); |
| 66 }); |
| 67 }); |
| 68 |
| 69 group(".addAll()", () { |
| 70 test("adds the assets to the set", () { |
| 71 var set = new AssetSet(); |
| 72 var foo = new Asset.fromString(fooId, "foo"); |
| 73 var bar = new Asset.fromString(barId, "bar"); |
| 74 set.addAll([foo, bar]); |
| 75 expect(set.contains(foo), isTrue); |
| 76 expect(set.contains(bar), isTrue); |
| 77 }); |
| 78 |
| 79 test("replaces assets earlier in the sequence with later ones", () { |
| 80 var set = new AssetSet(); |
| 81 var foo1 = new Asset.fromString(fooId, "before"); |
| 82 var foo2 = new Asset.fromString(fooId, "after"); |
| 83 set.addAll([foo1, foo2]); |
| 84 expect(set[fooId].readAsString(), completion(equals("after"))); |
| 85 }); |
| 86 }); |
| 87 |
| 88 group(".clear()", () { |
| 89 test("empties the set", () { |
| 90 var set = new AssetSet(); |
| 91 var foo = new Asset.fromString(fooId, "foo"); |
| 92 set.add(foo); |
| 93 set.clear(); |
| 94 |
| 95 expect(set.length, equals(0)); |
| 96 expect(set.contains(foo), isFalse); |
| 97 }); |
| 98 }); |
| 99 |
| 100 group(".contains()", () { |
| 101 test("returns true if the asset is in the set", () { |
| 102 var set = new AssetSet(); |
| 103 var foo = new Asset.fromString(fooId, "foo"); |
| 104 var bar = new Asset.fromString(barId, "bar"); |
| 105 set.add(foo); |
| 106 |
| 107 expect(set.contains(foo), isTrue); |
| 108 expect(set.contains(bar), isFalse); |
| 109 }); |
| 110 }); |
| 111 |
| 112 group(".containsId()", () { |
| 113 test("returns true if an asset with the ID is in the set", () { |
| 114 var set = new AssetSet(); |
| 115 var foo = new Asset.fromString(fooId, "foo"); |
| 116 set.add(foo); |
| 117 |
| 118 expect(set.containsId(fooId), isTrue); |
| 119 expect(set.containsId(barId), isFalse); |
| 120 }); |
| 121 }); |
| 122 |
| 123 group(".removeId()", () { |
| 124 test("removes the asset with the ID from the set", () { |
| 125 var set = new AssetSet(); |
| 126 var foo = new Asset.fromString(fooId, "foo"); |
| 127 set.add(foo); |
| 128 |
| 129 set.removeId(fooId); |
| 130 expect(set.containsId(fooId), isFalse); |
| 131 }); |
| 132 |
| 133 test("returns the removed asset", () { |
| 134 var set = new AssetSet(); |
| 135 var foo = new Asset.fromString(fooId, "foo"); |
| 136 set.add(foo); |
| 137 |
| 138 expect(set.removeId(fooId).readAsString(), completion(equals("foo"))); |
| 139 }); |
| 140 |
| 141 test("returns null when removing an asset not in the set", () { |
| 142 var set = new AssetSet(); |
| 143 var foo = new Asset.fromString(fooId, "foo"); |
| 144 set.add(foo); |
| 145 |
| 146 expect(set.removeId(barId), isNull); |
| 147 }); |
| 148 }); |
| 149 |
| 150 group(".ids", () { |
| 151 test("contains the ids of all the assets in the set", () { |
| 152 var set = new AssetSet(); |
| 153 var foo = new Asset.fromString(fooId, "foo"); |
| 154 var bar = new Asset.fromString(barId, "bar"); |
| 155 set.addAll([foo, bar]); |
| 156 expect(set.ids, unorderedEquals([fooId, barId])); |
| 157 }); |
| 158 }); |
| 159 } |
OLD | NEW |