зеркало из https://github.com/github/tainted_hash.git
Fix bug in fetch and add unit tests.
This commit is contained in:
Родитель
4174686e82
Коммит
a6252d24aa
|
@ -90,14 +90,16 @@ class TaintedHash < Hash
|
|||
#
|
||||
# Returns the value of the key, or the default.
|
||||
def fetch(key, *default)
|
||||
raise ArgumentError, "wrong number of arguments (#{default.size + 1} for 1..2)" if default.size > 1
|
||||
|
||||
key_s = key.to_s
|
||||
if @original_hash.key?(key_s)
|
||||
self[key_s]
|
||||
elsif block_given?
|
||||
yield
|
||||
elsif default
|
||||
elsif !default.empty?
|
||||
default[0]
|
||||
else
|
||||
elsif
|
||||
raise KeyError, "key not found: #{key}"
|
||||
end
|
||||
end
|
||||
|
|
|
@ -74,6 +74,10 @@ class TaintedHashTest < Test::Unit::TestCase
|
|||
assert !@tainted.include?(:d)
|
||||
assert_equal 1, @tainted.fetch(:a, :default)
|
||||
assert_equal :default, @tainted.fetch(:d, :default)
|
||||
assert_raises KeyError do
|
||||
@tainted.fetch(:d)
|
||||
end
|
||||
assert_equal :default, @tainted.fetch(:d) { :default }
|
||||
assert !@tainted.include?(:a)
|
||||
assert !@tainted.include?(:d)
|
||||
end
|
||||
|
|
Загрузка…
Ссылка в новой задаче