mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
Add first part of the bootstrap code
This commit is contained in:
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user