File: 02fields.t

package info (click to toggle)
libfeature-compat-class-perl 0.07-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 188 kB
  • sloc: perl: 434; makefile: 2; sh: 1
file content (121 lines) | stat: -rw-r--r-- 2,398 bytes parent folder | download
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
#!/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/02field.t because a
# lot of those use init_expr

class Counter {
   field $count = 0;

   method inc { $count++ }

   method describe { "Count is now $count" }
}

{
   my $counter = Counter->new;
   $counter->inc;
   $counter->inc;
   $counter->inc;

   is( $counter->describe, "Count is now 3",
      '$counter->describe after $counter->inc x 3' );

   my $counter2 = Counter->new;
   is( $counter2->describe, "Count is now 0",
      '$counter2 has its own $count' );
}

# Basic init expressions
{
   class AllTheTypes {
      field $scalar = 123;
      field @array  = ( 45, 67 );
      field %hash   = ( 89 => 10 );

      method test {
         Test::More::is(        $scalar, 123,         '$scalar field' );
         Test::More::is_deeply( \@array, [ 45, 67 ],  '@array field' );
         Test::More::is_deeply( \%hash, { 89 => 10 }, '%hash field' );
      }
   }

   AllTheTypes->new->test;
}

# Fields are visible to string-eval()
{
   class Evil {
      field $field;

      method test {
         $field = "the value";
         ::is( eval '$field', "the value", 'fields are visible to string eval()' );
      }
   }

   Evil->new->test;
}

# fields can be captured by anon subs
{
   class ClosureCounter {
      field $count;

      method make_incrsub {
         return sub { $count++ };
      }

      method count { $count }
   }

   my $counter = ClosureCounter->new;
   my $inc = $counter->make_incrsub;

   $inc->();
   $inc->();

   is( $counter->count, 2, '->count after invoking incrsub x 2' );
}

# fields can be captured by anon methods
{
   class MClosureCounter {
      field $count;

      method make_incrmeth {
         return method { $count++ };
      }

      method count { $count }
   }

   my $counter = MClosureCounter->new;
   my $inc = $counter->make_incrmeth;

   $counter->$inc();
   $counter->$inc();

   is( $counter->count, 2, '->count after invoking incrmeth x 2' );
}

# fields are visible during initialiser expressions of later fields
{
   class FieldFromField {
      field $one   = 1;
      field $two   = $one + 1;
      field $three = $two + 1;

      method three { return $three; }
   }

   is( FieldFromField->new->three, 3, 'Scalar fields can be initialised from earlier fields' );
}

done_testing;