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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
|
#!./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';
# We can't test fields in isolation without having at least one method to
# use them from. We'll try to keep most of the heavy testing of method
# abilities to t/class/method.t
# field in method
{
class Testcase1 {
field $f;
method incr { return ++$f; }
}
my $obj = Testcase1->new;
$obj->incr;
is($obj->incr, 2, 'Field $f incremented twice');
my $obj2 = Testcase1->new;
is($obj2->incr, 1, 'Fields are distinct between instances');
}
# fields are distinct
{
class Testcase2 {
field $x;
field $y;
method setpos { $x = $_[0]; $y = $_[1] }
method x { return $x; }
method y { return $y; }
}
my $obj = Testcase2->new;
$obj->setpos(10, 20);
is($obj->x, 10, '$pos->x');
is($obj->y, 20, '$pos->y');
}
# fields of all variable types
{
class Testcase3 {
field $s;
field @a;
field %h;
method setup {
$s = "scalar";
@a = ( "array" );
%h = ( key => "hash" );
return $self; # test chaining
}
method test {
::is($s, "scalar", 'scalar storage');
::is($a[0], "array", 'array storage');
::is($h{key}, "hash", 'hash storage');
}
}
Testcase3->new->setup->test;
}
# fields can be captured by anon subs
{
class Testcase4 {
field $count;
method make_incrsub {
return sub { $count++ };
}
method count { return $count }
}
my $obj = Testcase4->new;
my $incr = $obj->make_incrsub;
$incr->();
$incr->();
$incr->();
is($obj->count, 3, '$obj->count after invoking closure x 3');
}
# fields can be captured by anon methods
{
class Testcase5 {
field $count;
method make_incrmeth {
return method { $count++ };
}
method count { return $count }
}
my $obj = Testcase5->new;
my $incr = $obj->make_incrmeth;
$obj->$incr;
$obj->$incr;
$obj->$incr;
is($obj->count, 3, '$obj->count after invoking method-closure x 3');
}
# fields of multiple unit classes are distinct
{
class Testcase6::A;
field $x = "A";
method m { return "unit-$x" }
class Testcase6::B;
field $x = "B";
method m { return "unit-$x" }
package main;
ok(eq_array([Testcase6::A->new->m, Testcase6::B->new->m], ["unit-A", "unit-B"]),
'Fields of multiple unit classes remain distinct');
}
# fields can be initialised with constant expressions
{
class Testcase7 {
field $scalar = 123;
method scalar { return $scalar; }
field @array = (4, 5, 6);
method array { return @array; }
field %hash = (7 => 89);
method hash { return %hash; }
}
my $obj = Testcase7->new;
is($obj->scalar, 123, 'Scalar field can be constant initialised');
ok(eq_array([$obj->array], [4, 5, 6]), 'Array field can be constant initialised');
ok(eq_hash({$obj->hash}, {7 => 89}), 'Hash field can be constant initialised');
}
# field initialiser expressions are evaluated within the constructor of each
# instance
{
my $next_x = 1;
my @items;
my %mappings;
class Testcase8 {
field $x = $next_x++;
method x { return $x; }
field @y = ("more", @items);
method y { return @y; }
field %z = (first => "value", %mappings);
method z { return %z; }
}
is($next_x, 1, '$next_x before any objects');
@items = ("values");
$mappings{second} = "here";
my $obj1 = Testcase8->new;
my $obj2 = Testcase8->new;
is($obj1->x, 1, 'Object 1 has x == 1');
is($obj2->x, 2, 'Object 2 has x == 2');
is($next_x, 3, '$next_x after constructing two');
ok(eq_array([$obj1->y], ["more", "values"]),
'Object 1 has correct array field');
ok(eq_hash({$obj1->z}, {first => "value", second => "here"}),
'Object 1 has correct hash field');
}
# fields are visible during initialiser expressions of later fields
{
class Testcase9 {
field $one = 1;
field $two = $one + 1;
field $three = $two + 1;
field @four = $one;
field @five = (@four, $two, $three);
field @six = grep { $_ > 1 } @five;
method three { return $three; }
method six { return @six; }
}
my $obj = Testcase9->new;
is($obj->three, 3, 'Scalar fields initialised from earlier fields');
ok(eq_array([$obj->six], [2, 3]), 'Array fields initialised from earlier fields');
}
# fields can take :param attributes to consume constructor parameters
{
my $next_gamma = 4;
class Testcase10 {
field $alpha :param = undef;
field $beta :param = 123;
field $gamma :param(delta) = $next_gamma++;
method values { return ($alpha, $beta, $gamma); }
}
my $obj = Testcase10->new(
alpha => "A",
beta => "B",
delta => "G",
);
ok(eq_array([$obj->values], [qw(A B G)]),
'Field initialised by :params');
is($next_gamma, 4, 'Defaulting expression not evaluated for passed value');
$obj = Testcase10->new();
ok(eq_array([$obj->values], [undef, 123, 4]),
'Field initialised by defaulting expressions');
is($next_gamma, 5, 'Defaulting expression evaluated for missing value');
}
# fields can be made non-optional
{
class Testcase11 {
field $x :param;
field $y :param;
}
Testcase11->new(x => 1, y => 1);
ok(!eval { Testcase11->new(x => 2) },
'Constructor fails without y');
like($@, qr/^Required parameter 'y' is missing for "Testcase11" constructor at /,
'Failure from missing y argument');
}
# field assignment expressions on :param can use //= and ||=
{
class Testcase12 {
field $if_exists :param(e) = "DEF";
field $if_defined :param(d) //= "DEF";
field $if_true :param(t) ||= "DEF";
method values { return ($if_exists, $if_defined, $if_true); }
}
ok(eq_array(
[Testcase12->new(e => "yes", d => "yes", t => "yes")->values],
["yes", "yes", "yes"]),
'Values for "yes"');
ok(eq_array(
[Testcase12->new(e => 0, d => 0, t => 0)->values],
[0, 0, "DEF"]),
'Values for 0');
ok(eq_array(
[Testcase12->new(e => undef, d => undef, t => undef)->values],
[undef, "DEF", "DEF"]),
'Values for undef');
ok(eq_array(
[Testcase12->new()->values],
["DEF", "DEF", "DEF"]),
'Values for missing');
}
# field initialiser expressions permit `goto` in do {} blocks
{
class Testcase13 {
field $forwards = do { goto HERE; HERE: 1 };
field $backwards = do { my $x; HERE: ; goto HERE if !$x++; 2 };
method values { return ($forwards, $backwards) }
}
ok(eq_array(
[Testcase13->new->values],
[1, 2],
'Values for goto inside do {} blocks in field initialisers'));
}
# field initialiser expressions permit a __CLASS__
{
class Testcase14 {
field $classname = __CLASS__;
method classname { return $classname }
}
is(Testcase14->new->classname, "Testcase14", '__CLASS__ in field initialisers');
}
done_testing;
|