mirah 0.0.12-java → 0.1.0-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (171) hide show
  1. data/History.txt +372 -0
  2. data/README.txt +4 -5
  3. data/Rakefile +178 -55
  4. data/examples/appengine/Readme +3 -3
  5. data/examples/appengine/src/org/mirah/MirahApp.mirah +1 -1
  6. data/examples/appengine/src/org/mirah/list.dhtml +1 -1
  7. data/examples/bintrees.mirah +1 -1
  8. data/examples/edb.mirah +1 -1
  9. data/examples/fib.mirah +1 -1
  10. data/examples/interfaces.mirah +1 -1
  11. data/examples/macros/{string-each-char.mirah → string_each_char.mirah} +4 -5
  12. data/examples/maven/README.txt +1 -1
  13. data/examples/maven/src/main/mirah/hello_mirah.mirah +1 -1
  14. data/examples/plugins/appengine/Rakefile +1 -1
  15. data/examples/plugins/appengine/src/com/google/appengine/ext/duby/db/MetaModel.mirah +1 -1
  16. data/examples/plugins/appengine/src/com/google/appengine/ext/duby/db/Model.duby +1 -1
  17. data/examples/plugins/appengine/test/com/google/appengine/ext/duby/db/ModelTest.duby +1 -1
  18. data/examples/rosettacode/100-doors.mirah +6 -6
  19. data/examples/rosettacode/README.txt +3 -3
  20. data/examples/rosettacode/boolean-values.mirah +1 -1
  21. data/examples/rosettacode/comments.mirah +1 -1
  22. data/examples/rosettacode/count-occurrences-of-a-substring.mirah +1 -1
  23. data/examples/rosettacode/factorial.mirah +1 -1
  24. data/examples/rosettacode/fibonacci.mirah +1 -1
  25. data/examples/rosettacode/fizz-buzz.mirah +2 -2
  26. data/examples/rosettacode/flatten-a-list.mirah +4 -4
  27. data/examples/rosettacode/guess-the-number.mirah +2 -2
  28. data/examples/rosettacode/hamming-numbers.mirah +4 -4
  29. data/examples/rosettacode/is-string-numeric.mirah +22 -22
  30. data/examples/rosettacode/palindrome.mirah +2 -2
  31. data/examples/rosettacode/random-numbers.mirah +1 -1
  32. data/examples/rosettacode/repeat-a-string.mirah +1 -1
  33. data/examples/rosettacode/reverse-a-string.mirah +1 -1
  34. data/examples/rosettacode/rot-13.mirah +5 -5
  35. data/examples/rosettacode/secure-temporary-file.mirah +2 -2
  36. data/examples/rosettacode/sleep.mirah +1 -1
  37. data/examples/rosettacode/string-length.mirah +5 -5
  38. data/examples/swing.mirah +1 -1
  39. data/examples/test.edb +1 -1
  40. data/javalib/mirah-bootstrap.jar +0 -0
  41. data/javalib/mirah-builtins.jar +0 -0
  42. data/javalib/mirah-parser.jar +0 -0
  43. data/javalib/mirah-util.jar +0 -0
  44. data/lib/duby.rb +1 -1
  45. data/lib/mirah.rb +50 -28
  46. data/lib/mirah/ast.rb +15 -605
  47. data/lib/mirah/ast/scope.rb +98 -69
  48. data/lib/mirah/commands.rb +1 -1
  49. data/lib/mirah/commands/base.rb +7 -7
  50. data/lib/mirah/commands/compile.rb +3 -3
  51. data/lib/mirah/commands/parse.rb +7 -5
  52. data/lib/mirah/commands/run.rb +12 -19
  53. data/lib/mirah/compiler.rb +15 -23
  54. data/lib/mirah/errors.rb +16 -1
  55. data/lib/mirah/generator.rb +79 -39
  56. data/lib/mirah/jvm/compiler.rb +1 -19
  57. data/lib/mirah/jvm/compiler/base.rb +233 -90
  58. data/lib/mirah/jvm/compiler/jvm_bytecode.rb +675 -363
  59. data/lib/mirah/jvm/method_lookup.rb +134 -65
  60. data/lib/mirah/jvm/typer.rb +10 -5
  61. data/lib/mirah/jvm/types.rb +10 -2
  62. data/lib/mirah/jvm/types/array_type.rb +10 -12
  63. data/lib/mirah/{compiler/type.rb → jvm/types/ast_ext.rb} +12 -8
  64. data/lib/mirah/jvm/types/basic_types.rb +26 -33
  65. data/lib/mirah/jvm/types/bitescript_ext.rb +1 -1
  66. data/lib/mirah/jvm/types/block_type.rb +15 -0
  67. data/lib/mirah/jvm/types/boolean.rb +8 -4
  68. data/lib/mirah/jvm/types/dynamic_type.rb +12 -13
  69. data/lib/mirah/jvm/types/enumerable.rb +7 -7
  70. data/lib/mirah/jvm/types/extensions.rb +11 -6
  71. data/lib/mirah/jvm/types/factory.rb +624 -94
  72. data/lib/mirah/jvm/types/floats.rb +21 -15
  73. data/lib/mirah/jvm/types/generic_type.rb +72 -0
  74. data/lib/mirah/jvm/types/implicit_nil_type.rb +29 -0
  75. data/lib/mirah/jvm/types/integers.rb +26 -71
  76. data/lib/mirah/jvm/types/interface_definition.rb +3 -3
  77. data/lib/mirah/jvm/types/intrinsics.rb +203 -168
  78. data/lib/mirah/jvm/types/literals.rb +6 -6
  79. data/lib/mirah/jvm/types/meta_type.rb +13 -4
  80. data/lib/mirah/jvm/types/methods.rb +281 -93
  81. data/lib/mirah/jvm/types/null_type.rb +17 -5
  82. data/lib/mirah/jvm/types/number.rb +10 -7
  83. data/lib/mirah/jvm/types/primitive_type.rb +17 -6
  84. data/lib/mirah/jvm/types/source_mirror.rb +12 -7
  85. data/lib/mirah/jvm/types/type.rb +107 -23
  86. data/lib/mirah/jvm/types/type_definition.rb +25 -10
  87. data/lib/mirah/jvm/types/unreachable_type.rb +1 -1
  88. data/lib/mirah/jvm/types/void_type.rb +3 -3
  89. data/lib/mirah/parser.rb +154 -16
  90. data/lib/mirah/plugin/edb.rb +1 -1
  91. data/lib/mirah/transform.rb +1 -2
  92. data/lib/mirah/transform/ast_ext.rb +24 -43
  93. data/lib/mirah/transform/transformer.rb +29 -224
  94. data/lib/mirah/typer.rb +2 -16
  95. data/lib/mirah/util/argument_processor.rb +25 -10
  96. data/lib/mirah/util/class_loader.rb +1 -1
  97. data/lib/mirah/util/compilation_state.rb +16 -17
  98. data/lib/mirah/util/delegate.rb +2 -2
  99. data/lib/mirah/util/logging.rb +110 -0
  100. data/lib/mirah/util/process_errors.rb +69 -11
  101. data/lib/mirah/version.rb +1 -1
  102. data/test/core/commands_test.rb +6 -24
  103. data/test/core/env_test.rb +5 -5
  104. data/{lib/mirah/jvm/source_generator/typer.rb → test/core/generator_test.rb} +9 -9
  105. data/test/core/typer_test.rb +196 -158
  106. data/test/core/util/argument_processor_test.rb +10 -10
  107. data/test/core/util/class_loader_test.rb +6 -5
  108. data/test/fixtures/org/foo/LowerCaseInnerClass$inner.class +0 -0
  109. data/test/fixtures/org/foo/LowerCaseInnerClass.class +0 -0
  110. data/test/fixtures/org/foo/LowerCaseInnerClass.java +7 -0
  111. data/test/jvm/annotations_test.rb +5 -5
  112. data/test/jvm/blocks_test.rb +140 -88
  113. data/test/jvm/bytecode_test_helper.rb +112 -94
  114. data/test/jvm/cast_test.rb +162 -0
  115. data/test/jvm/constructors_test.rb +18 -8
  116. data/test/jvm/enumerable_test.rb +77 -44
  117. data/test/jvm/example_test.rb +53 -0
  118. data/test/jvm/factory_test.rb +7 -1
  119. data/test/jvm/generics_test.rb +57 -0
  120. data/test/jvm/hash_test.rb +106 -0
  121. data/test/jvm/import_test.rb +81 -0
  122. data/test/jvm/interface_test.rb +73 -0
  123. data/test/jvm/java_typer_test.rb +92 -66
  124. data/{lib/mirah/typer/base.rb → test/jvm/jvm_commands_test.rb} +6 -10
  125. data/test/jvm/jvm_compiler_test.rb +170 -604
  126. data/test/jvm/list_extensions_test.rb +23 -0
  127. data/test/jvm/macros_test.rb +197 -32
  128. data/test/jvm/main_method_test.rb +4 -4
  129. data/test/jvm/numeric_extensions_test.rb +13 -0
  130. data/test/jvm/rescue_test.rb +73 -16
  131. data/test/jvm/varargs_test.rb +65 -0
  132. data/test/test_helper.rb +1 -2
  133. metadata +234 -251
  134. data/examples/SortClosure$__xform_tmp_1.class +0 -0
  135. data/examples/SortClosure$__xform_tmp_2.class +0 -0
  136. data/examples/SortClosure.class +0 -0
  137. data/examples/macros/StringEachChar$Extension1.class +0 -0
  138. data/lib/mirah/ast/call.rb +0 -345
  139. data/lib/mirah/ast/class.rb +0 -359
  140. data/lib/mirah/ast/flow.rb +0 -381
  141. data/lib/mirah/ast/intrinsics.rb +0 -563
  142. data/lib/mirah/ast/literal.rb +0 -178
  143. data/lib/mirah/ast/local.rb +0 -112
  144. data/lib/mirah/ast/method.rb +0 -408
  145. data/lib/mirah/ast/structure.rb +0 -387
  146. data/lib/mirah/ast/type.rb +0 -146
  147. data/lib/mirah/commands/base.rb~ +0 -57
  148. data/lib/mirah/compiler/call.rb +0 -45
  149. data/lib/mirah/compiler/class.rb +0 -81
  150. data/lib/mirah/compiler/flow.rb +0 -109
  151. data/lib/mirah/compiler/literal.rb +0 -130
  152. data/lib/mirah/compiler/local.rb +0 -59
  153. data/lib/mirah/compiler/method.rb +0 -44
  154. data/lib/mirah/compiler/structure.rb +0 -65
  155. data/lib/mirah/jvm/compiler/java_source.rb +0 -787
  156. data/lib/mirah/jvm/method_lookup.rb~ +0 -247
  157. data/lib/mirah/jvm/source_generator/builder.rb +0 -468
  158. data/lib/mirah/jvm/source_generator/loops.rb +0 -131
  159. data/lib/mirah/jvm/source_generator/precompile.rb +0 -210
  160. data/lib/mirah/plugin/gwt.rb +0 -189
  161. data/lib/mirah/plugin/java.rb +0 -70
  162. data/lib/mirah/transform/error.rb +0 -13
  163. data/lib/mirah/transform/helper.rb +0 -765
  164. data/lib/mirah/typer/simple.rb +0 -384
  165. data/lib/mirah/version.rb~ +0 -18
  166. data/test/core/ast_test.rb +0 -382
  167. data/test/core/compilation_test.rb +0 -130
  168. data/test/core/macros_test.rb +0 -61
  169. data/test/jvm/javac_test_helper.rb +0 -89
  170. data/test/jvm/jvm_compiler_test.rb~ +0 -2181
  171. data/test/plugins/gwt_test.rb +0 -69
