mirah 0.1.4-java → 0.2.0-java

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (98) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +0 -0
  3. data/CODE_OF_CONDUCT.md +74 -0
  4. data/History.txt +531 -0
  5. data/README.md +23 -10
  6. data/Rakefile +239 -156
  7. data/TODO.md +71 -10
  8. data/bin/mirah +1 -1
  9. data/bin/mirahc +1 -1
  10. data/dist/mirahc.jar +0 -0
  11. data/examples/bintrees.mirah +2 -2
  12. data/examples/construction.mirah +2 -2
  13. data/examples/fields.mirah +1 -1
  14. data/examples/fractal.mirah +1 -1
  15. data/examples/fractal.rb +70 -0
  16. data/examples/interfaces.mirah +1 -1
  17. data/examples/java_thing.mirah +1 -1
  18. data/examples/macros/square.mirah +3 -3
  19. data/examples/macros/square_int.mirah +3 -3
  20. data/examples/macros/string_each_char.mirah +6 -6
  21. data/examples/rosettacode/100-doors.mirah +0 -2
  22. data/examples/rosettacode/count-occurrences-of-a-substring.mirah +3 -3
  23. data/examples/rosettacode/empty-string.mirah +1 -1
  24. data/examples/rosettacode/fizz-buzz.mirah +4 -4
  25. data/examples/rosettacode/is-string-numeric.mirah +7 -7
  26. data/examples/rosettacode/palindrome.mirah +2 -2
  27. data/examples/rosettacode/reverse-a-string.mirah +1 -1
  28. data/examples/rosettacode/rot-13.mirah +1 -1
  29. data/examples/{edb.mirah → rosettacode/simple_character_math.mirah} +13 -4
  30. data/examples/rosettacode/string-case.mirah +2 -2
  31. data/examples/rosettacode/string-length.mirah +1 -1
  32. data/examples/swing.mirah +9 -14
  33. data/extensions_and_macros.md +117 -0
  34. data/lib/mirah.rb +1 -1
  35. data/lib/mirah/errors.rb +3 -1
  36. data/lib/mirah/transform/ast_ext.rb +3 -2
  37. data/lib/mirah/util/process_errors.rb +1 -2
  38. data/lib/mirah/version.rb +1 -1
  39. data/test/A.class +0 -0
  40. data/test/core/util/jvm_version_test.rb +10 -0
  41. data/test/core/util/mirah_arguments_test.rb +51 -4
  42. data/test/fixtures/cp1251_test.mirah +7 -0
  43. data/test/fixtures/org/foo/AbstractExecutorJava8.java +30 -0
  44. data/test/fixtures/org/foo/ClassWithSelfReferencingTypeParameter.java +24 -0
  45. data/test/fixtures/org/foo/InnerInterfaceClass.java +12 -0
  46. data/test/fixtures/org/foo/IntAnno.class +0 -0
  47. data/test/fixtures/org/foo/TypeFixtureJava8.java +10 -0
  48. data/test/fixtures/utf8_test.mirah +7 -0
  49. data/test/jvm/access_levels_test.rb +31 -0
  50. data/test/jvm/annotations_test.rb +3 -6
  51. data/test/jvm/blocks_test.rb +303 -120
  52. data/test/jvm/cast_test.rb +123 -50
  53. data/test/jvm/closure_test.rb +242 -0
  54. data/test/jvm/constructors_test.rb +1 -3
  55. data/test/jvm/example_test.rb +6 -2
  56. data/test/jvm/extensions/array_extensions_test.rb +181 -0
  57. data/test/jvm/extensions/collection_extensions_test.rb +195 -0
  58. data/test/jvm/{enumerable_test.rb → extensions/enumerable_test.rb} +81 -13
  59. data/test/jvm/extensions/hash_extensions_test.rb +56 -0
  60. data/test/jvm/extensions/list_extensions_test.rb +143 -0
  61. data/test/jvm/extensions/lock_extensions_test.rb +43 -0
  62. data/test/jvm/{numeric_extensions_test.rb → extensions/numeric_extensions_test.rb} +0 -0
  63. data/test/jvm/extensions/numeric_operators_test.rb +86 -0
  64. data/test/jvm/extensions/object_extensions_test.rb +122 -0
  65. data/test/jvm/{string_builder_extensions_test.rb → extensions/string_builder_extensions_test.rb} +0 -0
  66. data/test/jvm/{string_extensions_test.rb → extensions/string_extensions_test.rb} +57 -4
  67. data/test/jvm/generics_test.rb +14 -6
  68. data/test/jvm/import_test.rb +38 -1
  69. data/test/jvm/interface_test.rb +17 -0
  70. data/test/jvm/jvm_commands_test.rb +9 -0
  71. data/test/jvm/jvm_compiler_test.rb +568 -43
  72. data/test/jvm/macros_test.rb +343 -19
  73. data/test/jvm/main_method_test.rb +1 -3
  74. data/test/jvm/new_backend_test_helper.rb +54 -7
  75. data/test/jvm/rescue_test.rb +20 -5
  76. data/test/jvm/static_fields_test.rb +52 -10
  77. data/test/jvm/{mirror_compilation_test_helper.rb → string_test.rb} +10 -9
  78. data/test/jvm/varargs_test.rb +6 -16
  79. data/test/mirrors/base_type_test.rb +20 -7
  80. data/test/mirrors/bytecode_mirror_test.rb +8 -3
  81. data/test/mirrors/generics_test.rb +89 -10
  82. data/test/mirrors/member_test.rb +1 -1
  83. data/test/mirrors/method_lookup_test.rb +10 -3
  84. data/test/mirrors/mirrors_test.rb +20 -20
  85. data/test/mirrors/simple_async_mirror_loader_test.rb +1 -1
  86. data/test/mirrors/simple_mirror_loader_test.rb +1 -1
  87. data/test/newMirahClass$Closure2.class +0 -0
  88. data/test/newMirahClass.class +0 -0
  89. data/test/test_helper.rb +8 -1
  90. metadata +31 -16
  91. data/bin/bundler +0 -16
  92. data/bin/rake +0 -16
  93. data/examples/ant/example-build.xml~ +0 -7
  94. data/examples/test.edb +0 -9
  95. data/lib/mirah/compiler.rb +0 -67
  96. data/lib/mirah/parser.rb +0 -224
  97. data/lib/mirah/util/delegate.rb +0 -65
  98. data/test/jvm/list_extensions_test.rb +0 -23
