mirah 0.1.3-java → 0.1.4-java

Sign up to get free protection for your applications and to get access to all the features.
Binary file
@@ -0,0 +1,8 @@
1
+ Ant support
2
+ =============
3
+
4
+ mirahc.jar includes a simple ant compile script. `example-build.xml` has an example usage.
5
+
6
+ Try it out by compiling mirah, then running:
7
+
8
+ $ ant -lib ../../dist compile
@@ -1,7 +1,7 @@
1
1
  <project name="test">
2
- <taskdef name="dubyc" classname="org.mirah.ant.Compile"/>
2
+ <taskdef name="mirahc" classname="org.mirah.ant.Compile"/>
3
3
 
4
- <target name="blah">
5
- <dubyc src="examples/fib.duby" target="build"/>
4
+ <target name="compile">
5
+ <mirahc src="../fib.mirah" target="build"/>
6
6
  </target>
7
7
  </project>
@@ -0,0 +1,7 @@
1
+ <project name="test">
2
+ <taskdef name="dubyc" classname="org.mirah.ant.Compile"/>
3
+
4
+ <target name="blah">
5
+ <dubyc src="examples/fib.duby" target="build"/>
6
+ </target>
7
+ </project>
@@ -47,8 +47,8 @@ end
47
47
 
48
48
  1.upto(100) do |multiplier|
49
49
  index = 0
50
- doors.each do |door|
51
- Door(door).toggle if (index+1)%multiplier == 0
50
+ doors.each do |door: Door|
51
+ door.toggle if (index+1)%multiplier == 0
52
52
  index += 1
53
53
  end
54
54
  end
@@ -18,5 +18,5 @@ import java.util.ArrayList
18
18
 
19
19
  list = ArrayList.new [9,5,2,6,8,5,0,3,6,1,8,3,6,4,7,5,0,8,5,6,7,2,3]
20
20
  puts "unsorted: #{list}"
21
- Collections.sort(list) {|a,b| Integer(a).compareTo(Integer(b))}
21
+ Collections.sort(list) {|a: Integer, b: Integer| a.compareTo(b)}
22
22
  puts "sorted: #{list}"
@@ -26,8 +26,6 @@ require 'mirah/typer'
26
26
 
27
27
  require "mirah/util/process_errors"
28
28
  require "mirah/util/logging"
29
- require "mirah/util/class_loader"
30
-
31
29
 
32
30
  module Mirah
33
31
  java_import 'org.mirah.tool.RunCommand'
@@ -14,5 +14,5 @@
14
14
  # limitations under the License.
15
15
 
16
16
  module Mirah
17
- VERSION = "0.1.3"
17
+ VERSION = "0.1.4"
18
18
  end
@@ -23,7 +23,7 @@ class ClassLoaderTest < Test::Unit::TestCase
23
23
 
24
24
  def test_mirah_class_loader_find_class_in_map_successful
25
25
  class_map = {
26
- 'org.foo.A' => Mirah::Util.binary_string(File.open(A_CLASS, 'rb') {|f| f.read })
26
+ 'org.foo.A' => File.open(A_CLASS, 'rb') {|f| java.lang.String.new f.read.to_java_bytes, "ISO-8859-1" }
27
27
  }
28
28
  class_loader = MirahClassLoader.new nil, class_map
29
29
  cls = class_loader.load_class 'org.foo.A'
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2013 The Mirah project authors. All Rights Reserved.
1
+ # Copyright (c) 2013-2014 The Mirah project authors. All Rights Reserved.
2
2
  # All contributing project authors may be found in the NOTICE file.
3
3
  #
4
4
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -14,7 +14,7 @@
14
14
  # limitations under the License.
15
15
  require 'test_helper'
16
16
 
17
- class ArgumentProcessorTest < Test::Unit::TestCase
17
+ class MirahArgumentsTest < Test::Unit::TestCase
18
18
  java_import 'org.mirah.tool.MirahArguments'
19
19
  def test_arg_dash_v_prints_version_and_has_exit_0
20
20
  arg_processor = MirahArguments.new
@@ -54,5 +54,24 @@ class AnnotationsTest < Test::Unit::TestCase
54
54
  cls.main nil
