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
|
#!/usr/bin/perl
use v5.14;
use warnings;
use Test::More;
use Feature::Compat::Class;
# We can't just clone many of the tests from Object-Pad/t/01method.t because a
# lot of those use `BUILD` blocks
#
# We'll mostly copy core perl's t/class/class.t and t/class/method.t instead
{
class Test1 {
method hello { return "Hello, world!" }
method classname { return __CLASS__; }
}
my $obj = Test1->new;
isa_ok( $obj, "Test1", '$obj' );
is( $obj->hello, "Hello, world!", '$obj->hello' );
is( $obj->classname, "Test1", '$obj->classname yields __CLASS__' );
}
# $self in method
{
class Test2 {
method retself { return $self }
}
my $obj = Test2->new;
is( $obj->retself, $obj, '$self inside method' );
}
# $self is shifted from @_
{
class Test3 {
method args { return @_ }
}
my $obj = Test3->new;
is_deeply( [ $obj->args( "a", "b" ) ], [ "a", "b" ],
'$self is shifted from @_' );
}
# anonymous methods
{
class Test4 {
method genanon {
return method { "Result" };
}
}
my $obj = Test4->new;
my $mref = $obj->genanon;
is( $obj->$mref, "Result", 'anonymous method can be invoked' );
}
done_testing;
|