duby 0.0.2-java → 0.0.3-java

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. data/History.txt +7 -0
  2. data/README.txt +18 -7
  3. data/Rakefile +72 -0
  4. data/examples/ant/example-build.xml +7 -0
  5. data/examples/appengine/Rakefile +8 -67
  6. data/examples/appengine/Readme +4 -3
  7. data/examples/appengine/lib/duby/appengine_tasks.rb +173 -0
  8. data/examples/appengine/lib/duby/plugin/datastore.rb +92 -31
  9. data/examples/appengine/lib/duby_task.rb +61 -0
  10. data/examples/appengine/src/com/ribrdb/DubyApp.duby +32 -6
  11. data/examples/appengine/src/com/ribrdb/list.dhtml +2 -2
  12. data/examples/appengine/{config.ru → src/config.ru} +0 -0
  13. data/examples/bintrees.duby +66 -0
  14. data/examples/dynamic.duby +17 -0
  15. data/examples/fib.duby +3 -11
  16. data/examples/fields.duby +3 -3
  17. data/examples/fractal.duby +1 -3
  18. data/examples/sort_closure.duby +7 -0
  19. data/examples/swing.duby +11 -11
  20. data/javalib/duby-bootstrap.jar +0 -0
  21. data/javalib/dynalang-invoke-0.1.jar +0 -0
  22. data/lib/duby.rb +168 -35
  23. data/lib/duby/ast.rb +224 -27
  24. data/lib/duby/ast/call.rb +85 -25
  25. data/lib/duby/ast/class.rb +112 -28
  26. data/lib/duby/ast/flow.rb +65 -44
  27. data/lib/duby/ast/intrinsics.rb +223 -21
  28. data/lib/duby/ast/literal.rb +67 -16
  29. data/lib/duby/ast/local.rb +36 -40
  30. data/lib/duby/ast/method.rb +83 -67
  31. data/lib/duby/ast/structure.rb +105 -23
  32. data/lib/duby/compiler.rb +83 -28
  33. data/lib/duby/env.rb +33 -0
  34. data/lib/duby/jvm/base.rb +210 -0
  35. data/lib/duby/jvm/compiler.rb +293 -219
  36. data/lib/duby/jvm/method_lookup.rb +77 -67
  37. data/lib/duby/jvm/source_compiler.rb +250 -157
  38. data/lib/duby/jvm/source_generator/builder.rb +53 -49
  39. data/lib/duby/jvm/source_generator/loops.rb +9 -9
  40. data/lib/duby/jvm/source_generator/precompile.rb +35 -25
  41. data/lib/duby/jvm/typer.rb +19 -10
  42. data/lib/duby/jvm/types.rb +127 -68
  43. data/lib/duby/jvm/types/basic_types.rb +26 -13
  44. data/lib/duby/jvm/types/enumerable.rb +6 -4
  45. data/lib/duby/jvm/types/factory.rb +49 -13
  46. data/lib/duby/jvm/types/floats.rb +16 -0
  47. data/lib/duby/jvm/types/integers.rb +63 -2
  48. data/lib/duby/jvm/types/intrinsics.rb +43 -21
  49. data/lib/duby/jvm/types/methods.rb +326 -86
  50. data/lib/duby/jvm/types/number.rb +3 -0
  51. data/lib/duby/nbcompiler.rb +1 -1
  52. data/lib/duby/plugin/edb.rb +1 -1
  53. data/lib/duby/plugin/java.rb +10 -1
  54. data/lib/duby/transform.rb +134 -46
  55. data/lib/duby/typer.rb +75 -50
  56. data/test/test_ast.rb +106 -106
  57. data/test/test_compilation.rb +46 -32
  58. data/test/test_env.rb +42 -0
  59. data/test/test_java_typer.rb +35 -51
  60. data/test/test_javac_compiler.rb +4 -1
  61. data/test/test_jvm_compiler.rb +564 -133
  62. data/test/test_typer.rb +68 -92
  63. metadata +37 -21
  64. data/examples/README +0 -16
  65. data/lib/duby/c/compiler.rb +0 -134
  66. data/lib/duby/old/compiler_old.rb +0 -845
  67. data/lib/duby/old/declaration.rb +0 -72
  68. data/lib/duby/old/mapper.rb +0 -72
  69. data/lib/duby/old/signature.rb +0 -52
  70. data/lib/duby/old/typer_old.rb +0 -163
  71. data/lib/duby/plugin/math.rb +0 -84
  72. data/test/test_math_plugin.rb +0 -87
@@ -5,89 +5,103 @@ require 'jruby'
5
5
 
6
6
  class TestAst < Test::Unit::TestCase
7
7
  include Duby
8
-
8
+
9
9
  class MockCompiler
10
10
  attr_accessor :calls
11
-
11
+
12
12
  def initialize
13
13
  @calls = []
14
14
  end
15
15
  def compile(ast)
16
16
  ast.compile(self, true)
17
17
  end
18
-
18
+
19
19
  def line(num)
20
20
  # ignore newlines
21
21
  end
22
-
22
+
23
23
  def method_missing(sym, *args, &block)
24
24
  calls << [sym, *args]
25
25
  block.call if block
26
26
  end
27
27
  end
28
-
28
+
29
+ class ClassComparison
30
+ def initialize(klass)
31
+ @class = klass
32
+ end
33
+
34
+ def ==(other)
35
+ other.kind_of?(@class)
36
+ end
37
+ end
38
+
39
+ def a(klass)
40
+ ClassComparison.new(klass)
41
+ end
42
+
29
43
  def setup
30
44
  @compiler = MockCompiler.new
31
45
  end
32
-
46
+
33
47
  def test_fixnum
34
- new_ast = AST.parse("1").body
35
-
48
+ new_ast = AST.parse("1").body[0]
49
+
36
50
  new_ast.compile(@compiler, true)
37
-
51
+
38
52
  assert_equal([[:fixnum, 1]], @compiler.calls)
39
53
  end
40
-
54
+
41
55
  def test_string
42
- new_ast = AST.parse("'foo'").body
43
-
56
+ new_ast = AST.parse("'foo'").body[0]
57
+
44
58
  new_ast.compile(@compiler, true)
45
-
59
+
46
60
  assert_equal([[:string, "foo"]], @compiler.calls)
47
61
  end
48
-
62
+
49
63
  def test_float
50
- new_ast = AST.parse("1.0").body
51
-
64
+ new_ast = AST.parse("1.0").body[0]
65
+
52
66
  new_ast.compile(@compiler, true)
53
-
67
+
54
68
  assert_equal([[:float, 1.0]], @compiler.calls)
55
69
  end
56
-
70
+
57
71
  def test_boolean
58
- new_ast = AST.parse("true").body
59
-
72
+ new_ast = AST.parse("true").body[0]
73
+
60
74
  new_ast.compile(@compiler, true)
61
-
75
+
62
76
  assert_equal([[:boolean, true]], @compiler.calls)
63
77
  end
64
-
78
+
65
79
  def test_local
66
- new_ast = AST.parse("a = 1").body
67
-
80
+ new_ast = AST.parse("a = 1").body[0]
81
+
68
82
  new_ast.compile(@compiler, true)
69
-
70
- assert_equal([[:local_assign, "a", nil, true, AST.fixnum(nil, nil, 1)]], @compiler.calls)
83
+
84
+ assert_equal([[:local_assign, a(Duby::AST::StaticScope), "a", nil, true, AST.fixnum(nil, nil, 1)]], @compiler.calls)
71
85
  end
72
-
86
+
73
87
  def test_local_typed
74
- new_ast = AST.parse("a = 1").body
88
+ new_ast = AST.parse("a = 1").body[0]
75
89
  typer = Typer::Simple.new(:bar)
76
90
  new_ast.infer(typer)
77
91
  new_ast.compile(@compiler, true)
78
-
79
- assert_equal([[:local_assign, "a", AST.type(:fixnum), true, AST.fixnum(nil, nil, 1)]], @compiler.calls)
92
+
93
+ assert_equal([[:local_assign, a(Duby::AST::StaticScope), "a", AST.type(:fixnum), true, AST.fixnum(nil, nil, 1)]], @compiler.calls)
80
94
  end
81
95
 
82
96
  def test_return
83
- new_ast = AST.parse("return 1").body
97
+ new_ast = AST.parse("return 1").body[0]
84
98
  new_ast.compile(@compiler, true)
