1#!./perl 2 3BEGIN { 4 chdir 't' if -d 't'; 5 require './test.pl'; 6 set_up_inc('../lib'); 7 require Config; 8} 9 10use v5.36; 11use feature 'class'; 12no warnings qw( experimental::class experimental::builtin ); 13 14use builtin qw( blessed reftype ); 15 16{ 17 class Testcase1 { 18 field $x :param; 19 method x { return $x; } 20 } 21 22 my $obj = Testcase1->new(x => 123); 23 is($obj->x, 123, 'Value of $x set by constructor'); 24 25 # The following tests aren't really related to construction, just the 26 # general nature of object instance refs. If this test file gets too long 27 # they could be moved to their own file. 28 is(ref $obj, "Testcase1", 'ref of $obj'); 29 is(blessed $obj, "Testcase1", 'blessed of $obj'); 30 is(reftype $obj, "OBJECT", 'reftype of $obj'); 31 32 # num/stringification of object without overload 33 is($obj+0, builtin::refaddr($obj), 'numified object'); 34 like("$obj", qr/^Testcase1=OBJECT\(0x[[:xdigit:]]+\)$/, 'stringified object' ); 35 36 ok(!eval { Testcase1->new(x => 123, y => 456); 1 }, 'Unrecognised parameter fails'); 37 like($@, qr/^Unrecognised parameters for "Testcase1" constructor: y at /, 38 'Exception thrown by constructor for unrecogniser parameter'); 39} 40 41{ 42 class Testcase2 { 43 use overload 44 '0+' => sub { return 12345 }, 45 '""' => sub { "<Testcase2 instance>" }, 46 fallback => 1; 47 } 48 49 my $obj = Testcase2->new; 50 is($obj+0, 12345, 'numified object with overload'); 51 is("$obj", "<Testcase2 instance>", 'stringified object with overload' ); 52} 53 54done_testing; 55