mirah 0.0.9-java → 0.0.10-java

Sign up to get free protection for your applications and to get access to all the features.
@@ -14,6 +14,13 @@
14
14
  # limitations under the License.
15
15
 
16
16
  class TestJVMCompiler < 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
17
24
 
18
25
  def test_local
19
26
  cls, = compile("def foo; a = 1; a; end")
@@ -876,21 +883,13 @@ class TestJVMCompiler < Test::Unit::TestCase
876
883
  end
877
884
  end
878
885
 
879
- def assert_throw(type, message="")
880
- ex = assert_raise(NativeException) do
881
- yield
882
- end
883
- assert_equal type, ex.cause.class
884
- assert_equal message, ex.cause.message.to_s
885
- end
886
-
887
886
  def test_raise
888
887
  cls, = compile(<<-EOF)
889
888
  def foo
890
889
  raise
891
890
  end
892
891
  EOF
893
- assert_throw(java.lang.RuntimeException) do
892
+ assert_raise_java(java.lang.RuntimeException) do
894
893
  cls.foo
895
894
  end
896
895
 
@@ -899,7 +898,7 @@ class TestJVMCompiler < Test::Unit::TestCase
899
898
  raise "Oh no!"
900
899
  end
901
900
  EOF
902
- ex = assert_throw(java.lang.RuntimeException, 'Oh no!') do
901
+ ex = assert_raise_java(java.lang.RuntimeException, 'Oh no!') do
903
902
  cls.foo
904
903
  end
905
904
 
@@ -908,7 +907,7 @@ class TestJVMCompiler < Test::Unit::TestCase
908
907
  raise IllegalArgumentException
909
908
  end
910
909
  EOF
911
- ex = assert_throw(java.lang.IllegalArgumentException) do
910
+ ex = assert_raise_java(java.lang.IllegalArgumentException) do
912
911
  cls.foo
913
912
  end
914
913
 
@@ -918,7 +917,7 @@ class TestJVMCompiler < Test::Unit::TestCase
918
917
  raise Exception, "oops"
919
918
  end
920
919
  EOF
921
- ex = assert_throw(java.lang.Exception, "oops") do
920
+ ex = assert_raise_java(java.lang.Exception, "oops") do
922
921
  cls.foo
923
922
  end
924
923
 
@@ -928,128 +927,9 @@ class TestJVMCompiler < Test::Unit::TestCase
928
927
  raise Throwable.new("darn")
929
928
  end
930
929
  EOF
931
- ex = assert_throw(java.lang.Throwable, "darn") do
932
- cls.foo
933
- end
934
- end
935
-
936
- def test_rescue
937
- cls, = compile(<<-EOF)
938
- def foo
939
- begin
940
- puts "body"
941
- rescue
942
- puts "rescue"
943
- end
944
- end
945
- EOF
946
-
947
- output = capture_output do
948
- cls.foo
949
- end
950
- assert_equal("body\n", output)
951
-
952
- cls, = compile(<<-EOF)
953
- def foo
954
- begin
955
- puts "body"
956
- raise
957
- rescue
958
- puts "rescue"
959
- end
960
- end
961
- EOF
962
-
963
- output = capture_output do
964
- cls.foo
965
- end
966
- assert_equal("body\nrescue\n", output)
967
-
968
- cls, = compile(<<-EOF)
969
- def foo(a:int)
970
- begin
971
- puts "body"
972
- if a == 0
973
- raise IllegalArgumentException
974
- else
975
- raise
976
- end
977
- rescue IllegalArgumentException
978
- puts "IllegalArgumentException"
979
- rescue
980
- puts "rescue"
981
- end
982
- end
983
- EOF
984
-
985
- output = capture_output do
986
- cls.foo(1)
987
- cls.foo(0)
988
- end
989
- assert_equal("body\nrescue\nbody\nIllegalArgumentException\n", output)
990
-
991
- cls, = compile(<<-EOF)
992
- def foo(a:int)
993
- begin
994
- puts "body"
995
- if a == 0
996
- raise IllegalArgumentException
997
- elsif a == 1
998
- raise Throwable
999
- else
1000
- raise
1001
- end
1002
- rescue IllegalArgumentException, RuntimeException
1003
- puts "multi"
1004
- rescue Throwable
1005
- puts "other"
1006
- end
1007
- end
1008
- EOF
1009
-
1010
- output = capture_output do
1011
- cls.foo(0)
1012
- cls.foo(1)
1013
- cls.foo(2)
1014
- end
1015
- assert_equal("body\nmulti\nbody\nother\nbody\nmulti\n", output)
1016
-
1017
- cls, = compile(<<-EOF)
1018
- def foo
1019
- begin
1020
- raise "foo"
1021
- rescue => ex
1022
- puts ex.getMessage
1023
- end
1024
- end
1025
- EOF
1026
-
1027
- output = capture_output do
930
+ ex = assert_raise_java(java.lang.Throwable, "darn") do
1028
931
  cls.foo
