mirah 0.0.9-java → 0.0.10-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.
@@ -73,7 +73,7 @@ module Mirah::JVM::Types
73
73
  end
74
74
 
75
75
  def add_compiled_macro(klass, name, arg_types)
76
- add_macro(name, *arg_types) do |duby, call|
76
+ add_macro(name, *arg_types) do |mirah, call|
77
77
  # Ick. We need to preserve the scope of the arguments to the macro.
78
78
  # However the only way to do that is to wrap them in a ScopedBody.
79
79
  # It'd be better if we didn't have to expose this wrapper node to
@@ -87,15 +87,20 @@ module Mirah::JVM::Types
87
87
  wrap_with_scoped_body call, arg
88
88
  end
89
89
 
90
- expander = klass.constructors[0].newInstance(duby, call)
90
+ expander = klass.constructors[0].newInstance(mirah, call)
91
91
  ast = expander.expand
92
92
  if ast
93
93
  body = Mirah::AST::ScopedBody.new(call.parent, call.position)
94
94
  body << ast
95
- if call.target
95
+
96
+ # if the macro was called on something, and that something
97
+ # was not a class only containing macros, reassign self for
98
+ # the body.
99
+ if call.target && !call.target.is_a?(Mirah::AST::Builtin)
96
100
  body.static_scope.self_type = call.target.inferred_type!
97
101
  body.static_scope.self_node = call.target
98
102
  end
103
+
99
104
  body
100
105
  else
101
106
  Mirah::AST::Noop.new(call.parent, call.position)
@@ -406,7 +406,7 @@ module Mirah
406
406
  Mirah::AST::Block.new(parent, position(node)) do |block|
407
407
  [
408
408
  args ? transformer.transform(args, block) : Mirah::AST::Arguments.new(block, position(node)),
409
- body ? transformer.transform(body, block) : nil,
409
+ body ? transformer.transform(body, block) : Mirah::AST::Body.new(block, position(node)),
410
410
  ]
411
411
  end
412
412
  end
@@ -330,7 +330,7 @@ module Mirah
330
330
  end
331
331
  end
332
332
 
333
- if @deferred_nodes.size == 0
333
+ if @deferred_nodes.empty?
334
334
  log "[Cycle #{i}]: Resolved all types, exiting"
335
335
  break
336
336
  elsif old_deferred == @deferred_nodes
@@ -52,7 +52,7 @@ module Mirah
52
52
  else
53
53
  puts "-j/--java flag only applies to \"compile\" mode."
54
54
  print_help
55
- throw :exit
55
+ throw :exit, 1
56
56
  end
57
57
  when '--jvm'
58
58
  args.shift
@@ -79,7 +79,7 @@ module Mirah
79
79
  else
80
80
  puts "unrecognized flag: " + args[0]
81
81
  print_help
82
- throw :exit
82
+ throw :exit, 1
83
83
  end
84
84
  end
85
85
  state.destination ||= File.join(File.expand_path('.'), '')
@@ -16,6 +16,7 @@
16
16
  module Mirah
17
17
  module Util
18
18
  module ProcessErrors
19
+ # errors - array of NodeErrors
19
20
  def process_errors(errors)
20
21
  errors.each do |ex|
21
22
  puts ex
@@ -26,7 +27,7 @@ module Mirah
26
27
  end
27
28
  puts ex.backtrace if @verbose
28
29
  end
29
- throw :exit unless errors.empty?
30
+ throw :exit, 1 unless errors.empty?
30
31
  end
31
32
  end
32
33
  end
@@ -14,5 +14,5 @@
14
14
  # limitations under the License.
15
15
 
16
16
  module Mirah
17
- VERSION = "0.0.9"
17
+ VERSION = "0.0.10"
18
18
  end
@@ -12,10 +12,7 @@
12
12
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
13
  # See the License for the specific language governing permissions and
14
14
  # limitations under the License.
15
-
16
- require 'mirah'
17
- require 'test/unit'
18
- require 'jruby'
15
+ require 'test_helper'
19
16
 
20
17
  class TestAst < Test::Unit::TestCase
21
18
  include Mirah
@@ -360,7 +357,8 @@ class TestAst < Test::Unit::TestCase
360
357
  assert_not_nil(new_ast)
361
358
  assert(AST::EmptyArray === new_ast)
362
359
  assert_equal(5, new_ast.size.literal)
363
- assert_equal(AST.type(:int), new_ast.inferred_type)
360
+
361
+ assert_equal("int", new_ast.type_node.name)
364
362
  end
365
363
 
366
364
  def test_block_comment
@@ -377,4 +375,4 @@ class TestAst < Test::Unit::TestCase
377
375
  AST.parse("puts( 'aoue'")
378
376
  end
379
377
  end
