1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
lib = File.dirname(File.dirname(__FILE__)) + '/lib'
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'test/unit'
require 'symboltable'
class SymbolTableTest < Test::Unit::TestCase
class A < SymbolTable
end
class B < SymbolTable
end
def test_empty
t = SymbolTable.new
assert_equal(nil, t[:a])
assert_equal(nil, t['a'])
end
def test_bracket
t = SymbolTable.new
t[:a] = 1
assert_equal(1, t[:a])
assert_equal(1, t['a'])
t['b'] = 1
assert_equal(1, t[:b])
assert_equal(1, t['b'])
end
def test_store
t = SymbolTable.new
t.store(:a, 1)
assert_equal(1, t[:a])
assert_equal(1, t['a'])
t.store('b', 1)
assert_equal(1, t[:b])
assert_equal(1, t['b'])
end
def test_store_symboltable
a = A.new
b = B.new
a.a = b
assert_equal(B, a.a.class)
end
def test_update
t = SymbolTable.new
t.update(:a => 1)
assert_equal(1, t[:a])
assert_equal(1, t['a'])
t.update('b' => 1)
assert_equal(1, t[:b])
assert_equal(1, t['b'])
end
def test_merge!
t = SymbolTable.new
t.merge!(:a => 1)
assert_equal(1, t[:a])
assert_equal(1, t['a'])
t.merge!('b' => 1)
assert_equal(1, t[:b])
assert_equal(1, t['b'])
end
def test_method_missing
t = SymbolTable.new
t.a = 1
assert_equal(1, t[:a])
assert_equal(1, t['a'])
assert_equal(1, t.a)
end
def test_nested_tables
t = SymbolTable.new
t[:a] = {:a => 1}
assert_equal(1, t.a[:a])
assert_equal(1, t.a['a'])
t[:a] = {'a' => 1}
assert_equal(1, t.a[:a])
assert_equal(1, t.a['a'])
end
def test_key?
t = SymbolTable.new
t[:a] = 1
assert(t.key?(:a))
assert(t.key?('a'))
end
def test_new
t = SymbolTable[:a, 1]
assert_equal(1, t[:a])
assert_equal(1, t['a'])
end
def test_merge
t = SymbolTable.new
t[:a] = 1
b = t.merge(:a => 2)
assert_equal([:a], t.keys)
assert_equal([1], t.values)
assert_equal([:a], b.keys)
assert_equal([2], b.values)
end
def test_to_hash
t = SymbolTable[:a, 1]
assert_equal(t.to_hash.class, Hash)
assert_equal(t.to_hash[:a], 1)
end
end
|