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