From 769a7e0ed3a33c5ec4925491202c09db35e3c485 Mon Sep 17 00:00:00 2001 From: Daniel Bovensiepen Date: Wed, 16 May 2012 15:47:22 +0800 Subject: handle exceptions in tests and reduce syntax features in assert code --- test/t/_assert.rb | 59 ++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 41 insertions(+), 18 deletions(-) diff --git a/test/t/_assert.rb b/test/t/_assert.rb index 91751f769..d132e4a30 100644 --- a/test/t/_assert.rb +++ b/test/t/_assert.rb @@ -1,6 +1,19 @@ $ok_test = 0 $ko_test = 0 +$kill_test = 0 $asserts = [] +$exceptions = [] + +## +# Print the assertion in a readable way +def print_assertion_string(str, iso) + print(str) + if(iso != '') + print(' [') + print(iso) + print(']') + end +end ## # Verify a code block. @@ -11,13 +24,18 @@ $asserts = [] # which will be tested by this # assertion def assert(str = 'Assertion failed', iso = '') - if(!yield) - $asserts.push([str, iso]) - $ko_test += 1 - print "F" - else - $ok_test += 1 - print "." + begin + if(!yield) + $asserts.push([str, iso]) + $ko_test += 1 + print('F') + else + $ok_test += 1 + print('.') + end + rescue + $kill_test += 1 + print('X') end end @@ -27,18 +45,23 @@ end def report() print "\n" $asserts.each do |str, iso| - print("Test Failed: #{str} [#{iso}]\n"); + print('Fail: '); + print_assertion_string(str, iso) + print("\n") end - $total_test = $ok_test + $ko_test - print 'Total tests:' - print $total_test - print "\n" + $total_test = $ok_test.+($ko_test) + print('Total: ') + print($total_test) + print("\n") - print ' OK: ' - print $ok_test - print "\n" - print ' KO: ' - print $ko_test - print "\n" + print(' OK: ') + print($ok_test) + print("\n") + print(' KO: ') + print($ko_test) + print("\n") + print(' Crash: ') + print($kill_test) + print("\n") end -- cgit v1.2.3 From 254023f1e795491cacd31944526c7c4d037599cf Mon Sep 17 00:00:00 2001 From: Daniel Bovensiepen Date: Wed, 16 May 2012 15:49:40 +0800 Subject: Remove array for now due to the reason that they are lost after exception was raised --- test/t/_assert.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/test/t/_assert.rb b/test/t/_assert.rb index d132e4a30..ecca78656 100644 --- a/test/t/_assert.rb +++ b/test/t/_assert.rb @@ -2,7 +2,6 @@ $ok_test = 0 $ko_test = 0 $kill_test = 0 $asserts = [] -$exceptions = [] ## # Print the assertion in a readable way -- cgit v1.2.3 From b4a80d7485beb63eb9a4baf5e149ccf58542ecb4 Mon Sep 17 00:00:00 2001 From: Daniel Bovensiepen Date: Wed, 16 May 2012 15:50:54 +0800 Subject: Add first part of the bootstrap code --- test/t/bs_block.rb | 390 +++++++++++++++++++++++++++++++++++++++++++++++++ test/t/bs_class.rb | 120 +++++++++++++++ test/t/bs_exception.rb | 157 ++++++++++++++++++++ test/t/bs_literal.rb | 38 +++++ 4 files changed, 705 insertions(+) create mode 100644 test/t/bs_block.rb create mode 100644 test/t/bs_class.rb create mode 100644 test/t/bs_exception.rb create mode 100644 test/t/bs_literal.rb diff --git a/test/t/bs_block.rb b/test/t/bs_block.rb new file mode 100644 index 000000000..b290cb914 --- /dev/null +++ b/test/t/bs_block.rb @@ -0,0 +1,390 @@ +## +# Bootstrap tests for blocks + +assert('BS Block 1') do + 1.times{ + begin + a = 1 + ensure + foo = nil + end + } == 1 +end + +assert('BS Block 2') do + [1,2,3].find{|x| x == 2} == 2 +end + +assert('BS Block 3') do + class E + include Enumerable + def each(&block) + [1, 2, 3].each(&block) + end + end + E.new.find {|x| x == 2 } == 2 +end + +assert('BS Block 3') do + sum = 0 + for x in [1, 2, 3] + sum += x + end + sum == 6 +end + +assert('BS Block 4') do + sum = 0 + for x in (1..5) + sum += x + end + sum == 15 +end + +assert('BS Block 5') do + sum = 0 + for x in [] + sum += x + end + sum == 0 +end + +assert('BS Block 6') do + ans = [] + 1.times{ + for n in 1..3 + a = n + ans << a + end + } == 1 +end + +assert('BS Block 7') do + ans = [] + for m in 1..3 + for n in 1..3 + a = [m, n] + ans << a + end + end == 1..3 +end + +assert('BS Block 8') do + (1..3).to_a == [1, 2, 3] +end + +assert('BS Block 9') do + (1..3).map{|e| + e * 4 + } == [4, 8, 12] +end + +assert('BS Block 10') do + def m + yield + end + def n + yield + end + + m{ + n{ + 100 + } + } == 100 +end + +assert('BS Block 11') do + def m + yield 1 + end + + m{|ib| + m{|jb| + i = 20 + } + } == 20 +end + +assert('BS Block 12') do + def m + yield 1 + end + + m{|ib| + m{|jb| + ib = 20 + kb = 2 + } + } == 2 +end + +assert('BS Block 13') do + def iter1 + iter2{ + yield + } + end + + def iter2 + yield + end + + iter1{ + jb = 2 + iter1{ + jb = 3 + } + jb + } == 3 +end + +assert('BS Block 14') do + def iter1 + iter2{ + yield + } + end + + def iter2 + yield + end + + iter1{ + jb = 2 + iter1{ + jb + } + jb + } == 2 +end + +assert('BS Block 15') do + def m + yield 1 + end + m{|ib| + ib*2 + } == 2 +end + +assert('BS Block 16') do + def m + yield 12345, 67890 + end + m{|ib,jb| + ib*2+jb + } == 92580 +end + +assert('BS Block 17') do + def iter + yield 10 + end + + a = nil + [iter{|a| + a + }, a] == [10, nil] +end + +assert('BS Block 18') do + def iter + yield 10 + end + + iter{|a| + iter{|a| + a + 1 + } + a + } == 21 +end + +assert('BS Block 19') do + def iter + yield 10, 20, 30, 40 + end + + a = b = c = d = nil + iter{|a, b, c, d| + [a, b, c, d] + } + [a, b, c, d] == [10, 20, 30, 40, nil, nil, nil, nil] +end + +assert('BS Block 20') do + def iter + yield 10, 20, 30, 40 + end + + a = b = nil + iter{|a, b, c, d| + [a, b, c, d] + } + [a, b] == [10, 20, 30, 40, nil, nil] +end + +assert('BS Block 21') do + def iter + yield 1, 2 + end + + iter{|a, *b| + [a, b] + } == [1, [2]] +end + +assert('BS Block 22') do + def iter + yield 1, 2 + end + + iter{|*a| + [a] + } == [[1, 2]] +end + +assert('BS Block 23') do + def iter + yield 1, 2 + end + + iter{|a, b, *c| + [a, b, c] + } == [1, 2, []] +end + +assert('BS Block 24') do + def m + yield + end + m{ + 1 + } == 1 +end + +assert('BS Block 25') do + def m + yield 123 + end + m{|ib| + m{|jb| + ib*jb + } + } == 15129 +end + +assert('BS Block 26') do + def m a + yield a + end + m(1){|ib| + m(2){|jb| + ib*jb + } + } == 2 +end + +assert('BS Block 27') do + sum = 0 + 3.times{|ib| + 2.times{|jb| + sum += ib + jb + }} + sum == 9 +end + +assert('BS Block 28') do + 3.times{|bl| + break 10 + } == 10 +end + +assert('BS Block 29') do + def iter + yield 1,2,3 + end + + iter{|i, j| + [i, j] + } == [1, 2] +end + +assert('BS Block 30') do + def iter + yield 1 + end + + iter{|i, j| + [i, j] + } == [1, nil] +end + +assert('BS Block [ruby-dev:31147]') do + def m + yield + end + m{|&b| b}.inspect == 'nil' +end + +assert('BS Block [ruby-dev:31160]') do + def m() + yield + end + m {|(v,(*))|}.inspect == 'nil' +end + +assert('BS Block 31') do + def m() + yield + end + m {|((*))|}.inspect == 'nil' +end + +assert('BS Block [ruby-dev:31440]') do + def m + yield [0] + end + m{|v, &b| v}.inspect == '[0]' +end + +assert('BS Block 32') do + r = false; 1.times{|&b| r = b}; r.class == NilClass +end + +assert('BS Block [ruby-core:14395]') do + class Controller + def respond_to(&block) + responder = Responder.new + block.call(responder) + responder.respond + end + def test_for_bug + respond_to{|format| + format.js{ + "in test" + render{|obj| + obj + } + } + } + end + def render(&block) + "in render" + end + end + + class Responder + def method_missing(symbol, &block) + "enter method_missing" + @response = Proc.new{ + 'in method missing' + block.call + } + "leave method_missing" + end + def respond + @response.call + end + end + t = Controller.new + t.test_for_bug +end diff --git a/test/t/bs_class.rb b/test/t/bs_class.rb new file mode 100644 index 000000000..ae8668230 --- /dev/null +++ b/test/t/bs_class.rb @@ -0,0 +1,120 @@ +## +# Bootstrap tests for Class + +assert('BS Class 1') do + class C; end + C.class == Class +end + +assert('BS Class 2') do + class C; end + C.new.class == C +end + +assert('BS Class 3') do + class C; end + C.new.class.class == Class +end + +assert('BS Class 4') do + class A; end + class C < A; end + C.class == Class +end + +assert('BS Class 5') do + class A; end + class C < A; end + C.new.class == C +end + +assert('BS Class 6') do + class A; end + class C < A; end + C.new.class.class == Class +end + +assert('BS Class Module 1') do + module M; end + M.class == Module +end + +assert('BS Class Module 2') do + module M; end + class C; include M; end + C.new.class == C +end + +# nested class +assert('BS Class Nested 1') do + class A; end + class A::B; end + A::B == A::B +end + +assert('BS Class Nested 2') do + class A; end + class A::B; end + A::B.new.class == A::B +end + +assert('BS Class Nested 3') do + class A; end + class A::B; end + A::B.new.class.class == Class +end + +assert('BS Class Nested 4') do + class A; end + class A::B; end + class A::B::C; end + A::B::C == A::B::C +end + +assert('BS Class Nested 5') do + class A; end + class A::B; end + class A::B::C; end + A::B::C.class == Class +end + +assert('BS Class Nested 6') do + class A; end + class A::B; end + class A::B::C; end + A::B::C.new.class == A::B::C +end + +assert('BS Class Nested 7') do + class A; end + class A::B; end + class A::B2 < A::B; end + A::B2 == A::B2 +end + +assert('BS Class Nested 8') do + class A; end + class A::B; end + class A::B2 < A::B; end + A::B2.class == Class +end + +assert('BS Class Colon 1') do + class A; end; A::C = 1; A::C == 1 +end + +assert('BS Class Colon 2') do + class A; class ::C; end end; C == C +end + +assert('BS Class Colon 3') do + class A; class ::C; end end; C.class == Class +end + +assert('BS Class Dup 1') do + class C; end; C.dup.class == Class +end + +assert('BS Class Dup 2') do + module M; end; M.dup.class == Module +end diff --git a/test/t/bs_exception.rb b/test/t/bs_exception.rb new file mode 100644 index 000000000..406c07467 --- /dev/null +++ b/test/t/bs_exception.rb @@ -0,0 +1,157 @@ +## +# Bootstrap tests for Exceptions + +assert('BS Exception 1') do + begin + 1+1 + ensure + 2+2 + end == 2 +end + +assert('BS Exception 2') do + begin + 1+1 + begin + 2+2 + ensure + 3+3 + end + ensure + 4+4 + end == 4 +end + +assert('BS Exception 3') do + begin + 1+1 + begin + 2+2 + ensure + 3+3 + end + ensure + 4+4 + begin + 5+5 + ensure + 6+6 + end + end == 4 +end + +assert('BS Exception 4') do + a = nil + 1.times{|e| + begin + rescue => err + end + a = err.class + } + a == NilClass +end + +assert('BS Exception 5') do + $ans = [] + def m + $! + end + def m2 + 1.times{ + begin + return + ensure + $ans << m + end + } + end + m2 + $ans == [nil] +end + +assert('BS Exception 6') do + $i = 0 + def m + iter{ + begin + $i += 1 + begin + $i += 2 + break + ensure + + end + ensure + $i += 4 + end + $i = 0 + } + end + + def iter + yield + end + m + $i == 7 +end + +assert('BS Exception 7') do + $i = 0 + def m + begin + $i += 1 + begin + $i += 2 + return + ensure + $i += 3 + end + ensure + $i += 4 + end + p :end + end + m + $i == 10 +end + +assert('BS Exception 8') do + begin + 1 + rescue + 2 + else + 3 + end == 3 +end + +assert('BS Exception 9') do + begin + 1+1 + rescue + 2+2 + else + 3+3 + ensure + 4+4 + end == 6 +end + +assert('BS Exception 10') do + begin + 1+1 + begin + 2+2 + rescue + 3+3 + else + 4+4 + end + rescue + 5+5 + else + 6+6 + ensure + 7+7 + end == 12 +end diff --git a/test/t/bs_literal.rb b/test/t/bs_literal.rb new file mode 100644 index 000000000..a79b0b045 --- /dev/null +++ b/test/t/bs_literal.rb @@ -0,0 +1,38 @@ +## +# Bootstrap test for literals + +assert('BS Literal 1') do + true == true +end + +assert('BS Literal 2') do + TrueClass == true.class +end + +assert('BS Literal 3') do + false == false +end + +assert('BS Literal 4') do + FalseClass == false.class +end + +assert('BS Literal 5') do + 'nil' == nil.inspect +end + +assert('BS Literal 6') do + NilClass == nil.class +end + +assert('BS Literal 7') do + Symbol == :sym.class +end + +assert('BS Literal 8') do + 1234 == 1234 +end + +assert('BS Literal 9') do + Fixnum == 1234.class +end \ No newline at end of file -- cgit v1.2.3 From dd6af6d2ec17c88c87844e75cd973fb4569c4c88 Mon Sep 17 00:00:00 2001 From: Daniel Bovensiepen Date: Wed, 16 May 2012 16:00:05 +0800 Subject: Add newline in the end of test case files so that the concat of all files works in all cases proper --- test/t/_assert.rb | 1 + test/t/bs_block.rb | 1 + test/t/bs_class.rb | 1 + test/t/bs_exception.rb | 3 ++- test/t/bs_literal.rb | 3 ++- test/t/time.rb | 1 + 6 files changed, 8 insertions(+), 2 deletions(-) diff --git a/test/t/_assert.rb b/test/t/_assert.rb index ecca78656..b2ce616e0 100644 --- a/test/t/_assert.rb +++ b/test/t/_assert.rb @@ -64,3 +64,4 @@ def report() print($kill_test) print("\n") end + diff --git a/test/t/bs_block.rb b/test/t/bs_block.rb index b290cb914..acbade449 100644 --- a/test/t/bs_block.rb +++ b/test/t/bs_block.rb @@ -388,3 +388,4 @@ assert('BS Block [ruby-core:14395]') do t = Controller.new t.test_for_bug end + diff --git a/test/t/bs_class.rb b/test/t/bs_class.rb index ae8668230..d8bb63c05 100644 --- a/test/t/bs_class.rb +++ b/test/t/bs_class.rb @@ -118,3 +118,4 @@ end assert('BS Class Dup 2') do module M; end; M.dup.class == Module end + diff --git a/test/t/bs_exception.rb b/test/t/bs_exception.rb index 406c07467..6ab2cee2a 100644 --- a/test/t/bs_exception.rb +++ b/test/t/bs_exception.rb @@ -154,4 +154,5 @@ assert('BS Exception 10') do ensure 7+7 end == 12 -end +end + diff --git a/test/t/bs_literal.rb b/test/t/bs_literal.rb index a79b0b045..b1ae3a5d6 100644 --- a/test/t/bs_literal.rb +++ b/test/t/bs_literal.rb @@ -35,4 +35,5 @@ end assert('BS Literal 9') do Fixnum == 1234.class -end \ No newline at end of file +end + diff --git a/test/t/time.rb b/test/t/time.rb index f33cdde0d..98b99f2c2 100644 --- a/test/t/time.rb +++ b/test/t/time.rb @@ -71,3 +71,4 @@ end assert('Time#new') do Time.new.class == Time end + -- cgit v1.2.3 From 26c6031a53867011eb33b624ed245d5603b778cf Mon Sep 17 00:00:00 2001 From: Daniel Bovensiepen Date: Wed, 16 May 2012 16:07:55 +0800 Subject: Move assert lib for tests one directory up and add it always in the beginning --- test/Makefile | 3 ++- test/assert.rb | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ test/t/_assert.rb | 67 ------------------------------------------------------- 3 files changed, 69 insertions(+), 68 deletions(-) create mode 100644 test/assert.rb delete mode 100644 test/t/_assert.rb diff --git a/test/Makefile b/test/Makefile index 26c898704..7f5c16980 100644 --- a/test/Makefile +++ b/test/Makefile @@ -12,6 +12,7 @@ INIT := init_$(TARGET).c DLIB := $(TARGET).ctmp RLIB := $(TARGET).rbtmp DEPLIB := $(TARGET).d driver.d +ASSLIB := $(BASEDIR)/assert.rb MRBS := $(BASEDIR)/t/*.rb OBJS := driver.o $(MLIB) @@ -95,7 +96,7 @@ $(MRBC) : ../src/opcode.h ../src/codegen.c ../src/parse.y $(MAKE) -C ../tools/mrbc $(MAKE_FLAGS) # merge mruby sources -$(RLIB) : $(MRBS) +$(RLIB) : $(ASSLIB) $(MRBS) cat $? > $@ # clean up diff --git a/test/assert.rb b/test/assert.rb new file mode 100644 index 000000000..b2ce616e0 --- /dev/null +++ b/test/assert.rb @@ -0,0 +1,67 @@ +$ok_test = 0 +$ko_test = 0 +$kill_test = 0 +$asserts = [] + +## +# Print the assertion in a readable way +def print_assertion_string(str, iso) + print(str) + if(iso != '') + print(' [') + print(iso) + print(']') + end +end + +## +# Verify a code block. +# +# str : A remark which will be printed in case +# this assertion fails +# iso : The ISO reference code of the feature +# which will be tested by this +# assertion +def assert(str = 'Assertion failed', iso = '') + begin + if(!yield) + $asserts.push([str, iso]) + $ko_test += 1 + print('F') + else + $ok_test += 1 + print('.') + end + rescue + $kill_test += 1 + print('X') + end +end + +## +# Report the test result and print all assertions +# which were reported broken. +def report() + print "\n" + $asserts.each do |str, iso| + print('Fail: '); + print_assertion_string(str, iso) + print("\n") + end + + $total_test = $ok_test.+($ko_test) + print('Total: ') + print($total_test) + print("\n") + + print(' OK: ') + print($ok_test) + print("\n") + print(' KO: ') + print($ko_test) + print("\n") + print(' Crash: ') + print($kill_test) + print("\n") +end + diff --git a/test/t/_assert.rb b/test/t/_assert.rb deleted file mode 100644 index b2ce616e0..000000000 --- a/test/t/_assert.rb +++ /dev/null @@ -1,67 +0,0 @@ -$ok_test = 0 -$ko_test = 0 -$kill_test = 0 -$asserts = [] - -## -# Print the assertion in a readable way -def print_assertion_string(str, iso) - print(str) - if(iso != '') - print(' [') - print(iso) - print(']') - end -end - -## -# Verify a code block. -# -# str : A remark which will be printed in case -# this assertion fails -# iso : The ISO reference code of the feature -# which will be tested by this -# assertion -def assert(str = 'Assertion failed', iso = '') - begin - if(!yield) - $asserts.push([str, iso]) - $ko_test += 1 - print('F') - else - $ok_test += 1 - print('.') - end - rescue - $kill_test += 1 - print('X') - end -end - -## -# Report the test result and print all assertions -# which were reported broken. -def report() - print "\n" - $asserts.each do |str, iso| - print('Fail: '); - print_assertion_string(str, iso) - print("\n") - end - - $total_test = $ok_test.+($ko_test) - print('Total: ') - print($total_test) - print("\n") - - print(' OK: ') - print($ok_test) - print("\n") - print(' KO: ') - print($ko_test) - print("\n") - print(' Crash: ') - print($kill_test) - print("\n") -end - -- cgit v1.2.3