@@ -1,3 +1,5 @@
1
+ # encoding: UTF-8
2
+ #
1
3
  # Copyright (c) 2013 The Mirah project authors. All Rights Reserved.
2
4
  # All contributing project authors may be found in the NOTICE file.
3
5
  #
@@ -55,18 +57,69 @@ class StringExtensionsTest < Test::Unit::TestCase
55
57
  end
56
58
 
57
59
  def test_string_match
60
+ cls, = compile(%q{puts 'byeby'.match(/b(.*)y/)[1]})
61
+ assert_run_output("yeb\n", cls)
62
+ end
63
+
64
+ def test_string_matches
58
65
  cls, = compile("def match; 'abcdef' =~ /d/; end")
59
66
  assert cls.match, 'failed to match string'
60
67
  end
61
68
 
62
- def test_string_dont_match
63
- cls, = compile("def dont_match; 'abcdef' =~ /g/; end")
64
- refute cls.dont_match, 'mistakenly matched string'
69
+ def test_string_doesnt_match
70
+ cls, = compile("def doesnt_match; 'abcdef' =~ /g/; end")
71
+ refute cls.doesnt_match, 'mistakenly matched string'
65
72
  end
66
73
 
67
- def test_string_match_wrong_type
74
+ def test_string_matches_wrong_type
68
75
  assert_raises Mirah::MirahError do
69
76
  compile("def match_wrong_type; 'abcdef' =~ 'd'; end")
70
77
  end
71
78
  end
79
+
80
+ def test_string_to_int_invalid_number
81
+ cls, = compile(<<-EOF)
82
+ puts 'abc'.to_int
83
+ EOF
84
+ assert_raise_java Java::JavaLang::NumberFormatException do
85
+ cls.main nil
86
+ end
87
+ end
88
+
89
+ def test_string_to_int_valid_number
90
+ cls, = compile(<<-EOF)
91
+ puts '-987654321'.to_int
92
+ EOF
93
+ assert_run_output("-987654321\n", cls)
94
+ end
95
+
96
+ def test_string_to_int_too_big_number
97
+ cls, = compile(<<-EOF)
98
+ puts '-9876543210'.to_int
99
+ EOF
100
+ assert_raise_java Java::JavaLang::NumberFormatException do
101
+ cls.main nil
102
+ end
103
+ end
104
+
105
+ def test_string_each_codepoint_1_block_argument
106
+ cls, = compile(%q{
107
+ "abc\u1234\U0010FFFF\u5678de𠀘f".each_codepoint do |codepoint|
108
+ puts codepoint
109
+ end
110
+ })
111
+ assert_run_output("97\n98\n99\n4660\n1114111\n22136\n100\n101\n131096\n102\n", cls)
112
+ end
113
+
114
+ def test_string_each_codepoint_0_block_arguments
115
+ cls, = compile(%q{
116
+ count = 0
117
+ "abc\u1234\U0010FFFF\u5678de𠀘f".each_codepoint do
118
+ count+=1
119
+ end
120
+ puts count
121
+ })
122
+ assert_run_output("10\n", cls)
123
+ end
72
124
  end
125
+
@@ -27,9 +27,7 @@ class GenericsTest < Test::Unit::TestCase
27
27
  puts(foo.get(1).substring(2))
28
28
  EOF
29
29
 
30
- assert_output("cond string\n") do
31
- cls.main(nil)
32
- end
30
+ assert_run_output("cond string\n", cls)
33
31
  end
34
32
 
35
33
  def test_generics_generic_payload
@@ -44,10 +42,20 @@ class GenericsTest < Test::Unit::TestCase
44
42
  puts(bar.get(0).get(1).substring(2))
45
43
  EOF
46
44
 