55
55
  end
56
56
  end
57
+ def test_annotation_from_constant
58
+ return
59
+ cls, = compile(<<-EOF)
60
+ import org.foo.IntAnno
61
+ class IntValAnnotation
62
+ Value = 1
63
+ $IntAnno[name: "bar", value: Value]
64
+ def bar
65
+ end
66
+ end
67
+ method = IntValAnnotation.class.getMethod("bar")
68
+ anno = method.getAnnotation(IntAnno.class)
69
+ puts anno.value
70
+ EOF
71
+
72
+ assert_output "1\n" do
73
+ cls.main nil
74
+ end
75
+ end
57
76
 
58
77
  end
@@ -20,7 +20,7 @@ class BlocksTest < Test::Unit::TestCase
20
20
  parse_and_resolve_types name, code
21
21
  end
22
22
 
23
- #this should probably be a core test
23
+ # this should probably be a core test
24
24
  def test_empty_block_parses_and_types_without_error
25
25
  assert_nothing_raised do
26
26
  parse_and_type(<<-CODE)
@@ -47,7 +47,7 @@ class BlocksTest < Test::Unit::TestCase
47
47
 
48
48
  class NotEmptyAccepter
49
49
  def initialize; end
50
- def foo(a:Bar)
50
+ def foo(a: Bar)
51
51
  1
52
52
  end
53
53
  end
@@ -136,7 +136,7 @@ class BlocksTest < Test::Unit::TestCase
136
136
  x.run
137
137
  end
138
138
  def foo a: int
139
- run {a += 1}
139
+ run { a += 1 }
140
140
  a
141
141
  end
142
142
  EOF
@@ -202,16 +202,11 @@ class BlocksTest < Test::Unit::TestCase
202
202
  end
203
203
 
204
204
  def test_parameter_used_in_block
205
- cls, = compile(<<-EOF)
206
- def foo(x:String):void
207
- thread = Thread.new do
208
- puts "Hello \#{x}"
209
- end
210
- begin
211
- thread.run
212
- thread.join
213
- rescue
214
- puts "Uh Oh!"
205
+ cls, = compile(<<-'EOF')
206
+ def r(r:Runnable); r.run; end
207
+ def foo(x: String): void
208
+ r do
209
+ puts "Hello #{x}"
215
210
  end
216
211
  end
217
212
 
@@ -239,23 +234,12 @@ class BlocksTest < Test::Unit::TestCase
239
234
  end
240
235
  end
241
236
 
242
- def assert_jraise(klass)
243
- assert_block("#{klass} expected, but none thrown") do
244
- begin
245
- yield
246
- rescue klass
247
- break
248
- end
249
- false
250
- end
251
- end
252
-
253
237
  def test_block_impling_interface_w_multiple_methods
254
238
  begin
255
239
  parse_and_type(<<-CODE)
256
240
  interface RunOrRun2 do
257
241
  def run:void;end
258
- def run2:void;end;
242
+ def run2:void;end
259
243
  end
260
244
 
261
245
  class RunOrRun2Fooer
@@ -295,7 +279,7 @@ class BlocksTest < Test::Unit::TestCase
295
279
  end
296
280
 
297
281
  def test_block_with_too_many_params
298
- assert_raises Mirah::MirahError do
282
+ exception = assert_raises Mirah::MirahError do
299
283
  parse_and_type(<<-CODE)
300
284
  interface SingleArgMethod do
301
285
  def run(a:String):void;end
@@ -310,10 +294,35 @@ class BlocksTest < Test::Unit::TestCase
310
294
  1
311
295
  end
312
296
  CODE
313
- end
297
+ end
298
+ assert_equal 'Does not override a method from a supertype.',
299
+ exception.message
300
+ end
301
+
302
+ def test_call_with_block_assigned_to_macro
303
+ #cls, = with_finest_logging{compile(<<-CODE)}
304
+ cls, = compile(<<-CODE)
305
+ class S
306
+ def initialize(run: Runnable)
307
+ run.run
308
+ end
309
+ def toString
310
+ "1"
311
+ end
312
+ end
313
+ if true
314
+ a = {}
315
+ a["wut"]= b = S.new { puts "hey" }
316
+ puts a
317
+ puts b
318
+ end
319
+ CODE
320
+ assert_output "hey\n{wut=1}\n1\n" do
321
+ cls.main(nil)
322
+ end
314
323
  end