1029
932
  end
1030
- assert_equal("foo\n", output)
1031
-
1032
-
1033
- cls, = compile(<<-EOF)
1034
- def foo(x:boolean)
1035
- throws Exception
1036
- if x
1037
- raise Exception, "x"
1038
- end
1039
- rescue Exception
1040
- "x"
1041
- else
1042
- raise Exception, "!x"
1043
- end
1044
- EOF
1045
-
1046
- assert_equal "x", cls.foo(true)
1047
- begin
1048
- cls.foo(false)
1049
- fail
1050
- rescue java.lang.Exception => ex
1051
- assert_equal "java.lang.Exception: !x", ex.message
1052
- end
1053
933
  end
1054
934
 
1055
935
  def test_ensure
@@ -1654,38 +1534,6 @@ class TestJVMCompiler < Test::Unit::TestCase
1654
1534
  cls.main(nil)
1655
1535
  end
1656
1536
  end
1657
-
1658
- def test_annotations
1659
- deprecated = java.lang.Deprecated.java_class
1660
- cls, = compile(<<-EOF)
1661
- $Deprecated
1662
- def foo
1663
- 'foo'
1664
- end
1665
- EOF
1666
-
1667
- assert_not_nil cls.java_class.java_method('foo').annotation(deprecated)
1668
- assert_nil cls.java_class.annotation(deprecated)
1669
-
1670
- script, cls = compile(<<-EOF)
1671
- $Deprecated
1672
- class Annotated
1673
- end
1674
- EOF
1675
- assert_not_nil cls.java_class.annotation(deprecated)
1676
-
1677
- cls, = compile(<<-EOF)
1678
- class AnnotatedField
1679
- def initialize
1680
- $Deprecated
1681
- @foo = 1
1682
- end
1683
- end
1684
- EOF
1685
-
1686
- assert_not_nil cls.java_class.declared_fields[0].annotation(deprecated)
1687
- end
1688
-
1689
1537
  def test_super
1690
1538
  cls, = compile(<<-EOF)
1691
1539
  class Foo
@@ -2191,6 +2039,33 @@ class TestJVMCompiler < Test::Unit::TestCase
2191
2039
  assert_equal("foo", cls.set("foo"))
2192
2040
  end
2193
2041
 
2042
+ def test_hash_with_value_from_static_method
2043
+ cls, = compile(<<-EOF)
2044
+ def foo1
2045
+ {a: a, b:"B"}
2046
+ end
2047
+ def a
2048
+ return "A"
2049
+ end
2050
+ EOF
2051
+ assert_equal("A", cls.foo1["a"])
2052
+ end
2053
+
2054
+ def test_hash_with_value_from_instance_method
2055
+ cls, = compile(<<-EOF)
2056
+ class HashTesty
2057
+ def foo1
2058
+ {a: a, b:"B"}
2059
+ end
2060
+ def a
2061
+ return "A"
2062
+ end
2063
+ end
2064
+ EOF
2065
+ assert_equal("A", cls.new.foo1["a"])
2066
+ end
2067
+
2068
+
2194
2069
  def test_loop_in_ensure
2195
2070
  cls, = compile(<<-EOF)
2196
2071
  begin
@@ -2246,11 +2121,8 @@ class TestJVMCompiler < Test::Unit::TestCase
2246
2121
  a.foo
2247
2122
  a.bar
2248
2123
  end
2249
- begin
2124
+ assert_raise_java java.lang.InstantiationException do
2250
2125
  abstract_class.new
2251
- fail "Expected InstantiationException"
2252
- rescue java.lang.InstantiationException
2253
- # expected
2254
2126
  end
2255
2127
  end
2256
2128
 
@@ -2478,29 +2350,9 @@ class TestJVMCompiler < Test::Unit::TestCase
2478
2350
  EOF
2479
2351
  end
2480
2352
 
