public
Last active

Unit tests for my symbol.patch (https://gist.github.com/phluid61/5105345)

  • Download Gist
test_symbol_lookup.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
require 'test/unit'
 
class TestSymbolLookup < Test::Unit::TestCase # :nodoc:
def test_success
syms = Hash[ *Symbol.all_symbols.map{|s| [s.to_s, s] }.flatten ]
defined_str = syms.keys.first
defined_sym = syms[defined_str]
undef_str = 'undefined symbol'
# ensure the undefined symbol really is undefined
while syms.has_key? undef_str
undef_str << 'X'
end
 
assert_equal( defined_sym, Symbol[defined_str] )
assert_equal( defined_sym, Symbol[defined_sym] ) # with warning?
assert_nil( Symbol[undef_str] )
end
 
def test_failure
assert_raise(TypeError) { Symbol[1] } # TypeError: 1 is not a symbol
assert_raise(ArgumentError) { Symbol[] } # ArgumentError: wrong number of arguments (0 for 1)
assert_raise(ArgumentError) { Symbol['a','b'] } # ArgumentError: wrong number of arguments (2 for 1)
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.