root / test / core / Hash / instance / test_aref.rb @ f1655c8fc1e30a4fbdb60fb04f6ae8b6c9c1fc50

View | Annotate | Download (1 KB)

1
#######################################################################
2
# test_aref.rb
3
#
4
# Test suite 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
   def test_aref_basic
14
      assert_respond_to(@hash, :[])
15
      assert_nothing_raised{ @hash['bar'] }
16
   end
17
18
   def test_aref
19
      assert_equal(1, @hash[:foo])
20
      assert_equal(2, @hash['bar'])
21
      assert_equal(3, @hash[nil])
22
      assert_equal(4, @hash[false])
23
      assert_equal(5, @hash['foo'])
24
      assert_equal(6.0, @hash[3.7])
25
      assert_equal(nil, @hash['bogus'])
26
   end
27
28
   def test_aref_slice
29
      assert_equal([1, 2], @hash[:foo, 'bar'])
30
      assert_equal([1, nil], @hash[:foo, 'bogus'])
31
   end
32
33
   def test_aref_expected_errors
34
      assert_raise(ArgumentError){ @hash[] }
35
   end
36
37
   def teardown
38
      @hash = nil
39
   end
40
end