47
- assert_output("cond string\n") do
48
- cls.main(nil)
49
- end
45
+ assert_run_output("cond string\n", cls)
50
46
  end
51
47
 
48
+ def test_generics_recursion
49
+ omit_if JVMCompiler::JVM_VERSION.to_f < 1.8
50
+
51
+ # Class signature for java.util.stream.Stream is recursive
52
+ #<T:Ljava/lang/Object;>Ljava/lang/Object;Ljava/util/stream/BaseStream<TT;Ljava/util/stream/Stream<TT;>;>;
53
+ cls, = compile(<<-EOF)
54
+ [10,5,8].stream.sorted.forEach { |i| puts i}
55
+ EOF
56
+ assert_run_output("5\n8\n10\n", cls)
57
+ end
58
+
59
+
52
60
  end
53
61
 
@@ -64,6 +64,21 @@ class ImportTest < Test::Unit::TestCase
64
64
  assert_equal(["1", "2", "3"], list.to_a)
65
65
  end
66
66
 
67
+ def test_static_import_constants
68
+ cls, = compile(<<-EOF)
69
+ import java.awt.Color
70
+ import static java.awt.Color.*
71
+ def _red:Color
72
+ red
73
+ end
74
+ def _RED:Color
75
+ RED
76
+ end
77
+ puts _RED === _red
78
+ EOF
79
+ assert_run_output("true\n", cls)
80
+ end
81
+
67
82
  def test_static_import_nested_in_class
68
83
  cls, = compile(<<-EOF)
69
84
  import java.util.Arrays
@@ -92,7 +107,29 @@ class ImportTest < Test::Unit::TestCase
92
107
  end
93
108
  end
94
109
  EOF
95
- assert_output("hi\n") { cls.main(nil) }
110
+ assert_run_output("hi\n", cls)
111
+ end
112
+
113
+ def test_static_import_in_closure
114
+ cls, = compile(%q[
115
+ class StaticImportInClosure
116
+ def baz
117
+ b = 3
118
+ perform do
119
+ import static java.lang.Math.*
120
+ b = max(-9,-8)
121
+ end
122
+ b
123
+ end
124
+
125
+ def perform(runnable:Runnable)
126
+ runnable.run
127
+ end
128
+ end
129
+
130
+ puts StaticImportInClosure.new.baz
131
+ ])
132
+ assert_run_output("-8\n", cls)
96
133
  end
97
134
 
98
135
  end
@@ -109,4 +109,21 @@ class InterfaceTest < Test::Unit::TestCase
109
109
  end
110
110
  EOF
111
111
  end
112
+
113
+ def test_interface_with_default_method_compiles_on_java_8
114
+ omit_if JVMCompiler::JVM_VERSION.to_f < 1.8
115
+
116
+ cls, = compile(<<-'EOF', java_version: '1.8')
117
+ interface DefaultMe
118
+ def act(messages:String):void
119
+ puts "#{messages} all the things!"
120
+ end
121
+ end
122
+
123
+ class WithDefault implements DefaultMe
124
+ end
125
+ WithDefault.new.act("default")
126
+ EOF
127
+ assert_run_output "default all the things!\n", cls
128
+ end
112
129
  end
@@ -49,4 +49,13 @@ class JVMCommandsTest < Test::Unit::TestCase
49
49
  )
50
50
  end
51
51
  end
52
+
53
+ def test_encoding
54
+ assert_output "default utf8 encoding test\n" do
55
+ Mirah.run(File.dirname(__FILE__)+"/../fixtures/utf8_test.mirah")
56
+ end
57
+ assert_output "cp1251 encoding test\n" do
58
+ Mirah.run('-encoding','cp1251',File.dirname(__FILE__)+"/../fixtures/cp1251_test.mirah")
59
+ end
60
+ end
52
61
  end
@@ -271,12 +271,68 @@ class JVMCompilerTest < Test::Unit::TestCase
271
271
 
272
272
  def test_class_name_from_file_with_underscore
273
273
  foo, = compile("puts 'blah'", :name => 'class_name_test.mirah')
274
- assert_equal('ClassNameTest', foo.java_class.name)
274
+ assert_equal('ClassNameTestTopLevel', foo.java_class.name)
275
275
  end
276
276
 
277
277
  def test_class_name_from_file_with_dash
278
278
  foo, = compile("puts 'blah'", :name => 'class-dash-test.mirah')