85
99
 
86
100
  assert_equal([[:return, new_ast]], @compiler.calls)
87
101
  end
88
102
 
89
103
  def test_empty_array
90
- new_ast = AST.parse("int[5]").body
104
+ new_ast = AST.parse("int[5]").body[0]
91
105
  new_ast.compile(@compiler, true)
92
106
 
93
107
  assert_equal(1, @compiler.calls.size)
@@ -0,0 +1,42 @@
1
+ require 'test/unit'
2
+ require 'duby'
3
+
4
+ class TestEnv < Test::Unit::TestCase
5
+ include Duby
6
+
7
+ def test_path_seperator
8
+ # Check that env var PATH_SEPERATOR is used
9
+ RbConfig::CONFIG['PATH_SEPARATOR'] = '*'
10
+ assert_equal('*', Duby::Env.path_seperator)
11
+
12
+ # Check that : (colon) is returned if no PATH_SEPERATOR env var set
13
+ RbConfig::CONFIG['PATH_SEPARATOR'] = ''
14
+ assert_equal(':', Duby::Env.path_seperator)
15
+ end
16
+
17
+ def test_encode_paths
18
+ RbConfig::CONFIG['PATH_SEPARATOR'] = ':'
19
+
20
+ assert_equal('a:b:c', Duby::Env.encode_paths(['a','b','c']))
21
+ assert_equal('a', Duby::Env.encode_paths(['a']))
22
+ assert_equal('', Duby::Env.encode_paths([]))
23
+
24
+ RbConfig::CONFIG['PATH_SEPARATOR'] = ';'
25
+
26
+ assert_equal('a;b;c', Duby::Env.encode_paths(['a','b','c']))
27
+ end
28
+
29
+ def test_decode_paths
30
+ RbConfig::CONFIG['PATH_SEPARATOR'] = ':'
31
+
32
+ path_array = ['1','2']
33
+ assert_equal(['1','2','a','b','c','d'], Duby::Env.decode_paths('a:b:c:d', path_array))
34
+ assert_equal(['1','2','a','b','c','d'], path_array)
35
+
36
+ assert_equal(['a','b','c','d'], Duby::Env.decode_paths('a:b:c:d'))
37
+ assert_equal(['a'], Duby::Env.decode_paths('a'))
38
+
39
+ RbConfig::CONFIG['PATH_SEPARATOR'] = ';'
40
+ assert_equal(['a','b','c','d'], Duby::Env.decode_paths('a;b;c;d'))
41
+ end
42
+ end
@@ -8,12 +8,12 @@ require 'duby/jvm/typer'
8
8
 
9
9
  class TestJavaTyper < Test::Unit::TestCase
10
10
  include Duby
11
-
11
+
12
12
  def setup
13
13
  AST.type_factory = Duby::JVM::Types::TypeFactory.new
14
14
  @typer = Typer::JVM.new('foobar', nil)
15
15
  compiler = Duby::Compiler::JVM.new('foobar')
16
-
16
+
17
17
  @java_typer = Typer::JavaTyper.new
18
18
  end
19
19
 
@@ -24,97 +24,81 @@ class TestJavaTyper < Test::Unit::TestCase
24
24
  def test_simple_overtyped_meta_method
25
25
  string_meta = AST::type('java.lang.String', false, true)
26
26
  string = AST::type('java.lang.String')
27
-
27
+
28
28
  # integral types
29
29
  ['boolean', 'char', 'double', 'float', 'int', 'long'].each do |type_name|
30
30
  type = AST::type(type_name)
31
31
  return_type = @java_typer.method_type(@typer, string_meta, 'valueOf', [type])
32
32
  assert_equal(string, return_type, "valueOf(#{type}) should return #{string}")
33
33
  end
34
-
34
+
35
35
  # char[]
36
36
  type = AST::type('char', true)
37
37
  return_type = @java_typer.method_type(@typer, string_meta, 'valueOf', [type])
38
38
  assert_equal(string, return_type)
39
-
39
+
40
40
  # Object
41
41
  type = AST::type('java.lang.Object')
42
42
  return_type = @java_typer.method_type(@typer, string_meta, 'valueOf', [type])
43
43
  assert_equal(string, return_type)
44
44
  end
45
-
45
+
46
46
  def test_non_overtyped_method
47
47
  string = AST::type('java.lang.String')
48
-
48
+
49
49
  int = AST::type('int')
50
50
  return_type = @java_typer.method_type(@typer, string, 'length', [])
51
51
  assert_equal(int, return_type)
52
-
52
+
53
53
  byte_array = AST::type('byte', true)
54
54
  return_type = @java_typer.method_type(@typer, string, 'getBytes', [])
55
55
  assert_equal(byte_array, return_type)
56
56
  end
57
-
57
+
58
58
  def test_simple_overtyped_method
59
59
  string_meta = AST::type('java.lang.String', false, true)
60
60
  string = AST::type('java.lang.String')
61
-
61
+
62
62
  return_type = @java_typer.method_type(@typer, string_meta, 'valueOf', [string])
63
63
  assert_equal(string, return_type)
64
64
  end
65
-
65
+
66
66
  def test_primitive_conversion_method
67
67
  string = AST::type('java.lang.String')
68
68
  byte = AST::type('byte')
69
69
  char = AST::type('char')
70
70
  long = AST::type('long')
71
-
71
+
72
72
  return_type = @java_typer.method_type(@typer, string, 'charAt', [byte])
73
73
  assert_equal(char, return_type)
74
-
74
+
75
75
  assert_raise NoMethodError do
76
76
  @java_typer.method_type(@typer, string, 'charAt', [long])
77
77
  end
78
78
  end
79
-
79
+
80
80
  include Duby::JVM::MethodLookup
81
-
82
- def test_complex_overtyped_method
83
- printstream = java.io.PrintStream.java_class
84
- object = java.lang.Object.java_class
85
- string = java.lang.String.java_class
86
-
87
- # best match is Object
88
- method = find_method(printstream, "println", [object], false)
89
- assert_match('println(java.lang.Object)', method.to_s)
90
- # best match is String
91
- method = find_method(printstream, "println", [string], false)
92
- assert_match('println(java.lang.String)', method.to_s)
93
- # only match is Object
94
- method = find_method(printstream, "println", [printstream], false)
95
- assert_match('println(java.lang.Object)', method.to_s)
96
- end
97
-
81
+
98
82
  def test_is_more_specific
99
83
  object = java.lang.Object.java_class
100
84
  charseq = java.lang.CharSequence.java_class
101
85
  string = java.lang.String.java_class
102
-
86
+
103
87
  assert @java_typer.is_more_specific?([string], [object])
104
88
  assert @java_typer.is_more_specific?([charseq], [object])
105
89
  assert @java_typer.is_more_specific?([string], [charseq])
106
90
  end
107
-
91
+
108
92
  def test_primitive_convertible
109
- boolean = Java::boolean.java_class
110
- byte = Java::byte.java_class
111
- short = Java::short.java_class
112
- char = Java::char.java_class
113
- int = Java::int.java_class
114
- long = Java::long.java_class
115
- float = Java::float.java_class
116
- double = Java::double.java_class
117
-
93
+ boolean = Duby::JVM::Types::Boolean
94
+ byte = Duby::JVM::Types::Byte
95
+ short = Duby::JVM::Types::Short
96
+ char = Duby::JVM::Types::Char
97
+ int = Duby::JVM::Types::Int
98
+ long = Duby::JVM::Types::Long
99
+ float = Duby::JVM::Types::Float
100
+ double = Duby::JVM::Types::Double
101
+
118
102
  assert !primitive_convertible?(boolean, byte)
119
103
  assert !primitive_convertible?(boolean, short)
120
104
  assert !primitive_convertible?(boolean, char)
@@ -123,16 +107,16 @@ class TestJavaTyper < Test::Unit::TestCase
123
107
  assert !primitive_convertible?(boolean, float)
124
108
  assert !primitive_convertible?(boolean, double)
125
109
  assert primitive_convertible?(boolean, boolean)
126
-
110
+
127
111
  assert !primitive_convertible?(byte, boolean)
128
112
  assert primitive_convertible?(byte, byte)
129
113
  assert primitive_convertible?(byte, short)
130
- assert primitive_convertible?(byte, char)
114
+ assert !primitive_convertible?(byte, char)
131
115
  assert primitive_convertible?(byte, int)
