|
| 1 | +plan(39); |
| 2 | + |
| 3 | +sub one_arg_sub($arg) { |
| 4 | +} |
| 5 | + |
| 6 | +dies-ok({ one_arg_sub('first arg', 'second arg')}, 'sub with too many arguments dies'); |
| 7 | + |
| 8 | +dies-ok({ one_arg_sub()}, 'sub with not enough arguments dies'); |
| 9 | + |
| 10 | +my sub int_arg(int $arg) { |
| 11 | + $arg; |
| 12 | +} |
| 13 | +my sub num_arg(num $arg) { |
| 14 | + $arg; |
| 15 | +} |
| 16 | +my sub str_arg(str $arg) { |
| 17 | + $arg; |
| 18 | +} |
| 19 | + |
| 20 | +dies-ok({int_arg('foo')}, "can't pass str to an int arg"); |
| 21 | +dies-ok({int_arg(123.3)}, "can't pass num to an int arg"); |
| 22 | +dies-ok({int_arg(nqp::list())}, "can't pass obj to an int arg"); |
| 23 | +is(int_arg(123), '123', "can pass int to an int arg"); |
| 24 | + |
| 25 | +dies-ok({num_arg('foo')}, "can't pass str to an num arg"); |
| 26 | +dies-ok({num_arg(123)}, "can't pass int to an int arg"); |
| 27 | +dies-ok({num_arg(nqp::list())}, "can't pass obj to an num arg"); |
| 28 | +is(num_arg(123.1), '123.1', "can pass num to an num arg"); |
| 29 | + |
| 30 | +dies-ok({str_arg(123)}, "can't pass int to an str arg"); |
| 31 | +dies-ok({str_arg(123.3)}, "can't pass num to an str arg"); |
| 32 | +dies-ok({str_arg(nqp::list())}, "can't pass obj to an str arg"); |
| 33 | +is(str_arg('foo'), 'foo', "can pass str to an str arg"); |
| 34 | + |
| 35 | +my class BoxedInt is repr('P6int') { |
| 36 | +} |
| 37 | +my class BoxedStr is repr('P6str') { |
| 38 | +} |
| 39 | +my class BoxedNum is repr('P6num') { |
| 40 | +} |
| 41 | + |
| 42 | +my $boxed_int := nqp::box_i(4, BoxedInt); |
| 43 | +my $boxed_num := nqp::box_n(123.1, BoxedNum); |
| 44 | +my $boxed_str := nqp::box_s('foo', BoxedStr); |
| 45 | + |
| 46 | +dies-ok({int_arg($boxed_str)}, "can't pass a boxed str to an int arg"); |
| 47 | +dies-ok({int_arg($boxed_num)}, "can't pass a boxed num to an int arg"); |
| 48 | +is(int_arg($boxed_int), '4', "can pass a boxed int to an int arg"); |
| 49 | + |
| 50 | +dies-ok({num_arg($boxed_str)}, "can't pass a boxed str to an num arg"); |
| 51 | +dies-ok({num_arg($boxed_int)}, "can't pass a boxed int to an int arg"); |
| 52 | +is(num_arg($boxed_num), '123.1', "can pass a boxed num to an num arg"); |
| 53 | + |
| 54 | +dies-ok({str_arg($boxed_int)}, "can't pass a boxed int to an str arg"); |
| 55 | +dies-ok({str_arg($boxed_num)}, "can't pass a boxed num to an str arg"); |
| 56 | +is(str_arg($boxed_str), 'foo', "can pass a boxed str to an str arg"); |
| 57 | + |
| 58 | + |
| 59 | +my sub int_named_arg(int :$arg) { |
| 60 | + $arg; |
| 61 | +} |
| 62 | +my sub num_named_arg(num :$arg) { |
| 63 | + $arg; |
| 64 | +} |
| 65 | +my sub str_named_arg(str :$arg) { |
| 66 | + $arg; |
| 67 | +} |
| 68 | + |
| 69 | +dies-ok({int_named_arg(:arg('foo'))}, "can't pass str to an int named arg"); |
| 70 | +dies-ok({int_named_arg(:arg(123.3))}, "can't pass num to an int named arg"); |
| 71 | +dies-ok({int_named_arg(:arg(nqp::list()))}, "can't pass obj to an int named arg"); |
| 72 | +is(int_named_arg(:arg(123)), '123', "can pass int to an int named arg"); |
| 73 | + |
| 74 | +dies-ok({num_named_arg(:arg('foo'))}, "can't pass str to an num named arg"); |
| 75 | +dies-ok({num_named_arg(:arg(123))}, "can't pass int to an int named arg"); |
| 76 | +dies-ok({num_named_arg(:arg(nqp::list()))}, "can't pass obj to an num named arg"); |
| 77 | +is(num_named_arg(:arg(123.1)), '123.1', "can pass num to an num named arg"); |
| 78 | + |
| 79 | +dies-ok({str_named_arg(:arg(123))}, "can't pass int to an str named arg"); |
| 80 | +dies-ok({str_named_arg(:arg(123.3))}, "can't pass num to an str named arg"); |
| 81 | +dies-ok({str_named_arg(:arg(nqp::list()))}, "can't pass obj to an str named arg"); |
| 82 | +is(str_named_arg(:arg('foo')), 'foo', "can pass str to an str named arg"); |
| 83 | + |
| 84 | +sub foo(int $defaulted = 77) { |
| 85 | + nqp::add_i($defaulted, 700); |
| 86 | +} |
| 87 | +is(foo(), 777, 'a default value for a positional int argument works'); |
| 88 | + |
| 89 | +sub bar(int :$defaulted = 77) { |
| 90 | + nqp::add_i($defaulted, 700); |
| 91 | +} |
| 92 | + |
| 93 | +is(bar(), 777, 'a default value for a named int argument works'); |
| 94 | + |
| 95 | +is(bar(:defaulted(66)), 766, 'passing a native int argument (default not used)'); |
| 96 | + |
| 97 | +my %hash; |
| 98 | +sub is_arg_null($arg) { |
| 99 | + nqp::isnull($arg); |
| 100 | +} |
| 101 | +ok(!is_arg_null(%hash<foo>), "%hash<foo> isn't null when used as an argument to a call"); |
0 commit comments