2016-08-06 12:03:25 -04:00
|
|
|
require "abstract_unit"
|
|
|
|
require "active_support/json"
|
|
|
|
require "active_support/core_ext/object/json"
|
|
|
|
require "active_support/core_ext/hash/indifferent_access"
|
|
|
|
require "active_support/core_ext/array/extract_options"
|
2008-05-14 13:48:37 -04:00
|
|
|
|
2012-01-05 20:12:46 -05:00
|
|
|
class OrderedHashTest < ActiveSupport::TestCase
|
2008-05-14 13:48:37 -04:00
|
|
|
def setup
|
|
|
|
@keys = %w( blue green red pink orange )
|
|
|
|
@values = %w( 000099 009900 aa0000 cc0066 cc6633 )
|
2009-03-04 19:20:49 -05:00
|
|
|
@hash = Hash.new
|
2008-05-14 13:48:37 -04:00
|
|
|
@ordered_hash = ActiveSupport::OrderedHash.new
|
|
|
|
|
|
|
|
@keys.each_with_index do |key, index|
|
2009-03-04 19:20:49 -05:00
|
|
|
@hash[key] = @values[index]
|
2008-05-14 13:48:37 -04:00
|
|
|
@ordered_hash[key] = @values[index]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_order
|
|
|
|
assert_equal @keys, @ordered_hash.keys
|
|
|
|
assert_equal @values, @ordered_hash.values
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_access
|
2009-03-04 19:20:49 -05:00
|
|
|
assert @hash.all? { |k, v| @ordered_hash[k] == v }
|
2008-05-14 13:48:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_assignment
|
2016-08-06 12:03:25 -04:00
|
|
|
key, value = "purple", "5422a8"
|
2008-05-14 13:48:37 -04:00
|
|
|
|
|
|
|
@ordered_hash[key] = value
|
|
|
|
assert_equal @keys.length + 1, @ordered_hash.length
|
|
|
|
assert_equal key, @ordered_hash.keys.last
|
|
|
|
assert_equal value, @ordered_hash.values.last
|
|
|
|
assert_equal value, @ordered_hash[key]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_delete
|
2016-08-06 12:03:25 -04:00
|
|
|
key, value = "white", "ffffff"
|
|
|
|
bad_key = "black"
|
2008-05-14 13:48:37 -04:00
|
|
|
|
|
|
|
@ordered_hash[key] = value
|
|
|
|
assert_equal @keys.length + 1, @ordered_hash.length
|
2008-12-11 10:19:03 -05:00
|
|
|
assert_equal @ordered_hash.keys.length, @ordered_hash.length
|
2008-05-14 13:48:37 -04:00
|
|
|
|
|
|
|
assert_equal value, @ordered_hash.delete(key)
|
|
|
|
assert_equal @keys.length, @ordered_hash.length
|
2008-12-11 10:19:03 -05:00
|
|
|
assert_equal @ordered_hash.keys.length, @ordered_hash.length
|
2008-05-14 13:48:37 -04:00
|
|
|
|
|
|
|
assert_nil @ordered_hash.delete(bad_key)
|
|
|
|
end
|
2008-06-03 15:15:33 -04:00
|
|
|
|
2009-03-03 22:10:33 -05:00
|
|
|
def test_to_hash
|
|
|
|
assert_same @ordered_hash, @ordered_hash.to_hash
|
|
|
|
end
|
|
|
|
|
2009-05-09 18:35:31 -04:00
|
|
|
def test_to_a
|
|
|
|
assert_equal @keys.zip(@values), @ordered_hash.to_a
|
|
|
|
end
|
|
|
|
|
2008-06-03 15:15:33 -04:00
|
|
|
def test_has_key
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_equal true, @ordered_hash.has_key?("blue")
|
|
|
|
assert_equal true, @ordered_hash.key?("blue")
|
|
|
|
assert_equal true, @ordered_hash.include?("blue")
|
|
|
|
assert_equal true, @ordered_hash.member?("blue")
|
2008-06-03 15:15:33 -04:00
|
|
|
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_equal false, @ordered_hash.has_key?("indigo")
|
|
|
|
assert_equal false, @ordered_hash.key?("indigo")
|
|
|
|
assert_equal false, @ordered_hash.include?("indigo")
|
|
|
|
assert_equal false, @ordered_hash.member?("indigo")
|
2008-06-03 15:15:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_has_value
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_equal true, @ordered_hash.has_value?("000099")
|
|
|
|
assert_equal true, @ordered_hash.value?("000099")
|
|
|
|
assert_equal false, @ordered_hash.has_value?("ABCABC")
|
|
|
|
assert_equal false, @ordered_hash.value?("ABCABC")
|
2008-06-03 15:15:33 -04:00
|
|
|
end
|
2008-11-18 17:00:35 -05:00
|
|
|
|
|
|
|
def test_each_key
|
|
|
|
keys = []
|
2011-02-02 09:58:40 -05:00
|
|
|
assert_equal @ordered_hash, @ordered_hash.each_key { |k| keys << k }
|
2008-11-18 17:00:35 -05:00
|
|
|
assert_equal @keys, keys
|
2011-12-20 12:22:21 -05:00
|
|
|
assert_kind_of Enumerator, @ordered_hash.each_key
|
2008-11-18 17:00:35 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_each_value
|
|
|
|
values = []
|
2011-02-02 09:58:40 -05:00
|
|
|
assert_equal @ordered_hash, @ordered_hash.each_value { |v| values << v }
|
2008-11-18 17:00:35 -05:00
|
|
|
assert_equal @values, values
|
2011-12-20 12:22:21 -05:00
|
|
|
assert_kind_of Enumerator, @ordered_hash.each_value
|
2008-11-18 17:00:35 -05:00
|
|
|
end
|
2008-12-09 15:36:24 -05:00
|
|
|
|
|
|
|
def test_each
|
|
|
|
values = []
|
2016-08-16 03:30:11 -04:00
|
|
|
assert_equal @ordered_hash, @ordered_hash.each { |key, value| values << value }
|
2008-12-09 15:36:24 -05:00
|
|
|
assert_equal @values, values
|
2011-12-20 12:22:21 -05:00
|
|
|
assert_kind_of Enumerator, @ordered_hash.each
|
2008-12-09 15:36:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_each_with_index
|
2016-08-16 03:30:11 -04:00
|
|
|
@ordered_hash.each_with_index { |pair, index| assert_equal [@keys[index], @values[index]], pair }
|
2008-12-09 15:36:24 -05:00
|
|
|
end
|
2008-12-11 08:12:06 -05:00
|
|
|
|
2008-12-11 10:19:03 -05:00
|
|
|
def test_each_pair
|
|
|
|
values = []
|
|
|
|
keys = []
|
|
|
|
@ordered_hash.each_pair do |key, value|
|
|
|
|
keys << key
|
|
|
|
values << value
|
|
|
|
end
|
|
|
|
assert_equal @values, values
|
|
|
|
assert_equal @keys, keys
|
2011-12-20 12:22:21 -05:00
|
|
|
assert_kind_of Enumerator, @ordered_hash.each_pair
|
2008-12-11 10:19:03 -05:00
|
|
|
end
|
|
|
|
|
2010-10-20 11:01:02 -04:00
|
|
|
def test_find_all
|
|
|
|
assert_equal @keys, @ordered_hash.find_all { true }.map(&:first)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_select
|
2014-02-26 15:22:52 -05:00
|
|
|
new_ordered_hash = @ordered_hash.select { true }
|
|
|
|
assert_equal @keys, new_ordered_hash.map(&:first)
|
|
|
|
assert_instance_of ActiveSupport::OrderedHash, new_ordered_hash
|
2010-10-20 11:01:02 -04:00
|
|
|
end
|
|
|
|
|
2008-12-11 08:12:06 -05:00
|
|
|
def test_delete_if
|
2009-01-04 09:01:23 -05:00
|
|
|
copy = @ordered_hash.dup
|
2016-08-06 12:03:25 -04:00
|
|
|
copy.delete("pink")
|
|
|
|
assert_equal copy, @ordered_hash.delete_if { |k, _| k == "pink" }
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_not_includes @ordered_hash.keys, "pink"
|
2008-12-11 08:12:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_reject!
|
2016-08-06 12:03:25 -04:00
|
|
|
(copy = @ordered_hash.dup).delete("pink")
|
|
|
|
@ordered_hash.reject! { |k, _| k == "pink" }
|
2008-12-11 08:12:06 -05:00
|
|
|
assert_equal copy, @ordered_hash
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_not_includes @ordered_hash.keys, "pink"
|
2008-12-11 08:12:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_reject
|
|
|
|
copy = @ordered_hash.dup
|
2016-08-06 12:03:25 -04:00
|
|
|
new_ordered_hash = @ordered_hash.reject { |k, _| k == "pink" }
|
2008-12-11 08:12:06 -05:00
|
|
|
assert_equal copy, @ordered_hash
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_not_includes new_ordered_hash.keys, "pink"
|
|
|
|
assert_includes @ordered_hash.keys, "pink"
|
2014-02-26 15:22:52 -05:00
|
|
|
assert_instance_of ActiveSupport::OrderedHash, new_ordered_hash
|
2008-12-11 08:12:06 -05:00
|
|
|
end
|
2008-12-11 10:19:03 -05:00
|
|
|
|
|
|
|
def test_clear
|
|
|
|
@ordered_hash.clear
|
|
|
|
assert_equal [], @ordered_hash.keys
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_merge
|
|
|
|
other_hash = ActiveSupport::OrderedHash.new
|
2016-08-06 12:03:25 -04:00
|
|
|
other_hash["purple"] = "800080"
|
|
|
|
other_hash["violet"] = "ee82ee"
|
2008-12-11 10:19:03 -05:00
|
|
|
merged = @ordered_hash.merge other_hash
|
|
|
|
assert_equal merged.length, @ordered_hash.length + other_hash.length
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_equal @keys + ["purple", "violet"], merged.keys
|
2010-06-11 23:28:09 -04:00
|
|
|
end
|
2008-12-11 10:19:03 -05:00
|
|
|
|
2010-06-11 23:28:09 -04:00
|
|
|
def test_merge_with_block
|
2010-06-13 00:37:54 -04:00
|
|
|
hash = ActiveSupport::OrderedHash.new
|
|
|
|
hash[:a] = 0
|
|
|
|
hash[:b] = 0
|
2016-08-06 13:38:33 -04:00
|
|
|
merged = hash.merge(b: 2, c: 7) do |key, old_value, new_value|
|
2010-06-13 00:37:54 -04:00
|
|
|
new_value + 1
|
2010-06-11 18:38:18 -04:00
|
|
|
end
|
2010-06-12 23:53:42 -04:00
|
|
|
|
2010-06-13 00:37:54 -04:00
|
|
|
assert_equal 0, merged[:a]
|
|
|
|
assert_equal 3, merged[:b]
|
|
|
|
assert_equal 7, merged[:c]
|
2010-06-11 23:28:09 -04:00
|
|
|
end
|
2010-06-11 18:38:18 -04:00
|
|
|
|
2010-06-11 23:28:09 -04:00
|
|
|
def test_merge_bang_with_block
|
2010-06-13 00:37:54 -04:00
|
|
|
hash = ActiveSupport::OrderedHash.new
|
|
|
|
hash[:a] = 0
|
|
|
|
hash[:b] = 0
|
2016-08-06 13:38:33 -04:00
|
|
|
hash.merge!(a: 1, c: 7) do |key, old_value, new_value|
|
2010-06-13 00:37:54 -04:00
|
|
|
new_value + 3
|
2010-06-11 23:28:09 -04:00
|
|
|
end
|
2010-06-13 00:37:54 -04:00
|
|
|
|
|
|
|
assert_equal 4, hash[:a]
|
|
|
|
assert_equal 0, hash[:b]
|
|
|
|
assert_equal 7, hash[:c]
|
2008-12-11 10:19:03 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_shift
|
|
|
|
pair = @ordered_hash.shift
|
|
|
|
assert_equal [@keys.first, @values.first], pair
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_not_includes @ordered_hash.keys, pair.first
|
2008-12-11 10:19:03 -05:00
|
|
|
end
|
2010-06-11 23:28:09 -04:00
|
|
|
|
2009-01-04 09:01:23 -05:00
|
|
|
def test_keys
|
|
|
|
original = @ordered_hash.keys.dup
|
|
|
|
@ordered_hash.keys.pop
|
|
|
|
assert_equal original, @ordered_hash.keys
|
|
|
|
end
|
2009-03-04 19:20:49 -05:00
|
|
|
|
|
|
|
def test_inspect
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_includes @ordered_hash.inspect, @hash.inspect
|
2009-03-04 19:20:49 -05:00
|
|
|
end
|
2009-05-07 18:58:07 -04:00
|
|
|
|
2010-06-26 05:57:43 -04:00
|
|
|
def test_json
|
|
|
|
ordered_hash = ActiveSupport::OrderedHash[:foo, :bar]
|
|
|
|
hash = Hash[:foo, :bar]
|
|
|
|
assert_equal ordered_hash.to_json, hash.to_json
|
|
|
|
end
|
|
|
|
|
2009-06-24 13:51:20 -04:00
|
|
|
def test_alternate_initialization_with_splat
|
2009-05-07 18:58:07 -04:00
|
|
|
alternate = ActiveSupport::OrderedHash[1,2,3,4]
|
|
|
|
assert_kind_of ActiveSupport::OrderedHash, alternate
|
|
|
|
assert_equal [1, 3], alternate.keys
|
|
|
|
end
|
2009-06-24 13:51:20 -04:00
|
|
|
|
|
|
|
def test_alternate_initialization_with_array
|
|
|
|
alternate = ActiveSupport::OrderedHash[ [
|
|
|
|
[1, 2],
|
|
|
|
[3, 4],
|
2016-08-06 12:03:25 -04:00
|
|
|
[ "missing value" ]
|
2009-06-24 13:51:20 -04:00
|
|
|
]]
|
|
|
|
|
|
|
|
assert_kind_of ActiveSupport::OrderedHash, alternate
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_equal [1, 3, "missing value"], alternate.keys
|
2009-06-24 13:51:20 -04:00
|
|
|
assert_equal [2, 4, nil ], alternate.values
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_alternate_initialization_raises_exception_on_odd_length_args
|
2012-07-27 15:50:21 -04:00
|
|
|
assert_raises ArgumentError do
|
2010-07-22 23:36:34 -04:00
|
|
|
ActiveSupport::OrderedHash[1,2,3,4,5]
|
2009-06-24 13:51:20 -04:00
|
|
|
end
|
|
|
|
end
|
2009-11-04 10:13:59 -05:00
|
|
|
|
|
|
|
def test_replace_updates_keys
|
2016-08-06 12:03:25 -04:00
|
|
|
@other_ordered_hash = ActiveSupport::OrderedHash[:black, "000000", :white, "000000"]
|
2009-11-04 10:13:59 -05:00
|
|
|
original = @ordered_hash.replace(@other_ordered_hash)
|
|
|
|
assert_same original, @ordered_hash
|
|
|
|
assert_equal @other_ordered_hash.keys, @ordered_hash.keys
|
|
|
|
end
|
2010-01-26 06:02:31 -05:00
|
|
|
|
2011-05-08 05:21:06 -04:00
|
|
|
def test_nested_under_indifferent_access
|
2016-08-16 03:30:11 -04:00
|
|
|
flash = { a: ActiveSupport::OrderedHash[:b, 1, :c, 2] }.with_indifferent_access
|
2011-05-08 05:21:06 -04:00
|
|
|
assert_kind_of ActiveSupport::OrderedHash, flash[:a]
|
|
|
|
end
|
|
|
|
|
2010-01-26 06:02:31 -05:00
|
|
|
def test_each_after_yaml_serialization
|
2013-04-11 12:01:23 -04:00
|
|
|
assert_equal @values, YAML.load(YAML.dump(@ordered_hash)).values
|
2010-01-26 06:02:31 -05:00
|
|
|
end
|
|
|
|
|
2011-05-27 07:04:39 -04:00
|
|
|
def test_each_when_yielding_to_block_with_splat
|
|
|
|
hash_values = []
|
|
|
|
ordered_hash_values = []
|
|
|
|
|
|
|
|
@hash.each { |*v| hash_values << v }
|
|
|
|
@ordered_hash.each { |*v| ordered_hash_values << v }
|
|
|
|
|
|
|
|
assert_equal hash_values.sort, ordered_hash_values.sort
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_each_pair_when_yielding_to_block_with_splat
|
|
|
|
hash_values = []
|
|
|
|
ordered_hash_values = []
|
|
|
|
|
|
|
|
@hash.each_pair { |*v| hash_values << v }
|
|
|
|
@ordered_hash.each_pair { |*v| ordered_hash_values << v }
|
|
|
|
|
|
|
|
assert_equal hash_values.sort, ordered_hash_values.sort
|
|
|
|
end
|
|
|
|
|
2010-01-26 06:02:31 -05:00
|
|
|
def test_order_after_yaml_serialization
|
2011-01-04 17:09:16 -05:00
|
|
|
@deserialized_ordered_hash = YAML.load(YAML.dump(@ordered_hash))
|
2010-01-26 06:02:31 -05:00
|
|
|
|
|
|
|
assert_equal @keys, @deserialized_ordered_hash.keys
|
|
|
|
assert_equal @values, @deserialized_ordered_hash.values
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_order_after_yaml_serialization_with_nested_arrays
|
|
|
|
@ordered_hash[:array] = %w(a b c)
|
|
|
|
|
2011-01-04 17:09:16 -05:00
|
|
|
@deserialized_ordered_hash = YAML.load(YAML.dump(@ordered_hash))
|
2010-01-26 06:02:31 -05:00
|
|
|
|
|
|
|
assert_equal @ordered_hash.keys, @deserialized_ordered_hash.keys
|
|
|
|
assert_equal @ordered_hash.values, @deserialized_ordered_hash.values
|
|
|
|
end
|
2010-06-25 10:35:11 -04:00
|
|
|
|
2012-01-04 11:30:57 -05:00
|
|
|
def test_psych_serialize
|
|
|
|
@deserialized_ordered_hash = Psych.load(Psych.dump(@ordered_hash))
|
2011-01-04 18:01:51 -05:00
|
|
|
|
2012-01-04 11:30:57 -05:00
|
|
|
values = @deserialized_ordered_hash.map { |_, value| value }
|
|
|
|
assert_equal @values, values
|
|
|
|
end
|
2011-01-04 18:01:51 -05:00
|
|
|
|
2012-01-04 11:30:57 -05:00
|
|
|
def test_psych_serialize_tag
|
|
|
|
yaml = Psych.dump(@ordered_hash)
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_match "!omap", yaml
|
2011-01-04 18:01:51 -05:00
|
|
|
end
|
|
|
|
|
2011-01-04 17:09:16 -05:00
|
|
|
def test_has_yaml_tag
|
|
|
|
@ordered_hash[:array] = %w(a b c)
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_match "!omap", YAML.dump(@ordered_hash)
|
2011-01-04 17:09:16 -05:00
|
|
|
end
|
|
|
|
|
2010-06-25 10:35:11 -04:00
|
|
|
def test_update_sets_keys
|
|
|
|
@updated_ordered_hash = ActiveSupport::OrderedHash.new
|
2016-08-06 13:38:33 -04:00
|
|
|
@updated_ordered_hash.update(name: "Bob")
|
2010-06-25 10:35:11 -04:00
|
|
|
assert_equal [:name], @updated_ordered_hash.keys
|
|
|
|
end
|
2010-06-26 04:52:20 -04:00
|
|
|
|
|
|
|
def test_invert
|
2010-06-26 06:14:25 -04:00
|
|
|
expected = ActiveSupport::OrderedHash[@values.zip(@keys)]
|
|
|
|
assert_equal expected, @ordered_hash.invert
|
2010-06-26 06:09:40 -04:00
|
|
|
assert_equal @values.zip(@keys), @ordered_hash.invert.to_a
|
2010-06-26 04:52:20 -04:00
|
|
|
end
|
2011-09-03 03:43:33 -04:00
|
|
|
|
|
|
|
def test_extractable
|
|
|
|
@ordered_hash[:rails] = "snowman"
|
|
|
|
assert_equal @ordered_hash, [1, 2, @ordered_hash].extract_options!
|
|
|
|
end
|
2009-03-03 22:10:33 -05:00
|
|
|
end
|