3 class TestSymbol < Test::Unit::TestCase
6 def assert_eval_inspected(sym)
8 assert_nothing_raised(SyntaxError) {assert_equal(sym, eval(n))}
11 def test_inspect_invalid
12 # 2) Symbol#inspect sometimes returns invalid symbol representations:
13 assert_eval_inspected(:"!")
14 assert_eval_inspected(:"=")
15 assert_eval_inspected(:"0")
16 assert_eval_inspected(:"$1")
17 assert_eval_inspected(:"@1")
18 assert_eval_inspected(:"@@1")
19 assert_eval_inspected(:"@")
20 assert_eval_inspected(:"@@")
23 def assert_inspect_evaled(n)
24 assert_nothing_raised(SyntaxError) {assert_equal(n, eval(n).inspect)}
27 def test_inspect_suboptimal
28 # 3) Symbol#inspect sometimes returns suboptimal symbol representations:
29 assert_inspect_evaled(':foo')
30 assert_inspect_evaled(':foo!')
31 assert_inspect_evaled(':bar?')
32 assert_inspect_evaled(':<<')
33 assert_inspect_evaled(':>>')
34 assert_inspect_evaled(':<=')
35 assert_inspect_evaled(':>=')
36 assert_inspect_evaled(':=~')
37 assert_inspect_evaled(':==')
38 assert_inspect_evaled(':===')
39 assert_raise(SyntaxError) {eval ':='}
40 assert_inspect_evaled(':*')
41 assert_inspect_evaled(':**')
42 assert_raise(SyntaxError) {eval ':***'}
43 assert_inspect_evaled(':+')
44 assert_inspect_evaled(':-')
45 assert_inspect_evaled(':+@')
46 assert_inspect_evaled(':-@')
47 assert_inspect_evaled(':|')
48 assert_inspect_evaled(':^')
49 assert_inspect_evaled(':&')
50 assert_inspect_evaled(':/')
51 assert_inspect_evaled(':%')
52 assert_inspect_evaled(':~')
53 assert_inspect_evaled(':`')
54 assert_inspect_evaled(':[]')
55 assert_inspect_evaled(':[]=')
56 assert_raise(SyntaxError) {eval ':||'}
57 assert_raise(SyntaxError) {eval ':&&'}
58 assert_raise(SyntaxError) {eval ':['}
61 def test_inspect_dollar
62 # 4) :$- always treats next character literally:
64 assert_nothing_raised(SyntaxError) {assert_equal(sym, eval(':$-'))}
65 assert_nothing_raised(SyntaxError) {assert_equal(sym, eval(":$-\n"))}
66 assert_nothing_raised(SyntaxError) {assert_equal(sym, eval(":$- "))}
67 assert_nothing_raised(SyntaxError) {assert_equal(sym, eval(":$-#"))}
68 assert_raise(SyntaxError) {eval ':$-('}
71 def test_inspect_number
72 # 5) Inconsistency between :$0 and :$1? The first one is valid, but the
74 assert_inspect_evaled(':$0')
75 assert_inspect_evaled(':$1')
79 assert_equal %w(1 2 3), (1..3).map(&:to_s)
86 ary_id = ary.object_id
87 assert_equal ary_id, :object_id.to_proc.call(ary)
88 ary_ids = ary.collect{|x| x.object_id }
89 assert_equal ary_ids, ary.collect(&:object_id)
95 def o.foo(x, y); x + y; end
97 assert_equal(3, :foo.to_proc.call(o, 1, 2))
98 assert_raise(ArgumentError) { :foo.to_proc.call }
102 assert_equal(:fop, :foo.succ)
106 assert_equal(0, :FoO <=> :FoO)
107 assert_equal(-1, :FoO <=> :fOO)
108 assert_equal(1, :fOO <=> :FoO)
109 assert_nil(:foo <=> "foo")
113 assert_equal(0, :FoO.casecmp(:fOO))
114 assert_equal(1, :FoO.casecmp(:BaR))
115 assert_equal(-1, :baR.casecmp(:FoO))
116 assert_nil(:foo.casecmp("foo"))
120 assert_equal(3, :FoO.length)
121 assert_equal(3, :FoO.size)
125 assert_equal(false, :FoO.empty?)
126 assert_equal(true, :"".empty?)
130 assert_equal(:FOO, :FoO.upcase)
131 assert_equal(:foo, :FoO.downcase)
132 assert_equal(:Foo, :foo.capitalize)
133 assert_equal(:fOo, :FoO.swapcase)