diff options
author | OKURA Masafumi <masafumi.o1988@gmail.com> | 2023-11-26 18:06:45 +0900 |
---|---|---|
committer | Nobuyoshi Nakada <nobu@ruby-lang.org> | 2023-11-30 21:35:04 +0900 |
commit | 18f218d6a14f1a4744327d814554bfda04f1f119 (patch) | |
tree | 3fd538594a9d0fbcde57d5384572815f8b232158 | |
parent | 722cb9a56d091a10fd6ff9eddfcc7773f06a393e (diff) | |
download | ruby-18f218d6a14f1a4744327d814554bfda04f1f119.tar.gz |
Add some test cases to Data test
I noticed that `deconstruct` and `hash` don't have enough coverage.
The behavior of `hash` is documented so I copied it.
-rw-r--r-- | test/ruby/test_data.rb | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/test/ruby/test_data.rb b/test/ruby/test_data.rb index b911776bde..bb38f8ec91 100644 --- a/test/ruby/test_data.rb +++ b/test/ruby/test_data.rb @@ -117,14 +117,31 @@ class TestData < Test::Unit::TestCase assert_equal({foo: 1, bar: 2}, test.to_h) assert_equal({"foo"=>"1", "bar"=>"2"}, test.to_h { [_1.to_s, _2.to_s] }) + assert_equal([1, 2], test.deconstruct) assert_equal({foo: 1, bar: 2}, test.deconstruct_keys(nil)) assert_equal({foo: 1}, test.deconstruct_keys(%i[foo])) assert_equal({foo: 1}, test.deconstruct_keys(%i[foo baz])) + assert_equal({}, test.deconstruct_keys(%i[foo bar baz])) assert_raise(TypeError) { test.deconstruct_keys(0) } assert_kind_of(Integer, test.hash) end + def test_hash + measure = Data.define(:amount, :unit) + + assert_equal(measure[1, 'km'].hash, measure[1, 'km'].hash) + assert_not_equal(measure[1, 'km'].hash, measure[10, 'km'].hash) + assert_not_equal(measure[1, 'km'].hash, measure[1, 'm'].hash) + assert_not_equal(measure[1, 'km'].hash, measure[1.0, 'km'].hash) + + # Structurally similar data class, but shouldn't be considered + # the same hash key + measurement = Data.define(:amount, :unit) + + assert_not_equal(measure[1, 'km'].hash, measurement[1, 'km'].hash) + end + def test_inspect klass = Data.define(:a) o = klass.new(1) |