OSDN Git Service

ruby-1.9.1-rc1
[splhack/AndroidRuby.git] / lib / ruby-1.9.1-rc1 / test / ruby / test_literal.rb
1 require 'test/unit'
2
3 class TestRubyLiteral < Test::Unit::TestCase
4
5   def test_special_const
6     assert_equal 'true', true.inspect
7     assert_instance_of TrueClass, true
8     assert_equal 'false', false.inspect
9     assert_instance_of FalseClass, false
10     assert_equal 'nil', nil.inspect
11     assert_instance_of NilClass, nil
12     assert_equal ':sym', :sym.inspect
13     assert_instance_of Symbol, :sym
14     assert_equal '1234', 1234.inspect
15     assert_instance_of Fixnum, 1234
16     assert_equal '1234', 1_2_3_4.inspect
17     assert_instance_of Fixnum, 1_2_3_4
18     assert_equal '18', 0x12.inspect
19     assert_instance_of Fixnum, 0x12
20     assert_raise(SyntaxError) { eval("0x") }
21     assert_equal '15', 0o17.inspect
22     assert_instance_of Fixnum, 0o17
23     assert_raise(SyntaxError) { eval("0o") }
24     assert_equal '5', 0b101.inspect
25     assert_instance_of Fixnum, 0b101
26     assert_raise(SyntaxError) { eval("0b") }
27     assert_equal '123456789012345678901234567890', 123456789012345678901234567890.inspect
28     assert_instance_of Bignum, 123456789012345678901234567890
29     assert_instance_of Float, 1.3
30   end
31
32   def test_self
33     assert_equal self, self
34     assert_instance_of TestRubyLiteral, self
35     assert_respond_to self, :test_self
36   end
37
38   def test_string
39     assert_instance_of String, ?a
40     assert_equal "a", ?a
41     assert_instance_of String, ?A
42     assert_equal "A", ?A
43     assert_instance_of String, ?\n
44     assert_equal "\n", ?\n
45     assert_equal " ", ?\   # space
46     assert_equal '', ''
47     assert_equal 'string', 'string'
48     assert_equal 'string string', 'string string'
49     assert_equal ' ', ' '
50     assert_equal ' ', " "
51     assert_equal "\0", "\0"
52     assert_equal "\1", "\1"
53     assert_equal "3", "\x33"
54     assert_equal "\n", "\n"
55   end
56
57   def test_dstring
58     assert_equal '2', "#{1+1}"
59     assert_equal '16', "#{2 ** 4}"
60     s = "string"
61     assert_equal s, "#{s}"
62   end
63
64   def test_dsymbol
65     assert_equal :a3c, :"a#{1+2}c"
66   end
67
68   def test_xstring
69     assert_equal "foo\n", `echo foo`
70     s = 'foo'
71     assert_equal "foo\n", `echo #{s}`
72   end
73
74   def test_regexp
75     assert_instance_of Regexp, //
76     assert_match //, 'a'
77     assert_match //, ''
78     assert_instance_of Regexp, /a/
79     assert_match /a/, 'a'
80     assert_no_match /test/, 'tes'
81     re = /test/
82     assert_match re, 'test'
83     str = 'test'
84     assert_match re, str
85     assert_match /test/, str
86     assert_equal 0, (/test/ =~ 'test')
87     assert_equal 0, (re =~ 'test')
88     assert_equal 0, (/test/ =~ str)
89     assert_equal 0, (re =~ str)
90     assert_equal 0, ('test' =~ /test/)
91     assert_equal 0, ('test' =~ re)
92     assert_equal 0, (str =~ /test/)
93     assert_equal 0, (str =~ re)
94   end
95
96   def test_dregexp
97     assert_instance_of Regexp, /re#{'ge'}xp/
98     assert_equal(/regexp/, /re#{'ge'}xp/)
99   end
100
101   def test_array
102     assert_instance_of Array, []
103     assert_equal [], []
104     assert_equal 0, [].size
105     assert_instance_of Array, [0]
106     assert_equal [3], [3]
107     assert_equal 1, [3].size
108     a = [3]
109     assert_equal 3, a[0]
110     assert_instance_of Array, [1,2]
111     assert_equal [1,2], [1,2]
112     assert_instance_of Array, [1,2,3,4,5]
113     assert_equal [1,2,3,4,5], [1,2,3,4,5]
114     assert_equal 5, [1,2,3,4,5].size
115     a = [1,2]
116     assert_equal 1, a[0]
117     assert_equal 2, a[1]
118     a = [1 + 2, 3 + 4, 5 + 6]
119     assert_instance_of Array, a
120     assert_equal [3, 7, 11], a
121     assert_equal 7, a[1]
122     assert_equal 1, ([0][0] += 1)
123     assert_equal 1, ([2][0] -= 1)
124     a = [obj = Object.new]
125     assert_instance_of Array, a
126     assert_equal 1, a.size
127     assert_equal obj, a[0]
128     a = [1,2,3]
129     a[1] = 5
130     assert_equal 5, a[1]
131   end
132
133   def test_hash
134     assert_instance_of Hash, {}
135     assert_equal({}, {})
136     assert_instance_of Hash, {1 => 2}
137     assert_equal({1 => 2}, {1 => 2})
138     h = {1 => 2}
139     assert_equal 2, h[1]
140     h = {"string" => "literal", "goto" => "hell"}
141     assert_equal h, h
142     assert_equal 2, h.size
143     assert_equal h, h
144     assert_equal "literal", h["string"]
145   end
146
147   def test_range
148     assert_instance_of Range, (1..2)
149     assert_equal(1..2, 1..2)
150     r = 1..2
151     assert_equal 1, r.begin
152     assert_equal 2, r.end
153     assert_equal false, r.exclude_end?
154     assert_instance_of Range, (1...3)
155     assert_equal(1...3, 1...3)
156     r = 1...3
157     assert_equal 1, r.begin
158     assert_equal 3, r.end
159     assert_equal true, r.exclude_end?
160     r = 1+2 .. 3+4
161     assert_instance_of Range, r
162     assert_equal 3, r.begin
163     assert_equal 7, r.end
164     assert_equal false, r.exclude_end?
165     r = 1+2 ... 3+4
166     assert_instance_of Range, r
167     assert_equal 3, r.begin
168     assert_equal 7, r.end
169     assert_equal true, r.exclude_end?
170     assert_instance_of Range, 'a'..'c'
171     r = 'a'..'c'
172     assert_equal 'a', r.begin
173     assert_equal 'c', r.end
174   end
175
176   def test__FILE__
177     assert_instance_of String, __FILE__
178     assert_equal __FILE__, __FILE__
179     assert_equal 'test_literal.rb', File.basename(__FILE__)
180   end
181
182   def test__LINE__
183     assert_instance_of Fixnum, __LINE__
184     assert_equal __LINE__, __LINE__
185   end
186
187   def test_integer
188     head = ['', '0x', '0o', '0b', '0d', '-', '+']
189     chars = ['0', '1', '_', '9', 'f']
190     head.each {|h|
191       4.times {|len|
192         a = [h]
193         len.times { a = a.product(chars).map {|x| x.join('') } }
194         a.each {|s|
195           next if s.empty?
196           begin
197             r1 = Integer(s)
198           rescue ArgumentError
199             r1 = :err
200           end
201           begin
202             r2 = eval(s)
203           rescue NameError, SyntaxError
204             r2 = :err
205           end
206           assert_equal(r1, r2, "Integer(#{s.inspect}) != eval(#{s.inspect})")
207         }
208       }
209     }
210   end
211
212   def test_float
213     head = ['', '-', '+']
214     chars = ['0', '1', '_', '9', 'f', '.']
215     head.each {|h|
216       6.times {|len|
217         a = [h]
218         len.times { a = a.product(chars).map {|x| x.join('') } }
219         a.each {|s|
220           next if s.empty?
221           next if /\.\z/ =~ s
222           next if /\A[-+]?\./ =~ s
223           next if /\A[-+]?0/ =~ s
224           begin
225             r1 = Float(s)
226           rescue ArgumentError
227             r1 = :err
228           end
229           begin
230             r2 = eval(s)
231           rescue NameError, SyntaxError
232             r2 = :err
233           end
234           r2 = :err if Range === r2
235           assert_equal(r1, r2, "Float(#{s.inspect}) != eval(#{s.inspect})")
236         }
237       }
238     }
239   end
240
241 end