315
324
 
316
- def test_closure_in_closure_doesnt_raise_error
325
+ def test_nested_closure_in_closure_doesnt_raise_error
317
326
  cls, = compile(<<-CODE)
318
327
  interface BarRunner do;def run:void;end;end
319
328
 
@@ -334,6 +343,32 @@ class BlocksTest < Test::Unit::TestCase
334
343
  end
335
344
  end
336
345
 
346
+ def test_nested_closure_with_var_from_outer_closure
347
+ pend "bindings in nested closures are incorrectly generated" do
348
+ #cls, = with_finest_logging{compile(<<-'CODE')}
349
+ cls, = compile(<<-'CODE')
350
+ interface BarRunner do;def run:void;end;end
351
+
352
+ class Nestable
353
+ def foo(a:BarRunner)
354
+ a.run
355
+ end
356
+ end
357
+ Nestable.new.foo do
358
+ c = "closure"
359
+ puts "first #{c}"
360
+ Nestable.new.foo do
361
+ puts "second #{c}"
362
+ end
363
+ end
364
+ CODE
365
+ assert_output "first closure\nsecond closure\n" do
366
+ cls.main(nil)
367
+ end
368
+ end
369
+ end
370
+
371
+
337
372
  def test_method_requiring_subclass_of_abstract_class_finds_abstract_method
338
373
  cls, = compile(<<-EOF)
339
374
  import java.io.OutputStream
@@ -436,7 +471,6 @@ class BlocksTest < Test::Unit::TestCase
436
471
  end
437
472
 
438
473
  def test_when_non_local_return_types_incompatible_has_error
439
- pend "nlr doesnt work right now" do
440
474
  error = assert_raises Mirah::MirahError do
441
475
  parse_and_type(<<-CODE)
442
476
  class NonLocalMe
@@ -446,13 +480,15 @@ class BlocksTest < Test::Unit::TestCase
446
480
  end
447
481
  end
448
482
  def nlr: int
449
- NonLocalMe.new.foo { return "not an int"}
483
+ NonLocalMe.new.foo { return "not an int" }
450
484
  5678
451
485
  end
452
486
 
453
487
  CODE
454
488
  end
455
- assert error.message.include? 'int'
489
+ pend "differing type signatures for nlr" do
490
+ # could be better, if future knew it was a return type
491
+ assert_equal "Invalid return type java.lang.String, expected int", error.message
456
492
  end
457
493
  end
458
494
 
@@ -512,7 +548,6 @@ class BlocksTest < Test::Unit::TestCase
512
548
  end
513
549
  ClosureInVoidMethodInClass.new.nlr
514
550
  EOF
515
- puts "before running"
516
551
  assert_output "before\n" do
517
552
  cls.main(nil)
518
553
  end
@@ -592,6 +627,56 @@ class BlocksTest < Test::Unit::TestCase
592
627
  end
593
628
 
594
629
 
630
+ def test_method_returning_init_call_with_closure
631
+ cls, = compile(<<-EOF)
632
+ class InitWithRunnable
633
+ def initialize(a: Runnable)
634
+ a.run
635
+ end
636
+ def finish
637
+ "finish"
638
+ end
639
+ end
640
+ class Huh
641
+ def wut(i: InitWithRunnable)
642
+ puts i.finish
643
+ end
644
+ def regular
645
+ InitWithRunnable.new { puts "Closure!"; nil }
646
+ end
647
+ end
648
+ Huh.new.wut(Huh.new.regular)
649
+ EOF
650
+
651
+ assert_output "Closure!\nfinish\n" do
652
+ cls.main(nil)
653
+ end
654
+ end
655
+
656
+
657
+ def test_closure_with_or
658
+ cls, = with_finest_logging{compile(<<-EOF)}
659
+ def r(run: Runnable) run.run; end
660
+ r { puts "a" || "b"}
661
+ EOF
662
+
663
+ assert_output "a\n" do
664
+ cls.main(nil)
665
+ end
666
+ end
667
+
668
+ def test_closure_with_or_ii
669
+ cls, = with_finest_logging{compile(<<-EOF)}
670
+ interface C; def c(): String;end;end
671
+ def r(cee: C) puts cee.c; end
672
+ r { "a" || "b"}
673
+ EOF
674
+
675
+ assert_output "a\n" do
676
+ cls.main(nil)
677
+ end
678
+ end
679
+
595
680
  def test_two_closures_in_the_same_method
