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
|
#!/usr/bin/perl
use v5.18;
use warnings;
use Test2::V0;
use Object::Pad 0.800;
class Base :abstract {
field $f :param;
method m1;
method m2 { return $f; }
}
like( dies { Base->new },
qr/^Cannot directly construct an instance of abstract class 'Base' at /,
'failure from trying to ->new an abstract class' );
class Derived {
inherit Base;
method m1 { return "concrete"; }
}
pass( 'Able to derive from abstract class Base by providing m1' );
{
my $obj = Derived->new( f => "field-value" );
ok( $obj, 'Able to construct an instance of Derived class' );
is( $obj->m1, "concrete", 'Derived->m1' );
is( $obj->m2, "field-value", 'Derived->m2' );
}
ok( !eval <<'EOPERL',
class Base2 {
inherit Base;
}
EOPERL
'derived concrete class without required method fails' );
like( $@, qr/^Class Base2 does not provide a required method named 'm1' at /,
'message from failure to derive concrete class' );
class Base3 :abstract {
inherit Base;
}
pass( 'Able to derive an abstract class from another without implementing missing methods' );
class Derived3 {
inherit Base3;
method m1 { return "non-abstract"; }
}
{
my $obj = Derived3->new( f => "field-value" );
ok( $obj, 'Able to construct an instance of Derived3 class' );
is( $obj->m1, "non-abstract", 'Derived3->m1' );
}
done_testing;
|