root / test / core / Hash / instance / test_aref.rb @ e8a15c2800e475a59845e9807ee30cc04ce8f059
View | Annotate | Download (1.3 KB)
| 1 | #######################################################################
|
|---|---|
| 2 | # test_aref.rb
|
| 3 | #
|
| 4 | # Tests for the Hash#[] instance method.
|
| 5 | #######################################################################
|
| 6 | require 'test/unit'
|
| 7 | |
| 8 | class Test_Hash_Aref_InstanceMethod < Test::Unit::TestCase |
| 9 | def setup |
| 10 | @hash = {:foo, 1, 'bar', 2, nil, 3, false, 4, 'foo', 5, 3.7, 6.0} |
| 11 | end
|
| 12 | |
| 13 | test "aref basic functionality" do |
| 14 | assert_respond_to(@hash, :[]) |
| 15 | assert_nothing_raised{ @hash['bar'] }
|
| 16 | end
|
| 17 | |
| 18 | test "aref expected results" do |
| 19 | assert_equal(1, @hash[:foo]) |
| 20 | assert_equal(2, @hash['bar']) |
| 21 | assert_equal(5, @hash['foo']) |
| 22 | assert_equal(nil, @hash['bogus']) |
| 23 | end
|
| 24 | |
| 25 | test "aref with explicit nil key works as expected" do |
| 26 | assert_equal(3, @hash[nil]) |
| 27 | end
|
| 28 | |
| 29 | test "aref with explicit false key works as expected" do |
| 30 | assert_equal(4, @hash[false]) |
| 31 | end
|
| 32 | |
| 33 | test "aref with float works as expected" do |
| 34 | assert_equal(6.0, @hash[3.7]) |
| 35 | end
|
| 36 | |
| 37 | test "aref slice returns expected array" do |
| 38 | assert_equal([1, 2], @hash[:foo, 'bar']) |
| 39 | assert_equal([1, nil], @hash[:foo, 'bogus']) |
| 40 | end
|
| 41 | |
| 42 | test "aref with nested hash returns expected result" do |
| 43 | hash = {{:a, 1}, {:b, 2}}
|
| 44 | assert_equal({:b, 2}, hash[{:a, 1}])
|
| 45 | end
|
| 46 | |
| 47 | test "aref requires at least on argument" do |
| 48 | assert_raise(ArgumentError){ @hash[] } |
| 49 | end
|
| 50 | |
| 51 | def teardown |
| 52 | @hash = nil |
| 53 | end
|
| 54 | end
|