1 # frozen_string_literal: false
4 class TestRubyPrimitive < Test::Unit::TestCase
7 assert_equal false, !true
8 assert_equal true, !false
9 assert_equal true, !nil
10 assert_equal false, !(1+1)
11 assert_equal false, !!nil
12 assert_equal true, !!1
30 remove_const :C if defined? ::TestRubyPrimitive::C
31 remove_const :A if defined? ::TestRubyPrimitive::A
56 assert_equal 1, A::Const
57 assert_equal 2, A::B::Const
58 assert_equal 3, A::B::C::Const
59 assert_equal 3, A::B::C.new.const
60 assert_equal 1, ::TestRubyPrimitive::A::Const
61 A::B::C.send(:remove_const, :Const)
62 assert_equal 2, A::B::C.new.const
63 assert_raise(TypeError) {
76 def test_constant_cache
92 r = r = A3::B3::C # cache
100 def test_constant_cache2
101 assert_equal 4, A3::B3::C
107 $test_ruby_primitive_constant_cache3 = self::Const
111 def test_constant_cache3
112 assert_equal 7, $test_ruby_primitive_constant_cache3
118 $test_ruby_primitive_constant_cache4 = eval('self')::Const
122 def test_constatant_cache4
123 assert_equal 8, $test_ruby_primitive_constant_cache4
138 $test_ruby_primitive_constant_cache5 = [A6.foo, B6.foo, C6.foo]
140 def test_constant_cache5
141 assert_equal [0, 1, 2], $test_ruby_primitive_constant_cache5
145 $test_ruby_primitive_gvar = 7
146 assert_equal 7, $test_ruby_primitive_gvar
147 assert_equal 7, $test_ruby_primitive_gvar
148 $test_ruby_primitive_gvar = 88
149 assert_equal 88, $test_ruby_primitive_gvar
150 assert_equal 88, $test_ruby_primitive_gvar
151 assert_equal 7, ($test_ruby_primitive_gvar = 7)
152 assert_equal 7, ($test_ruby_primitive_gvar = 7)
156 remove_const :A7 if defined? TestRubyPrimitive::A7
166 def test_cvar_from_instance_method
169 assert_equal 2, A7.new.m
170 assert_equal 3, A7.new.m
171 assert_equal 4, A7.new.m
175 remove_const :A8 if defined? TestRubyPrimitive::A8
187 def test_cvar_from_singleton_method
196 remove_const :A8 if defined? TestRubyPrimitive::A8
206 def test_cvar_from_singleton_method2
244 assert_equal 7, obj.a
248 assert_equal 2, obj.a
251 assert_equal 3, obj.a
261 a = [0, 1, nil, 3, 4]
263 assert_equal [0, 1, :foo, 3, 4], a
265 assert_equal [0, :bar, 4], a
268 def test_opassign_and_or
297 /a(b)(c)d/ =~ 'xyzabcdefgabcdefg'
301 assert_instance_of MatchData, $~
302 assert_equal 'abcd', $&
303 assert_equal 'xyz', $`
304 assert_equal 'efgabcdefg', $'
307 /(?!)/ =~ 'xyzabcdefgabcdefg'
319 assert_equal 306057512216440636035370461297268629388588804173576999416776741259476533176716867465515291422477573349939147888701726368864263907759003154226842927906974559841225476930271954604008012215776252176854255965356903506788725264321896264299365204576448830388909753943489625436053225980776521270822437639449120128678675368305712293681943649956460498166450227716500185176546469340112226034729724066333258583506870150169794168850353752137554910289126407157154830282284937952636580145235233156936482233436799254594095276820608062232812387383880817049600000000000000000000000000000000000000000000000000000000000000000000000000, fact(300)
331 assert_equal 0, 2 * 0
332 assert_equal 0, 0 * 2
333 assert_equal 4, 2 * 2
343 assert_equal 1, 3 / 2
344 assert_equal 1.5, 3.0 / 2.0
345 assert_equal 300, MyNum.new / 3
355 assert_equal 0, [].length
356 assert_equal 1, [1].length
357 assert_equal 2, [1,2].length
358 assert_equal 0, {}.length
359 assert_equal 1, {1=>1}.length
360 assert_equal 2, {1=>1, 2=>2}.length
361 assert_equal 'string', MyArr.new.length
371 assert_equal 2, 5 % 3
372 assert_equal 1.0, 3.0 % 2.0
373 assert_equal 300, MyNum2.new % 3
396 assert_equal [0], obj[0]
397 assert_equal [0,1], obj[0,1]
398 assert_equal [0,1,2], obj[0,1,2]
403 assert_equal 7, (obj[0] = 7)
404 assert_equal 7, (obj[0,1] = 7)
405 assert_equal 7, (obj[0,1,2] = 7)
416 assert_equal 1, (obj.attr = 1)
421 assert_equal [0], [0, *a]
423 assert_equal [0,1], [0, *a]
425 assert_equal [0,1,2], [0, *a]
427 assert_equal [0,1,2,3], [0, *a]
429 #assert_equal [0,1,2,3,4], [0, *a, 4]
432 def test_concatarray_ruby_dev_41933
433 bug3658 = '[ruby-dev:41933]'
435 assert_equal(1, x, bug3658)
437 assert_equal(1, x, bug3658)
438 class << (x = Object.new)
439 attr_accessor :to_a_called
445 x.to_a_called = false
447 assert_predicate(x, :to_a_called, bug3658)
448 x.to_a_called = false
450 assert_predicate(x, :to_a_called, bug3658)