380
- end
378
+ end
@@ -0,0 +1,80 @@
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
+
18
+ class TestCommands < Test::Unit::TestCase
19
+ def teardown
20
+ Mirah::AST.type_factory = nil
21
+ end
22
+
23
+ class RaisesMirahErrorCommand < Mirah::Commands::Base
24
+ def execute
25
+ execute_base { raise Mirah::MirahError, "just an error" }
26
+ end
27
+ def command_name; :foo; end
28
+ end
29
+
30
+ class MirahProcessesErrorCommand < Mirah::Commands::Base
31
+ include Mirah::Util::ProcessErrors
32
+ def execute
33
+ execute_base { process_errors [Mirah::NodeError.new("just an error")] }
34
+ end
35
+ def command_name; :foo; end
36
+ end
37
+
38
+ class SuccessfulCommand < Mirah::Commands::Base
39
+ def execute
40
+ execute_base { "something" }
41
+ end
42
+ def command_name; :foo; end
43
+ end
44
+
45
+ def test_on_Mirah_error_has_non_zero_exit_code
46
+ assert_non_zero_exit do
47
+ RaisesMirahErrorCommand.new([]).execute
48
+ end
49
+ end
50
+
51
+ def test_on_bad_argument_has_non_zero_exit_code
52
+ assert_non_zero_exit do
53
+ RaisesMirahErrorCommand.new(['-bad-argument']).execute
54
+ end
55
+ end
56
+
57
+ def test_on_j_option_when_command_is_not_compile_has_non_zero_exit_code
58
+ assert_non_zero_exit do
59
+ RaisesMirahErrorCommand.new(['-j']).execute
60
+ end
61
+ end
62
+
63
+ def test_success_is_truthy
64
+ assert SuccessfulCommand.new([]).execute, "expected it to be truthy"
65
+ end
66
+
67
+ def test_process_errors_causes_a_non_zero_exit
68
+ assert_non_zero_exit do
69
+ MirahProcessesErrorCommand.new([]).execute
70
+ end
71
+ end
72
+
73
+ def assert_non_zero_exit
74
+ ex = assert_raise SystemExit do
75
+ yield
76
+ end
77
+ assert_not_equal 0, ex.status
78
+ end
79
+
80
+ end
@@ -12,13 +12,9 @@
12
12
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
13
  # See the License for the specific language governing permissions and
14
14
  # limitations under the License.
15
+ require 'test_helper'
15
16
 
16
- require 'mirah'
17
- require 'mirah/compiler'
18
- require 'test/unit'
19
- require 'jruby'
20
-
21
- class TestAst < Test::Unit::TestCase
17
+ class TestCompilation < Test::Unit::TestCase
22
18
  include Mirah
23
19
 
24
20
  class MockCompiler
@@ -12,9 +12,7 @@
12
12
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
13
  # See the License for the specific language governing permissions and
14
14
  # limitations under the License.
15
-
16
- require 'test/unit'
17
- require 'mirah'
15
+ require 'test_helper'
18
16
 
19
17
  class TestEnv < Test::Unit::TestCase
20
18
  include Mirah
@@ -1,7 +1,19 @@
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.
1
15
  # TODO refactor this and test_jvm_compiler to use mirah.rb
2
-
3
- require 'test/unit'
4
- require 'mirah'
16
+ require 'test_helper'
5
17
 
6
18
  class TestMacros < Test::Unit::TestCase
7
19
  java_import 'java.lang.System'
@@ -12,9 +12,7 @@
12
12
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
13
  # See the License for the specific language governing permissions and
14
14
  # limitations under the License.
15
-
16
- require 'test/unit'
17
- require 'mirah'
15
+ require 'test_helper'
18
16
 
19
17
  class TestTyper < Test::Unit::TestCase
20
18
  include Mirah
@@ -222,6 +220,21 @@ class TestTyper < Test::Unit::TestCase
222
220
 
223
221
  assert_equal(typer.float_type, ast2.body[0].inferred_type)
224
222
  end
223
+
224
+ def test_rescue_w_different_type_raises_inference_error_when_expression
225
+ ast = AST.parse("begin true; 1.0; rescue; ''; end").body[0]
226
+ typer = Typer::Simple.new("bar")
227
+
228
+ assert_raise(Mirah::InferenceError) {ast.infer(typer, true)}
229
+ end
230
+
231
+ def test_rescue_w_different_type_doesnt_raise_inference_error_when_statement
232
+ ast = AST.parse("begin true; 1.0; rescue; ''; end").body[0]
233
+ typer = Typer::Simple.new("bar")
234
+
235
+ assert_nothing_raised {ast.infer(typer, false)}
236
+ end
237
+
225
238
 
226
239
  def test_class
227
240
  ast = AST.parse("class Foo; def foo; 1; end; def baz; foo; end; end")
@@ -234,4 +247,4 @@ class TestTyper < Test::Unit::TestCase
234
247
 
235
248
  assert_nothing_raised {typer.resolve(true)}
236
249
  end
237
- end
250
+ end
@@ -12,15 +12,10 @@
12
12
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
13
  # See the License for the specific language governing permissions and