132
116
  assert primitive_convertible?(byte, long)
133
117
  assert primitive_convertible?(byte, float)
134
118
  assert primitive_convertible?(byte, double)
135
-
119
+
136
120
  assert !primitive_convertible?(short, boolean)
137
121
  assert !primitive_convertible?(short, byte)
138
122
  assert !primitive_convertible?(short, char)
@@ -141,7 +125,7 @@ class TestJavaTyper < Test::Unit::TestCase
141
125
  assert primitive_convertible?(short, long)
142
126
  assert primitive_convertible?(short, float)
143
127
  assert primitive_convertible?(short, double)
144
-
128
+
145
129
  assert !primitive_convertible?(char, boolean)
146
130
  assert !primitive_convertible?(char, byte)
147
131
  assert !primitive_convertible?(char, short)
@@ -150,7 +134,7 @@ class TestJavaTyper < Test::Unit::TestCase
150
134
  assert primitive_convertible?(char, long)
151
135
  assert primitive_convertible?(char, float)
152
136
  assert primitive_convertible?(char, double)
153
-
137
+
154
138
  assert !primitive_convertible?(int, boolean)
155
139
  assert !primitive_convertible?(int, byte)
156
140
  assert !primitive_convertible?(int, short)
@@ -159,16 +143,16 @@ class TestJavaTyper < Test::Unit::TestCase
159
143
  assert primitive_convertible?(int, long)
160
144
  assert primitive_convertible?(int, float)
161
145
  assert primitive_convertible?(int, double)
162
-
146
+
163
147
  assert !primitive_convertible?(long, boolean)
164
148
  assert !primitive_convertible?(long, byte)
165
149
  assert !primitive_convertible?(long, short)
166
150
  assert !primitive_convertible?(long, char)
167
151
  assert !primitive_convertible?(long, int)
168
152
  assert primitive_convertible?(long, long)
169
- assert !primitive_convertible?(long, float)
153
+ assert primitive_convertible?(long, float)
170
154
  assert primitive_convertible?(long, double)
171
-
155
+
172
156
  assert !primitive_convertible?(float, boolean)
173
157
  assert !primitive_convertible?(float, byte)
174
158
  assert !primitive_convertible?(float, short)
@@ -177,7 +161,7 @@ class TestJavaTyper < Test::Unit::TestCase
177
161
  assert !primitive_convertible?(float, long)
178
162
  assert primitive_convertible?(float, float)
179
163
  assert primitive_convertible?(float, double)
180
-
164
+
181
165
  assert !primitive_convertible?(double, boolean)
182
166
  assert !primitive_convertible?(double, byte)
183
167
  assert !primitive_convertible?(double, short)
@@ -7,6 +7,9 @@ require 'jruby'
7
7
  require 'stringio'
8
8
  require File.join(File.dirname(__FILE__), 'test_jvm_compiler')
9
9
 
10
+ # make sure . is in CLASSPATH
11
+ $CLASSPATH << '.'
12
+
10
13
  class TestJavacCompiler < TestJVMCompiler
11
14
  import javax.tools.ToolProvider
12
15
  import java.util.Arrays
@@ -37,7 +40,7 @@ class TestJavacCompiler < TestJVMCompiler
37
40
  File.unlink(*@tmp_classes)
38
41
  @tmp_classes.clear
39
42
  AST.type_factory = Duby::JVM::Types::TypeFactory.new
40
- transformer = Duby::Transform::Transformer.new
43
+ transformer = Duby::Transform::Transformer.new(Duby::CompilationState.new)
41
44
  ast = AST.parse(code, name, true, transformer)
42
45
  name = "script" + System.nano_time.to_s
43
46
  typer = Typer::JVM.new(name, transformer)
@@ -10,12 +10,12 @@ unless Duby::AST.macro "__gloop__"
10
10
  Duby::AST::Loop.new(parent, parent.position, true, false) do |loop|
11
11
  init, condition, check_first, pre, post = fcall.args_node.child_nodes.to_a
12
12
  loop.check_first = check_first.kind_of?(Duby::AST::JRubyAst::TrueNode)
13
-
13
+
14
14
  nil_t = Duby::AST::JRubyAst::NilNode
15
15
  loop.init = transformer.transform(init, loop) unless init.kind_of?(nil_t)
16
16
  loop.pre = transformer.transform(pre, loop) unless pre.kind_of?(nil_t)
17
17
  loop.post = transformer.transform(post, loop) unless post.kind_of?(nil_t)
18
-
18
+
19
19
  body = transformer.transform(fcall.iter_node, loop).body
20
20
  body.parent = loop
21
21
  [
@@ -54,27 +54,29 @@ class TestJVMCompiler < Test::Unit::TestCase
54
54
  @tmp_classes.clear
55
55
  AST.type_factory = Duby::JVM::Types::TypeFactory.new
56
56
  name = "script" + System.nano_time.to_s
57
- transformer = Duby::Transform::Transformer.new
57
+ transformer = Duby::Transform::Transformer.new(Duby::CompilationState.new)
58
58
  ast = AST.parse(code, name, true, transformer)
59
59
  typer = Typer::JVM.new(name, transformer)
60
60
  ast.infer(typer)
61
61
  typer.resolve(true)
62
62
  compiler = Compiler::JVM.new(name)
63
63
  compiler.compile(ast)
64
- classes = []
65
- loader = org.jruby.util.JRubyClassLoader.new(
66
- JRuby.runtime.jruby_class_loader)
64
+ classes = {}
65
+ loader = DubyClassLoader.new(JRuby.runtime.jruby_class_loader, classes)
67
66
  compiler.generate do |name, builder|
68
67
  bytes = builder.generate
69
68
  open("#{name}", "w") do |f|
70
69
  f << bytes
71
70
  end
72
- cls = loader.define_class(name[0..-7], bytes.to_java_bytes)
73
- classes << JavaUtilities.get_proxy_class(cls.name)
74
- @tmp_classes << "#{name}"
71
+ classes[name[0..-7]] = bytes
75
72
  end
76
73
 
77
- classes
74
+ classes.keys.map do |name|
75
+ cls = loader.load_class(name)
76
+ proxy = JavaUtilities.get_proxy_class(cls.name)
77
+ @tmp_classes << "#{name}.class"
78
+ proxy
79
+ end
78
80
  end
79
81
 
80
82
  def test_local
@@ -119,7 +121,7 @@ class TestJVMCompiler < Test::Unit::TestCase
119
121
  cls, = compile("def foo; a = 6.0; b = 3.0; a / b; end")
120
122
  assert_equal(2.0, cls.foo)
121
123
  end
122
-
124
+
123
125
  def test_rem
124
126
  cls, = compile("def foo; a = 7; b = 3; a % b; end")
125
127
  assert_equal(1, cls.foo)
@@ -184,7 +186,7 @@ class TestJVMCompiler < Test::Unit::TestCase
184
186
  cls, = compile("def foo; a = boolean[2]; a.length; end")
185
187
  assert_equal(Fixnum, cls.foo.class)
186
188
  assert_equal(2, cls.foo)
187
-
189
+
188
190
  cls, = compile("def foo; a = byte[2]; a; end")
189
191
  assert_equal(Java::byte[].java_class, cls.foo.class.java_class)
190
192
  assert_equal([0,0], cls.foo.to_a)
@@ -255,7 +257,7 @@ class TestJVMCompiler < Test::Unit::TestCase
255
257
  assert_equal(Java::int[].java_class, array.class.java_class)
256
258
  assert_equal([0,0,0,0], array.to_a)
257
259
  end
258
-
260
+
259
261
  def test_object_array
260
262
  cls, = compile("import java.lang.Object;def foo; a = Object[2];end")
261
263
  assert_equal(Java::JavaLang::Object[].java_class, cls.foo.class.java_class)
@@ -271,7 +273,7 @@ class TestJVMCompiler < Test::Unit::TestCase
271
273
  cls, = compile("import 'System', 'java.lang.System'; def foo; System.gc; end; foo")
272
274
  assert_nothing_raised {cls.foo}
273
275
  end
274
-
276
+
275
277
  def test_import
276
278
  cls, = compile("import 'AL', 'java.util.ArrayList'; def foo; AL.new; end; foo")
277
279
  assert_equal java.util.ArrayList.java_class, cls.foo.java_class
@@ -279,11 +281,11 @@ class TestJVMCompiler < Test::Unit::TestCase
279
281
  cls, = compile("import 'java.util.ArrayList'; def foo; ArrayList.new; end; foo")
280
282
  assert_equal java.util.ArrayList.java_class, cls.foo.java_class
281
283
  end
282
-
284
+
283
285
  def test_no_quote_import
284
286
  cls, = compile("import java.util.ArrayList as AL; def foo; AL.new; end; foo")
285
287
  assert_equal java.util.ArrayList.java_class, cls.foo.java_class
286
-
288
+
287
289
  cls, = compile("import java.util.ArrayList; def foo; ArrayList.new; end; foo")
288
290
  assert_equal java.util.ArrayList.java_class, cls.foo.java_class
289
291
  end
@@ -293,6 +295,16 @@ class TestJVMCompiler < Test::Unit::TestCase
293
295
  assert_equal 0, cls.foo(java.util.ArrayList.new)
294
296
  end
295
297
 
298
+ def test_import_package
299
+ cls, = compile(<<-EOF)
300
+ import java.util.*
301
+ def foo
302
+ ArrayList.new
303
+ end
304
+ EOF
305
+ assert_equal java.util.ArrayList.java_class, cls.foo.java_class
306
+ end
307
+
296
308
  def test_interface
297
309
  cls, = compile(<<-EOF)
298
310
  import 'java.util.concurrent.Callable'
@@ -305,7 +317,7 @@ class TestJVMCompiler < Test::Unit::TestCase
305
317
  assert_equal 0, result
306
318
  m = cls.java_class.java_method 'foo', java.util.concurrent.Callable
307
319
  assert_equal([java.lang.Exception.java_class], m.exception_types)
308
-
320
+
309
321
  end
310
322
 
311
323
  def test_class_decl
@@ -325,7 +337,7 @@ class TestJVMCompiler < Test::Unit::TestCase
325
337
  System.setOut(saved_output)
326
338
  end
327
339
  end
328
-
340
+
329
341
  def assert_output(expected, &block)
330
342
  assert_equal(expected, capture_output(&block))
331
343
  end
@@ -338,6 +350,14 @@ class TestJVMCompiler < Test::Unit::TestCase
338
350
  assert_equal("Hello World!\n", output)
339
351
  end
340
352
 
353
+ def test_print
354
+ cls, = compile("def foo;print 'Hello World!';end")
355
+ output = capture_output do
356
+ cls.foo
357
+ end
358
+ assert_equal("Hello World!", output)
359
+ end
360
+
341
361
  def test_constructor
342
362
  cls, = compile(
343
363
  "class InitializeTest;def initialize;puts 'Constructed';end;end")
@@ -349,7 +369,7 @@ class TestJVMCompiler < Test::Unit::TestCase
349
369
  def test_method
350
370
  # TODO auto generate a constructor
351
371
  cls, = compile(
352
- "class MethodTest; def initialize; ''; end; def foo; 'foo';end;end")
372
+ "class MethodTest; def foo; 'foo';end;end")
353
373
  instance = cls.new
