2006-07-08 14:14:49 -04:00
|
|
|
require File.dirname(__FILE__) + '/abstract_unit'
|
2005-09-30 02:51:42 -04:00
|
|
|
|
2006-06-02 13:28:15 -04:00
|
|
|
class OrderedHashTest < Test::Unit::TestCase
|
|
|
|
def setup
|
|
|
|
@keys = %w( blue green red pink orange )
|
|
|
|
@values = %w( 000099 009900 aa0000 cc0066 cc6633 )
|
|
|
|
@ordered_hash = ActiveSupport::OrderedHash.new(@keys.zip(@values))
|
|
|
|
end
|
2006-07-08 14:14:49 -04:00
|
|
|
|
2006-06-02 13:28:15 -04:00
|
|
|
def test_order
|
|
|
|
assert_equal @keys, @ordered_hash.keys
|
|
|
|
assert_equal @values, @ordered_hash.values
|
|
|
|
end
|
2006-07-08 14:14:49 -04:00
|
|
|
|
2006-06-02 13:28:15 -04:00
|
|
|
def test_access
|
|
|
|
assert @keys.zip(@values).all? { |k, v| @ordered_hash[k] == v }
|
|
|
|
end
|
2006-07-08 14:14:49 -04:00
|
|
|
|
2006-06-02 13:28:15 -04:00
|
|
|
def test_assignment
|
|
|
|
key, value = 'purple', '5422a8'
|
2006-07-08 14:14:49 -04:00
|
|
|
|
2006-06-02 13:28:15 -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
|
|
|
|
end
|
|
|
|
|
2005-09-30 02:51:42 -04:00
|
|
|
class OrderedOptionsTest < Test::Unit::TestCase
|
|
|
|
def test_usage
|
|
|
|
a = OrderedOptions.new
|
|
|
|
|
|
|
|
assert_nil a[:not_set]
|
|
|
|
|
2006-07-08 14:14:49 -04:00
|
|
|
a[:allow_concurreny] = true
|
2005-09-30 02:51:42 -04:00
|
|
|
assert_equal 1, a.size
|
|
|
|
assert a[:allow_concurreny]
|
|
|
|
|
|
|
|
a[:allow_concurreny] = false
|
|
|
|
assert_equal 1, a.size
|
|
|
|
assert !a[:allow_concurreny]
|
|
|
|
|
|
|
|
a["else_where"] = 56
|
|
|
|
assert_equal 2, a.size
|
|
|
|
assert_equal 56, a[:else_where]
|
|
|
|
end
|
2006-07-08 14:14:49 -04:00
|
|
|
|
2005-09-30 03:02:28 -04:00
|
|
|
def test_looping
|
|
|
|
a = OrderedOptions.new
|
|
|
|
|
2006-07-08 14:14:49 -04:00
|
|
|
a[:allow_concurreny] = true
|
2005-09-30 03:02:28 -04:00
|
|
|
a["else_where"] = 56
|
2006-07-08 14:14:49 -04:00
|
|
|
|
2005-09-30 03:02:28 -04:00
|
|
|
test = [[:allow_concurreny, true], [:else_where, 56]]
|
2006-07-08 14:14:49 -04:00
|
|
|
|
2005-09-30 03:02:28 -04:00
|
|
|
a.each_with_index do |(key, value), index|
|
|
|
|
assert_equal test[index].first, key
|
|
|
|
assert_equal test[index].last, value
|
|
|
|
end
|
|
|
|
end
|
2006-07-08 14:14:49 -04:00
|
|
|
|
2005-09-30 03:14:37 -04:00
|
|
|
def test_method_access
|
|
|
|
a = OrderedOptions.new
|
|
|
|
|
|
|
|
assert_nil a.not_set
|
|
|
|
|
2006-07-08 14:14:49 -04:00
|
|
|
a.allow_concurreny = true
|
2005-09-30 03:14:37 -04:00
|
|
|
assert_equal 1, a.size
|
|
|
|
assert a.allow_concurreny
|
|
|
|
|
|
|
|
a.allow_concurreny = false
|
|
|
|
assert_equal 1, a.size
|
|
|
|
assert !a.allow_concurreny
|
|
|
|
|
|
|
|
a.else_where = 56
|
|
|
|
assert_equal 2, a.size
|
|
|
|
assert_equal 56, a.else_where
|
|
|
|
end
|
2005-09-30 02:51:42 -04:00
|
|
|
end
|