279
- assert_equal('ClassDashTest', foo.java_class.name)
279
+ assert_equal('ClassDashTestTopLevel', foo.java_class.name)
280
+ end
281
+
282
+ def test_class_name_from_file_used_within_source_match
283
+ cls, = compile(%q{
284
+
285
+ package array_subclass_test
286
+
287
+ class Subclass < Superclass2
288
+ def self.run
289
+ ArraySubclassTest.new.baz
290
+ end
291
+ end
292
+
293
+ class ArraySubclassTest
294
+
295
+ def baz()
296
+ bar(Subclass[3])
297
+ end
298
+
299
+ def bar(foo:Superclass2[])
300
+ "Success"
301
+ end
302
+ end
303
+
304
+ class Superclass2
305
+ end
306
+ }, :name => 'Superclass2.mirah')
307
+ assert_equal('Success', cls.run)
308
+ end
309
+
310
+ def test_class_name_from_file_used_within_source_mismatch
311
+ cls, = compile(%q{
312
+
313
+ package array_subclass_test
314
+
315
+ class Subclass < Superclass2
316
+ def self.run
317
+ ArraySubclassTest.new.baz
318
+ end
319
+ end
320
+
321
+ class ArraySubclassTest
322
+
323
+ def baz()
324
+ bar(Subclass[3])
325
+ end
326
+
327
+ def bar(foo:Superclass2[])
328
+ "Success"
329
+ end
330
+ end
331
+
332
+ class Superclass2
333
+ end
334
+ }, :name => 'Superclass3.mirah')
335
+ assert_equal('Success', cls.run)
280
336
  end
281
337
 
282
338
  def test_puts
@@ -287,6 +343,35 @@ class JVMCompilerTest < Test::Unit::TestCase
287
343
  assert_equal("Hello World!\n", output)
288
344
  end
289
345
 
346
+ def test_puts_classmethod_no_args
347
+ cls, = compile(%q{
348
+ def foo
349
+ puts
350
+ puts
351
+ end
352
+ })
353
+ output = capture_output do
354
+ cls.foo
355
+ end
356
+ assert_equal("\n\n", output)
357
+ end
358
+
359
+ def test_puts_instancemethod_no_args
360
+ cls, = compile(%q{
361
+ class Foo
362
+ def foo
363
+ puts
364
+ puts
365
+ puts
366
+ end
367
+ end
368
+ })
369
+ output = capture_output do
370
+ cls.new.foo
371
+ end
372
+ assert_equal("\n\n\n", output)
373
+ end
374
+
290
375
  def test_print
291
376
  cls, = compile("def foo;print 'Hello World!';end")
292
377
  output = capture_output do
@@ -766,7 +851,7 @@ class JVMCompilerTest < Test::Unit::TestCase
766
851
  end
767
852
  to_#{primitive} #{primitive}(1)
768
853
  EOF
769
- assert_output("1\n") { cls.main nil}
854
+ assert_run_output("1\n", cls)
770
855
  rescue => e
771
856
  raise "#{primitive} #{e.message}"
772
857
  end
@@ -779,7 +864,7 @@ class JVMCompilerTest < Test::Unit::TestCase
779
864
  end
780
865
  to_#{type} #{type}(1)
781
866
  EOF
782
- assert_output("1.0\n") { cls.main nil}
867
+ assert_run_output("1.0\n", cls)
783
868
  rescue => e
784
869
  raise "#{type} #{e.message}"
785
870
  end
@@ -791,7 +876,22 @@ class JVMCompilerTest < Test::Unit::TestCase
791
876
  end
792
877
  to_character char(65)
793
878
  EOF
794
- assert_output("A\n") { cls.main nil}
879
+ assert_run_output("A\n", cls)
880
+ end
881
+
882
+ def test_return_boxing_and_unboxing
883
+ cls, = compile(<<-EOF)
884
+ def box:Boolean
885
+ return true
886
+ end
887
+
888
+ def unbox:boolean
889
+ return Boolean.new(false)
890
+ end
891
+
892
+ EOF
893
+ assert_equal(true, cls.box)
894
+ assert_equal(false, cls.unbox)
795
895
  end
796
896
 
797
897
  def test_raise
@@ -859,11 +959,42 @@ class JVMCompilerTest < Test::Unit::TestCase
859
959
  assert_equal(2, cls.foo_set(2))
860
960
  assert_equal(2, cls.foo)
861
961
  end