14
14
  # limitations under the License.
15
- require 'bundler/setup'
16
- require 'test/unit'
17
- require 'mirah'
18
- require 'jruby'
15
+ require 'test_helper'
19
16
  require 'stringio'
20
17
  require 'fileutils'
21
18
 
22
- #$CLASSPATH << File.expand_path("#{File.dirname(__FILE__)}/../..")
23
-
24
19
  unless Mirah::AST.macro "__gloop__"
25
20
  Mirah::AST.defmacro "__gloop__" do |transformer, fcall, parent|
26
21
  Mirah::AST::Loop.new(parent, parent.position, true, false) do |loop|
@@ -125,7 +120,7 @@ module JVMCompiler
125
120
  compiler
126
121
  end
127
122
 
128
- def compile(code, name = "script" + System.nano_time.to_s)
123
+ def compile(code, name = tmp_script_name)
129
124
  clear_tmp_files
130
125
  reset_type_factory
131
126
 
@@ -135,6 +130,10 @@ module JVMCompiler
135
130
 
136
131
  generate_classes compiler
137
132
  end
133
+
134
+ def tmp_script_name
135
+ "script" + System.nano_time.to_s
136
+ end
138
137
  end
139
138
 
140
139
 
@@ -12,14 +12,7 @@
12
12
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
13
  # See the License for the specific language governing permissions and
14
14
  # limitations under the License.
15
-
16
- require 'bundler/setup'
17
- require 'test/unit'
18
- require 'mirah'
19
-
20
15
  require 'bytecode_test_helper'
21
-
22
-
23
16
  require 'mirah/jvm/compiler/java_source'
24
17
 
25
18
 
@@ -0,0 +1,58 @@
1
+ class TestAnnotations < Test::Unit::TestCase
2
+ def deprecated
3
+ @deprecated ||= java.lang.Deprecated.java_class
4
+ end
5
+
6
+ def test_annotation_on_a_method
7
+ cls, = compile(<<-EOF)
8
+ $Deprecated
9
+ def foo
10
+ 'foo'
11
+ end
12
+ EOF
13
+
14
+ assert_not_nil cls.java_class.java_method('foo').annotation(deprecated)
15
+ assert_nil cls.java_class.annotation(deprecated)
16
+ end
17
+
18
+ def test_annotation_on_a_class
19
+ script, cls = compile(<<-EOF)
20
+ $Deprecated
21
+ class Annotated
22
+ end
23
+ EOF
24
+ assert_not_nil cls.java_class.annotation(deprecated)
25
+ end
26
+
27
+ def test_annotation_on_a_field
28
+ cls, = compile(<<-EOF)
29
+ class AnnotatedField
30
+ def initialize
31
+ $Deprecated
32
+ @foo = 1
33
+ end
34
+ end
35
+ EOF
36
+
37
+ assert_not_nil cls.java_class.declared_fields[0].annotation(deprecated)
38
+ end
39
+
40
+ def test_annotation_with_an_integer
41
+ jruby_method = Java::OrgJrubyAnno::JRubyMethod.java_class
42
+
43
+ cls, = compile(<<-EOF)
44
+ import org.jruby.*
45
+ import org.jruby.anno.*
46
+ import org.jruby.runtime.*
47
+ import org.jruby.runtime.builtin.*
48
+
49
+ $JRubyMethod["name" => "bar", "optional" => 1]
50
+ def bar(baz:int)
51
+ end
52
+ EOF
53
+ method_annotation = cls.java_class.java_method('bar', :int).annotation(jruby_method)
54
+
55
+ assert_equal 1, method_annotation.optional
56
+ end
57
+
58
+ end
@@ -0,0 +1,62 @@
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 TestBlocks < Test::Unit::TestCase
17
+
18
+ def setup
19
+ super
20
+ clear_tmp_files
21
+ reset_type_factory
22
+ end
23
+
24
+ def parse_and_type code, name=tmp_script_name
25
+ parse_and_resolve_types name, code
26
+ end
27
+
28
+ #this should probably be a core test
29
+ def test_empty_block_parses_and_types_without_error
30
+ assert_nothing_raised do
31
+ parse_and_type(<<-CODE)
32
+ interface Bar do;def run:void;end;end
33
+
34
+ class Foo
35
+ def foo(a:Bar)
36
+ 1
37
+ end
38
+ end
39
+ Foo.new.foo do
40
+ end
41
+ CODE
42
+ end
43
+ end
44
+
45
+ def test_non_empty_block_parses_and_types_without_error
46
+ assert_nothing_raised do
47
+ parse_and_type(<<-CODE)
48
+ interface Bar do;def run:void;end;end
49
+
50
+ class Foo
51
+ def foo(a:Bar)
52
+ 1
53
+ end
54
+ end
55
+ Foo.new.foo do
56
+ 1
57
+ end
58
+ CODE
59
+ end
60
+ end
61
+
62
+ end