OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016, 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:test/test.dart"; |
| 6 |
| 7 import "package:collection/collection.dart"; |
| 8 |
| 9 void main() { |
| 10 group("mapMap()", () { |
| 11 test("with an empty map returns an empty map", () { |
| 12 expect( |
| 13 mapMap({}, |
| 14 key: expectAsync((_, __) {}, count: 0), |
| 15 value: expectAsync((_, __) {}, count: 0)), |
| 16 isEmpty); |
| 17 }); |
| 18 |
| 19 test("with no callbacks, returns a copy of the map", () { |
| 20 var map = {"foo": 1, "bar": 2}; |
| 21 var result = mapMap(map); |
| 22 expect(result, equals({"foo": 1, "bar": 2})); |
| 23 |
| 24 // The resulting map should be a copy. |
| 25 result["foo"] = 3; |
| 26 expect(map, equals({"foo": 1, "bar": 2})); |
| 27 }); |
| 28 |
| 29 test("maps the map's keys", () { |
| 30 expect(mapMap({"foo": 1, "bar": 2}, key: (key, value) => key[value]), |
| 31 equals({"o": 1, "r": 2})); |
| 32 }); |
| 33 |
| 34 test("maps the map's values", () { |
| 35 expect(mapMap({"foo": 1, "bar": 2}, value: (key, value) => key[value]), |
| 36 equals({"foo": "o", "bar": "r"})); |
| 37 }); |
| 38 |
| 39 test("maps both the map's keys and values", () { |
| 40 expect( |
| 41 mapMap({"foo": 1, "bar": 2}, |
| 42 key: (key, value) => "$key$value", |
| 43 value: (key, value) => key[value]), |
| 44 equals({"foo1": "o", "bar2": "r"})); |
| 45 }); |
| 46 }); |
| 47 |
| 48 group("mergeMaps()", () { |
| 49 test("with empty maps returns an empty map", () { |
| 50 expect(mergeMaps({}, {}, value: expectAsync((_, __) {}, count: 0)), |
| 51 isEmpty); |
| 52 }); |
| 53 |
| 54 test("returns a map with all values in both input maps", () { |
| 55 expect(mergeMaps({"foo": 1, "bar": 2}, {"baz": 3, "qux": 4}), |
| 56 equals({"foo": 1, "bar": 2, "baz": 3, "qux": 4})); |
| 57 }); |
| 58 |
| 59 test("the second map's values win by default", () { |
| 60 expect(mergeMaps({"foo": 1, "bar": 2}, {"bar": 3, "baz": 4}), |
| 61 equals({"foo": 1, "bar": 3, "baz": 4})); |
| 62 }); |
| 63 |
| 64 test("uses the callback to merge values", () { |
| 65 expect(mergeMaps({"foo": 1, "bar": 2}, {"bar": 3, "baz": 4}, |
| 66 value: (value1, value2) => value1 + value2), |
| 67 equals({"foo": 1, "bar": 5, "baz": 4})); |
| 68 }); |
| 69 }); |
| 70 |
| 71 group("groupBy()", () { |
| 72 test("returns an empty map for an empty iterable", () { |
| 73 expect(groupBy([], expectAsync((_) {}, count: 0)), isEmpty); |
| 74 }); |
| 75 |
| 76 test("groups elements by the function's return value", () { |
| 77 expect( |
| 78 groupBy(["foo", "bar", "baz", "bop", "qux"], (string) => string[1]), |
| 79 equals({"o": ["foo", "bop"], "a": ["bar", "baz"], "u": ["qux"]})); |
| 80 }); |
| 81 }); |
| 82 |
| 83 group("minBy()", () { |
| 84 test("returns null for an empty iterable", () { |
| 85 expect( |
| 86 minBy([], expectAsync((_) {}, count: 0), |
| 87 compare: expectAsync((_, __) {}, count: 0)), |
| 88 isNull); |
| 89 }); |
| 90 |
| 91 test("returns the element for which the ordering function returns the " |
| 92 "smallest value", () { |
| 93 expect( |
| 94 minBy( |
| 95 [{"foo": 3}, {"foo": 5}, {"foo": 4}, {"foo": 1}, {"foo": 2}], |
| 96 (map) => map["foo"]), |
| 97 equals({"foo": 1})); |
| 98 }); |
| 99 |
| 100 test("uses a custom comparator if provided", () { |
| 101 expect( |
| 102 minBy( |
| 103 [{"foo": 3}, {"foo": 5}, {"foo": 4}, {"foo": 1}, {"foo": 2}], |
| 104 (map) => map, |
| 105 compare: (map1, map2) => map1["foo"].compareTo(map2["foo"])), |
| 106 equals({"foo": 1})); |
| 107 }); |
| 108 }); |
| 109 |
| 110 group("maxBy()", () { |
| 111 test("returns null for an empty iterable", () { |
| 112 expect( |
| 113 maxBy([], expectAsync((_) {}, count: 0), |
| 114 compare: expectAsync((_, __) {}, count: 0)), |
| 115 isNull); |
| 116 }); |
| 117 |
| 118 test("returns the element for which the ordering function returns the " |
| 119 "largest value", () { |
| 120 expect( |
| 121 maxBy( |
| 122 [{"foo": 3}, {"foo": 5}, {"foo": 4}, {"foo": 1}, {"foo": 2}], |
| 123 (map) => map["foo"]), |
| 124 equals({"foo": 5})); |
| 125 }); |
| 126 |
| 127 test("uses a custom comparator if provided", () { |
| 128 expect( |
| 129 maxBy( |
| 130 [{"foo": 3}, {"foo": 5}, {"foo": 4}, {"foo": 1}, {"foo": 2}], |
| 131 (map) => map, |
| 132 compare: (map1, map2) => map1["foo"].compareTo(map2["foo"])), |
| 133 equals({"foo": 5})); |
| 134 }); |
| 135 }); |
| 136 |
| 137 group("transitiveClosure()", () { |
| 138 test("returns an empty map for an empty graph", () { |
| 139 expect(transitiveClosure({}), isEmpty); |
| 140 }); |
| 141 |
| 142 test("returns the input when there are no transitive connections", () { |
| 143 expect(transitiveClosure({ |
| 144 "foo": ["bar"], |
| 145 "bar": [], |
| 146 "bang": ["qux", "zap"], |
| 147 "qux": [], |
| 148 "zap": [] |
| 149 }), equals({ |
| 150 "foo": ["bar"], |
| 151 "bar": [], |
| 152 "bang": ["qux", "zap"], |
| 153 "qux": [], |
| 154 "zap": [] |
| 155 })); |
| 156 }); |
| 157 |
| 158 test("flattens transitive connections", () { |
| 159 expect(transitiveClosure({ |
| 160 "qux": [], |
| 161 "bar": ["baz"], |
| 162 "baz": ["qux"], |
| 163 "foo": ["bar"] |
| 164 }), equals({ |
| 165 "foo": ["bar", "baz", "qux"], |
| 166 "bar": ["baz", "qux"], |
| 167 "baz": ["qux"], |
| 168 "qux": [] |
| 169 })); |
| 170 }); |
| 171 |
| 172 test("handles loops", () { |
| 173 expect(transitiveClosure({ |
| 174 "foo": ["bar"], |
| 175 "bar": ["baz"], |
| 176 "baz": ["foo"] |
| 177 }), equals({ |
| 178 "foo": ["bar", "baz", "foo"], |
| 179 "bar": ["baz", "foo", "bar"], |
| 180 "baz": ["foo", "bar", "baz"] |
| 181 })); |
| 182 }); |
| 183 }); |
| 184 } |
OLD | NEW |