862
-
863
- def test_null_is_false
864
- cls, = compile("def foo(a:String);if a;true;else;false;end;end")
865
- assert_equal(true, cls.foo("a"))
866
- assert_equal(false, cls.foo(nil))
962
+
963
+ def test_promotion_to_boolean
964
+ cases = [
965
+ [ "String", "a" => true, nil => false ],
966
+ [ "boolean", true => true, false => false ],
967
+ [ "byte", 1 => true, 0 => true ],
968
+ [ "short", 1 => true, 0 => true ],
969
+ [ "int", 1 => true, 0 => true ],
970
+ [ "long", 1 => true, 0 => true ],
971
+ [ "char", 1 => true, 0 => true ],
972
+ [ "float", 1.0 => true, 0.0 => true ],
973
+ [ "double", 1.0 => true, 0.0 => true ],
974
+ ].each do |type, cases|
975
+ cls, = compile(%Q[
976
+ class Foo
977
+ def self.foo(a:#{type})
978
+ if a
979
+ true
980
+ else
981
+ false
982
+ end
983
+ end
984
+ def self.foo_inverted(a:#{type})
985
+ unless a
986
+ true
987
+ else
988
+ false
989
+ end
990
+ end
991
+ end
992
+ ])
993
+ cases.each do |input,boolean_value|
994
+ assert_equal( boolean_value, cls.foo(input))
995
+ assert_equal(!boolean_value, cls.foo_inverted(input))
996
+ end
997
+ end
867
998
  end
868
999
 
869
1000
  def test_if_expr
@@ -1238,9 +1369,7 @@ class JVMCompilerTest < Test::Unit::TestCase
1238
1369
  puts B1.new.foo("There")
1239
1370
  EOF
1240
1371
 
1241
- assert_output("Hi\nThere\n") do
1242
- cls.main(nil)
1243
- end
1372
+ assert_run_output("Hi\nThere\n", cls)
1244
1373
  end
1245
1374
 
1246
1375
  def test_super
@@ -1279,18 +1408,14 @@ class JVMCompilerTest < Test::Unit::TestCase
1279
1408
  foo(0,0)
1280
1409
  foo(0,0,0)
1281
1410
  EOF
1282
- assert_output("0\n1\n2\n0\n0\n2\n0\n0\n0\n") do
1283
- cls.main([].to_java :string)
1284
- end
1411
+ assert_run_output("0\n1\n2\n0\n0\n2\n0\n0\n0\n", cls)
1285
1412
  end
1286
1413
 
1287
1414
  def test_field_read
1288
1415
  cls, = compile(<<-EOF)
1289
1416
  puts System.out.getClass.getName
1290
1417
  EOF
1291
- assert_output("java.io.PrintStream\n") do
1292
- cls.main([].to_java :String)
1293
- end
1418
+ assert_run_output("java.io.PrintStream\n", cls)
1294
1419
  end
1295
1420
 
1296
1421
  def test_array_arguments
@@ -1341,9 +1466,7 @@ class JVMCompilerTest < Test::Unit::TestCase
1341
1466
  cls, = compile <<-CODE
1342
1467
  print "apples \#{'oranges'}".replace('apples', 'oranges')
1343
1468
  CODE
1344
- assert_output "oranges oranges" do
1345
- cls.main nil
1346
- end
1469
+ assert_run_output("oranges oranges", cls)
1347
1470
  end
1348
1471
 
1349
1472
  def test_self_dot_static_methods
@@ -1440,9 +1563,7 @@ class JVMCompilerTest < Test::Unit::TestCase
1440
1563
  # EOF
1441
1564
  # raise
1442
1565
  # cls.main([].to_java :string)
1443
- # assert_output("3.0\n") do
1444
- # cls.main([].to_java :string)
1445
- # end
1566
+ # assert_run_output("3.0\n", cls)
1446
1567
  # end
1447
1568
 
1448
1569
  def test_class_append_self
@@ -1485,6 +1606,19 @@ class JVMCompilerTest < Test::Unit::TestCase
1485
1606
  end
1486
1607
  end
1487
1608
 
1609
+ def test_native
1610
+ cls, = compile(<<-EOF)
1611
+ class Foo
1612
+ native def foo; end
1613
+ end
1614
+ EOF
1615
+
1616
+ assert_raise_java java.lang.UnsatisfiedLinkError do
1617
+ a = cls.new
1618
+ a.foo
1619
+ end
1620
+ end
1621
+
1488
1622
  def test_abstract
1489
1623
  abstract_class, concrete_class = compile(<<-EOF)
1490
1624
  abstract class Abstract
@@ -1506,6 +1640,54 @@ class JVMCompilerTest < Test::Unit::TestCase
1506
1640
  end
1507
1641
  end
1508
1642
 
1643
+ def test_synchronized
1644
+ cls, = compile(<<-EOF)
1645
+ class Synchronized
1646
+ attr_accessor locked:boolean
1647
+
1648
+ synchronized def lock_and_unlock:void
1649
+ puts "Locking."
1650
+ Thread.sleep(100)
1651
+ self.locked = true
1652
+ puts "Waiting."
1653
+ self.wait
1654
+ puts "Unlocking."
1655
+ self.locked = false
1656
+ end
1657
+
1658
+ synchronized def locked?
1659
+ self.locked
1660
+ end
1661
+
1662
+ synchronized def notify_synchronized:void
1663
+ puts "Notifying."
1664
+ self.notify
1665
+ puts "Notified."
1666
+ end
1667
+
1668
+ def trigger:void
1669
+ while ! locked?
1670
+ Thread.sleep(10)
1671
+ end
1672
+ self.notify_synchronized
1673
+ end
1674
+
1675
+ def start_trigger
1676
+ s = self
1677
+ Thread.new do
1678
+ s.trigger
1679
+ end.start
1680
+ end
1681
+ end
1682
+ EOF
1683
+
1684
+ assert_output("Locking.\nWaiting.\nNotifying.\nNotified.\nUnlocking.\n") do
1685
+ a = cls.new
1686
+ a.start_trigger
1687
+ a.lock_and_unlock
1688
+ end
1689
+ end
1690
+
1509
1691
  def test_return_void
1510
1692
  script, = compile(<<-EOF)
1511
1693
  def foo:void
@@ -1629,9 +1811,7 @@ class JVMCompilerTest < Test::Unit::TestCase
1629
1811
  puts java::util::Arrays.toString(String[5])
1630
1812
  EOF
1631
1813
 
1632
- assert_output("[null, null, null, null, null]\n") do
1633
- cls.main(nil)
1634
- end
1814
+ assert_run_output("[null, null, null, null, null]\n", cls)
1635
1815
  end
1636
1816
 
1637
1817
  def test_getClass_on_object_array
@@ -1639,9 +1819,7 @@ class JVMCompilerTest < Test::Unit::TestCase
1639
1819
  puts Object[0].getClass.getName
1640
1820
  EOF
1641
1821
 
1642
- assert_output("[Ljava.lang.Object;\n") do
1643
- cls.main(nil)
1644
- end
1822
+ assert_run_output("[Ljava.lang.Object;\n", cls)
1645
1823
  end
1646
1824
 
1647
1825
  def test_nil_assign
@@ -1660,9 +1838,7 @@ class JVMCompilerTest < Test::Unit::TestCase
1660
1838
  puts Object(a)
1661
1839
  EOF
1662
1840
 
1663
- assert_output("null\n") do
1664
- cls.main(nil)
1665
- end
1841
+ assert_run_output("null\n", cls)
1666
1842
  end
1667
1843
 
1668
1844
  def test_long_generation
@@ -1672,6 +1848,41 @@ class JVMCompilerTest < Test::Unit::TestCase
1672
1848
  EOF
1673
1849
  end
1674
1850
 
1851
+
1852
+ def test_inconvertible_classes_cause_cast_error
1853
+ pend "waiting on better error picking" do
1854
+ ex = assert_raise Mirah::MirahError do
1855
+ compile(<<-EOF)
1856
+ class A;end
1857
+ class B;end
1858
+ def f(a:A): B
1859
+ B(a)
1860
+ end
1861
+ EOF
1862
+ end
1863
+ assert_equal("Cannot cast A to B.", ex.message)
1864
+ end
1865
+ end
1866
+
1867
+ def test_inconvertible_default_classes_cause_cast_error
1868
+ pend "waiting on better error picking" do
1869
+ ex = assert_raise Mirah::MirahError do
1870
+ compile(<<-EOF)
1871
+ import java.lang.Integer
1872
+ java.lang.Integer("a string")
1873
+ EOF
1874
+ end
1875
+ assert_equal("Cannot cast java.lang.Integer to java.lang.String.", ex.message)
1876
+ end
1877
+ end
1878
+
1879
+ def test_casting_up_should_work
1880
+ cls, = compile(<<-EOF)
1881
+ puts Object("a string")
1882
+ EOF
1883
+ assert_run_output("a string\n", cls)
1884
+ end
1885
+
1675
1886
  def test_missing_class_with_block_raises_inference_error
1676
1887
  # TODO(ribrdb): What is this test for?
1677
1888
  ex = assert_raise Mirah::MirahError do
@@ -1682,15 +1893,165 @@ class JVMCompilerTest < Test::Unit::TestCase
1682
1893
 
1683
1894
  def test_bool_equality
1684
1895
  cls, = compile("puts true == false")
1685
- assert_output("false\n") do
1686
- cls.main(nil)
1687
- end
1896
+ assert_run_output("false\n", cls)
1688
1897
  end
1689
1898
 
1690
1899
  def test_bool_inequality
1691
1900
  cls, = compile("puts true != false")
1692
- assert_output("true\n") do
1693
- cls.main(nil)
1901
+ assert_run_output("true\n", cls)
1902
+ end
1903
+
1904
+ def test_double_equals_calls_equals_when_first_arg_not_nil
1905
+ cls, = compile(<<-EOF)
1906
+ class DoubleEqualsCalling
1907
+ def equals(other)
1908
+ puts "called"
1909
+ true
1910
+ end
1911
+ end
1912
+ DoubleEqualsCalling.new == nil
1913
+ EOF
1914
+ assert_run_output("called\n", cls)
1915
+ end
1916
+
1917
+ def test_double_equals_does_not_call_equals_when_first_arg_nil
1918
+ cls, = compile(<<-EOF)
1919
+ class DoubleEqualsCalling2
1920
+ def equals(other)
1921
+ puts "called"
1922
+ true
1923
+ end
1924
+ end
1925
+ nil == DoubleEqualsCalling2.new
1926
+ EOF
1927
+ assert_run_output("", cls)
1928
+ end
1929
+
1930
+ def test_double_equals_nil_literal_equals_nil_literal
1931
+ cls, = compile(<<-EOF)
1932
+ puts nil == nil
1933
+ EOF
1934
+ assert_run_output("true\n", cls)
1935
+ end
1936
+
1937
+ def test_double_equals_nil_ref_equals_nil_literal
1938
+ cls, = compile(<<-EOF)
1939
+ a = nil
1940
+ puts a == nil
1941
+ EOF
1942
+ assert_run_output("true\n", cls)
1943
+ end
1944
+
1945
+ def test_double_equals_cast_nil_ref_equals_nil_literal
1946
+ cls, = compile(<<-EOF)
1947
+ a = Object(nil)
1948
+ puts a == nil
1949
+ EOF
1950
+ assert_run_output("true\n", cls)
1951
+ end
1952
+
1953
+ def test_double_equals_compare_to_self_in_a_equals_method_def_has_warning
1954
+ cls = nil
1955
+ output = capture_output do
1956
+ cls, = compile(<<-EOF)
1957
+ class DoubleEqualsSelf
1958
+ def equals(other)
1959
+ other == self
1960
+ end
1961
+ end
1962
+ puts DoubleEqualsSelf.new == nil
1963
+ EOF
1964
+ end
1965
+ assert_include(
1966
+ "WARNING: == is now an alias for Object#equals(), === is now used for identity.\n" +
1967
+ "This use of == with self in equals() definition may cause a stack overflow in next release!",
1968
+ output)
1969
+ assert_run_output("false\n", cls)
1970
+ end
1971
+
1972
+ def test_double_equals_compare_to_self_in_a_equals_method_def_warning_includes_source
1973
+ cls = nil
1974
+ output = capture_output do
1975
+ cls, = compile(<<-EOF)
1976
+ class DoubleEqualsSelf
1977
+ def equals(other)
1978
+ other == self
1979
+ end
1980
+ end
1981
+ puts DoubleEqualsSelf.new == nil
1982
+ EOF
1983
+ end
1984
+ assert_include(
1985
+ " def equals(other)
1986
+ other == self
1987
+ end", output)
1988
+ assert_run_output("false\n", cls)
1989
+ end
1990
+
1991
+ def test_double_equals_self_compare_to_other_in_a_equals_method_def_has_warning
1992
+ cls = nil
1993
+ output = capture_output do
1994
+ cls, = compile(<<-EOF)
1995
+ class SelfDoubleEqualsOther
1996
+ def equals(other)
1997
+ self == other
1998
+ end
1999
+ end
2000
+ puts SelfDoubleEqualsOther.new == nil
2001
+ EOF
2002
+ end
2003
+ assert_include(
2004
+ "WARNING: == is now an alias for Object#equals(), === is now used for identity.\n" +
2005
+ "This use of == with self in equals() definition may cause a stack overflow in next release!",
2006
+ output)
2007
+ assert_run_output("false\n", cls)
2008
+ end
2009
+
2010
+ def test_triple_equals_with_ints
2011
+ cls, = compile(<<-EOF)
2012
+ puts 1 === 1
2013
+ puts 1 === 2
2014
+ puts 1 !== 2
2015
+ puts 1 !== 1
2016
+ EOF
2017
+ assert_run_output("true\nfalse\ntrue\nfalse\n", cls)
2018
+ end
2019
+
2020
+ def test_triple_equals_with_objects
2021
+ cls, = compile(<<-EOF)
2022
+ a = Object.new
2023
+ b = Object.new
2024
+ puts a === a
2025
+ puts a === b
2026
+ puts a !== b
2027
+ puts a !== a
2028
+ EOF
2029
+ assert_run_output("true\nfalse\ntrue\nfalse\n", cls)
2030
+ end
2031
+
2032
+ def test_triple_equals_with_arrays
2033
+ cls, = compile(<<-EOF)
2034
+ a = Object[1]
2035
+ b = Object[1]
2036
+ puts a === a
2037
+ puts a === b
2038
+ puts a !== b
2039
+ puts a !== a
2040
+ EOF
2041
+ assert_run_output("true\nfalse\ntrue\nfalse\n", cls)
2042
+ end
2043
+
2044
+ def test_double_equals_with_arrays
2045
+ pend "arrays are tricky" do
2046
+ cls, = compile(<<-EOF)
2047
+ a = Object[1]
2048
+ b = Object[1]
2049
+ puts a == a
2050
+ puts a == b
2051
+ puts a != b
2052
+ puts a != a
2053
+ EOF
2054
+ assert_run_output("true\true\nfalse\nfalse\n", cls)
1694
2055
  end
1695
2056
  end
1696
2057
 
@@ -1704,7 +2065,7 @@ class JVMCompilerTest < Test::Unit::TestCase
1704
2065
  print "OK"
1705
2066
  EOF
1706
2067
 
1707
- assert_output("OK") { cls.main(nil) }
2068
+ assert_run_output("OK", cls)
1708
2069
  end
1709
2070
 
1710
2071
  def test_assign_int_to_double
@@ -1792,9 +2153,7 @@ class JVMCompilerTest < Test::Unit::TestCase
1792
2153
  puts Child.my_method
1793
2154
  EOF
1794
2155
 
1795
- assert_output "ran my method\n" do
1796
- cls.main(nil)
1797
- end
2156
+ assert_run_output("ran my method\n", cls)
1798
2157
  end
1799
2158
 
1800
2159
  def test_incompatible_meta_change
@@ -1831,6 +2190,44 @@ class JVMCompilerTest < Test::Unit::TestCase
1831
2190
  assert_output("0\n") { cls.foo(nil)}
1832
2191
  assert_output("2\n") { cls.foo(arg.new)}
1833
2192
  end
2193
+
2194
+ def test_local_method_conflict2
2195
+ cls, arg = compile(%q{
2196
+
2197
+ class Foo1
2198
+
2199
+ def equals(o:Foo1)
2200
+ self===o
2201
+ end
2202
+ end
2203
+
2204
+ class Bar
2205
+ attr_reader foo1:Foo1
2206
+ attr_reader foo2:Foo2
2207
+
2208
+ def foo1method(foo1:Foo1)
2209
+ puts (@foo1==foo1)
2210
+ @foo1 = foo1
2211
+ end
2212
+
2213
+ def foo2method(foo2:Foo2)
2214
+ puts (@foo2==foo2)
2215
+ @foo2 = foo2
2216
+ end
2217
+ end
2218
+
2219
+ class Foo2
2220
+
2221
+ def equals(o:Foo2)
2222
+ self===o
2223
+ end
2224
+ end
2225
+
2226
+ Bar.new.foo1method(Foo1.new)
2227
+ Bar.new.foo2method(Foo2.new)
2228
+ })
2229
+ assert_run_output("false\nfalse\n", cls)
2230
+ end
1834
2231
 
1835
2232
  def test_incompatible_return_type_error_message
1836
2233
  e = assert_raise_kind_of Mirah::MirahError do
@@ -1843,4 +2240,132 @@ class JVMCompilerTest < Test::Unit::TestCase
1843
2240
  end
1844
2241
  assert_equal "Invalid return type double, expected int",e.message
1845
2242
  end
2243
+
2244
+ def test_inner_interface
2245
+ cls, arg = compile(%q{
2246
+
2247
+ class Foo1
2248
+
2249
+ interface Bar
2250
+ def baz:String; end
2251
+ end
2252
+
2253
+ class Foo2 implements Bar
2254
+ def baz
2255
+ "BAZ"
2256
+ end
2257
+ end
2258
+ end
2259
+
2260
+
2261
+ puts Foo2.new.baz
2262
+ })
2263
+ assert_run_output("BAZ\n", cls)
2264
+ end
2265
+
2266
+ def test_line_number_increase_by_multiline_sstring_literal
2267
+ e = assert_raise_kind_of Mirah::MirahError do
2268
+ cls, arg = compile(%q{
2269
+ class Foo
2270
+ CONST = 'a
2271
+
2272
+
2273
+
2274
+ b'
2275
+ end
2276
+
2277
+
2278
+ ERROR_SHOULD_BE_HERE
2279
+ })
2280
+ end
2281
+ assert_equal 11,e.diagnostic.getLineNumber
2282
+ end
2283
+
2284
+ def test_line_number_increase_by_multiline_dstring_literal
2285
+ e = assert_raise_kind_of Mirah::MirahError do
2286
+ cls, arg = compile(%q{
2287
+ class Foo
2288
+ CONST = "a
2289
+
2290
+
2291
+
2292
+ b"
2293
+ end
2294
+
2295
+
2296
+ ERROR_SHOULD_BE_HERE
2297
+ })
2298
+ end
2299
+ assert_equal 11,e.diagnostic.getLineNumber
2300
+ end
2301
+
2302
+ def test_late_superclass
2303
+ cls, arg = compile(%q{
2304
+ package subclass_test
2305
+
2306
+ class TestSubclassAsMethodParameter
2307
+ def bar(b:SuperClass)
2308
+ "baz"
2309
+ end
2310
+
2311
+ def foo
2312
+ a = SubClass.new
2313
+ bar(a)
2314
+ end
2315
+ end
2316
+
2317
+ class SubClass < SuperClass
2318
+ end
2319
+
2320
+ class SuperClass
2321
+ end
2322
+
2323
+ puts TestSubclassAsMethodParameter.new.foo
2324
+ })
2325
+ assert_run_output("baz\n", cls)
2326
+ end
2327
+
2328
+ def test_late_superinterface
2329
+ cls, arg = compile(%q{
2330
+ package late_superinterface
2331
+
2332
+ interface Interface2 < Interface1
2333
+ end
2334
+
2335
+ interface Interface3 < Interface2
2336
+ end
2337
+
2338
+ interface Interface1
2339
+ end
2340
+ })
2341
+ end
2342
+
2343
+ def test_init_before_use_in_loop
2344
+ cls, arg = compile(%q{
2345
+ macro def loop_with_init(block:Block)
2346
+ i = block.arguments.required(0).name.identifier
2347
+ last = gensym
2348
+ quote do
2349
+ while `i` < `last`
2350
+ init { `i` = 0; `last` = 4}
2351
+ post { `i` = `i` + 1 }
2352
+ `block.body`
2353
+ end
2354
+ end
2355
+ end
2356
+ loop_with_init do |i|
2357
+ print i
2358
+ end
2359
+ })
2360
+ assert_run_output("0123", cls)
2361
+ end
2362
+
2363
+ def test_filename_shows_up_in_exception_upon_syntax_error
2364
+ begin
2365
+ foo, = compile("puts('foo',)", name: 'somespecificfilename.mirah')
2366
+ assert false
2367
+ rescue => e
2368
+ assert e.message.match(/somespecificfilename/)
2369
+ end
2370
+ end
1846
2371
  end