1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
module ReferenceKeyMapBehavior
def test_uses_the_proper_references
assert_equal reference_class, map_class.reference_class
end
def test_keeps_entries_with_strong_references
Ref::Mock.use do
hash = map_class.new
key_1 = Object.new
key_2 = Object.new
hash[key_1] = "value 1"
hash[key_2] = "value 2"
assert_equal "value 1", hash[key_1]
assert_equal "value 2", hash[key_2]
end
end
def test_removes_entries_that_have_been_garbage_collected
Ref::Mock.use do
hash = map_class.new
key_1 = Object.new
key_2 = Object.new
hash[key_1] = "value 1"
hash[key_2] = "value 2"
assert_equal "value 1", hash[key_1]
assert_equal "value 2", hash[key_2]
Ref::Mock.gc(key_2)
assert_equal "value 1", hash[key_1]
assert_nil hash[key_2]
end
end
def test_can_clear_the_map
Ref::Mock.use do
hash = map_class.new
value_1 = "value 1"
value_2 = "value 2"
key_1 = Object.new
key_2 = Object.new
hash[key_1] = value_1
hash[key_2] = value_2
hash.clear
assert_nil hash[key_1]
assert_nil hash[key_2]
end
end
def test_can_delete_entries
Ref::Mock.use do
hash = map_class.new
value_1 = "value 1"
value_2 = "value 2"
key_1 = Object.new
key_2 = Object.new
hash[key_1] = value_1
hash[key_2] = value_2
Ref::Mock.gc(key_2)
assert_nil hash.delete(key_2)
assert_equal value_1, hash.delete(key_1)
assert_nil hash[key_1]
end
end
def test_can_merge_in_another_hash
Ref::Mock.use do
hash = map_class.new
value_1 = "value 1"
value_2 = "value 2"
value_3 = "value 3"
key_1 = Object.new
key_2 = Object.new
key_3 = Object.new
hash[key_1] = value_1
hash[key_2] = value_2
hash.merge!(key_3 => value_3)
assert_equal "value 2", hash[key_2]
assert_equal value_1, hash[key_1]
Ref::Mock.gc(key_2)
assert_nil hash[key_2]
assert_equal value_1, hash[key_1]
assert_equal value_3, hash[key_3]
end
end
def test_can_get_all_keys
Ref::Mock.use do
hash = map_class.new
value_1 = "value 1"
value_2 = "value 2"
value_3 = "value 3"
key_1 = Object.new
key_2 = Object.new
key_3 = Object.new
hash[key_1] = value_1
hash[key_2] = value_2
hash[key_3] = value_3
assert_equal [], [key_1, key_2, key_3] - hash.keys
Ref::Mock.gc(key_2)
assert_equal [key_2], [key_1, key_2, key_3] - hash.keys
end
end
def test_can_turn_into_an_array
Ref::Mock.use do
hash = map_class.new
value_1 = "value 1"
value_2 = "value 2"
value_3 = "value 3"
key_1 = Object.new
key_2 = Object.new
key_3 = Object.new
hash[key_1] = value_1
hash[key_2] = value_2
hash[key_3] = value_3
order = lambda{|a,b| a.last <=> b.last}
assert_equal [[key_1, "value 1"], [key_2, "value 2"], [key_3, "value 3"]].sort(&order), hash.to_a.sort(&order)
Ref::Mock.gc(key_2)
assert_equal [[key_1, "value 1"], [key_3, "value 3"]].sort(&order), hash.to_a.sort(&order)
end
end
def test_can_iterate_over_all_entries
Ref::Mock.use do
hash = map_class.new
value_1 = "value 1"
value_2 = "value 2"
value_3 = "value 3"
key_1 = Object.new
key_2 = Object.new
key_3 = Object.new
hash[key_1] = value_1
hash[key_2] = value_2
hash[key_3] = value_3
keys = []
values = []
hash.each{|k,v| keys << k; values << v}
assert_equal [], [key_1, key_2, key_3] - keys
assert_equal ["value 1", "value 2", "value 3"], values.sort
Ref::Mock.gc(key_2)
keys = []
values = []
hash.each{|k,v| keys << k; values << v}
assert_equal [key_2], [key_1, key_2, key_3] - keys
assert_equal ["value 1", "value 3"], values.sort
end
end
def test_inspect
Ref::Mock.use do
hash = map_class.new
hash[Object.new] = "value 1"
assert hash.inspect
end
end
end
|