|
| 1 | +#!/usr/bin/env perl6 |
| 2 | + |
| 3 | +use v6; |
| 4 | +use Inline::Python; |
| 5 | + |
| 6 | +say "1..12"; |
| 7 | + |
| 8 | +my $py = Inline::Python.new(); |
| 9 | +say $py.run(' |
| 10 | +
|
| 11 | +def test(): |
| 12 | + print "ok 1 - executing a parameterless function without return value"; |
| 13 | + return; |
| 14 | +
|
| 15 | +def test_int_params(a, b): |
| 16 | + if a == 2 and b == 2: |
| 17 | + print "ok 2 - int params"; |
| 18 | + else: |
| 19 | + print "not ok 2 - int params"; |
| 20 | + return; |
| 21 | +
|
| 22 | +def test_str_params(a, b): |
| 23 | + if a == "Hello" and b == "Python": |
| 24 | + print "ok 3 - str params"; |
| 25 | + else: |
| 26 | + print "not ok 3 - str params"; |
| 27 | + return; |
| 28 | +
|
| 29 | +def test_int_retval(): |
| 30 | + return 1; |
| 31 | +
|
| 32 | +def test_int_retvals(): |
| 33 | + return 3, 1, 2; |
| 34 | +
|
| 35 | +def test_str_retval(): |
| 36 | + return "Hello Perl 6!"; |
| 37 | +
|
| 38 | +def test_mixed_retvals(): |
| 39 | + return ("Hello", "Perl", 6); |
| 40 | +
|
| 41 | +def test_none(undef): |
| 42 | + return undef is None; |
| 43 | +
|
| 44 | +import types |
| 45 | +def test_hash(h): |
| 46 | + return ( |
| 47 | + isinstance(h, types.DictType) |
| 48 | + and len(h.keys()) == 2 |
| 49 | + and "a" in h |
| 50 | + and "b" in h |
| 51 | + and h["a"] == 2 |
| 52 | + and isinstance(h["b"], types.DictType) |
| 53 | + and isinstance(h["b"]["c"], types.ArrayType) |
| 54 | + and len(h["b"]["c"]) == 2 |
| 55 | + and h["b"]["c"][0] == 4 |
| 56 | + and h["b"]["c"][1] == 3 |
| 57 | + ) |
| 58 | +
|
| 59 | +def test_foo(foo): |
| 60 | + return foo.test(); |
| 61 | +
|
| 62 | +class Foo: |
| 63 | + def __init__(self, val): |
| 64 | + self.val = val |
| 65 | +
|
| 66 | + def test(self): |
| 67 | + return self.val |
| 68 | +
|
| 69 | + def sum(a, b): |
| 70 | + return a + b |
| 71 | +'); |
| 72 | + |
| 73 | +$py.call('__main__', 'test'); |
| 74 | +$py.call('__main__', 'test_int_params', 2, 1); |
| 75 | +$py.call('__main__', 'test_str_params', 'Hello', 'Perl 5'); |
| 76 | +if ($py.call('__main__', 'test_int_retval') == 1) { |
| 77 | + say "ok 4 - return one int"; |
| 78 | +} |
| 79 | +else { |
| 80 | + say "not ok 4 - return one int"; |
| 81 | +} |
| 82 | +my @retvals = $py.call('__main__', 'test_int_retvals'); |
| 83 | +if (@retvals == 3 and @retvals[0] == 3 and @retvals[1] == 1 and @retvals[2] == 2) { |
| 84 | + say "ok 5 - return one int"; |
| 85 | +} |
| 86 | +else { |
| 87 | + say "not ok 5 - return one int"; |
| 88 | + say " got: {@retvals}"; |
| 89 | + say " expected: 3, 1, 2"; |
| 90 | +} |
| 91 | +if ($py.call('__main__', 'test_str_retval') eq 'Hello Perl 6!') { |
| 92 | + say "ok 6 - return one string"; |
| 93 | +} |
| 94 | +else { |
| 95 | + say "not ok 6 - return one string"; |
| 96 | +} |
| 97 | +@retvals = $py.call('__main__', 'test_mixed_retvals'); |
| 98 | +if (@retvals == 3 and @retvals[0] eq 'Hello' and @retvals[1] eq 'Perl' and @retvals[2] == 6) { |
| 99 | + say "ok 7 - return mixed values"; |
| 100 | +} |
| 101 | +else { |
| 102 | + say "not ok 7 - return mixed values"; |
| 103 | + say " got: {@retvals}"; |
| 104 | + say " expected: 'Hello', 'Perl', 6"; |
| 105 | +} |
| 106 | + |
| 107 | +if ($py.call('__main__', 'Foo::new', 'Foo', 1).test() == 1) { |
| 108 | + say "ok 8 - Perl 5 method call"; |
| 109 | +} |
| 110 | +else { |
| 111 | + say "not ok 8 - Perl 5 method call"; |
| 112 | +} |
| 113 | + |
| 114 | +if ($py.call('__main__', 'Foo::new', 'Foo', 1).sum(3, 1) == 4) { |
| 115 | + say "ok 9 - Perl 5 method call with parameters"; |
| 116 | +} |
| 117 | +else { |
| 118 | + say "not ok 9 - Perl 5 method call with parameters"; |
| 119 | +} |
| 120 | + |
| 121 | +if ($py.call('__main__', 'test_none', Any) == 1) { |
| 122 | + say "ok 10 - Any converted to undef"; |
| 123 | +} |
| 124 | +else { |
| 125 | + say "not ok 10 - Any converted to undef"; |
| 126 | +} |
| 127 | + |
| 128 | +if ($py.call('__main__', 'test_hash', {a => 2, b => {c => [4, 3]}}) == 1) { |
| 129 | + say "ok 11 - Passing hashes to Perl 5"; |
| 130 | +} |
| 131 | +else { |
| 132 | + say "not ok 11 - Passing hashes to Perl 5"; |
| 133 | +} |
| 134 | + |
| 135 | +if ($py.call('__main__', 'test_foo', $py.call('__main__', 'Foo', 6)) == 6) { |
| 136 | + say "ok 12 - Passing Perl 5 objects back from Perl 6"; |
| 137 | +} |
| 138 | +else { |
| 139 | + say "not ok 12 - Passing Perl 5 objects back from Perl 6"; |
| 140 | +} |
| 141 | + |
| 142 | +# vim: ft=perl6 |
0 commit comments