@@ -1,130 +0,0 @@
1
- # Copyright (c) 2010 The Mirah project authors. All Rights Reserved.
2
- # All contributing project authors may be found in the NOTICE file.
3
- #
4
- # Licensed under the Apache License, Version 2.0 (the "License");
5
- # you may not use this file except in compliance with the License.
6
- # You may obtain a copy of the License at
7
- #
8
- # http://www.apache.org/licenses/LICENSE-2.0
9
- #
10
- # Unless required by applicable law or agreed to in writing, software
11
- # distributed under the License is distributed on an "AS IS" BASIS,
12
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- # See the License for the specific language governing permissions and
14
- # limitations under the License.
15
- require 'test_helper'
16
-
17
- class CompilationTest < Test::Unit::TestCase
18
- include Mirah
19
-
20
- class MockCompiler
21
- attr_accessor :calls
22
-
23
- def initialize
24
- @calls = []
25
- end
26
- def compile(ast)
27
- ast.compile(self, true)
28
- end
29
-
30
- def line(num)
31
- # ignore newlines
32
- end
33
-
34
- def method_missing(sym, *args, &block)
35
- calls << [sym, *args]
36
- block.call if block
37
- end
38
- end
39
-
40
- class ClassComparison
41
- def initialize(klass)
42
- @class = klass
43
- end
44
-
45
- def ==(other)
46
- other.kind_of?(@class)
47
- end
48
- end
49
-
50
- def a(klass)
51
- ClassComparison.new(klass)
52
- end
53
-
54
- def setup
55
- @compiler = MockCompiler.new
56
- end
57
-
58
- def test_fixnum
59
- new_ast = AST.parse("1").body[0]
60
-
61
- new_ast.compile(@compiler, true)
62
-
63
- assert_equal([[:fixnum, nil, 1]], @compiler.calls)
64
- end
65
-
66
- def test_string
67
- new_ast = AST.parse("'foo'").body[0]
68
-
69
- new_ast.compile(@compiler, true)
70
-
71
- assert_equal([[:string, "foo"]], @compiler.calls)
72
- end
73
-
74
- def test_float
75
- new_ast = AST.parse("1.0").body[0]
76
-
77
- new_ast.compile(@compiler, true)
78
-
79
- assert_equal([[:float, nil, 1.0]], @compiler.calls)
80
- end
81
-
82
- def test_boolean
83
- new_ast = AST.parse("true").body[0]
84
-
85
- new_ast.compile(@compiler, true)
86
-
87
- assert_equal([[:boolean, true]], @compiler.calls)
88
- end
89
-
90
- def test_local
91
- new_ast = AST.parse("a = 1").body[0]
92
-
93
- new_ast.compile(@compiler, true)
94
-
95
- assert_equal([[:local_assign, a(Mirah::AST::StaticScope), "a", nil, true, AST.fixnum(nil, nil, 1)]], @compiler.calls)
96
- end
97
-
98
- def test_local_typed
99
- new_ast = AST.parse("a = 1").body[0]
100
- typer = Typer::Simple.new(:bar)
101
- new_ast.infer(typer, true)
102
- new_ast.compile(@compiler, true)
103
-
104
- assert_equal([[:local_assign, a(Mirah::AST::StaticScope), "a", AST.type(nil, :fixnum), true, AST.fixnum(nil, nil, 1)]], @compiler.calls)
105
- end
106
-
107
- def test_return
108
- new_ast = AST.parse("return 1").body[0]
109
- new_ast.compile(@compiler, true)
110
-
111
- assert_equal([[:return, new_ast]], @compiler.calls)
112
- end
113
-
114
- def test_empty_array
115
- new_ast = AST.parse("int[5]").body[0]
116
- new_ast.compile(@compiler, true)
117
-
118
- assert_equal(1, @compiler.calls.size)
119
- size = @compiler.calls[0].pop
120
- assert_equal([[:empty_array, nil]], @compiler.calls)
121
- assert_equal(5, size.literal)
122
- end
123
-
124
- def test_empty_literal_array
125
- new_ast = AST.parse("[]").body[0]
126
- new_ast.compile(@compiler, true)
127
-
128
- assert_equal([:array, a(Mirah::AST::Array), true], @compiler.calls.first)
129
- end
130
- end
@@ -1,61 +0,0 @@
1
- # Copyright (c) 2010 The Mirah project authors. All Rights Reserved.
2
- # All contributing project authors may be found in the NOTICE file.
3
- #
4
- # Licensed under the Apache License, Version 2.0 (the "License");
5
- # you may not use this file except in compliance with the License.
6
- # You may obtain a copy of the License at
7
- #
8
- # http://www.apache.org/licenses/LICENSE-2.0
9
- #
10
- # Unless required by applicable law or agreed to in writing, software
11
- # distributed under the License is distributed on an "AS IS" BASIS,
12
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- # See the License for the specific language governing permissions and
14
- # limitations under the License.
15
- # TODO refactor this and test_jvm_compiler to use mirah.rb
16
- require 'test_helper'
17
-
18
- class MacrosTest < Test::Unit::TestCase
19
- java_import 'java.lang.System'
20
-
21
- def parse(code)
22
- Mirah::AST.type_factory = Mirah::JVM::Types::TypeFactory.new
23
- name = "script" + System.nano_time.to_s
24
- state = Mirah::Util::CompilationState.new
25
- state.save_extensions = false
26
- transformer = Mirah::Transform::Transformer.new(state)
27
- Java::MirahImpl::Builtin.initialize_builtins(transformer)
28
- ast = Mirah::AST.parse(code, name, true, transformer)
29
- typer = Mirah::JVM::Typer.new(transformer)
30
- ast.infer(typer, true)
31
- typer.resolve(true)
32
- ast
33
- end
34
-
35
- def test_macro_helper
36
- script = parse(<<-EOF)
37
- import duby.lang.compiler.Compiler
38
-
39
- def helper(mirah:Compiler)
40
- name = "foobar"
41
- mirah.quote { `name` }
42
- end
43
- EOF
44
- end
45
-
46
- def test_self_call_in_unquote
47
- script = parse(<<-EOF)
48
- import duby.lang.compiler.Compiler
49
-
50
- def foobar(name:String)
51
- name
52
- end
53
-
54
- def helper(mirah:Compiler)
55
- name = "foobar"
56
- mirah.quote { `foobar(name)` }
57
- end
58
- EOF
59
- end
60
-
61
- end
@@ -1,89 +0,0 @@
1
- # Copyright (c) 2010 The Mirah project authors. All Rights Reserved.
2
- # All contributing project authors may be found in the NOTICE file.
3
- #
4
- # Licensed under the Apache License, Version 2.0 (the "License");
5
- # you may not use this file except in compliance with the License.
6
- # You may obtain a copy of the License at
7
- #
8
- # http://www.apache.org/licenses/LICENSE-2.0
9
- #
10
- # Unless required by applicable law or agreed to in writing, software
11
- # distributed under the License is distributed on an "AS IS" BASIS,
12
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- # See the License for the specific language governing permissions and
14
- # limitations under the License.
15
- require 'bytecode_test_helper'
16
- require 'mirah/jvm/compiler/java_source'
17
-
18
-
19
- # make sure . is in CLASSPATH
20
- $CLASSPATH << '.'
21
-
22
-
23
- module JavacCompiler
24
- import javax.tools.ToolProvider
25
- import java.util.Arrays
26
- import java.lang.System
27
- import java.io.PrintStream
28
- include Mirah
29
-
30
- def javac(files)
31
- compiler = ToolProvider.system_java_compiler
32
- fm = compiler.get_standard_file_manager(nil, nil, nil)
33
- units = fm.get_java_file_objects_from_strings(Arrays.as_list(files.to_java :string))
34
- unless compiler.get_task(nil, fm, nil, nil, nil, units).call
35
- raise "Compilation error"
36
- end
37
- loader = org.jruby.util.ClassCache::OneShotClassLoader.new(
38
- JRuby.runtime.jruby_class_loader)
39
- classes = []
40
- files.each do |name|
41
- classfile = name.sub /java$/, 'class'
42
- if File.exist? classfile
43
- bytecode = IO.read(classfile)
44
- cls = loader.define_class(name[0..-6].tr('/', '.'), bytecode.to_java_bytes)
45
- classes << JavaUtilities.get_proxy_class(cls.name)
46
- @tmp_classes << name
47
- @tmp_classes << classfile
48
- pattern = classfile.sub /\.class$/, '$*.class'
49
- @tmp_classes.concat(Dir.glob(pattern))
50
- end
51
- end
52
- classes
53
- end
54
-
55
- def create_compiler
56
- JVM::Compiler::JavaSource.new
57
- end
58
-
59
- def compile_ast ast
60
- compiler = create_compiler
61
- ast.compile(compiler, false)
62
- compiler
63
- end
64
-
65
- def generate_classes compiler
66
- java_files = []
67
- compiler.generate do |name, builder|
68
- bytes = builder.generate
69
- FileUtils.mkdir_p(File.dirname(name))
70
- open("#{name}", "w") do |f|
71
- f << bytes
72
- end
73
- java_files << name
74
- end
75
- classes = javac(java_files)
76
- end
77
-
78
- def clear_tmp_files
79
- super
80
- # wipe out Script*_xform_* classes, since we're messy
81
- File.unlink(*Dir['Script*_xform_*.class'])
82
- end
83
- end
84
-
85
-
86
- class Test::Unit::TestCase
87
- include JavacCompiler
88
-
89
- end
@@ -1,2181 +0,0 @@
1
- # Copyright (c) 2010 The Mirah project authors. All Rights Reserved.
2
- # All contributing project authors may be found in the NOTICE file.
3
- #
4
- # Licensed under the Apache License, Version 2.0 (the "License");
5
- # you may not use this file except in compliance with the License.
6
- # You may obtain a copy of the License at
7
- #
8
- # http://www.apache.org/licenses/LICENSE-2.0
9
- #
10
- # Unless required by applicable law or agreed to in writing, software
11
- # distributed under the License is distributed on an "AS IS" BASIS,
12
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- # See the License for the specific language governing permissions and
14
- # limitations under the License.
15
-
16
- class JVMCompilerTest < Test::Unit::TestCase
17
- def assert_raise_java(type, message="")
18
- ex = assert_raise(NativeException) do
19
- yield
20
- end
21
- assert_equal type, ex.cause.class
22
- assert_equal message, ex.cause.message.to_s
23
- end
24
-
25
- def test_local
26
- cls, = compile("def foo; a = 1; a; end")
27
- assert_equal(1, cls.foo)
28
-
29
- cls, = compile("def foo; a = 1.0; a; end")
30
- assert_equal(1.0, cls.foo)
31
-
32
- cls, = compile("def foo; a = 'bar'; a; end")
33
- assert_equal('bar', cls.foo)
34
- end
35
-
36
- def test_addition
37
- cls, = compile("def foo; a = 1; b = 2; a + b; end")
38
- assert_equal(3, cls.foo)
39
-
40
- cls, = compile("def foo; a = 1.0; b = 2.0; a + b; end")
41
- assert_equal(3.0, cls.foo)
42
- end
43
-
44
- def test_subtraction
45
- cls, = compile("def foo; a = 3; b = 2; a - b; end")
46
- assert_equal(1, cls.foo)
47
-
48
- cls, = compile("def foo; a = 3.0; b = 2.0; a - b; end")
49
- assert_equal(1.0, cls.foo)
50
- end
51
-
52
- def test_multiplication
53
- cls, = compile("def foo; a = 2; b = 3; a * b; end")
54
- assert_equal(6, cls.foo)
55
-
56
- cls, = compile("def foo; a = 2.0; b = 3.0; a * b; end")
57
- assert_equal(6.0, cls.foo)
58
- end
59
-
60
- def test_division
61
- cls, = compile("def foo; a = 6; b = 3; a / b; end")
62
- assert_equal(2, cls.foo)
63
-
64
- cls, = compile("def foo; a = 6.0; b = 3.0; a / b; end")
65
- assert_equal(2.0, cls.foo)
66
- end
67
-
68
- def test_rem
69
- cls, = compile("def foo; a = 7; b = 3; a % b; end")
70
- assert_equal(1, cls.foo)
71
-
72
- cls, = compile("def foo; a = 8.0; b = 3.0; a % b; end")
73
- assert_equal(2.0, cls.foo)
74
- end
75
-
76
- def test_shift_left
77
- cls, = compile("def foo; a = 1; b = 3; a << b; end")
78
- assert_equal(8, cls.foo)
79
- end
80
-
81
- def test_shift_right
82
- cls, = compile("def foo; a = 7; b = 2; a >> b; end")
83
- assert_equal(1, cls.foo)
84
-
85
- cls, = compile("def foo; a = -1; b = 1; a >> b; end")
86
- assert_equal(-1, cls.foo)
87
- end
88
-
89
- # TODO the parser doesn't like >>>
90
-
91
- # def test_unsigned_shift_right
92
- # cls, = compile("def foo; a = -1; b = 31; a >>> b; end")
93
- # assert_equal(1, cls.foo)
94
- # end
95
-
96
- def test_binary_and
97
- cls, = compile("def foo; a = 7; b = 3; a & b; end")
98
- assert_equal(3, cls.foo)
99
- end
100
-
101
- def test_binary_or
102
- cls, = compile("def foo; a = 4; b = 3; a | b; end")
103
- assert_equal(7, cls.foo)
104
- end
105
-
106
- def test_binary_xor
107
- cls, = compile("def foo; a = 5; b = 3; a ^ b; end")
108
- assert_equal(6, cls.foo)
109
- end
110
-
111
- def test_return
112
- cls, = compile("def foo; return 1; end")
113
- assert_equal(1, cls.foo)
114
-
115
- cls, = compile("def foo; return 1.0; end")
116
- assert_equal(1.0, cls.foo)
117
-
118
- cls, = compile("def foo; return 'bar'; end")
119
- assert_equal('bar', cls.foo)
120
- end
121
-
122
- def test_primitive_array
123
- cls, = compile("def foo; a = boolean[2]; a; end")
124
- assert_equal(Java::boolean[].java_class, cls.foo.class.java_class)
125
- assert_equal([false,false], cls.foo.to_a)
126
- cls, = compile("def foo; a = boolean[2]; a[0] = true; a[0]; end")
127
- assert_equal(TrueClass, cls.foo.class)
128
- assert_equal(true, cls.foo)
129
- cls, = compile("def foo; a = boolean[2]; a.length; end")
130
- assert_equal(Fixnum, cls.foo.class)
131
- assert_equal(2, cls.foo)
132
-
133
- cls, = compile("def foo; a = byte[2]; a; end")
134
- assert_equal(Java::byte[].java_class, cls.foo.class.java_class)
135
- assert_equal([0,0], cls.foo.to_a)
136
- cls, = compile("def foo; a = byte[2]; a[0] = 1; a[0]; end")
137
- assert_equal(Fixnum, cls.foo.class)
138
- assert_equal(1, cls.foo)
139
-
140
- cls, = compile("def foo; a = short[2]; a; end")
141
- assert_equal(Java::short[].java_class, cls.foo.class.java_class)
142
- assert_equal([0,0], cls.foo.to_a)
143
- cls, = compile("def foo; a = short[2]; a[0] = 1; a[0]; end")
144
- assert_equal(Fixnum, cls.foo.class)
145
- assert_equal(1, cls.foo)
146
-
147
- cls, = compile("def foo; a = char[2]; a; end")
148
- assert_equal(Java::char[].java_class, cls.foo.class.java_class)
149
- assert_equal([0,0], cls.foo.to_a)
150
- # Pending char constants or casts
151
- # cls, = compile("def foo; a = char[2]; a[0] = 1; a[0]; end")
152
- # assert_equal(Fixnum, cls.foo.class)
153
- # assert_equal(1, cls.foo)
154
-
155
- cls, = compile("def foo; a = int[2]; a; end")
156
- assert_equal(Java::int[].java_class, cls.foo.class.java_class)
157
- assert_equal([0,0], cls.foo.to_a)
158
- cls, = compile("def foo; a = int[2]; a[0] = 1; a[0]; end")
159
- assert_equal(Fixnum, cls.foo.class)
160
- assert_equal(1, cls.foo)
161
-
162
- cls, = compile("def foo; a = long[2]; a; end")
163
- assert_equal(Java::long[].java_class, cls.foo.class.java_class)
164
- assert_equal([0,0], cls.foo.to_a)
165
- cls, = compile(<<-EOF)
166
- def foo
167
- a = long[2]
168
- a[0] = 1
169
- a[0]
170
- end
171
- EOF
172
- assert_equal(Fixnum, cls.foo.class)
173
- assert_equal(1, cls.foo)
174
-
175
- cls, = compile("def foo; a = float[2]; a; end")
176
- assert_equal(Java::float[].java_class, cls.foo.class.java_class)
177
- assert_equal([0.0,0.0], cls.foo.to_a)
178
- cls, = compile("def foo; a = float[2]; a[0] = float(1.0); a[0]; end")
179
- assert_equal(Float, cls.foo.class)
180
- assert_equal(1.0, cls.foo)
181
-
182
- cls, = compile("def foo; a = double[2]; a; end")
183
- assert_equal(Java::double[].java_class, cls.foo.class.java_class)
184
- assert_equal([0.0,0.0], cls.foo.to_a)
185
- cls, = compile(<<-EOF)
186
- def foo
187
- a = double[2]
188
- a[0] = 1.0
189
- a[0]
190
- end
191
- EOF
192
- assert_equal(Float, cls.foo.class)
193
- assert_equal(1.0, cls.foo)
194
- end
195
-
196
- def test_array_with_dynamic_size
197
- cls, = compile("def foo(size:int); a = int[size + 1];end")
198
- array = cls.foo(3)
199
- assert_equal(Java::int[].java_class, array.class.java_class)
200
- assert_equal([0,0,0,0], array.to_a)
201
- end
202
-
203
- def test_object_array
204
- cls, = compile("import java.lang.Object;def foo; a = Object[2];end")
205
- assert_equal(Java::JavaLang::Object[].java_class, cls.foo.class.java_class)
206
- assert_equal([nil, nil], cls.foo.to_a)
207
- end
208
-
209
- def test_string_concat
210
- cls, = compile("
211
- def str_str; a = 'a'; b = 'b'; a + b; end
212
- def str_boolean; a = 'a'; b = false; a + b; end
213
- def str_float; a = 'a'; b = float(1.0); a + b; end
214
- def str_double; a = 'a'; b = 1.0; a + b; end
215
- def str_byte; a = 'a'; b = byte(1); a + b; end
216
- def str_short; a = 'a'; b = short(1); a + b; end
217
- def str_char; a = 'a'; b = char(123); a + b; end
218
- def str_int; a = 'a'; b = 1; a + b; end
219
- def str_long; a = 'a'; b = long(1); a + b; end
220
- ")
221
- assert_equal("ab", cls.str_str)
222
- assert_equal("afalse", cls.str_boolean)
223
- assert_equal("a1.0", cls.str_float)
224
- assert_equal("a1.0", cls.str_double)
225
- assert_equal("a1", cls.str_byte)
226
- assert_equal("a1", cls.str_short)
227
- assert_equal("a{", cls.str_char)
228
- assert_equal("a1", cls.str_int)
229
- assert_equal("a1", cls.str_long)
230
- end
231
-
232
- def test_void_selfcall
233
- cls, = compile("import 'System', 'java.lang.System'; def foo; System.gc; end; foo")
234
- assert_nothing_raised {cls.foo}
235
- end
236
-
237
- def test_void_chain
238
- cls, = compile(<<-EOF)
239
- import java.io.*
240
- def foo
241
- throws IOException
242
- OutputStreamWriter.new(
243
- System.out).write("Hello ").write("there\n").flush
244
- end
245
- EOF
246
-
247
- assert_output("Hello there\n") { cls.foo }
248
-
249
- a, b = compile(<<-EOF)
250
- class VoidBase
251
- def foo
252
- returns void
253
- puts "foo"
254
- end
255
- end
256
- class VoidChain < VoidBase
257
- def bar
258
- returns void
259
- puts "bar"
260
- end
261
-
262
- def self.foobar
263
- VoidChain.new.foo.bar
264
- end
265
- end
266
- EOF
267
- assert_output("foo\nbar\n") { b.foobar }
268
-
269
- end
270
-
271
- def test_import
272
- cls, = compile("import 'AL', 'java.util.ArrayList'; def foo; AL.new; end; foo")
273
- assert_equal java.util.ArrayList.java_class, cls.foo.java_class
274
-
275
- cls, = compile("import 'java.util.ArrayList'; def foo; ArrayList.new; end; foo")
276
- assert_equal java.util.ArrayList.java_class, cls.foo.java_class
277
- end
278
-
279
- def test_no_quote_import
280
- cls, = compile("import java.util.ArrayList as AL; def foo; AL.new; end; foo")
281
- assert_equal java.util.ArrayList.java_class, cls.foo.java_class
282
-
283
- cls, = compile("import java.util.ArrayList; def foo; ArrayList.new; end; foo")
284
- assert_equal java.util.ArrayList.java_class, cls.foo.java_class
285
- end
286
-
287
- def test_imported_decl
288
- cls, = compile("import 'java.util.ArrayList'; def foo(a:ArrayList); a.size; end")
289
- assert_equal 0, cls.foo(java.util.ArrayList.new)
290
- end
291
-
292
- def test_import_package
293
- cls, = compile(<<-EOF)
294
- import java.util.*
295
- def foo
296
- ArrayList.new
297
- end
298
- EOF
299
- assert_equal java.util.ArrayList.java_class, cls.foo.java_class
300
- end
301
-
302
- def test_interface
303
- cls, = compile(<<-EOF)
304
- import 'java.util.concurrent.Callable'
305
- def foo(a:Callable)
306
- throws Exception
307
- a.call
308
- end
309
- EOF
310
- result = cls.foo {0}
311
- assert_equal 0, result
312
- m = cls.java_class.java_method 'foo', java.util.concurrent.Callable
313
- assert_equal([java.lang.Exception.java_class], m.exception_types)
314
-
315
- end
316
-
317
- def test_class_decl
318
- foo, = compile("class ClassDeclTest;end")
319
- assert_equal('ClassDeclTest', foo.java_class.name)
320
- end
321
-
322
- def test_class_name_from_file_with_underscore
323
- foo, = compile("puts 'blah'", 'class_name_test.mirah')
324
- assert_equal('ClassNameTest', foo.java_class.name)
325
- end
326
-
327
- def test_class_name_from_file_with_dash
328
- foo, = compile("puts 'blah'", 'class-dash-test.mirah')
329
- assert_equal('ClassDashTest', foo.java_class.name)
330
- end
331
-
332
- def test_puts
333
- cls, = compile("def foo;puts 'Hello World!';end")
334
- output = capture_output do
335
- cls.foo
336
- end
337
- assert_equal("Hello World!\n", output)
338
- end
339
-
340
- def test_print
341
- cls, = compile("def foo;print 'Hello World!';end")
342
- output = capture_output do
343
- cls.foo
344
- end
345
- assert_equal("Hello World!", output)
346
- end
347
-
348
- def test_method
349
- # TODO auto generate a constructor
350
- cls, = compile(
351
- "class MethodTest; def foo; 'foo';end;end")
352
- instance = cls.new
353
- assert_equal(cls, instance.class)
354
- assert_equal('foo', instance.foo)
355
- end
356
-
357
- def test_unless_fixnum
358
- cls, = compile(<<-EOF)
359
- def foo(a:fixnum)
360
- values = boolean[5]
361
- values[0] = true unless a < 0
362
- values[1] = true unless a <= 0
363
- values[2] = true unless a == 0
364
- values[3] = true unless a >= 0
365
- values[4] = true unless a > 0
366
- values
367
- end
368
- EOF
369
- assert_equal [true, true, true, false, false], cls.foo(1).to_a
370
- assert_equal [true, false, false, false, true], cls.foo(0).to_a
371
- assert_equal [false, false, true, true, true], cls.foo(-1).to_a
372
- end
373
-
374
- def test_unless_float
375
- cls, = compile(<<-EOF)
376
- def foo(a:float)
377
- values = boolean[5]
378
- values[0] = true unless a < 0.0
379
- values[1] = true unless a <= 0.0
380
- values[2] = true unless a == 0.0
381
- values[3] = true unless a >= 0.0
382
- values[4] = true unless a > 0.0
383
- values
384
- end
385
- EOF
386
- assert_equal [true, true, true, false, false], cls.foo(1.0).to_a
387
- assert_equal [true, false, false, false, true], cls.foo(0.0).to_a
388
- assert_equal [false, false, true, true, true], cls.foo(-1.0).to_a
389
- end
390
-
391
- def test_if_fixnum
392
- cls, = compile(<<-EOF)
393
- def foo(a:fixnum)
394
- if a < -5
395
- -6
396
- elsif a <= 0
397
- 0
398
- elsif a == 1
399
- 1
400
- elsif a > 4
401
- 5
402
- elsif a >= 3
403
- 3
404
- else
405
- 2
406
- end
407
- end
408
- EOF
409
- assert_equal(-6, cls.foo(-6))
410
- assert_equal(0, cls.foo(-5))
411
- assert_equal(0, cls.foo(0))
412
- assert_equal(1, cls.foo(1))
413
- assert_equal(2, cls.foo(2))
414
- assert_equal(3, cls.foo(3))
415
- assert_equal(3, cls.foo(4))
416
- assert_equal(5, cls.foo(5))
417
- end
418
-
419
- def test_if_float
420
- cls, = compile(<<-EOF)
421
- def foo(a:float)
422
- if a < -5.0
423
- -6
424
- elsif a <= 0.0
425
- 0
426
- elsif a == 1.0
427
- 1
428
- elsif a > 4.0
429
- 5
430
- elsif a >= 3.0
431
- 3
432
- else
433
- 2
434
- end
435
- end
436
- EOF
437
- assert_equal(-6, cls.foo(-5.1))
438
- assert_equal(0, cls.foo(-5.0))
439
- assert_equal(0, cls.foo(0.0))
440
- assert_equal(1, cls.foo(1.0))
441
- assert_equal(2, cls.foo(2.5))
442
- assert_equal(3, cls.foo(3.0))
443
- assert_equal(3, cls.foo(3.5))
444
- assert_equal(5, cls.foo(4.1))
445
- end
446
-
447
- def test_if_boolean
448
- cls, = compile(<<-EOF)
449
- def foo(a:boolean)
450
- if a
451
- 'true'
452
- else
453
- 'false'
454
- end
455
- end
456
- EOF
457
- assert_equal('true', cls.foo(true))
458
- assert_equal('false', cls.foo(false))
459
- end
460
-
461
- def test_if_int
462
- # conditions don't work with :int
463
- # cls, = compile("def foo(a:int); if a < 0; -a; else; a; end; end")
464
- # assert_equal 1, cls.foo(-1)
465
- # assert_equal 3, cls.foo(3)
466
- end
467
-
468
- def test_trailing_conditions
469
- cls, = compile(<<-EOF)
470
- def foo(a:fixnum)
471
- return '+' if a > 0
472
- return '0' unless a < 0
473
- '-'
474
- end
475
- EOF
476
- assert_equal '+', cls.foo(3)
477
- assert_equal '0', cls.foo(0)
478
- assert_equal '-', cls.foo(-1)
479
- end
480
-
481
-
482
- def test_local_decl
483
- cls, = compile(<<-EOF)
484
- import 'java.lang.String'
485
- a = :fixnum
486
- b = :int
487
- c = :long
488
- d = :float
489
- e = :string
490
- f = String
491
- puts a
492
- puts b
493
- puts c
494
- puts d
495
- puts e
496
- puts f
497
- EOF
498
- output = capture_output do
499
- cls.main([].to_java(:string))
500
- end
501
- assert_equal("0\n0\n0\n0.0\nnull\nnull\n", output)
502
- end
503
-
504
- def test_multi_assign
505
- cls, = compile(<<-EOF)
506
- def foo
507
- array = int[2]
508
- a = b = 2
509
- array[0] = a
510
- array[1] = b
511
- array
512
- end
513
- EOF
514
- assert_equal([2, 2], cls.foo.to_a)
515
-
516
- end
517
-
518
- def test_loop
519
- cls, = compile(
520
- 'def foo(a:fixnum);while a > 0; a -= 1; puts ".";end;end')
521
- assert_equal('', capture_output{cls.foo(0)})
522
- assert_equal(".\n", capture_output{cls.foo(1)})
523
- assert_equal(".\n.\n", capture_output{cls.foo(2)})
524
-
525
- cls, = compile(
526
- 'def foo(a:fixnum);begin;a -= 1; puts ".";end while a > 0;end')
527
- assert_equal(".\n", capture_output{cls.foo(0)})
528
- assert_equal(".\n", capture_output{cls.foo(1)})
529
- assert_equal(".\n.\n", capture_output{cls.foo(2)})
530
-
531
- cls, = compile(
532
- 'def foo(a:fixnum);until a <= 0; a -= 1; puts ".";end;end')
533
- assert_equal('', capture_output{cls.foo(0)})
534
- assert_equal(".\n", capture_output{cls.foo(1)})
535
- assert_equal(".\n.\n", capture_output{cls.foo(2)})
536
-
537
- cls, = compile(
538
- 'def foo(a:fixnum);begin;a -= 1; puts ".";end until a <= 0;end')
539
- assert_equal(".\n", capture_output{cls.foo(0)})
540
- assert_equal(".\n", capture_output{cls.foo(1)})
541
- assert_equal(".\n.\n", capture_output{cls.foo(2)})
542
-
543
- cls, = compile(
544
- 'def foo; a = 0; while a < 2; a+=1; end; end')
545
- assert_equal(nil, cls.foo)
546
-
547
- # TODO: loop doesn't work unless you're explicitly in a class
548
- # cls, = compile(<<-EOF)
549
- # def bar(a:fixnum)
550
- # loop do
551
- # a += 1
552
- # break if a > 2
553
- # end
554
- # a
555
- # end
556
- # EOF
557
- # assert_equal(3, cls.bar(0))
558
-
559
- loopy, = compile(<<-EOF)
560
- class Loopy
561
- def bar(a:fixnum)
562
- loop do
563
- a += 1
564
- break if a > 2
565
- end
566
- a
567
- end
568
- end
569
- EOF
570
-
571
- assert_equal(3, loopy.new.bar(0))
572
- end
573
-
574
- def test_break
575
- cls, = compile <<-EOF
576
- def foo
577
- count = 0
578
- while count < 5
579
- count += 1
580
- break if count == 1
581
- end
582
- count
583
- end
584
- EOF
585
- assert_equal(1, cls.foo)
586
-
587
- cls, = compile <<-EOF
588
- def foo
589
- a = 0
590
- b = 0
591
- while a < 2
592
- a += 1
593
- while b < 5
594
- b += 1
595
- break if b > 0
596
- end
597
- break if a == 1
598
- end
599
- a * 100 + b
600
- end
601
- EOF
602
- assert_equal(101, cls.foo)
603
-
604
- cls, = compile <<-EOF
605
- def foo
606
- count = 0
607
- begin
608
- count += 1
609
- break if count == 1
610
- end while count < 5
611
- count
612
- end
613
- EOF
614
- assert_equal(1, cls.foo)
615
- end
616
-
617
- def test_next
618
- cls, = compile <<-EOF
619
- def foo
620
- values = int[3]
621
- i = 0
622
- while i < 3
623
- i += 1
624
- next if i == 2
625
- values[i - 1] = i
626
- end
627
- values
628
- end
629
- EOF
630
- assert_equal([1, 0, 3], cls.foo.to_a)
631
-
632
- cls, = compile <<-EOF
633
- def foo
634
- i = 0
635
- while i < 5
636
- i += 1
637
- next if i == 5
638
- end
639
- i
640
- end
641
- EOF
642
- assert_equal(5, cls.foo)
643
-
644
- cls, = compile <<-EOF
645
- def foo
646
- values = int[3]
647
- a = 0
648
- b = 0
649
- while a < 3
650
- b = 0
651
- while b < 5
652
- b += 1
653
- next if b == a + 1
654
- # values[a] += b # TODO
655
- values[a] = values[a] + b
656
- end
657
- a += 1
658
- next if a == 2
659
- values[a - 1] = values[a - 1] + a * 100
660
- end
661
- values
662
- end
663
- EOF
664
- assert_equal([114, 13, 312], cls.foo.to_a)
665
-
666
- cls, = compile <<-EOF
667
- def foo
668
- count = 0
669
- sum = 0
670
- begin
671
- count += 1
672
- next if count == 2
673
- sum += count
674
- next if count == 5
675
- end while count < 5
676
- count * 100 + sum
677
- end
678
- EOF
679
- assert_equal(513, cls.foo)
680
- end
681
-
682
- def test_redo
683
- cls, = compile <<-EOF
684
- def foo
685
- i = 0
686
- while i < 5
687
- i += 1
688
- redo if i == 5
689
- end
690
- i
691
- end
692
- EOF
693
- assert_equal(6, cls.foo)
694
-
695
- cls, = compile <<-EOF
696
- def foo
697
- values = int[4]
698
- a = 0
699
- b = 0
700
- while a < 3
701
- b = a
702
- while b < 5
703
- b += 1
704
- redo if b == 5
705
- values[a] = values[a] + b
706
- end
707
- a += 1
708
- values[a - 1] = values[a - 1] + a * 100
709
- redo if a == 3
710
- end
711
- values
712
- end
713
- EOF
714
- assert_equal([116, 215, 313, 410], cls.foo.to_a)
715
-
716
- cls, = compile <<-EOF
717
- def foo
718
- i = 0
719
- begin
720
- i += 1
721
- redo if i == 5
722
- end while i < 5
723
- i
724
- end
725
- EOF
726
- assert_equal(6, cls.foo)
727
- end
728
-
729
- def test_fields
730
- cls, = compile(<<-EOF)
731
- class FieldTest
732
- def initialize(a:fixnum)
733
- @a = a
734
- end
735
-
736
- def a
737
- @a
738
- end
739
-
740
- def self.set_b(b:fixnum)
741
- @@b = b
742
- end
743
-
744
- def b
745
- @@b
746
- end
747
- end
748
- EOF
749
- first = cls.new(1)
750
- assert_equal(1, first.a)
751
-
752
- second = cls.new(2)
753
- assert_equal(1, first.a)
754
- assert_equal(2, second.a)
755
-
756
- cls.set_b 0
757
- assert_equal(0, first.b)
758
- assert_equal(0, second.b)
759
- assert_equal(1, cls.set_b(1))
760
- assert_equal(1, first.b)
761
- assert_equal(1, second.b)
762
- end
763
-
764
- def test_object_intrinsics
765
- cls, = compile(<<-EOF)
766
- import 'java.lang.Object'
767
- def nil(a:Object)
768
- a.nil?
769
- end
770
-
771
- def equal(a:Object, b:Object)
772
- a == b
773
- end
774
- EOF
775
-
776
- assert(cls.nil(nil))
777
- assert(!cls.nil("abc"))
778
-
779
- a = "foobar".to_java_string
780
- b = java.lang.Object.new
781
- assert(cls.equal(a, a))
782
- assert(cls.equal(b, b))
783
- assert(!cls.equal(a, b))
784
- end
785
-
786
- def test_implements
787
- cls, = compile(<<-EOF)
788
- import java.lang.Iterable
789
- class Foo; implements Iterable
790
- def iterator
791
- nil
792
- end
793
- end
794
- EOF
795
-
796
- assert_include java.lang.Iterable.java_class, cls.java_class.interfaces
797
- end
798
-
799
- def test_argument_widening
800
- cls, = compile(<<-EOF)
801
- def _Byte(a:byte)
802
- _Short(a)
803
- end
804
-
805
- def _Short(a:short)
806
- _Int(a)
807
- end
808
-
809
- def _Int(a:int)
810
- _Long(a)
811
- end
812
-
813
- def _Long(a:long)
814
- _Float(a)
815
- end
816
-
817
- def _Float(a:float)
818
- _Double(a)
819
- end
820
-
821
- def _Double(a:double)
822
- a
823
- end
824
- EOF
825
-
826
- assert_equal(1.0, cls._Byte(1))
827
- assert_equal(127.0, cls._Byte(127))
828
- assert_equal(128.0, cls._Short(128))
829
- assert_equal(32767.0, cls._Short(32767))
830
- assert_equal(32768.0, cls._Int(32768))
831
- assert_equal(2147483648.0, cls._Long(2147483648))
832
- end
833
-
834
- def test_interface_declaration
835
- interface = compile('interface A do; end').first
836
- assert(interface.java_class.interface?)
837
- assert_equal('A', interface.java_class.name)
838
-
839
- a, b = compile('interface A do; end; interface B < A do; end')
840
- assert_include(a, b.ancestors)
841
- assert_equal('A', a.java_class.name)
842
- assert_equal('B', b.java_class.name)
843
-
844
- a, b, c = compile(<<-EOF)
845
- interface A do
846
- end
847
-
848
- interface B do
849
- end
850
-
851
- interface C < A, B do
852
- end
853
- EOF
854
-
855
- assert_include(a, c.ancestors)
856
- assert_include(b, c.ancestors)
857
- assert_equal('A', a.java_class.name)
858
- assert_equal('B', b.java_class.name)
859
- assert_equal('C', c.java_class.name)
860
-
861
- assert_raise Mirah::Typer::InferenceError do
862
- compile(<<-EOF)
863
- interface A do
864
- def a
865
- returns :int
866
- end
867
- end
868
-
869
- class Impl; implements A
870
- def a
871
- "foo"
872
- end
873
- end
874
- EOF
875
- end
876
- end
877
-
878
- def test_raise
879
- cls, = compile(<<-EOF)
880
- def foo
881
- raise
882
- end
883
- EOF
884
- assert_raise_java(java.lang.RuntimeException) do
885
- cls.foo
886
- end
887
-
888
- cls, = compile(<<-EOF)
889
- def foo
890
- raise "Oh no!"
891
- end
892
- EOF
893
- ex = assert_raise_java(java.lang.RuntimeException, 'Oh no!') do
894
- cls.foo
895
- end
896
-
897
- cls, = compile(<<-EOF)
898
- def foo
899
- raise IllegalArgumentException
900
- end
901
- EOF
902
- ex = assert_raise_java(java.lang.IllegalArgumentException) do
903
- cls.foo
904
- end
905
-
906
- cls, = compile(<<-EOF)
907
- def foo
908
- throws Exception
909
- raise Exception, "oops"
910
- end
911
- EOF
912
- ex = assert_raise_java(java.lang.Exception, "oops") do
913
- cls.foo
914
- end
915
-
916
- cls, = compile(<<-EOF)
917
- def foo
918
- throws Throwable
919
- raise Throwable.new("darn")
920
- end
921
- EOF
922
- ex = assert_raise_java(java.lang.Throwable, "darn") do
923
- cls.foo
924
- end
925
- end
926
-
927
- def test_ensure
928
- cls, = compile(<<-EOF)
929
- def foo
930
- 1
931
- ensure
932
- puts "Hi"
933
- end
934
- EOF
935
- output = capture_output do
936
- assert_equal(1, cls.foo)
937
- end
938
- assert_equal "Hi\n", output
939
-
940
- cls, = compile(<<-EOF)
941
- def foo
942
- return 1
943
- ensure
944
- puts "Hi"
945
- end
946
- EOF
947
- output = capture_output do
948
- assert_equal(1, cls.foo)
949
- end
950
- assert_equal "Hi\n", output
951
-
952
- cls, = compile(<<-EOF)
953
- def foo
954
- begin
955
- break
956
- ensure
957
- puts "Hi"
958
- end while false
959
- end
960
- EOF
961
- output = capture_output do
962
- cls.foo
963
- end
964
- assert_equal "Hi\n", output
965
- end
966
-
967
- def test_cast
968
- cls, = compile(<<-EOF)
969
- def f2b; byte(1.0); end
970
- def f2s; short(1.0); end
971
- def f2c; char(1.0); end
972
- def f2i; int(1.0); end
973
- def f2l; long(1.0); end
974
- def f2d; int(1.0); end
975
-
976
- def i2b; byte(1); end
977
- def i2s; short(1); end
978
- def i2c; char(1); end
979
- def i2l; long(1); end
980
- def i2f; float(1); end
981
- def i2d; int(1); end
982
-
983
- def b2s; short(byte(1)); end
984
- def b2c; char(byte(1)); end
985
- def b2i; int(byte(1)); end
986
- def b2l; long(byte(1)); end
987
- def b2f; float(byte(1)); end
988
- def b2d; double(byte(1)); end
989
-
990
- def s2b; byte(short(1)); end
991
- def s2c; char(short(1)); end
992
- def s2i; int(short(1)); end
993
- def s2l; long(short(1)); end
994
- def s2f; float(short(1)); end
995
- def s2d; double(short(1)); end
996
-
997
- def c2b; byte(char(1)); end
998
- def c2s; short(char(1)); end
999
- def c2i; int(char(1)); end
1000
- def c2l; long(char(1)); end
1001
- def c2f; float(char(1)); end
1002
- def c2d; double(char(1)); end
1003
-
1004
- def l2b; byte(long(1)); end
1005
- def l2c; char(long(1)); end
1006
- def l2i; int(long(1)); end
1007
- def l2l; long(long(1)); end
1008
- def l2f; float(long(1)); end
1009
- def l2d; double(long(1)); end
1010
-
1011
- def d2b; byte(1.0); end
1012
- def d2s; short(1.0); end
1013
- def d2c; char(1.0); end
1014
- def d2i; int(1.0); end
1015
- def d2l; long(1.0); end
1016
- def d2f; float(1.0); end
1017
-
1018
- def hard_i2f(a:int)
1019
- float(if a < 0
1020
- a *= -1
1021
- a * 2
1022
- else
1023
- a * 2
1024
- end)
1025
- end
1026
- EOF
1027
-
1028
- assert_equal 1, cls.b2s
1029
- assert_equal 1, cls.b2c
1030
- assert_equal 1, cls.b2i
1031
- assert_equal 1, cls.b2l
1032
- assert_equal 1.0, cls.b2f
1033
- assert_equal 1.0, cls.b2d
1034
-
1035
- assert_equal 1, cls.s2b
1036
- assert_equal 1, cls.s2c
1037
- assert_equal 1, cls.s2i
1038
- assert_equal 1, cls.s2l
1039
- assert_equal 1.0, cls.s2f
1040
- assert_equal 1.0, cls.s2d
1041
-
1042
- assert_equal 1, cls.c2b
1043
- assert_equal 1, cls.c2s
1044
- assert_equal 1, cls.c2i
1045
- assert_equal 1, cls.c2l
1046
- assert_equal 1.0, cls.c2f
1047
- assert_equal 1.0, cls.c2d
1048
-
1049
- assert_equal 1, cls.i2b
1050
- assert_equal 1, cls.i2s
1051
- assert_equal 1, cls.i2c
1052
- assert_equal 1, cls.i2l
1053
- assert_equal 1.0, cls.i2f
1054
- assert_equal 1.0, cls.i2d
1055
-
1056
- assert_equal 1, cls.f2b
1057
- assert_equal 1, cls.f2s
1058
- assert_equal 1, cls.f2c
1059
- assert_equal 1, cls.f2i
1060
- assert_equal 1, cls.f2l
1061
- assert_equal 1.0, cls.f2d
1062
-
1063
- assert_equal 1, cls.d2b
1064
- assert_equal 1, cls.d2s
1065
- assert_equal 1, cls.d2c
1066
- assert_equal 1, cls.d2i
1067
- assert_equal 1, cls.d2l
1068
- assert_equal 1.0, cls.d2f
1069
-
1070
- assert_equal 2.0, cls.hard_i2f(1)
1071
- assert_equal 4.0, cls.hard_i2f(-2)
1072
- end
1073
-
1074
- def test_set
1075
- cls, = compile(<<-EOF)
1076
- def foo
1077
- @foo
1078
- end
1079
-
1080
- def foo=(foo:int)
1081
- @foo = foo
1082
- end
1083
- EOF
1084
-
1085
- assert_equal(0, cls.foo)
1086
- assert_equal(2, cls.foo_set(2))
1087
- assert_equal(2, cls.foo)
1088
- end
1089
-
1090
- def test_null_is_false
1091
- cls, = compile("def foo(a:String);if a;true;else;false;end;end")
1092
- assert_equal(true, cls.foo("a"))
1093
- assert_equal(false, cls.foo(nil))
1094
- end
1095
- def test_if_expr
1096
- cls, = compile(<<-EOF)
1097
- def foo(a:int)
1098
- return 1 if a == 1
1099
- end
1100
-
1101
- def bar(a:int)
1102
- return 1 unless a == 1
1103
- end
1104
- EOF
1105
-
1106
- assert_equal(0, cls.foo(0))
1107
- assert_equal(1, cls.foo(1))
1108
- assert_equal(1, cls.bar(0))
1109
- assert_equal(0, cls.bar(1))
1110
- end
1111
-
1112
- def test_and
1113
- cls, = compile(<<-EOF)
1114
- def bool(n:String, x:boolean)
1115
- puts n
1116
- x
1117
- end
1118
-
1119
- def foo(a:boolean, b:boolean)
1120
- return bool('a', a) && bool('b', b)
1121
- end
1122
-
1123
- def str(n:String, x:String)
1124
- puts n
1125
- x
1126
- end
1127
-
1128
- def bar(a:String, b:String)
1129
- return str('a', a) && str('b', b)
1130
- end
1131
- EOF
1132
-
1133
- assert_output("a\n") { assert_equal(false, cls.foo(false, true)) }
1134
- assert_output("a\nb\n") { assert_equal(false, cls.foo(true, false)) }
1135
- assert_output("a\nb\n") { assert_equal(true, cls.foo(true, true)) }
1136
-
1137
- assert_output("a\n") { assert_equal(nil, cls.bar(nil, "B")) }
1138
- assert_output("a\nb\n") { assert_equal(nil, cls.bar("A", nil)) }
1139
- assert_output("a\nb\n") { assert_equal("B", cls.bar("A", "B")) }
1140
-
1141
- cls, = compile(<<-EOF)
1142
- def s
1143
- @s
1144
- end
1145
-
1146
- def s=(s:String)
1147
- @s = s
1148
- end
1149
-
1150
- def b
1151
- @b
1152
- end
1153
-
1154
- def b=(b:boolean)
1155
- @b = b
1156
- end
1157
-
1158
- def foo(x:boolean)
1159
- @b &&= x
1160
- end
1161
-
1162
- def bar(x:String)
1163
- @s &&= x
1164
- end
1165
- EOF
1166
-
1167
- cls.b_set(false)
1168
- assert_equal(false, cls.foo(false))
1169
- assert_equal(false, cls.b)
1170
-
1171
- cls.b_set(true)
1172
- assert_equal(false, cls.foo(false))
1173
- assert_equal(false, cls.b)
1174
-
1175
- cls.b_set(true)
1176
- assert_equal(true, cls.foo(true))
1177
- assert_equal(true, cls.b)
1178
-
1179
- cls.s_set(nil)
1180
- assert_equal(nil, cls.bar(nil))
1181
- assert_equal(nil, cls.s)
1182
-
1183
- cls.s_set("S")
1184
- assert_equal(nil, cls.bar(nil))
1185
- assert_equal(nil, cls.s)
1186
-
1187
- cls.s_set("S")
1188
- assert_equal("x", cls.bar("x"))
1189
- assert_equal("x", cls.s)
1190
-
1191
- foo, = compile(<<-EOF)
1192
- class Foo2
1193
- def initialize
1194
- @count = 0
1195
- end
1196
-
1197
- def count
1198
- @count
1199
- end
1200
-
1201
- def a
1202
- @a
1203
- end
1204
-
1205
- def a=(a:String)
1206
- @count += 1
1207
- @a = a
1208
- end
1209
-
1210
- def foo(f:Foo2, x:String)
1211
- f.a &&= x
1212
- end
1213
- end
1214
- EOF
1215
-
1216
- f = foo.new
1217
- assert_equal(nil, f.foo(f, 'x'))
1218
- assert_equal(0, f.count)
1219
-
1220
- f = foo.new
1221
- f.a_set("A")
1222
- assert_equal(nil, f.foo(f, nil))
1223
- assert_equal(2, f.count)
1224
-
1225
- f = foo.new
1226
- f.a_set("A")
1227
- assert_equal('x', f.foo(f, 'x'))
1228
- assert_equal(2, f.count)
1229
- end
1230
-
1231
- def test_or
1232
- cls, = compile(<<-EOF)
1233
- def bool(n:String, x:boolean)
1234
- puts n
1235
- x
1236
- end
1237
-
1238
- def foo(a:boolean, b:boolean)
1239
- return bool('a', a) || bool('b', b)
1240
- end
1241
-
1242
- def str(n:String, x:String)
1243
- puts n
1244
- x
1245
- end
1246
-
1247
- def bar(a:String, b:String)
1248
- return str('a', a) || str('b', b)
1249
- end
1250
- EOF
1251
-
1252
- assert_output("a\n") { assert_equal(true, cls.foo(true, false)) }
1253
- assert_output("a\nb\n") { assert_equal(false, cls.foo(false, false)) }
1254
- assert_output("a\nb\n") { assert_equal(true, cls.foo(false, true)) }
1255
-
1256
- assert_output("a\n") { assert_equal("A", cls.bar("A", nil)) }
1257
- assert_output("a\nb\n") { assert_equal(nil, cls.bar(nil, nil)) }
1258
- assert_output("a\nb\n") { assert_equal("B", cls.bar(nil, "B")) }
1259
-
1260
- cls, = compile(<<-EOF)
1261
- def s
1262
- @s
1263
- end
1264
-
1265
- def s=(s:String)
1266
- @s = s
1267
- end
1268
-
1269
- def b
1270
- @b
1271
- end
1272
-
1273
- def b=(b:boolean)
1274
- @b = b
1275
- end
1276
-
1277
- def foo(x:boolean)
1278
- @b ||= x
1279
- end
1280
-
1281
- def bar(x:String)
1282
- @s ||= x
1283
- end
1284
- EOF
1285
-
1286
- cls.b_set(false)
1287
- assert_equal(false, cls.foo(false))
1288
- assert_equal(false, cls.b)
1289
-
1290
- cls.b_set(false)
1291
- assert_equal(true, cls.foo(true))
1292
- assert_equal(true, cls.b)
1293
-
1294
- cls.b_set(true)
1295
- assert_equal(true, cls.foo(false))
1296
- assert_equal(true, cls.b)
1297
-
1298
- cls.s_set(nil)
1299
- assert_equal(nil, cls.bar(nil))
1300
- assert_equal(nil, cls.s)
1301
-
1302
- cls.s_set(nil)
1303
- assert_equal("x", cls.bar("x"))
1304
- assert_equal("x", cls.s)
1305
-
1306
- cls.s_set("S")
1307
- assert_equal("S", cls.bar("x"))
1308
- assert_equal("S", cls.s)
1309
-
1310
- foo, = compile(<<-EOF)
1311
- class Foo3
1312
- def initialize
1313
- @count = 0
1314
- end
1315
-
1316
- def count
1317
- @count
1318
- end
1319
-
1320
- def a
1321
- @a
1322
- end
1323
-
1324
- def a=(a:String)
1325
- @count += 1
1326
- @a = a
1327
- end
1328
-
1329
- def foo(f:Foo3, x:String)
1330
- f.a ||= x
1331
- end
1332
- end
1333
- EOF
1334
-
1335
- f = foo.new
1336
- assert_equal('x', f.foo(f, 'x'))
1337
- assert_equal(1, f.count)
1338
-
1339
- f = foo.new
1340
- assert_equal(nil, f.foo(f, nil))
1341
- assert_equal(1, f.count)
1342
-
1343
- f = foo.new
1344
- f.a_set("A")
1345
- assert_equal("A", f.foo(f, nil))
1346
- assert_equal(1, f.count)
1347
-
1348
- f = foo.new
1349
- f.a_set("A")
1350
- assert_equal("A", f.foo(f, 'X'))
1351
- assert_equal(1, f.count)
1352
- end
1353
-
1354
- def test_op_elem_assign
1355
- foo, = compile(<<-EOF)
1356
- class Foo4
1357
- def initialize
1358
- @i = -1
1359
- end
1360
-
1361
- def i
1362
- @i += 1
1363
- end
1364
-
1365
- def a
1366
- @a
1367
- end
1368
-
1369
- def a=(a:String[])
1370
- @a = a
1371
- end
1372
-
1373
- def foo(x:String)
1374
- a[i] ||= x
1375
- end
1376
-
1377
- def bar(x:String)
1378
- a[i] &&= x
1379
- end
1380
- end
1381
- EOF
1382
-
1383
- f = foo.new
1384
- f.a_set([nil, nil, nil].to_java(:string))
1385
- assert_equal(nil, f.bar("x"))
1386
- assert_equal([nil, nil, nil], f.a.to_a)
1387
- assert_equal("x", f.foo("x"))
1388
- assert_equal([nil, "x", nil], f.a.to_a)
1389
- end
1390
-
1391
-
1392
- def test_literal_array
1393
- cls, = compile(<<-EOF)
1394
- def foo; puts "hello"; nil; end
1395
- def expr
1396
- [foo]
1397
- end
1398
- def nonexpr
1399
- [foo]
1400
- nil
1401
- end
1402
- EOF
1403
-
1404
- assert_output("hello\nhello\n") do
1405
- val = cls.expr
1406
- assert val
1407
-
1408
- val = cls.nonexpr
1409
- assert !val
1410
- end
1411
- end
1412
-
1413
- def test_literal_regexp
1414
- cls, = compile(<<-EOF)
1415
- def expr
1416
- /foo/
1417
- end
1418
- def matches
1419
- expr.matcher('barfoobaz').find
1420
- end
1421
- EOF
1422
-
1423
- val = cls.expr
1424
- assert_equal java.util.regex.Pattern, val.class
1425
- assert_equal 'foo', val.to_s
1426
-
1427
- assert cls.matches
1428
- end
1429
-
1430
- def test_array_return_type
1431
- cls, = compile(<<-EOF)
1432
- def split
1433
- /foo/.split('barfoobaz')
1434
- end
1435
- def puts
1436
- puts split
1437
- end
1438
- EOF
1439
-
1440
- assert_nothing_raised do
1441
- result = capture_output {cls.puts}
1442
- assert result =~ /\[Ljava\.lang\.String;@[a-f0-9]+/
1443
- end
1444
- assert_equal java.lang.String.java_class.array_class, cls.split.class.java_class
1445
- end
1446
-
1447
- def test_same_field_name
1448
- cls, = compile(<<-EOF)
1449
- class A1
1450
- def foo(bar:String)
1451
- @bar = bar
1452
- end
1453
- end
1454
-
1455
- class B1
1456
- def foo(bar:String)
1457
- @bar = bar
1458
- end
1459
- end
1460
-
1461
- puts A1.new.foo("Hi")
1462
- puts B1.new.foo("There")
1463
- EOF
1464
-
1465
- assert_output("Hi\nThere\n") do
1466
- cls.main(nil)
1467
- end
1468
- end
1469
- def test_super
1470
- cls, = compile(<<-EOF)
1471
- class Foo
1472
- def equals(other:Object); super(other); end
1473
- end
1474
- EOF
1475
-
1476
- obj = cls.new
1477
- assert obj.equals(obj)
1478
- assert !obj.equals(cls.new)
1479
- end
1480
-
1481
- def test_method_lookup_with_overrides
1482
- cls, = compile(<<-EOF)
1483
- class Bar; implements Runnable
1484
- def foo(x:Bar)
1485
- Thread.new(x)
1486
- end
1487
- def run
1488
- end
1489
- end
1490
- EOF
1491
-
1492
- # Just make sure this compiles.
1493
- # It shouldn't get confused by the Thread(String) constructor.
1494
- end
1495
-
1496
- def test_optional_args
1497
- cls, = compile(<<-EOF)
1498
- def foo(a:int, b:int = 1, c:int = 2)
1499
- puts a; puts b; puts c
1500
- end
1501
- foo(0)
1502
- foo(0,0)
1503
- foo(0,0,0)
1504
- EOF
1505
- assert_output("0\n1\n2\n0\n0\n2\n0\n0\n0\n") do
1506
- cls.main([].to_java :string)
1507
- end
1508
- end
1509
-
1510
- def test_field_read
1511
- cls, = compile(<<-EOF)
1512
- puts System.out.getClass.getName
1513
- EOF
1514
- assert_output("java.io.PrintStream\n") do
1515
- cls.main([].to_java :String)
1516
- end
1517
- end
1518
-
1519
- def test_array_arguments
1520
- cls, = compile(<<-EOF)
1521
- class ArrayArg
1522
- def initialize(foo:byte[]); end
1523
-
1524
- def self.make_one(foo:byte[])
1525
- ArrayArg.new(foo)
1526
- end
1527
- end
1528
- EOF
1529
- cls.make_one(nil)
1530
- end
1531
-
1532
- def test_block_with_duby_interface
1533
- cls, interface = compile(<<-EOF)
1534
- interface MyProc do
1535
- def call; returns :void; end
1536
- end
1537
- def foo(b:MyProc)
1538
- b.call
1539
- end
1540
- def bar
1541
- foo {puts "Hi"}
1542
- end
1543
- EOF
1544
- assert_output("Hi\n") do
1545
- cls.bar
1546
- end
1547
- end
1548
-
1549
- def test_duby_iterable
1550
- cls, = compile(<<-EOF)
1551
- import java.util.Iterator
1552
- class MyIterator; implements Iterable, Iterator
1553
- def initialize(x:Object)
1554
- @next = x
1555
- end
1556
-
1557
- def hasNext
1558
- @next != nil
1559
- end
1560
-
1561
- def next
1562
- result = @next
1563
- @next = nil
1564
- result
1565
- end
1566
-
1567
- def iterator
1568
- self
1569
- end
1570
-
1571
- def remove
1572
- raise UnsupportedOperationException
1573
- end
1574
-
1575
- def self.test(x:String)
1576
- MyIterator.new(x).each {|y| puts y}
1577
- end
1578
- end
1579
- EOF
1580
-
1581
- assert_output("Hi\n") do
1582
- cls.test("Hi")
1583
- end
1584
- end
1585
-
1586
- def test_java_lang_cast
1587
- cls, = compile(<<-EOF)
1588
- def foo(a:Object)
1589
- Integer(a).intValue
1590
- end
1591
- EOF
1592
-
1593
- assert_equal(2, cls.foo(java.lang.Integer.new(2)))
1594
- end
1595
-
1596
- def test_array_cast
1597
- cls, = compile(<<-EOF)
1598
- def foo(a:Object)
1599
- bar(String[].cast(a))
1600
- end
1601
-
1602
- def bar(a:String[])
1603
- a[0]
1604
- end
1605
- EOF
1606
-
1607
- assert_equal("foo", cls.foo(["foo", "bar"].to_java(:string)))
1608
-
1609
- cls, = compile(<<-EOF)
1610
- def foo(a:Object)
1611
- bar(int[].cast(a))
1612
- end
1613
-
1614
- def bar(a:int[])
1615
- a[0]
1616
- end
1617
- EOF
1618
-
1619
- assert_equal(2, cls.foo([2, 3].to_java(:int)))
1620
-
1621
- end
1622
-
1623
- def test_string_interpolation
1624
- cls, = compile(<<-EOF)
1625
- def foo(name:String)
1626
- print "Hello \#{name}."
1627
- end
1628
- EOF
1629
-
1630
- assert_output("Hello Fred.") do
1631
- cls.foo "Fred"
1632
- end
1633
-
1634
- cls, = compile(<<-EOF)
1635
- def foo(x:int)
1636
- print "\#{x += 1}"
1637
- x
1638
- end
1639
- EOF
1640
-
1641
- assert_output("2") do
1642
- assert_equal(2, cls.foo(1))
1643
- end
1644
-
1645
- cls, = compile(<<-EOF)
1646
- def foo(a:int)
1647
- "\#{a += 1}"
1648
- a
1649
- end
1650
- EOF
1651
- assert_equal(2, cls.foo(1))
1652
- end
1653
-
1654
- def test_string_interpolation_method_calls
1655
- cls, = compile <<-CODE
1656
- print "apples \#{'oranges'}".replace('apples', 'oranges')
1657
- CODE
1658
- assert_output "oranges oranges" do
1659
- cls.main nil
1660
- end
1661
- end
1662
-
1663
- def test_self_dot_static_methods
1664
- cls, = compile(<<-EOF)
1665
- class ClassWithStatics
1666
- def self.a
1667
- b
1668
- end
1669
- def self.b
1670
- print "b"
1671
- end
1672
- end
1673
- EOF
1674
-
1675
- assert_output("b") do
1676
- cls.a
1677
- end
1678
- end
1679
-
1680
- def test_evaluation_order
1681
- cls, = compile(<<-EOF)
1682
- def call(a:int, b:int, c:int)
1683
- print "\#{a}, \#{b}, \#{c}"
1684
- end
1685
-
1686
- def test_call(a:int)
1687
- call(a, if a < 10;a+=1;a;else;a;end, a)
1688
- end
1689
-
1690
- def test_string(a:int)
1691
- "\#{a}, \#{if a < 10;a += 1;a;else;a;end}, \#{a}"
1692
- end
1693
- EOF
1694
-
1695
- assert_output("1, 2, 2") do
1696
- cls.test_call(1)
1697
- end
1698
-
1699
- assert_equal("2, 3, 3", cls.test_string(2))
1700
- end
1701
-
1702
- def test_inner_class
1703
- cls, = compile(<<-EOF)
1704
- def foo
1705
- Character.UnicodeBlock.ARROWS
1706
- end
1707
- EOF
1708
-
1709
- subset = cls.foo
1710
- assert_equal("java.lang.Character$UnicodeBlock", subset.java_class.name)
1711
- end
1712
-
1713
- def test_class_literal
1714
- cls, = compile(<<-EOF)
1715
- def foo
1716
- String.class.getName
1717
- end
1718
- EOF
1719
-
1720
- assert_equal("java.lang.String", cls.foo)
1721
- end
1722
-
1723
- def test_instanceof
1724
- cls, = compile(<<-EOF)
1725
- def string(x:Object)
1726
- x.kind_of?(String)
1727
- end
1728
-
1729
- def dynamic(c:Class, o:Object)
1730
- o.kind_of?(c)
1731
- end
1732
- EOF
1733
-
1734
- assert_equal(true, cls.string("foo"))
1735
- assert_equal(false, cls.string(2))
1736
- assert_equal(true, cls.dynamic(java.lang.String, "foo"))
1737
- assert_equal(true, cls.dynamic(java.lang.Object, "foo"))
1738
- assert_equal(false, cls.dynamic(java.lang.Object, nil))
1739
- end
1740
-
1741
- def test_static_import
1742
- cls, = compile(<<-EOF)
1743
- import java.util.Arrays
1744
- include Arrays
1745
- def list(x:Object[])
1746
- asList(x)
1747
- end
1748
- EOF
1749
-
1750
- o = ["1", "2", "3"].to_java(:object)
1751
- list = cls.list(o)
1752
- assert_kind_of(Java::JavaUtil::List, list)
1753
- assert_equal(["1", "2", "3"], list.to_a)
1754
-
1755
- cls, = compile(<<-EOF)
1756
- import java.util.Arrays
1757
- class StaticImports
1758
- include Arrays
1759
- def list(x:Object[])
1760
- asList(x)
1761
- end
1762
- end
1763
- EOF
1764
-
1765
- list = cls.new.list(o)
1766
- assert_kind_of(Java::JavaUtil::List, list)
1767
- assert_equal(["1", "2", "3"], list.to_a)
1768
- end
1769
-
1770
- # TODO: need a writable field somewhere...
1771
- # def test_field_write
1772
- # cls, = compile(<<-EOF)
1773
- # old_pi = Math.PI
1774
- # Math.PI = 3.0
1775
- # puts Math.PI
1776
- # Math.PI = old_pi
1777
- # puts Math.PI
1778
- # EOF
1779
- # raise
1780
- # cls.main([].to_java :string)
1781
- # assert_output("3.0\n") do
1782
- # cls.main([].to_java :string)
1783
- # end
1784
- # end
1785
-
1786
- def test_class_append_self
1787
- cls, = compile(<<-EOF)
1788
- class Append
1789
- class << self
1790
- def hi
1791
- print 'Static Hello'
1792
- end
1793
- end
1794
- end
1795
- EOF
1796
-
1797
- output = capture_output do
1798
- cls.hi
1799
- end
1800
-
1801
- assert_equal('Static Hello', output)
1802
- end
1803
-
1804
- def test_hashes
1805
- cls, = compile(<<-EOF)
1806
- def foo1
1807
- {a:"A", b:"B"}
1808
- end
1809
- def foo2
1810
- return {a:"A", b:"B"}
1811
- end
1812
- EOF
1813
-
1814
- map = cls.foo1
1815
- assert_equal("A", map["a"])
1816
- assert_equal("B", map["b"])
1817
- map = cls.foo2
1818
- assert_equal("A", map["a"])
1819
- assert_equal("B", map["b"])
1820
-
1821
- cls, = compile(<<-'EOF')
1822
- def set(b:Object)
1823
- map = { }
1824
- map["key"] = b
1825
- map["key"]
1826
- end
1827
- EOF
1828
-
1829
- assert_equal("foo", cls.set("foo"))
1830
- end
1831
-
1832
- def test_hash_with_value_from_static_method
1833
- cls, = compile(<<-EOF)
1834
- def foo1
1835
- {a: a, b:"B"}
1836
- end
1837
- def a
1838
- return "A"
1839
- end
1840
- EOF
1841
- assert_equal("A", cls.foo1["a"])
1842
- end
1843
-
1844
- def test_hash_with_value_from_instance_method
1845
- cls, = compile(<<-EOF)
1846
- class HashTesty
1847
- def foo1
1848
- {a: a, b:"B"}
1849
- end
1850
- def a
1851
- return "A"
1852
- end
1853
- end
1854
- EOF
1855
- assert_equal("A", cls.new.foo1["a"])
1856
- end
1857
-
1858
-
1859
- def test_loop_in_ensure
1860
- cls, = compile(<<-EOF)
1861
- begin
1862
- puts "a"
1863
- begin
1864
- puts "b"
1865
- break
1866
- end while false
1867
- puts "c"
1868
- ensure
1869
- puts "ensure"
1870
- end
1871
- EOF
1872
-
1873
- assert_output("a\nb\nc\nensure\n") { cls.main(nil) }
1874
- end
1875
-
1876
- def test_return_type
1877
- assert_raise Mirah::Typer::InferenceError do
1878
- compile(<<-EOF)
1879
- class ReturnsA
1880
- def a:int
1881
- :foo
1882
- end
1883
- end
1884
- EOF
1885
- end
1886
-
1887
- assert_raise Mirah::Typer::InferenceError do
1888
- compile(<<-EOF)
1889
- class ReturnsB
1890
- def self.a:String
1891
- 2
1892
- end
1893
- end
1894
- EOF
1895
- end
1896
- end
1897
-
1898
- def test_abstract
1899
- abstract_class, concrete_class = compile(<<-EOF)
1900
- abstract class Abstract
1901
- abstract def foo:void; end
1902
- def bar; puts "bar"; end
1903
- end
1904
- class Concrete < Abstract
1905
- def foo; puts :foo; end
1906
- end
1907
- EOF
1908
-
1909
- assert_output("foo\nbar\n") do
1910
- a = concrete_class.new
1911
- a.foo
1912
- a.bar
1913
- end
1914
- assert_raise_java java.lang.InstantiationException do
1915
- abstract_class.new
1916
- end
1917
- end
1918
-
1919
- def test_return_void
1920
- script, = compile(<<-EOF)
1921
- def foo:void
1922
- puts :hi
1923
- return
1924
- end
1925
- EOF
1926
-
1927
- assert_output("hi\n") { script.foo }
1928
- end
1929
-
1930
- def test_package
1931
- script, cls = compile(<<-EOF)
1932
- package foo
1933
-
1934
- # package foo.bar {
1935
- # class PackagedBar
1936
- # def self.dosomething
1937
- # "bar"
1938
- # end
1939
- # end
1940
- # }
1941
-
1942
- def dosomething
1943
- "foo"
1944
- end
1945
- EOF
1946
-
1947
- package = script.java_class.name.split('.')[0]
1948
- assert_equal('foo', package)
1949
- assert_equal('foo', script.dosomething)
1950
-
1951
- # TODO move package to the parser so we can support blocks
1952
- # assert_equal('bar', cls.dosomething)
1953
- # assert_equal("foo.bar.PackagedBar", cls.java_class.name)
1954
- end
1955
-
1956
- def test_not
1957
- cls, = compile(<<-EOF)
1958
- def foo(x:boolean)
1959
- !x
1960
- end
1961
- def bar(x:Object)
1962
- !x
1963
- end
1964
- EOF
1965
- assert_equal(true, cls.foo(false))
1966
- assert_equal(false, cls.foo(true))
1967
- assert_equal(true, cls.bar(nil))
1968
- assert_equal(false, cls.bar(""))
1969
- end
1970
-
1971
- def test_rescue_scope
1972
- cls, = compile(<<-EOF)
1973
- def foo
1974
- a = 1
1975
- b = 2
1976
- begin
1977
- raise "Foo"
1978
- rescue => b
1979
- puts a
1980
- puts b.getMessage
1981
- end
1982
- puts b
1983
- end
1984
- EOF
1985
-
1986
- assert_output("1\nFoo\n2\n") { cls.foo }
1987
- end
1988
-
1989
- def test_scoped_self_through_method_call
1990
- cls, = compile(<<-EOF)
1991
- class ScopedSelfThroughMethodCall
1992
- def emptyMap
1993
- {}
1994
- end
1995
-
1996
- def foo
1997
- emptyMap["a"] = "A"
1998
- end
1999
- end
2000
- EOF
2001
-
2002
- # just make sure it can execute
2003
- m = cls.new.foo
2004
- end
2005
-
2006
- def test_self_call_preserves_scope
2007
- cls, = compile(<<-EOF)
2008
- class SelfCallPreservesScope
2009
- def key
2010
- "key"
2011
- end
2012
-
2013
- def foo
2014
- map = {}
2015
- map[key] = "value"
2016
- map
2017
- end
2018
- end
2019
- EOF
2020
-
2021
- map = cls.new.foo
2022
- assert_equal("value", map["key"])
2023
- end
2024
-
2025
- def test_wide_nonexpressions
2026
- script, cls1, cls2 = compile(<<-EOF)
2027
- class WideA
2028
- def a
2029
- 2.5
2030
- end
2031
- end
2032
-
2033
- class WideB < WideA
2034
- def a
2035
- super
2036
- 3.5
2037
- end
2038
- end
2039
-
2040
- def self.b
2041
- 1.5
2042
- end
2043
-
2044
- 1.5
2045
- WideA.new.a
2046
- WideB.new.a
2047
- b
2048
- EOF
2049
-
2050
- script.main(nil)
2051
- end
2052
-
2053
- def test_parameter_used_in_block
2054
- cls, = compile(<<-EOF)
2055
- def foo(x:String):void
2056
- thread = Thread.new do
2057
- puts "Hello \#{x}"
2058
- end
2059
- begin
2060
- thread.run
2061
- thread.join
2062
- rescue
2063
- puts "Uh Oh!"
2064
- end
2065
- end
2066
-
2067
- foo('there')
2068
- EOF
2069
- assert_output("Hello there\n") do
2070
- cls.main(nil)
2071
- end
2072
- end
2073
-
2074
- def test_colon2
2075
- cls, = compile(<<-EOF)
2076
- def foo
2077
- java.util::HashSet.new
2078
- end
2079
- EOF
2080
-
2081
- assert_kind_of(java.util.HashSet, cls.foo)
2082
- end
2083
-
2084
- def test_colon2_cast
2085
- cls, = compile(<<-EOF)
2086
- def foo(x:Object)
2087
- java.util::Map.Entry(x)
2088
- end
2089
- EOF
2090
-
2091
- entry = java.util.HashMap.new(:a => 1).entrySet.iterator.next
2092
- assert_equal(entry, cls.foo(entry))
2093
- end
2094
-
2095
- def test_covariant_arrays
2096
- cls, = compile(<<-EOF)
2097
- puts java::util::Arrays.toString(String[5])
2098
- EOF
2099
-
2100
- assert_output("[null, null, null, null, null]\n") do
2101
- cls.main(nil)
2102
- end
2103
- end
2104
-
2105
- def test_getClass_on_object_array
2106
- cls, = compile(<<-EOF)
2107
- puts Object[0].getClass.getName
2108
- EOF
2109
-
2110
- assert_output("[Ljava.lang.Object;\n") do
2111
- cls.main(nil)
2112
- end
2113
- end
2114
-
2115
- def test_nil_assign
2116
- cls, = compile(<<-EOF)
2117
- def foo
2118
- a = nil
2119
- a = 'hello'
2120
- a.length
2121
- end
2122
- EOF
2123
-
2124
- assert_equal(5, cls.foo)
2125
-
2126
- cls, = compile(<<-EOF)
2127
- a = nil
2128
- puts a
2129
- EOF
2130
-
2131
- assert_output("null\n") do
2132
- cls.main(nil)
2133
- end
2134
- end
2135
-
2136
- def test_long_generation
2137
- cls, = compile(<<-EOF)
2138
- c = 2_000_000_000_000
2139
- puts c
2140
- EOF
2141
- end
2142
- <<<<<<< HEAD:test/jvm/jvm_compiler_test.rb
2143
-
2144
- def test_missing_class_with_block_raises_inference_error
2145
- assert_raises Typer::InferenceError do
2146
- compile("Interface Implements_Go do; end")
2147
- end
2148
- end
2149
-
2150
- def test_bool_equality
2151
- cls, = compile("puts true == false")
2152
- assert_output("false\n") do
2153
- cls.main(nil)
2154
- end
2155
- end
2156
-
2157
- def test_bool_inequality
2158
- cls, = compile("puts true != false")
2159
- assert_output("true\n") do
2160
- =======
2161
-
2162
- def test_add_args_in_macro
2163
- cls, = compile(<<-EOF)
2164
- macro def foo(a)
2165
- import duby.lang.compiler.Node
2166
- quote { bar "1", `Node(a.child_nodes.get(0)).child_nodes`, "2"}
2167
- end
2168
-
2169
- def bar(a:String, b:String, c:String, d:String)
2170
- puts "\#{a} \#{b} \#{c} \#{d}"
2171
- end
2172
-
2173
- foo(["a", "b"])
2174
- EOF
2175
-
2176
- assert_output("1 a b 2\n") do
2177
- >>>>>>> parser_support:test/test_jvm_compiler.rb
2178
- cls.main(nil)
2179
- end
2180
- end
2181
- end