forked from Perl/perl5
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Recognise a :reader attribute on class fields
- Loading branch information
Showing
4 changed files
with
162 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#!./perl | ||
|
||
BEGIN { | ||
chdir 't' if -d 't'; | ||
require './test.pl'; | ||
set_up_inc('../lib'); | ||
require Config; | ||
} | ||
|
||
use v5.36; | ||
use feature 'class'; | ||
no warnings 'experimental::class'; | ||
|
||
# reader accessors | ||
{ | ||
class Test1 { | ||
field $s :reader = "the scalar"; | ||
field @a :reader = qw( the array ); | ||
field %h :reader = qw( the hash ); | ||
} | ||
|
||
my $o = Test1->new; | ||
is($o->s, "the scalar", '$o->s accessor'); | ||
ok(eq_array([$o->a], [qw( the array )]), '$o->a accessor'); | ||
ok(eq_hash({$o->h}, {qw( the hash )}), '$o->h accessor'); | ||
|
||
is(scalar $o->a, 2, '$o->a accessor in scalar context'); | ||
is(scalar $o->h, 1, '$o->h accessor in scalar context'); | ||
|
||
# Read accessor does not permit arguments | ||
ok(!eval { $o->s("value") }, | ||
'Reader accessor fails with argument'); | ||
like($@, qr/^Too many arguments for subroutine \'Test1::s\' \(got 1; expected 0\) at /, | ||
'Failure from argument to accessor'); | ||
} | ||
|
||
# Alternative names | ||
{ | ||
class Test2 { | ||
field $f :reader(get_f) = "value"; | ||
} | ||
|
||
is(Test2->new->get_f, "value", 'accessor with altered name'); | ||
} | ||
|
||
done_testing; |