summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-04-24 03:46:55 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-04-24 03:46:55 +0000
commit770ddcdfe069f8654a07e0fcc758aea591cdd17d (patch)
tree36c7009eeab34ae5c08ac62d6005c9ee145b46a3 /test
parentb05c95d93f39b95c6c743282ce4d9e674453f12f (diff)
* hash.c, object.c, struct.c, lib/ostruct.rb: add to_h methods.
[Feature #6276] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@35452 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test')
-rw-r--r--test/ostruct/test_ostruct.rb12
-rw-r--r--test/ruby/test_env.rb4
-rw-r--r--test/ruby/test_hash.rb26
-rw-r--r--test/ruby/test_struct.rb6
4 files changed, 47 insertions, 1 deletions
diff --git a/test/ostruct/test_ostruct.rb b/test/ostruct/test_ostruct.rb
index dca2382..1826e40 100644
--- a/test/ostruct/test_ostruct.rb
+++ b/test/ostruct/test_ostruct.rb
@@ -73,4 +73,16 @@ class TC_OpenStruct < Test::Unit::TestCase
assert_raise(NoMethodError) { o[:foo] }
end
+ def test_to_h
+ h = {name: "John Smith", age: 70, pension: 300}
+ os = OpenStruct.new(h)
+ to_h = os.to_h
+ assert_equal(h, to_h)
+
+ to_h[:age] = 71
+ assert_equal(70, os.age)
+ assert_equal(70, h[:age])
+
+ assert_equal(h, OpenStruct.new("name" => "John Smith", "age" => 70, pension: 300).to_h)
+ end
end
diff --git a/test/ruby/test_env.rb b/test/ruby/test_env.rb
index 9166f4d..8baf762 100644
--- a/test/ruby/test_env.rb
+++ b/test/ruby/test_env.rb
@@ -327,6 +327,10 @@ class TestEnv < Test::Unit::TestCase
assert_equal(h, ENV.to_hash)
end
+ def test_to_h
+ assert_equal(ENV.to_hash, ENV.to_h)
+ end
+
def test_reject
h1 = {}
ENV.each_pair {|k, v| h1[k] = v }
diff --git a/test/ruby/test_hash.rb b/test/ruby/test_hash.rb
index e3c9212..0c03bf4 100644
--- a/test/ruby/test_hash.rb
+++ b/test/ruby/test_hash.rb
@@ -77,7 +77,7 @@ class TestHash < Test::Unit::TestCase
# From rubicon
def setup
- @cls = Hash
+ @cls ||= Hash
@h = @cls[
1 => 'one', 2 => 'two', 3 => 'three',
self => 'self', true => 'true', nil => 'nil',
@@ -631,6 +631,20 @@ class TestHash < Test::Unit::TestCase
def test_to_hash
h = @h.to_hash
assert_equal(@h, h)
+ assert_instance_of(@cls, h)
+ end
+
+ def test_to_h
+ h = @h.to_h
+ assert_equal(@h, h)
+ assert_instance_of(Hash, h)
+ end
+
+ def test_nil_to_h
+ h = nil.to_h
+ assert_equal({}, h)
+ assert_nil(h.default)
+ assert_nil(h.default_proc)
end
def test_to_s
@@ -932,4 +946,14 @@ class TestHash < Test::Unit::TestCase
assert_not_equal(h.hash, h.invert.hash, feature4262)
end
end
+
+ class TestSubHash < TestHash
+ class SubHash < Hash
+ end
+
+ def setup
+ @cls = SubHash
+ super
+ end
+ end
end
diff --git a/test/ruby/test_struct.rb b/test/ruby/test_struct.rb
index d769e47..b319190 100644
--- a/test/ruby/test_struct.rb
+++ b/test/ruby/test_struct.rb
@@ -263,4 +263,10 @@ class TestStruct < Test::Unit::TestCase
end
assert_equal("Insecure: can't modify #{st}::S", error.message, bug5036)
end
+
+ def test_to_h
+ klass = Struct.new(:a, :b, :c, :d, :e, :f)
+ o = klass.new(1, 2, 3, 4, 5, 6)
+ assert_equal({a:1, b:2, c:3, d:4, e:5, f:6}, o.to_h)
+ end
end