Skip to content

Commit

Permalink
move block tests into block_test
Browse files Browse the repository at this point in the history
  • Loading branch information
baroquebobcat committed Feb 26, 2012
1 parent 6e6bfaf commit 77f110f
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 126 deletions.
125 changes: 125 additions & 0 deletions test/jvm/blocks_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,129 @@ def foo(a:Bar)
end
end

def test_block
cls, = compile(<<-EOF)
thread = Thread.new do
puts "Hello"
end
begin
thread.run
thread.join
rescue
puts "Uh Oh!"
end
EOF
assert_output("Hello\n") do
cls.main([].to_java :string)
end

script, cls = compile(<<-EOF)
import java.util.Observable
class MyObservable < Observable
def initialize
super
setChanged
end
end
o = MyObservable.new
o.addObserver {|x, a| puts a}
o.notifyObservers("Hello Observer")
EOF
assert_output("Hello Observer\n") do
script.main([].to_java :string)
end

cls, = compile(<<-EOF)
def foo
a = "Hello"
thread = Thread.new do
puts a
end
begin
a = a + " Closures"
thread.run
thread.join
rescue
puts "Uh Oh!"
end
return
end
EOF
assert_output("Hello Closures\n") do
cls.foo
end

cls, = compile(<<-EOF)
def run(x:Runnable)
x.run
end
def foo
a = 1
run {a += 1}
a
end
EOF
assert_equal(2, cls.foo)
end

def test_block_with_method_def
cls, = compile(<<-EOF)
import java.util.ArrayList
import java.util.Collections
list = ArrayList.new(["a", "ABC", "Cats", "b"])
Collections.sort(list) do
def equals(a:Object, b:Object)
String(a).equalsIgnoreCase(String(b))
end
def compare(a:Object, b:Object)
String(a).compareToIgnoreCase(String(b))
end
end
list.each {|x| puts x}
EOF

assert_output("a\nABC\nb\nCats\n") do
cls.main(nil)
end
end

def test_block_with_abstract_from_object
# Comparator interface also defines equals(Object) as abstract,
# but it can be inherited from Object. We test that here.
cls, = compile(<<-EOF)
import java.util.ArrayList
import java.util.Collections
list = ArrayList.new(["a", "ABC", "Cats", "b"])
Collections.sort(list) do |a, b|
String(a).compareToIgnoreCase(String(b))
end
list.each {|x| puts x}
EOF

assert_output("a\nABC\nb\nCats\n") do
cls.main(nil)
end
end

def test_block_with_no_arguments_and_return_value
cls, = compile(<<-EOF)
import java.util.concurrent.Callable
def foo c:Callable
throws Exception
puts c.call
end
begin
foo do
"an object"
end
rescue
puts "never get here"
end
EOF
assert_output("an object\n") do
cls.main(nil)
end
end

end
126 changes: 0 additions & 126 deletions test/jvm/jvm_compiler_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1493,132 +1493,6 @@ def run
# It shouldn't get confused by the Thread(String) constructor.
end

def test_block
cls, = compile(<<-EOF)
thread = Thread.new do
puts "Hello"
end
begin
thread.run
thread.join
rescue
puts "Uh Oh!"
end
EOF
assert_output("Hello\n") do
cls.main([].to_java :string)
end

script, cls = compile(<<-EOF)
import java.util.Observable
class MyObservable < Observable
def initialize
super
setChanged
end
end
o = MyObservable.new
o.addObserver {|x, a| puts a}
o.notifyObservers("Hello Observer")
EOF
assert_output("Hello Observer\n") do
script.main([].to_java :string)
end

cls, = compile(<<-EOF)
def foo
a = "Hello"
thread = Thread.new do
puts a
end
begin
a = a + " Closures"
thread.run
thread.join
rescue
puts "Uh Oh!"
end
return
end
EOF
assert_output("Hello Closures\n") do
cls.foo
end

cls, = compile(<<-EOF)
def run(x:Runnable)
x.run
end
def foo
a = 1
run {a += 1}
a
end
EOF
assert_equal(2, cls.foo)
end

def test_block_with_method_def
cls, = compile(<<-EOF)
import java.util.ArrayList
import java.util.Collections
list = ArrayList.new(["a", "ABC", "Cats", "b"])
Collections.sort(list) do
def equals(a:Object, b:Object)
String(a).equalsIgnoreCase(String(b))
end
def compare(a:Object, b:Object)
String(a).compareToIgnoreCase(String(b))
end
end
list.each {|x| puts x}
EOF

assert_output("a\nABC\nb\nCats\n") do
cls.main(nil)
end
end

def test_block_with_abstract_from_object
# Comparator interface also defines equals(Object) as abstract,
# but it can be inherited from Object. We test that here.
cls, = compile(<<-EOF)
import java.util.ArrayList
import java.util.Collections
list = ArrayList.new(["a", "ABC", "Cats", "b"])
Collections.sort(list) do |a, b|
String(a).compareToIgnoreCase(String(b))
end
list.each {|x| puts x}
EOF

assert_output("a\nABC\nb\nCats\n") do
cls.main(nil)
end
end

def test_block_with_no_arguments_and_return_value
cls, = compile(<<-EOF)
import java.util.concurrent.Callable
def foo c:Callable
throws Exception
puts c.call
end
begin
foo do
"an object"
end
rescue
puts "never get here"
end
EOF
assert_output("an object\n") do
cls.main(nil)
end
end


def test_optional_args
cls, = compile(<<-EOF)
def foo(a:int, b:int = 1, c:int = 2)
Expand Down

0 comments on commit 77f110f

Please sign in to comment.