Skip to content

Commit

Permalink
[js] Tests for calling methods on wrapped js objects
Browse files Browse the repository at this point in the history
The wrapped methods implement some Mu methods that a Perl 6 object
is expected to implement to make working with them more convinient.
We have an :INTERNAL named parameter to force using the method from
the js object.
  • Loading branch information
pmurias committed Oct 5, 2019
1 parent c51358f commit 37ac487
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion t/11-js/eval-js.t
Expand Up @@ -2,7 +2,7 @@ use Test;

BEGIN unless $*VM ~~ "js" { plan 0; skip-rest "js only test"; done-testing; exit 0; };

plan 15;
plan 22;

is EVAL(:lang<JavaScript>, 'return 123'), 123, 'getting a number from js';
is EVAL(:lang<JavaScript>, 'return "simple string"'), "simple string", 'getting a string from js';
Expand Down Expand Up @@ -88,3 +88,49 @@ lives-ok {
return new Foo();
END
}, 'can sink a wrapped js object that does not have a sink method';

my $wrapped-constructor = EVAL(:lang<JavaScript>, q:to/END/);
class Foo {
constructor(value) {
this.value = value;
}
getValue() {
return this.value;
}
static new(arg) {
return 'js land new[' + arg + ']';
}
item(arg) {
return 'js land item[' + arg + ']';
}
sink(arg) {
return 'js land sink[' + arg + ']';
}
Bool(arg) {
return 'js land Bool[' + arg + ']';
}
defined(arg) {
return 'js land defined[' + arg + ']';
}
}
return Foo;
END

my $instance = $wrapped-constructor.new('Passed Value');

is($instance.getValue, 'Passed Value', 'can use .new to create js objects');

is($wrapped-constructor.new(:INTERNAL, 'foo'), 'js land new[foo]', ':INTERNAL with new');

ok($instance.item === $instance, 'item');

is($instance.item(:INTERNAL, 'foo'), 'js land item[foo]', ':INTERNAL with item');
is($instance.sink(:INTERNAL, 'foo'), 'js land sink[foo]', ':INTERNAL with sink');
is($instance.Bool(:INTERNAL, 'foo'), 'js land Bool[foo]', ':INTERNAL with Bool');
is($instance.defined(:INTERNAL, 'foo'), 'js land defined[foo]', ':INTERNAL with defined');

0 comments on commit 37ac487

Please sign in to comment.