354
374
  assert_equal(cls, instance.class)
355
375
  assert_equal('foo', instance.foo)
@@ -513,7 +533,7 @@ class TestJVMCompiler < Test::Unit::TestCase
513
533
  end
514
534
  EOF
515
535
  assert_equal([2, 2], cls.foo.to_a)
516
-
536
+
517
537
  end
518
538
 
519
539
  def test_loop
@@ -522,19 +542,19 @@ class TestJVMCompiler < Test::Unit::TestCase
522
542
  assert_equal('', capture_output{cls.foo(0)})
523
543
  assert_equal(".\n", capture_output{cls.foo(1)})
524
544
  assert_equal(".\n.\n", capture_output{cls.foo(2)})
525
-
545
+
526
546
  cls, = compile(
527
547
  'def foo(a => :fixnum);begin;a -= 1; puts ".";end while a > 0;end')
528
548
  assert_equal(".\n", capture_output{cls.foo(0)})
529
549
  assert_equal(".\n", capture_output{cls.foo(1)})
530
550
  assert_equal(".\n.\n", capture_output{cls.foo(2)})
531
-
551
+
532
552
  cls, = compile(
533
553
  'def foo(a => :fixnum);until a <= 0; a -= 1; puts ".";end;end')
534
554
  assert_equal('', capture_output{cls.foo(0)})
535
555
  assert_equal(".\n", capture_output{cls.foo(1)})
536
556
  assert_equal(".\n.\n", capture_output{cls.foo(2)})
537
-
557
+
538
558
  cls, = compile(
539
559
  'def foo(a => :fixnum);begin;a -= 1; puts ".";end until a <= 0;end')
540
560
  assert_equal(".\n", capture_output{cls.foo(0)})
@@ -558,7 +578,7 @@ class TestJVMCompiler < Test::Unit::TestCase
558
578
  end
559
579
  EOF
560
580
  assert_equal(1, cls.foo)
561
-
581
+
562
582
  cls, = compile <<-EOF
563
583
  def foo
564
584
  a = 0
@@ -707,7 +727,7 @@ class TestJVMCompiler < Test::Unit::TestCase
707
727
  def initialize(a => :fixnum)
708
728
  @a = a
709
729
  end
710
-
730
+
711
731
  def a
712
732
  @a
713
733
  end
@@ -720,29 +740,29 @@ class TestJVMCompiler < Test::Unit::TestCase
720
740
  assert_equal(1, first.a)
721
741
  assert_equal(2, second.a)
722
742
  end
723
-
743
+
724
744
  def test_object_intrinsics
725
745
  cls, = compile(<<-EOF)
726
746
  import 'java.lang.Object'
727
747
  def nil(a => :Object)
728
748
  a.nil?
729
749
  end
730
-
750
+
731
751
  def equal(a => Object, b => Object)
732
752
  a == b
733
753
  end
734
754
  EOF
735
-
755
+
736
756
  assert(cls.nil(nil))
737
757
  assert(!cls.nil("abc"))
738
-
758
+
739
759
  a = "foobar".to_java_string
740
760
  b = java.lang.Object.new
741
761
  assert(cls.equal(a, a))
742
762
  assert(cls.equal(b, b))
743
763
  assert(!cls.equal(a, b))
744
764
  end
745
-
765
+
746
766
  def test_implements
747
767
  script, cls = compile(<<-EOF)
748
768
  import java.lang.Iterable
@@ -755,69 +775,69 @@ class TestJVMCompiler < Test::Unit::TestCase
755
775
 
756
776
  assert_include java.lang.Iterable.java_class, cls.java_class.interfaces
757
777
  end
758
-
778
+
759
779
  def test_argument_widening
760
780
  cls, = compile(<<-EOF)
761
- def Byte(a => :byte)
762
- Short(a)
781
+ def _Byte(a => :byte)
782
+ _Short(a)
763
783
  end
764
-
765
- def Short(a => :short)
766
- Int(a)
784
+
785
+ def _Short(a => :short)
786
+ _Int(a)
767
787
  end
768
-
769
- def Int(a => :int)
770
- Long(a)
788
+
789
+ def _Int(a => :int)
790
+ _Long(a)
771
791
  end
772
-
773
- def Long(a => :long)
774
- Float(a)
792
+
793
+ def _Long(a => :long)
794
+ _Float(a)
775
795
  end
776
-
777
- def Float(a => :float)
778
- Double(a)
796
+
797
+ def _Float(a => :float)
798
+ _Double(a)
779
799
  end
780
-
781
- def Double(a => :double)
800
+
801
+ def _Double(a => :double)
782
802
  a
783
803
  end
784
804
  EOF
785
805
 
786
- assert_equal(1.0, cls.Byte(1))
787
- assert_equal(127.0, cls.Byte(127))
788
- assert_equal(128.0, cls.Short(128))
789
- assert_equal(32767.0, cls.Short(32767))
790
- assert_equal(32768.0, cls.Int(32768))
791
- assert_equal(2147483648.0, cls.Long(2147483648))
806
+ assert_equal(1.0, cls._Byte(1))
807
+ assert_equal(127.0, cls._Byte(127))
808
+ assert_equal(128.0, cls._Short(128))
809
+ assert_equal(32767.0, cls._Short(32767))
810
+ assert_equal(32768.0, cls._Int(32768))
811
+ assert_equal(2147483648.0, cls._Long(2147483648))
792
812
  end