596
681
  cls, = compile(<<-EOF)
597
682
  def foo(a: Runnable)
@@ -609,6 +694,87 @@ class BlocksTest < Test::Unit::TestCase
609
694
  end
610
695
  end
611
696
 
697
+ def test_closures_in_a_rescue
698
+ cls, = compile(<<-EOF)
699
+ def foo(a: Runnable)
700
+ a.run
701
+ end
702
+ def regular: String
703
+ begin
704
+ foo { puts "Closure!" }
705
+ raise "wut"
706
+ rescue
707
+ foo { puts "We Want it" }
708
+ end
709
+ "finish"
710
+ end
711
+ regular
712
+ EOF
713
+ assert_output "Closure!\nWe Want it\n" do
714
+ cls.main(nil)
715
+ end
716
+ end
717
+
718
+ def test_lambda_with_type_defined_before
719
+ cls, = compile(<<-EOF)
720
+ interface Fooable
721
+ def foo: void; end
722
+ end
723
+ x = lambda(Fooable) { puts "hey you" }
724
+ x.foo
725
+ EOF
726
+ assert_output "hey you\n" do
727
+ cls.main(nil)
728
+ end
729
+ end
730
+
731
+ def test_lambda_with_type_defined_later
732
+ pend "I think it'd be nice if this worked" do
733
+ cls, = compile(<<-EOF)
734
+ x = lambda(Fooable) { puts "hey you" }
735
+ interface Fooable
736
+ def foo: void; end
737
+ end
738
+ x.foo
739
+ EOF
740
+ assert_output "hey you\n" do
741
+ cls.main(nil)
742
+ end
743
+ end
744
+ end
745
+
746
+ def test_closure_with_primitive_array_param
747
+ cls, = compile(<<-EOF)
748
+ interface Byter
749
+ def byteme(bytes: byte[]): void; end
750
+ end
751
+ def r b: Byter
752
+ b.byteme "yay".getBytes
753
+ end
754
+ r {|b| puts String.new(b) }
755
+ EOF
756
+ assert_output "yay\n" do
757
+ cls.main(nil)
758
+ end
759
+ end
760
+
761
+
762
+ def test_lambda_closure
763
+ pend "not working yet" do
764
+ cls, = compile(<<-EOF)
765
+ def r b: Runnable
766
+ b.run
767
+ end
768
+ msg = "yay"
769
+ l = lambda(Runnable) { puts msg }
770
+ r l
771
+ EOF
772
+ assert_output "yay\n" do
773
+ cls.main(nil)
774
+ end
775
+ end
776
+ end
777
+
612
778
  # nested nlr scopes
613
779
 
614
780
  # works with script as end
@@ -62,7 +62,9 @@ class ImportTest < Test::Unit::TestCase
62
62
  list = cls.list(o)
63
63
  assert_kind_of(Java::JavaUtil::List, list)
64
64
  assert_equal(["1", "2", "3"], list.to_a)
65
+ end
65
66
 
67
+ def test_static_import_nested_in_class
66
68
  cls, = compile(<<-EOF)
67
69
  import java.util.Arrays
68
70
  class StaticImports
@@ -73,6 +75,7 @@ class ImportTest < Test::Unit::TestCase
73
75
  end
74
76
  EOF
75
77
 
78
+ o = ["1", "2", "3"].to_java(:object)
76
79
  list = cls.new.list(o)
77
80
  assert_kind_of(Java::JavaUtil::List, list)
78
81
  assert_equal(["1", "2", "3"], list.to_a)