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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
require 'helper'
testcase KeyHash do
class_method :[] do
test 'creates new KeyHash' do
s = KeyHash[]
KeyHash.assert === s
end
test 'pre-assigns values' do
s = KeyHash[:a=>1, :b=>2]
s[:a].assert == 1
s[:b].assert == 2
end
end
method :[] do
test 'instance level fetch' do
s = KeyHash[:a=>1, :b=>2]
s[:a].assert == 1
s[:b].assert == 2
end
test 'by default keys are converted to strings' do
s = KeyHash[:a=>1, :b=>2]
s['a'].assert == 1
s['b'].assert == 2
end
end
method :[]= do
test do
s = KeyHash.new
s[:a] = 1
s[:b] = 2
s[:a].assert == 1
s[:b].assert == 2
s['a'].assert == 1
s['b'].assert == 2
end
end
method :initialize do
test do
StandardError.refute.raised? do
s = KeyHash.new
end
end
end
method :to_hash do
test do
s = KeyHash[:a=>1, :b=>2]
s.to_hash.assert == {'a'=>1, 'b'=>2}
end
end
method :to_h do
test do
s = KeyHash[:a=>1, :b=>2]
s.to_h.assert == {'a'=>1, 'b'=>2}
end
end
method :replace do
test do
s = KeyHash.new
s.replace(:a=>1, :b=>2)
s.to_h.assert == {'a'=>1, 'b'=>2}
end
end
method :delete do
test do
s = KeyHash[:a=>1, :b=>2]
s.delete(:a)
s.to_h.assert == {'b'=>2}
end
end
method :each do
test do
s = KeyHash[:a=>1, :b=>2]
s.each do |k,v|
String.assert === k
end
end
end
method :store do
test do
s = KeyHash.new
s.store(:a, 1)
s.to_h.assert == {'a'=>1}
end
end
method :update do
test do
s1 = KeyHash[:a=>1,:b=>2]
s2 = KeyHash[:c=>3,:d=>4]
s1.update(s2)
s1.to_h.assert == {'a'=>1,'b'=>2,'c'=>3,'d'=>4}
end
end
method :rekey do
test do
s = KeyHash[:a=>1,:b=>2,:c=>3]
x = s.rekey{ |k| k.upcase }
x.to_h.assert == {'A'=>1,'B'=>2,'C'=>3}
end
end
method :rekey! do
test do
s = KeyHash[:a=>1,:b=>2,:c=>3]
s.rekey!{ |k| k.upcase }
s.to_h.assert == {'A'=>1,'B'=>2,'C'=>3}
end
end
method :key? do
test do
s = KeyHash[:a=>1]
s.assert.key?(:a)
s.assert.key?('a')
end
end
method :has_key? do
test do
s = KeyHash[:a=>1]
s.assert.has_key?(:a)
s.assert.has_key?('a')
end
end
method :<< do
test do
s = KeyHash.new
s << [:a, 1]
s << [:b, 2]
s.to_h.assert == {'a'=>1, 'b'=>2}
end
end
method :merge! do
test do
s1 = KeyHash[:a=>1,:b=>2]
s2 = KeyHash[:c=>3,:d=>4]
s1.merge!(s2)
s1.to_h.assert == {'a'=>1,'b'=>2,'c'=>3,'d'=>4}
end
end
method :values_at do
test do
s = KeyHash[:a=>1,:b=>2,:c=>3]
s.values_at(:a, :b).assert == [1,2]
s.values_at('a','b').assert == [1,2]
end
end
method :fetch do
test do
s = KeyHash[:a=>1,:b=>2,:c=>3]
s.fetch(:a).assert == 1
s.fetch('a').assert == 1
end
end
#method :cast_key do
# test do
# s = KeyHash.new
# s.send(:cast_key, :a).assert == 'a'
# end
#end
end
|