2481
- def test_empty_rescues
2482
- cls, = compile(<<-EOF)
2483
- begin
2484
- rescue
2485
- nil
2486
- end
2487
- EOF
2488
-
2489
- cls, = compile(<<-EOF)
2490
- begin
2491
- ""
2492
- rescue
2493
- nil
2494
- end
2495
- nil
2496
- EOF
2497
- end
2498
-
2499
2353
  def test_missing_class_with_block_raises_inference_error
2500
2354
  assert_raises Typer::InferenceError do
2501
2355
  compile("Interface Implements_Go do; end")
2502
2356
  end
2503
2357
  end
2504
-
2505
-
2506
2358
  end
@@ -144,4 +144,17 @@ class TestMacros < Test::Unit::TestCase
144
144
  end
145
145
  end
146
146
 
147
+ def test_method_def_after_macro_def_with_same_name_raises_error
148
+ assert_raises Mirah::InferenceError do
149
+ compile(<<-EOF)
150
+ macro def foo
151
+ quote { puts :z }
152
+ end
153
+ def foo
154
+ :bar
155
+ end
156
+ EOF
157
+ end
158
+
159
+ end
147
160
  end
@@ -0,0 +1,152 @@
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 TestRescue < Test::Unit::TestCase
17
+
18
+ def test_rescue
19
+ cls, = compile(<<-EOF)
20
+ def foo
21
+ begin
22
+ puts "body"
23
+ rescue
24
+ puts "rescue"
25
+ end
26
+ end
27
+ EOF
28
+
29
+ output = capture_output do
30
+ cls.foo
31
+ end
32
+ assert_equal("body\n", output)
33
+
34
+ cls, = compile(<<-EOF)
35
+ def foo
36
+ begin
37
+ puts "body"
38
+ raise
39
+ rescue
40
+ puts "rescue"
41
+ end
42
+ end
43
+ EOF
44
+
45
+ output = capture_output do
46
+ cls.foo
47
+ end
48
+ assert_equal("body\nrescue\n", output)
49
+
50
+ cls, = compile(<<-EOF)
51
+ def foo(a:int)
52
+ begin
53
+ puts "body"
54
+ if a == 0
55
+ raise IllegalArgumentException
56
+ else
57
+ raise
58
+ end
59
+ rescue IllegalArgumentException
60
+ puts "IllegalArgumentException"
61
+ rescue
62
+ puts "rescue"
63
+ end
64
+ end
65
+ EOF
66
+
67
+ output = capture_output do
68
+ cls.foo(1)
69
+ cls.foo(0)
70
+ end
71
+ assert_equal("body\nrescue\nbody\nIllegalArgumentException\n", output)
72
+
73
+ cls, = compile(<<-EOF)
74
+ def foo(a:int)
75
+ begin
76
+ puts "body"
77
+ if a == 0
78
+ raise IllegalArgumentException
79
+ elsif a == 1
80
+ raise Throwable
81
+ else
82
+ raise
83
+ end
84
+ rescue IllegalArgumentException, RuntimeException
85
+ puts "multi"
86
+ rescue Throwable
87
+ puts "other"
88
+ end
89
+ end
90
+ EOF
91
+
92
+ output = capture_output do
93
+ cls.foo(0)
94
+ cls.foo(1)
95
+ cls.foo(2)
96
+ end
97
+ assert_equal("body\nmulti\nbody\nother\nbody\nmulti\n", output)
98
+
99
+ cls, = compile(<<-EOF)
100
+ def foo
101
+ begin
102
+ raise "foo"
103
+ rescue => ex
104
+ puts ex.getMessage
105
+ end
106
+ end
107
+ EOF
108
+
109
+ output = capture_output do
110
+ cls.foo
111
+ end
112
+ assert_equal("foo\n", output)
113
+
114
+
115
+ cls, = compile(<<-EOF)
116
+ def foo(x:boolean)
117
+ throws Exception
118
+ if x
119
+ raise Exception, "x"
120
+ end
121
+ rescue Exception
122
+ "x"
123
+ else
124
+ raise Exception, "!x"
125
+ end
126
+ EOF
127
+
128
+ assert_equal "x", cls.foo(true)
129
+ ex = assert_raise NativeException do
130
+ cls.foo(false)
131
+ end
132
+ assert_equal "java.lang.Exception: !x", ex.message
133
+ end
134
+
135
+ def test_empty_rescues
136
+ cls, = compile(<<-EOF)
137
+ begin
138
+ rescue
139
+ nil
140
+ end
141
+ EOF
142
+
143
+ cls, = compile(<<-EOF)
144
+ begin
145
+ ""
146
+ rescue
147
+ nil
148
+ end
149
+ nil
150
+ EOF
151
+ end
152
+ end