793
-
813
+
794
814
  def test_interface_declaration
795
815
  script, interface = compile('interface A do; end')
796
816
  assert(interface.java_class.interface?)
797
817
  assert_equal('A', interface.java_class.name)
798
-
818
+
799
819
  script, a, b = compile('interface A do; end; interface B < A do; end')
800
820
  assert_include(a, b.ancestors)
801
821
  assert_equal('A', a.java_class.name)
802
822
  assert_equal('B', b.java_class.name)
803
-
823
+
804
824
  script, a, b, c = compile(<<-EOF)
805
825
  interface A do
806
826
  end
807
-
827
+
808
828
  interface B do
809
829
  end
810
-
830
+
811
831
  interface C < A, B do
812
832
  end
813
833
  EOF
814
-
834
+
815
835
  assert_include(a, c.ancestors)
816
836
  assert_include(b, c.ancestors)
817
837
  assert_equal('A', a.java_class.name)
818
838
  assert_equal('B', b.java_class.name)
819
839
  assert_equal('C', c.java_class.name)
820
-
840
+
821
841
  assert_raise Duby::Typer::InferenceError do
822
842
  compile(<<-EOF)
823
843
  interface A do
@@ -825,7 +845,7 @@ class TestJVMCompiler < Test::Unit::TestCase
825
845
  returns :int
826
846
  end
827
847
  end
828
-
848
+
829
849
  class Impl; implements A
830
850
  def a
831
851
  "foo"
@@ -891,7 +911,7 @@ class TestJVMCompiler < Test::Unit::TestCase
891
911
  cls.foo
892
912
  end
893
913
  end
894
-
914
+
895
915
  def test_rescue
896
916
  cls, = compile(<<-EOF)
897
917
  def foo
@@ -1080,7 +1100,7 @@ class TestJVMCompiler < Test::Unit::TestCase
1080
1100
  def d2i; int(1.0); end
1081
1101
  def d2l; long(1.0); end
1082
1102
  def d2f; float(1.0); end
1083
-
1103
+
1084
1104
  def hard_i2f(a:int)
