forked from beeware/voc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_java.py
810 lines (690 loc) · 28.7 KB
/
test_java.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
from unittest import expectedFailure
from ..utils import TranspileTestCase
class JavaTests(TranspileTestCase):
def test_multiple_constructors(self):
"The appropriate constructor for a native Java class can be interpolated from args"
self.assertJavaExecution(
"""
from java.lang import StringBuilder
builder = StringBuilder("Hello, ")
builder.append("world")
print(builder)
print("Done.")
""",
"""
Hello, world
Done.
""", run_in_function=False)
def test_most_specific_constructor(self):
"The most specific constructor for a native Java class will be selected based on argument."
self.assertJavaExecution(
"""
from com.example import MyClass
obj1 = MyClass()
obj2 = MyClass(1.234)
obj3 = MyClass(3742)
print("Done.")
""",
java={
'com/example/MyClass': """
package com.example;
public class MyClass {
public MyClass() {
System.out.println("No argument");
}
public MyClass(int arg) {
System.out.println("Integer argument " + arg);
}
public MyClass(double arg) {
System.out.println("Double argument " + arg);
}
}
"""
},
out="""
No argument
Double argument 1.234
Integer argument 3742
Done.
""", run_in_function=False)
def test_field(self):
"Native fields on an instance can be accessed"
self.assertJavaExecution("""
from com.example import MyClass
print("Class is", MyClass)
obj1 = MyClass()
print("Field is", MyClass.field)
print("Field from instance is", obj1.field)
obj1.field = 37
print("Updated Field from instance is", obj1.field)
print("Done.")
""",
java={
'com/example/MyClass': """
package com.example;
public class MyClass {
public int field = 42;
}
"""
},
out="""
Class is <class 'com.example.MyClass'>
Field is <unbound native field public int com.example.MyClass.field>
Field from instance is 42
Updated Field from instance is 37
Done.
""")
def test_static_field(self):
"Class constants can be accessed"
self.assertJavaExecution("""
from com.example import MyClass
print("Class is", MyClass)
obj1 = MyClass()
print("Static field is", MyClass.static_field)
MyClass.static_field = 37
print("Updated static field is", MyClass.static_field)
print("Static field from instance is", obj1.static_field)
MyClass.static_field = 42
print("Updated static field from instance is", obj1.static_field)
print("Done.")
""",
java={
'com/example/MyClass': """
package com.example;
public class MyClass {
public static int static_field = 42;
}
"""
},
out="""
Class is <class 'com.example.MyClass'>
Static field is 42
Updated static field is 37
Static field from instance is 37
Updated static field from instance is 42
Done.
""")
def test_superclass_field(self):
"Native fields defined on a superclass can be accessed"
self.assertJavaExecution("""
from com.example import MyBase, MyClass
print("Base class is", MyBase)
print("Class is", MyClass)
obj1 = MyClass()
print("Base field on superclass is", MyBase.base_field)
print("Base field is", MyClass.base_field)
print("Base field from instance is", obj1.base_field)
print("Field is", MyClass.field)
print("Field from instance is", obj1.field)
print("Done.")
""",
java={
'com/example/MyBase': """
package com.example;
public class MyBase {
public int base_field = 37;
}
""",
'com/example/MyClass': """
package com.example;
public class MyClass extends MyBase {
public int field = 42;
}
"""
},
out="""
Base class is <class 'com.example.MyBase'>
Class is <class 'com.example.MyClass'>
Base field on superclass is <unbound native field public int com.example.MyBase.base_field>
Base field is <unbound native field public int com.example.MyBase.base_field>
Base field from instance is 37
Field is <unbound native field public int com.example.MyClass.field>
Field from instance is 42
Done.
""")
def test_superclass_static_field(self):
"Native static fields defined on a superclass can be accessed"
self.assertJavaExecution("""
from com.example import MyBase, MyClass
print("Base class is", MyBase)
print("Class is", MyClass)
obj1 = MyClass()
print("Static base field on superclass is", MyBase.base_static_field)
print("Static base field is", MyClass.base_static_field)
print("Static base field from instance is", obj1.base_static_field)
print("Static field is", MyClass.static_field)
print("Static field from instance is", obj1.static_field)
print("Done.")
""",
java={
'com/example/MyBase': """
package com.example;
public class MyBase {
public static int base_static_field = 37;
}
""",
'com/example/MyClass': """
package com.example;
public class MyClass extends MyBase {
public static int static_field = 42;
}
"""
},
out="""
Base class is <class 'com.example.MyBase'>
Class is <class 'com.example.MyClass'>
Static base field on superclass is 37
Static base field is 37
Static base field from instance is 37
Static field is 42
Static field from instance is 42
Done.
""")
def test_constant(self):
"Instance constants can be accessed"
self.assertJavaExecution("""
from com.example import MyClass
print("Class is", MyClass)
obj1 = MyClass()
print("Constant is", MyClass.CONSTANT)
print("Constant from instance is", obj1.CONSTANT)
print("Done.")
""",
java={
'com/example/MyClass': """
package com.example;
public class MyClass {
public final int CONSTANT = 42;
}
"""
},
out="""
Class is <class 'com.example.MyClass'>
Constant is <unbound native field public final int com.example.MyClass.CONSTANT>
Constant from instance is 42
Done.
""")
def test_static_constant(self):
"Class constants can be accessed"
self.assertJavaExecution("""
from com.example import MyClass
print("Class is", MyClass)
obj1 = MyClass()
print("Static constant is", MyClass.STATIC_CONSTANT)
print("Static constant from instance is", obj1.STATIC_CONSTANT)
print("Done.")
""",
java={
'com/example/MyClass': """
package com.example;
public class MyClass {
public static final int STATIC_CONSTANT = 42;
}
"""
},
out="""
Class is <class 'com.example.MyClass'>
Static constant is 42
Static constant from instance is 42
Done.
""")
def test_method(self):
"Native methods on an instance can be accessed"
self.assertJavaExecution("""
from com.example import MyClass
print("Class is", MyClass)
obj = MyClass()
print("Method is", MyClass.method)
print("Method from instance is", obj.method)
obj.method()
print("Done.")
""",
java={
'com/example/MyClass': """
package com.example;
public class MyClass {
public void method() {
System.out.println("Hello from the instance!");
}
}
"""
},
out="""
Class is <class 'com.example.MyClass'>
Method is <native function com.example.MyClass.method>
Method from instance is <bound native method com.example.MyClass.method of <Native com.example.MyClass object at 0xXXXXXXXX>>
Hello from the instance!
Done.
""")
def test_static_method(self):
"Native static methods on an instance can be accessed"
self.assertJavaExecution("""
from com.example import MyClass
print("Class is", MyClass)
obj = MyClass()
print("Static method is", MyClass.method)
MyClass.method()
print("Static method from instance is", obj.method)
obj.method()
print("Done.")
""",
java={
'com/example/MyClass': """
package com.example;
public class MyClass {
public static void method() {
System.out.println("Hello from the class!");
}
}
"""
},
out="""
Class is <class 'com.example.MyClass'>
Static method is <native function com.example.MyClass.method>
Hello from the class!
Static method from instance is <bound native method com.example.MyClass.method of <Native com.example.MyClass object at 0xXXXXXXXX>>
Hello from the class!
Done.
""")
def test_superclass_method(self):
"Native methods defined on a superclass can be accessed"
self.assertJavaExecution("""
from com.example import MyBase, MyClass
print("Base class is", MyBase)
print("Class is", MyClass)
print("Base method on superclass is", MyBase.base_method)
print("Method on superclass is", MyBase.method)
print("Base method is", MyClass.base_method)
print("Method is", MyClass.method)
obj1 = MyBase()
print("Base method from superclass instance is", obj1.base_method)
obj1.base_method()
print("Method from superclass instance is", obj1.method)
obj1.method()
obj2 = MyClass()
print("Base method from instance is", obj2.base_method)
obj2.base_method()
print("Method from instance is", obj2.method)
obj2.method()
print("Done.")
""",
java={
'com/example/MyBase': """
package com.example;
public class MyBase {
public void base_method() {
System.out.println("Hello from the base!");
}
public void method() {
System.out.println("Goodbye from the base!");
}
}
""",
'com/example/MyClass': """
package com.example;
public class MyClass extends MyBase {
public void method() {
System.out.println("Hello from the instance!");
}
}
"""
},
out="""
Base class is <class 'com.example.MyBase'>
Class is <class 'com.example.MyClass'>
Base method on superclass is <native function com.example.MyBase.base_method>
Method on superclass is <native function com.example.MyBase.method>
Base method is <native function com.example.MyBase.base_method>
Method is <native function com.example.MyClass.method>
Base method from superclass instance is <bound native method com.example.MyBase.base_method of <Native com.example.MyBase object at 0xXXXXXXXX>>
Hello from the base!
Method from superclass instance is <bound native method com.example.MyBase.method of <Native com.example.MyBase object at 0xXXXXXXXX>>
Goodbye from the base!
Base method from instance is <bound native method com.example.MyBase.base_method of <Native com.example.MyClass object at 0xXXXXXXXX>>
Hello from the base!
Method from instance is <bound native method com.example.MyClass.method of <Native com.example.MyClass object at 0xXXXXXXXX>>
Hello from the instance!
Done.
""")
def test_superclass_static_method(self):
"Native static methods defined on a superclass can be accessed"
self.assertJavaExecution("""
from com.example import MyBase, MyClass
print("Base class is", MyBase)
print("Class is", MyClass)
print("Static base method on superclass is", MyBase.base_static_method)
MyBase.base_static_method()
print("Static method on superclass is", MyBase.static_method)
MyBase.static_method()
print("Static base method is", MyClass.base_static_method)
MyClass.base_static_method()
print("Static method is", MyClass.static_method)
MyClass.static_method()
obj1 = MyBase()
print("Base static method from superclass instance is", obj1.base_static_method)
obj1.base_static_method()
print("Static method from superclass instance is", obj1.static_method)
obj1.static_method()
obj2 = MyClass()
print("Base static method from instance is", obj2.base_static_method)
obj2.base_static_method()
print("Static method from instance is", obj2.static_method)
obj2.static_method()
print("Done.")
""",
java={
'com/example/MyBase': """
package com.example;
public class MyBase {
public static void base_static_method() {
System.out.println("Hello from the base!");
}
public static void static_method() {
System.out.println("Goodbye from the base!");
}
}
""",
'com/example/MyClass': """
package com.example;
public class MyClass extends MyBase {
public static void static_method() {
System.out.println("Hello from the class!");
}
}
"""
},
out="""
Base class is <class 'com.example.MyBase'>
Class is <class 'com.example.MyClass'>
Static base method on superclass is <native function com.example.MyBase.base_static_method>
Hello from the base!
Static method on superclass is <native function com.example.MyBase.static_method>
Goodbye from the base!
Static base method is <native function com.example.MyBase.base_static_method>
Hello from the base!
Static method is <native function com.example.MyClass.static_method>
Hello from the class!
Base static method from superclass instance is <bound native method com.example.MyBase.base_static_method of <Native com.example.MyBase object at 0xXXXXXXXX>>
Hello from the base!
Static method from superclass instance is <bound native method com.example.MyBase.static_method of <Native com.example.MyBase object at 0xXXXXXXXX>>
Goodbye from the base!
Base static method from instance is <bound native method com.example.MyBase.base_static_method of <Native com.example.MyClass object at 0xXXXXXXXX>>
Hello from the base!
Static method from instance is <bound native method com.example.MyClass.static_method of <Native com.example.MyClass object at 0xXXXXXXXX>>
Hello from the class!
Done.
""")
def test_inner_class_constant(self):
"Constants on an inner class can be accessed"
self.assertJavaExecution("""
from com.example import OuterClass
print("Outer class is", OuterClass)
print("Outer constant is", OuterClass.OUTER_CONSTANT)
print("Inner class is", OuterClass.InnerClass)
print("Inner constant is", OuterClass.InnerClass.INNER_CONSTANT)
print("Done.")
""",
java={
'com/example/OuterClass': """
package com.example;
public class OuterClass {
public static final int OUTER_CONSTANT = 42;
public static class InnerClass {
public static final int INNER_CONSTANT = 37;
}
}
"""
},
out="""
Outer class is <class 'com.example.OuterClass'>
Outer constant is 42
Inner class is <class 'com.example.OuterClass$InnerClass'>
Inner constant is 37
Done.
""")
def test_inner_class_method(self):
"Inner classes can be instantiated, and methods invoked"
self.assertJavaExecution("""
from com.example import OuterClass
print("Outer class is", OuterClass)
obj1 = OuterClass()
obj1.method()
print("Inner class is", OuterClass.InnerClass)
obj2 = OuterClass.InnerClass(obj1)
obj2.method()
print("Done.")
""",
java={
'com/example/OuterClass': """
package com.example;
public class OuterClass {
public class InnerClass {
public void method() {
System.out.println("Hello from the inside!");
}
}
public void method() {
System.out.println("Hello from the outside!");
}
}
"""
},
out="""
Outer class is <class 'com.example.OuterClass'>
Hello from the outside!
Inner class is <class 'com.example.OuterClass$InnerClass'>
Hello from the inside!
Done.
""")
def test_static_inner_class_constant(self):
"Constants on a static inner class can be accessed"
self.assertJavaExecution("""
from com.example import OuterClass
print("Outer class is", OuterClass)
print("Outer constant is", OuterClass.OUTER_CONSTANT)
print("Inner class is", OuterClass.InnerClass)
print("Inner constant is", OuterClass.InnerClass.INNER_CONSTANT)
print("Done.")
""",
java={
'com/example/OuterClass': """
package com.example;
public class OuterClass {
public static final int OUTER_CONSTANT = 42;
public static class InnerClass {
public static final int INNER_CONSTANT = 37;
}
}
"""
},
out="""
Outer class is <class 'com.example.OuterClass'>
Outer constant is 42
Inner class is <class 'com.example.OuterClass$InnerClass'>
Inner constant is 37
Done.
""")
def test_static_inner_class_method(self):
"Static inner classes can be instantiated, and methods invoked"
self.assertJavaExecution("""
from com.example import OuterClass
print("Outer class is", OuterClass)
obj1 = OuterClass()
obj1.method()
print("Inner class is", OuterClass.InnerClass)
obj2 = OuterClass.InnerClass()
obj2.method()
print("Done.")
""",
java={
'com/example/OuterClass': """
package com.example;
public class OuterClass {
public static class InnerClass {
public void method() {
System.out.println("Hello from the inside!");
}
}
public void method() {
System.out.println("Hello from the outside!");
}
}
"""
},
out="""
Outer class is <class 'com.example.OuterClass'>
Hello from the outside!
Inner class is <class 'com.example.OuterClass$InnerClass'>
Hello from the inside!
Done.
""")
def test_primitive_conversion(self):
"Primitive types are converted correctly"
self.assertJavaExecution("""
from com.example import MyObject
obj = MyObject()
result = obj.method(3)
print('Result is', result)
result = obj.method(3.14159)
print('Result is', result)
result = obj.method(True)
print('Result is', result)
print("Done.")
""",
java={
'com/example/MyObject': """
package com.example;
public class MyObject {
public int method(int x) {
System.out.println("Hello from int method: " + x);
return x * 2;
}
public float method(float x) {
System.out.println("Hello from float method: " + x);
return x * 2.5f;
}
public boolean method(boolean x) {
System.out.println("Hello from boolean method: " + x);
return !x;
}
}
"""
},
out="""
Hello from int method: 3
Result is 6
Hello from float method: 3.14159
Result is 7.853975296020508
Hello from boolean method: true
Result is False
Done.
""")
def test_primitive_zero_conversion(self):
"Primitive representations of 'false' values are converted correctly"
self.assertJavaExecution("""
from com.example import MyObject
obj = MyObject()
result = obj.method(0)
print('Result is', result)
result = obj.method(0.0)
print('Result is', result)
result = obj.method(False)
print('Result is', result)
print("Done.")
""",
java={
'com/example/MyObject': """
package com.example;
public class MyObject {
public int method(int x) {
System.out.println("Hello from int method: " + x);
return x * 2;
}
public float method(float x) {
System.out.println("Hello from float method: " + x);
return x * 2.5f;
}
public boolean method(boolean x) {
System.out.println("Hello from boolean method: " + x);
return !x;
}
}
"""
},
out="""
Hello from int method: 0
Result is 0
Hello from float method: 0.0
Result is 0.0
Hello from boolean method: false
Result is True
Done.
""")
def test_attribute_on_reference(self):
self.assertJavaExecution("""
from com.example import BaseObject, Controller
class TestObject(extends=com.example.BaseObject):
def __init__(self, v1, v2):
# super().__init__(v1)
self.extra1 = v2
def process(self) -> None:
print("Processed Base data is %s" % self.base)
print("Processed Extra data 1 is %s" % self.extra1)
print("Processed Extra data 2 is %s" % self.extra2)
obj = TestObject(37, 99)
obj.extra2 = 42
print("Base data is %s" % obj.base)
print("Extra data 1 is %s" % obj.extra1)
print("Extra data 2 is %s" % obj.extra2)
controller = Controller()
controller.setObject(obj)
controller.poke()
print("Done.")
""",
java={
'com/example/ObjIFace': """
package com.example;
public interface ObjIFace {
public void process();
}
""",
'com/example/Controller': """
package com.example;
public class Controller {
public ObjIFace obj;
public Controller() {
}
public void setObject(ObjIFace o) {
this.obj = o;
}
public void poke() {
this.obj.process();
}
}
""",
'com/example/BaseObject': """
package com.example;
public class BaseObject implements com.example.ObjIFace {
public int base;
public BaseObject() {
base = 1;
}
public BaseObject(int v) {
base = v;
}
public void process() {
System.out.println("Do nothing...");
}
}
"""
},
out="""
Base data is 1
Extra data 1 is 99
Extra data 2 is 42
Processed Base data is 1
Processed Extra data 1 is 99
Processed Extra data 2 is 42
Done.
""",
run_in_function=False)