|
| 1 | +:class Map.Readable.Spec |
| 2 | + :is Spec |
| 3 | + :const describes: "Map.Readable" |
| 4 | + |
| 5 | + :fun new_mutable_map: Map(String, U64).new |
| 6 | + |
| 7 | + :: Create a mutable map, yield it to the caller for initialization, |
| 8 | + :: then return it as read-only using the Map.Readable trait as the type. |
| 9 | + :: This ensures that we are testing based only on the trait's methods, |
| 10 | + :: while still giving us a temporary time of mutability to initialize it. |
| 11 | + :fun build_map Map.Readable(String, U64) |
| 12 | + map = @new_mutable_map |
| 13 | + yield map |
| 14 | + map |
| 15 | + |
| 16 | + :it "yields each key and each value (separately)" |
| 17 | + map = @build_map -> (map | |
| 18 | + map["foo"] = 11 |
| 19 | + map["bar"] = 22 |
| 20 | + map["baz"] = 33 |
| 21 | + ) |
| 22 | + |
| 23 | + copy = @new_mutable_map |
| 24 | + map.each_key -> (key | try (copy[key] = map[key]!)) |
| 25 | + |
| 26 | + assert: copy.size == 3 |
| 27 | + assert: copy["foo"]! == 11 |
| 28 | + assert: copy["bar"]! == 22 |
| 29 | + assert: copy["baz"]! == 33 |
| 30 | + |
| 31 | + total_value = U64[0] |
| 32 | + map.each_value -> (value | total_value += value) |
| 33 | + assert: total_value == 66 |
| 34 | + |
| 35 | + :it "yields each key and each value (separately) until the criteria is met" |
| 36 | + map = @build_map -> (map | |
| 37 | + map["foo"] = 11 |
| 38 | + map["bar"] = 22 |
| 39 | + map["baz"] = 33 |
| 40 | + ) |
| 41 | + |
| 42 | + this_key = "" |
| 43 | + found_it = map.each_key_until -> (key | this_key = key, key.ends_with("o")) |
| 44 | + assert: found_it |
| 45 | + assert: this_key == "foo" |
| 46 | + |
| 47 | + this_key = "" |
| 48 | + found_it = map.each_key_until -> (key | this_key = key, key.ends_with("z")) |
| 49 | + assert: found_it |
| 50 | + assert: this_key == "baz" |
| 51 | + |
| 52 | + this_key = "" |
| 53 | + found_it = map.each_key_until -> (key | this_key = key, key.ends_with("x")) |
| 54 | + assert: found_it.is_false |
| 55 | + |
| 56 | + :it "checks if any pair in the map meets the given condition" |
| 57 | + map = @build_map -> (map | |
| 58 | + map["foo"] = 11 |
| 59 | + map["bar"] = 22 |
| 60 | + map["baz"] = 33 |
| 61 | + ) |
| 62 | + |
| 63 | + assert: map.has_any -> (key, value | key == "foo") |
| 64 | + assert: map.has_any -> (key, value | key == "food").is_false |
| 65 | + assert: map.has_any -> (key, value | value == 22) |
| 66 | + assert: map.has_any -> (key, value | value == 23).is_false |
| 67 | + |
| 68 | + :it "checks if all pairs in the map meet the given condition" |
| 69 | + map = @build_map -> (map | |
| 70 | + map["foo"] = 11 |
| 71 | + map["bar"] = 22 |
| 72 | + map["baz"] = 33 |
| 73 | + ) |
| 74 | + |
| 75 | + assert: map.has_all -> (key, value | key.size == 3) |
| 76 | + assert: map.has_all -> (key, value | key.starts_with("ba")).is_false |
| 77 | + assert: map.has_all -> (key, value | value < 50) |
| 78 | + assert: map.has_all -> (key, value | value < 30).is_false |
0 commit comments