1085
1105
  float(if a < 0
1086
1106
  a *= -1
@@ -1132,33 +1152,33 @@ class TestJVMCompiler < Test::Unit::TestCase
1132
1152
  assert_equal 1, cls.d2i
1133
1153
  assert_equal 1, cls.d2l
1134
1154
  assert_equal 1.0, cls.d2f
1135
-
1155
+
1136
1156
  assert_equal 2.0, cls.hard_i2f(1)
1137
1157
  assert_equal 4.0, cls.hard_i2f(-2)
1138
1158
  end
1139
-
1159
+
1140
1160
  def test_set
1141
1161
  cls, = compile(<<-EOF)
1142
1162
  def foo
1143
1163
  @foo
1144
1164
  end
1145
-
1165
+
1146
1166
  def foo=(foo:int)
1147
1167
  @foo = foo
1148
1168
  end
1149
1169
  EOF
1150
-
1170
+
1151
1171
  assert_equal(0, cls.foo)
1152
1172
  assert_equal(2, cls.foo_set(2))
1153
1173
  assert_equal(2, cls.foo)
1154
1174
  end
1155
-
1175
+
1156
1176
  def test_null_is_false
1157
1177
  cls, = compile("def foo(a:String);if a;true;else;false;end;end")
1158
1178
  assert_equal(true, cls.foo("a"))
1159
1179
  assert_equal(false, cls.foo(nil))
1160
1180
  end
1161
-
1181
+
1162
1182
  def test_for
1163
1183
  cls, = compile(<<-EOF)
1164
1184
  def foo
@@ -1170,7 +1190,7 @@ class TestJVMCompiler < Test::Unit::TestCase
1170
1190
  count
1171
1191
  end
1172
1192
  EOF
1173
-
1193
+
1174
1194
  cls, = compile(<<-EOF)
1175
1195
  def foo(a:int[])
1176
1196
  count = 0
@@ -1190,7 +1210,7 @@ class TestJVMCompiler < Test::Unit::TestCase
1190
1210
  end
1191
1211
  end
1192
1212
  EOF
1193
-
1213
+
1194
1214
  assert_output("1\n2\n3\n") do
1195
1215
  list = java.util.ArrayList.new
1196
1216
  list << "1"
@@ -1247,6 +1267,52 @@ class TestJVMCompiler < Test::Unit::TestCase
1247
1267
  assert_equal(false, cls.foo(["a", nil, "b"].to_java(:string)))
1248
1268
  end
1249
1269
 
1270
+ def test_downto
1271
+ cls, = compile(<<-EOF)
1272
+ def foo(i:int)
1273
+ i.downto(1) {|x| puts x }
1274
+ end
1275
+ EOF
1276
+
1277
+ assert_output("3\n2\n1\n") do
1278
+ cls.foo(3)
1279
+ end
1280
+ end
1281
+
1282
+ def test_upto
1283
+ cls, = compile(<<-EOF)
1284
+ def foo(i:int)
1285
+ i.upto(3) {|x| puts x }
1286
+ end
1287
+ EOF
1288
+
1289
+ assert_output("1\n2\n3\n") do
1290
+ cls.foo(1)
1291
+ end
1292
+ end
1293
+
1294
+ def test_times
1295
+ cls, = compile(<<-EOF)
1296
+ def foo(i:int)
1297
+ i.times {|x| puts x }
1298
+ end
1299
+ EOF
1300
+
1301
+ assert_output("0\n1\n2\n") do
1302
+ cls.foo(3)
1303
+ end
1304
+
1305
+ cls, = compile(<<-EOF)
1306
+ def foo(i:int)
1307
+ i.times { puts "Hi" }
1308
+ end
1309
+ EOF
1310
+
1311
+ assert_output("Hi\nHi\nHi\n") do
1312
+ cls.foo(3)
1313
+ end
1314
+ end
1315
+
1250
1316
  def test_general_loop
1251
1317
  cls, = compile(<<-EOF)
1252
1318
  def foo(x:boolean)
@@ -1256,7 +1322,7 @@ class TestJVMCompiler < Test::Unit::TestCase
1256
1322
  end
1257
1323
  EOF
1258
1324
  assert_equal("", cls.foo(false))
1259
-
1325
+
1260
1326
  cls, = compile(<<-EOF)
1261
1327
  def foo
1262
1328
  a = ""
@@ -1265,7 +1331,7 @@ class TestJVMCompiler < Test::Unit::TestCase
1265
1331
  end
1266
1332
  EOF
1267
1333
  assert_equal("<body>", cls.foo)
1268
-
1334
+
1269
1335
  cls, = compile(<<-EOF)
1270
1336
  def foo(x:boolean)
1271
1337
  a = ""
@@ -1276,7 +1342,7 @@ class TestJVMCompiler < Test::Unit::TestCase
1276
1342
  end
1277
1343
  EOF
1278
1344
  assert_equal("<init>", cls.foo(false))
1279
-
1345
+
1280
1346
  cls, = compile(<<-EOF)
1281
1347
  def foo
1282
1348
  a = ""
@@ -1287,7 +1353,7 @@ class TestJVMCompiler < Test::Unit::TestCase
1287
1353
  end
1288
1354
  EOF
1289
1355
  assert_equal("<init><pre><body><post>", cls.foo)
1290
-
1356
+
1291
1357
  cls, = compile(<<-EOF)
1292
1358
  def foo
1293
1359
  a = ""
@@ -1299,7 +1365,7 @@ class TestJVMCompiler < Test::Unit::TestCase
1299
1365
  end
1300
1366
  EOF
1301
1367
  assert_equal( "<init><pre><body><body><post>", cls.foo)
1302
-
1368
+
1303
1369
  cls, = compile(<<-EOF)
1304
1370
  def foo
1305
1371
  a = ""
@@ -1312,24 +1378,24 @@ class TestJVMCompiler < Test::Unit::TestCase
1312
1378
  EOF
1313
1379
  assert_equal("<init><pre><post><pre><body><post>", cls.foo)
1314
1380
  end
1315
-
1381
+
1316
1382
  def test_if_expr
1317
1383
  cls, = compile(<<-EOF)
1318
1384
  def foo(a:int)
1319
1385
  return 1 if a == 1
1320
1386
  end
1321
-
1387
+
1322
1388
  def bar(a:int)
1323
1389
  return 1 unless a == 1
1324
1390
  end
1325
1391
  EOF
1326
-
1392
+
1327
1393
  assert_equal(0, cls.foo(0))
1328
1394
  assert_equal(1, cls.foo(1))
1329
1395
  assert_equal(1, cls.bar(0))
1330
1396
  assert_equal(0, cls.bar(1))
1331
1397
  end
1332
-
1398
+
1333
1399
  def test_and
1334
1400
  cls, = compile(<<-EOF)
1335
1401
  def bool(n:String, x:boolean)
@@ -1340,21 +1406,21 @@ class TestJVMCompiler < Test::Unit::TestCase
1340
1406
  def foo(a:boolean, b:boolean)
1341
1407
  return bool('a', a) && bool('b', b)
1342
1408
  end
1343
-
1409
+
1344
1410
  def str(n:String, x:String)
1345
1411
  puts n
1346
1412
  x
1347
1413
  end
1348
-
1414
+
1349
1415
  def bar(a:String, b:String)
1350
1416
  return str('a', a) && str('b', b)
1351
1417
  end
1352
1418
  EOF
1353
-
1419
+
1354
1420
  assert_output("a\n") { assert_equal(false, cls.foo(false, true)) }
1355
1421
  assert_output("a\nb\n") { assert_equal(false, cls.foo(true, false)) }
1356
1422
  assert_output("a\nb\n") { assert_equal(true, cls.foo(true, true)) }
1357
-
1423
+
1358
1424
  assert_output("a\n") { assert_equal(nil, cls.bar(nil, "B")) }
1359
1425
  assert_output("a\nb\n") { assert_equal(nil, cls.bar("A", nil)) }
1360
1426
  assert_output("a\nb\n") { assert_equal("B", cls.bar("A", "B")) }
@@ -1363,15 +1429,15 @@ class TestJVMCompiler < Test::Unit::TestCase
1363
1429
  def s
1364
1430
  @s
1365
1431
  end
1366
-
1432
+
1367
1433
  def s=(s:String)
1368
1434
  @s = s
1369
1435
  end
1370
-
1436
+
1371
1437
  def b
1372
1438
  @b
1373
1439
  end
1374
-
1440
+
1375
1441
  def b=(b:boolean)
1376
1442
  @b = b
1377
1443
  end
@@ -1379,7 +1445,7 @@ class TestJVMCompiler < Test::Unit::TestCase
1379
1445
  def foo(x:boolean)
1380
1446
  @b &&= x
1381
1447
  end
1382
-
1448
+
1383
1449
  def bar(x:String)
1384
1450
  @s &&= x
1385
1451
  end
@@ -1396,7 +1462,7 @@ class TestJVMCompiler < Test::Unit::TestCase
1396
1462
  cls.b_set(true)
1397
1463
  assert_equal(true, cls.foo(true))
1398
1464
  assert_equal(true, cls.b)
1399
-
1465
+
1400
1466
  cls.s_set(nil)
1401
1467
  assert_equal(nil, cls.bar(nil))
1402
1468
  assert_equal(nil, cls.s)
@@ -1408,41 +1474,41 @@ class TestJVMCompiler < Test::Unit::TestCase
1408
1474
  cls.s_set("S")
1409
1475
  assert_equal("x", cls.bar("x"))
1410
1476
  assert_equal("x", cls.s)
1411
-
1477
+
1412
1478
  foo, = compile(<<-EOF)
1413
1479
  class Foo2
1414
1480
  def initialize
1415
1481
  @count = 0
1416
1482
  end
1417
-
1483
+
1418
1484
  def count
1419
1485
  @count
1420
1486
  end
1421
-
1487
+
1422
1488
  def a
1423
1489
  @a
1424
1490
  end
1425
-
1491
+
1426
1492
  def a=(a:String)
1427
1493
  @count += 1
1428
1494
  @a = a
1429
1495
  end
1430
-
1496
+
1431
1497
  def foo(f:Foo2, x:String)
1432
1498
  f.a &&= x
1433
1499
  end
1434
1500
  end
1435
1501
  EOF
1436
-
1502
+
1437
1503
  f = foo.new
1438
1504
  assert_equal(nil, f.foo(f, 'x'))
1439
1505
  assert_equal(0, f.count)
1440
-
1506
+
1441
1507
  f = foo.new
1442
1508
  f.a_set("A")
1443
1509
  assert_equal(nil, f.foo(f, nil))
1444
1510
  assert_equal(2, f.count)
1445
-
1511
+
1446
1512
  f = foo.new
1447
1513
  f.a_set("A")
1448
1514
  assert_equal('x', f.foo(f, 'x'))
@@ -1459,21 +1525,21 @@ class TestJVMCompiler < Test::Unit::TestCase
1459
1525
  def foo(a:boolean, b:boolean)
1460
1526
  return bool('a', a) || bool('b', b)
1461
1527
  end
1462
-
1528
+
1463
1529
  def str(n:String, x:String)
1464
1530
  puts n
1465
1531
  x
1466
1532
  end
1467
-
1533
+
1468
1534
  def bar(a:String, b:String)
1469
1535
  return str('a', a) || str('b', b)
1470
1536
  end
1471
1537
  EOF
1472
-
1538
+
1473
1539
  assert_output("a\n") { assert_equal(true, cls.foo(true, false)) }
1474
1540
  assert_output("a\nb\n") { assert_equal(false, cls.foo(false, false)) }
1475
1541
  assert_output("a\nb\n") { assert_equal(true, cls.foo(false, true)) }
1476
-
1542
+
1477
1543
  assert_output("a\n") { assert_equal("A", cls.bar("A", nil)) }
1478
1544
  assert_output("a\nb\n") { assert_equal(nil, cls.bar(nil, nil)) }
1479
1545
  assert_output("a\nb\n") { assert_equal("B", cls.bar(nil, "B")) }
@@ -1482,15 +1548,15 @@ class TestJVMCompiler < Test::Unit::TestCase
1482
1548
  def s
1483
1549
  @s
1484
1550
  end
1485
-
1551
+
1486
1552
  def s=(s:String)
1487
1553
  @s = s
1488
1554
  end
1489
-
1555
+
1490
1556
  def b
1491
1557
  @b
1492
1558
  end
1493
-
1559
+
1494
1560
  def b=(b:boolean)
1495
1561
  @b = b
1496
1562
  end
@@ -1498,7 +1564,7 @@ class TestJVMCompiler < Test::Unit::TestCase
1498
1564
  def foo(x:boolean)
1499
1565
  @b ||= x
1500
1566
  end
1501
-
1567
+
1502
1568
  def bar(x:String)
1503
1569
  @s ||= x
1504
1570
  end
@@ -1515,7 +1581,7 @@ class TestJVMCompiler < Test::Unit::TestCase
1515
1581
  cls.b_set(true)
1516
1582
  assert_equal(true, cls.foo(false))
1517
1583
  assert_equal(true, cls.b)
1518
-
1584
+
1519
1585
  cls.s_set(nil)
1520
1586
  assert_equal(nil, cls.bar(nil))
1521
1587
  assert_equal(nil, cls.s)
@@ -1533,34 +1599,34 @@ class TestJVMCompiler < Test::Unit::TestCase
1533
1599
  def initialize
1534
1600
  @count = 0
1535
1601
  end
1536
-
1602
+
1537
1603
  def count
1538
1604
  @count
1539
1605
  end
1540
-
1606
+
1541
1607
  def a
1542
1608
  @a
1543
1609
  end
1544
-
1610
+
1545
1611
  def a=(a:String)
1546
1612
  @count += 1
1547
1613
  @a = a
1548
1614
  end
1549
-
1615
+
1550
1616
  def foo(f:Foo3, x:String)
1551
1617
  f.a ||= x
1552
1618
  end
1553
1619
  end
1554
1620
  EOF
1555
-
1621
+
1556
1622
  f = foo.new
1557
1623
  assert_equal('x', f.foo(f, 'x'))
1558
1624
  assert_equal(1, f.count)
1559
-
1625
+
1560
1626
  f = foo.new
1561
1627
  assert_equal(nil, f.foo(f, nil))
1562
1628
  assert_equal(1, f.count)
1563
-
1629
+
1564
1630
  f = foo.new
1565
1631
  f.a_set("A")
1566
1632
  assert_equal("A", f.foo(f, nil))
@@ -1571,26 +1637,26 @@ class TestJVMCompiler < Test::Unit::TestCase
1571
1637
  assert_equal("A", f.foo(f, 'X'))
1572
1638
  assert_equal(1, f.count)
1573
1639
  end
1574
-
1640
+
1575
1641
  def test_op_elem_assign
1576
1642
  foo, = compile(<<-EOF)
1577
1643
  class Foo4
1578
1644
  def initialize
1579
1645
  @i = -1
1580
1646
  end
1581
-
1647
+
1582
1648
  def i
1583
1649
  @i += 1
1584
1650
  end
1585
-
1651
+
1586
1652
  def a
1587
1653
  @a
1588
1654
  end
1589
-
1655
+
1590
1656
  def a=(a:String[])
1591
1657
  @a = a
1592
1658
  end
1593
-
1659
+
1594
1660
  def foo(x:String)
1595
1661
  a[i] ||= x
1596
1662
  end
@@ -1600,7 +1666,7 @@ class TestJVMCompiler < Test::Unit::TestCase
1600
1666
  end
1601
1667
  end
1602
1668
  EOF
1603
-
1669
+
1604
1670
  f = foo.new
1605
1671
  f.a_set([nil, nil, nil].to_java(:string))
1606
1672
  assert_equal(nil, f.bar("x"))
@@ -1608,38 +1674,38 @@ class TestJVMCompiler < Test::Unit::TestCase
1608
1674
  assert_equal("x", f.foo("x"))
1609
1675
  assert_equal([nil, "x", nil], f.a.to_a)
1610
1676
  end
1611
-
1677
+
1612
1678
  def test_constructor_chaining
1613
1679
  foo, = compile(<<-EOF)
1614
1680
  class Foo5
1615
1681
  def initialize(s:String)
1616
1682
  initialize(s, "foo")
1617
1683
  end
1618
-
1684
+
1619
1685
  def initialize(s:String, f:String)
1620
1686
  @s = s
1621
1687
  @f = f
1622
1688
  end
1623
-
1689
+
1624
1690
  def f
1625
1691
  @f
1626
1692
  end
1627
-
1693
+
1628
1694
  def s
1629
1695
  @s
1630
1696
  end
1631
1697
  end
1632
1698
  EOF
1633
-
1699
+
1634
1700
  instance = foo.new("S")
1635
1701
  assert_equal("S", instance.s)
1636
1702
  assert_equal("foo", instance.f)
1637
-
1703
+
1638
1704
  instance = foo.new("foo", "bar")
1639
1705
  assert_equal("foo", instance.s)
1640
1706
  assert_equal("bar", instance.f)
1641
1707
  end
1642
-
1708
+
1643
1709
  def test_super_constructor
1644
1710
  cls, a, b = compile(<<-EOF)
1645
1711
  class SC_A
@@ -1647,7 +1713,7 @@ class TestJVMCompiler < Test::Unit::TestCase
1647
1713
  puts "A"
1648
1714
  end
1649
1715
  end
1650
-
1716
+
1651
1717
  class SC_B < SC_A
1652
1718
  def initialize
1653
1719
  super(0)
@@ -1655,7 +1721,7 @@ class TestJVMCompiler < Test::Unit::TestCase
1655
1721
  end
1656
1722
  end
1657
1723
  EOF
1658
-
1724
+
1659
1725
  assert_output("A\nB\n") do
1660
1726
  b.new
1661
1727
  end
@@ -1682,6 +1748,40 @@ class TestJVMCompiler < Test::Unit::TestCase
1682
1748
  end
1683
1749
  end
1684
1750
 
1751
+ def test_literal_regexp
1752
+ cls, = compile(<<-EOF)
1753
+ def expr
1754
+ /foo/
1755
+ end
1756
+ def matches
1757
+ expr.matcher('barfoobaz').find
1758
+ end
1759
+ EOF
1760
+
1761
+ val = cls.expr
1762
+ assert_equal java.util.regex.Pattern, val.class
1763
+ assert_equal 'foo', val.to_s
1764
+
1765
+ assert cls.matches
1766
+ end
1767
+
1768
+ def test_array_return_type
1769
+ cls, = compile(<<-EOF)
1770
+ def split
1771
+ /foo/.split('barfoobaz')
1772
+ end
1773
+ def puts
1774
+ puts split
1775
+ end
1776
+ EOF
1777
+
1778
+ assert_nothing_raised do
1779
+ result = capture_output {cls.puts}
1780
+ assert result =~ /\[Ljava\.lang\.String;@[a-f0-9]+/
1781
+ end
1782
+ assert_equal java.lang.String.java_class.array_class, cls.split.class.java_class
1783
+ end
1784
+
1685
1785
  def test_empty_constructor
1686
1786
  foo, = compile(<<-EOF)
1687
1787
  class Foo6
@@ -1694,19 +1794,17 @@ class TestJVMCompiler < Test::Unit::TestCase
1694
1794
  def test_same_field_name
1695
1795
  cls, = compile(<<-EOF)
1696
1796
  class A1
1697
- def initialize; end
1698
1797
  def foo(bar:String)
1699
1798
  @bar = bar
1700
1799
  end
1701
1800
  end
1702
1801
 
1703
1802
  class B1
1704
- def initialize; end
1705
1803
  def foo(bar:String)
1706
1804
  @bar = bar
1707
1805
  end
1708
1806
  end
1709
-
1807
+
1710
1808
  puts A1.new.foo("Hi")
1711
1809
  puts B1.new.foo("There")
1712
1810
  EOF
@@ -1744,13 +1842,12 @@ class TestJVMCompiler < Test::Unit::TestCase
1744
1842
  end
1745
1843
  EOF
1746
1844
 
1747
- assert_not_nil cls.java_class.declared_fields[0].annotation(deprecated)
1845
+ assert_not_nil cls.java_class.declared_fields[0].annotation(deprecated)
1748
1846
  end
1749
1847
 
1750
1848
  def test_super
1751
1849
  cls, = compile(<<-EOF)
1752
1850
  class Foo
1753
- def initialize; end
1754
1851
  def equals(other:Object); super(other); end
1755
1852
  end
1756
1853
  EOF
@@ -1759,6 +1856,7 @@ class TestJVMCompiler < Test::Unit::TestCase
1759
1856
  assert obj.equals(obj)
1760
1857
  assert !obj.equals(cls.new)
1761
1858
  end
1859
+
1762
1860
  def test_inexact_constructor
1763
1861
  # FIXME: this is a stupid test
1764
1862
  cls, = compile(
@@ -1767,4 +1865,337 @@ class TestJVMCompiler < Test::Unit::TestCase
1767
1865
  cls.empty_empty
1768
1866
  end
1769
1867
  end
1868
+
1869
+ def test_method_lookup_with_overrides
1870
+ cls, = compile(<<-EOF)
1871
+ class Bar; implements Runnable
1872
+ def foo(x:Bar)
1873
+ Thread.new(x)
1874
+ end
1875
+ def run
1876
+ end
1877
+ end
1878
+ EOF
1879
+
1880
+ # Just make sure this compiles.
1881
+ # It shouldn't get confused by the Thread(String) constructor.
1882
+ end
1883
+
1884
+ def test_block
1885
+ cls, = compile(<<-EOF)
1886
+ thread = Thread.new do
1887
+ puts "Hello"
1888
+ end
1889
+ begin
1890
+ thread.run
1891
+ thread.join
1892
+ rescue
1893
+ puts "Uh Oh!"
1894
+ end
1895
+ EOF
1896
+ assert_output("Hello\n") do
1897
+ cls.main([].to_java :string)
1898
+ end
1899
+
1900
+ script, cls = compile(<<-EOF)
1901
+ import java.util.Observable
1902
+ class MyObservable < Observable
1903
+ def initialize
1904
+ super
1905
+ setChanged
1906
+ end
1907
+ end
1908
+
1909
+ o = MyObservable.new
1910
+ o.addObserver {|o, a| puts a}
1911
+ o.notifyObservers("Hello Observer")
1912
+ EOF
1913
+ assert_output("Hello Observer\n") do
1914
+ script.main([].to_java :string)
1915
+ end
1916
+
1917
+ cls, = compile(<<-EOF)
1918
+ def foo
1919
+ a = "Hello"
1920
+ thread = Thread.new do
1921
+ puts a
1922
+ end
1923
+ begin
1924
+ a = a + " Closures"
1925
+ thread.run
1926
+ thread.join
1927
+ rescue
1928
+ puts "Uh Oh!"
1929
+ end
1930
+ return
1931
+ end
1932
+ EOF
1933
+ assert_output("Hello Closures\n") do
1934
+ cls.foo
1935
+ end
1936
+ end
1937
+
1938
+ def test_each
1939
+ cls, = compile(<<-EOF)
1940
+ def foo
1941
+ [1,2,3].each {|x| puts x}
1942
+ end
1943
+ EOF
1944
+ assert_output("1\n2\n3\n") do
1945
+ cls.foo
1946
+ end
1947
+ end
1948
+
1949
+ def test_any
1950
+ cls, = compile(<<-EOF)
1951
+ import java.lang.Integer
1952
+ def foo
1953
+ puts [1,2,3].any?
1954
+ puts [1,2,3].any? {|x| Integer(x).intValue > 3}
1955
+ end
1956
+ EOF
1957
+ assert_output("true\nfalse\n") do
1958
+ cls.foo
1959
+ end
1960
+ end
1961
+
1962
+ def test_all
1963
+ cls, = compile(<<-EOF)
1964
+ import java.lang.Integer
1965
+ def foo
1966
+ puts [1,2,3].all?
1967
+ puts [1,2,3].all? {|x| Integer(x).intValue > 3}
1968
+ end
1969
+ EOF
1970
+ assert_output("true\nfalse\n") do
1971
+ cls.foo
1972
+ end
1973
+ end
1974
+
1975
+ def test_optional_args
1976
+ cls, = compile(<<-EOF)
1977
+ def foo(a:int, b:int = 1, c:int = 2)
1978
+ puts a; puts b; puts c
1979
+ end
1980
+ foo(0)
1981
+ foo(0,0)
1982
+ foo(0,0,0)
1983
+ EOF
1984
+ assert_output("0\n1\n2\n0\n0\n2\n0\n0\n0\n") do
1985
+ cls.main([].to_java :string)
1986
+ end
1987
+ end
1988
+
1989
+ def test_field_read
1990
+ cls, = compile(<<-EOF)
1991
+ puts System.out.getClass.getName
1992
+ EOF
1993
+ assert_output("java.io.PrintStream\n") do
1994
+ cls.main([].to_java :String)
1995
+ end
1996
+ end
1997
+
1998
+ def test_array_arguments
1999
+ cls, = compile(<<-EOF)
2000
+ class ArrayArg
2001
+ def initialize(foo:byte[]); end
2002
+
2003
+ def self.make_one(foo:byte[])
2004
+ ArrayArg.new(foo)
2005
+ end
2006
+ end
2007
+ EOF
2008
+ cls.make_one(nil)
2009
+ end
2010
+
2011
+ def test_block_with_duby_interface
2012
+ cls, interface = compile(<<-EOF)
2013
+ interface MyProc do
2014
+ def call; returns :void; end
2015
+ end
2016
+ def foo(b:MyProc)
2017
+ b.call
2018
+ end
2019
+ def bar
2020
+ foo {puts "Hi"}
2021
+ end
2022
+ EOF
2023
+ assert_output("Hi\n") do
2024
+ cls.bar
2025
+ end
2026
+ end
2027
+
2028
+ def test_duby_iterable
2029
+ script, cls = compile(<<-EOF)
2030
+ import java.util.Iterator
2031
+ class MyIterator; implements Iterable, Iterator
2032
+ def initialize(x:Object)
2033
+ @next = x
2034
+ end
2035
+
2036
+ def hasNext
2037
+ @next != nil
2038
+ end
2039
+
2040
+ def next
2041
+ result = @next
2042
+ @next = nil
2043
+ result
2044
+ end
2045
+
2046
+ def iterator
2047
+ self
2048
+ end
2049
+
2050
+ def remove
2051
+ raise UnsupportedOperationException
2052
+ end
2053
+
2054
+ def self.test(x:String)
2055
+ MyIterator.new(x).each {|y| puts y}
2056
+ end
2057
+ end
2058
+ EOF
2059
+ assert_output("Hi\n") do
2060
+ cls.test("Hi")
2061
+ end
2062
+ end
2063
+
2064
+ def test_java_lang_cast
2065
+ cls, = compile(<<-EOF)
2066
+ def foo(a:Object)
2067
+ Integer(a).intValue
2068
+ end
2069
+ EOF
2070
+
2071
+ assert_equal(2, cls.foo(java.lang.Integer.new(2)))
2072
+ end
2073
+
2074
+ def test_string_concat
2075
+ cls, = compile(<<-EOF)
2076
+ def foo(name:String)
2077
+ print "Hello \#{name}."
2078
+ end
2079
+ EOF
2080
+
2081
+ assert_output("Hello Fred.") do
2082
+ cls.foo "Fred"
2083
+ end
2084
+
2085
+ cls, = compile(<<-EOF)
2086
+ def foo(x:int)
2087
+ print "\#{x += 1}"
2088
+ x
2089
+ end
2090
+ EOF
2091
+
2092
+ assert_output("2") do
2093
+ assert_equal(2, cls.foo(1))
2094
+ end
2095
+
2096
+ cls, = compile(<<-EOF)
2097
+ def foo(a:int)
2098
+ "\#{a += 1}"
2099
+ a
2100
+ end
2101
+ EOF
2102
+ assert_equal(2, cls.foo(1))
2103
+ end
2104
+
2105
+ def test_self_dot_static_methods
2106
+ cls, = compile(<<-EOF)
2107
+ class ClassWithStatics
2108
+ def self.a
2109
+ b
2110
+ end
2111
+ def self.b
2112
+ print "b"
2113
+ end
2114
+ end
2115
+ EOF
2116
+
2117
+ assert_output("b") do
2118
+ cls.a
2119
+ end
2120
+ end
2121
+
2122
+ def test_evaluation_order
2123
+ cls, = compile(<<-EOF)
2124
+ def call(a:int, b:int, c:int)
2125
+ print "\#{a}, \#{b}, \#{c}"
2126
+ end
2127
+
2128
+ def test_call(a:int)
2129
+ call(a, if a < 10;a+=1;a;else;a;end, a)
2130
+ end
2131
+
2132
+ def test_string(a:int)
2133
+ "\#{a}, \#{if a < 10;a += 1;a;else;a;end}, \#{a}"
2134
+ end
2135
+ EOF
2136
+
2137
+ assert_output("1, 2, 2") do
2138
+ cls.test_call(1)
2139
+ end
2140
+
2141
+ assert_equal("2, 3, 3", cls.test_string(2))
2142
+ end
2143
+
2144
+ def test_inner_class
2145
+ cls, = compile(<<-EOF)
2146
+ def foo
2147
+ Character.UnicodeBlock.ARROWS
2148
+ end
2149
+ EOF
2150
+
2151
+ subset = cls.foo
2152
+ assert_equal("java.lang.Character$UnicodeBlock", subset.java_class.name)
2153
+ end
2154
+
2155
+ def test_defmacro
2156
+ cls, = compile(<<-EOF)
2157
+ defmacro bar(x) do
2158
+ x
2159
+ end
2160
+
2161
+ def foo
2162
+ bar("bar")
2163
+ end
2164
+ EOF
2165
+
2166
+ assert_equal("bar", cls.foo)
2167
+ assert(!cls.respond_to?(:bar))
2168
+ end
2169
+
2170
+ def test_default_constructor
2171
+ script, cls = compile(<<-EOF)
2172
+ class DefaultConstructable
2173
+ def foo
2174
+ "foo"
2175
+ end
2176
+ end
2177
+
2178
+ print DefaultConstructable.new.foo
2179
+ EOF
2180
+
2181
+ assert_output("foo") do
2182
+ script.main(nil)
2183
+ end
2184
+ end
2185
+
2186
+ # TODO: need a writable field somewhere...
2187
+ # def test_field_write
2188
+ # cls, = compile(<<-EOF)
2189
+ # old_pi = Math.PI
2190
+ # Math.PI = 3.0
2191
+ # puts Math.PI
2192
+ # Math.PI = old_pi
2193
+ # puts Math.PI
2194
+ # EOF
2195
+ # raise
2196
+ # cls.main([].to_java :string)
2197
+ # assert_output("3.0\n") do
2198
+ # cls.main([].to_java :string)
2199
+ # end
2200
+ # end
1770
2201
  end