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
|
#!/usr/bin/perl
use v5.26;
use warnings;
use Test2::V0;
use Tangence::Constants;
use Tangence::Registry;
use lib ".";
use t::TestObj;
my $registry = Tangence::Registry->new(
tanfile => "t/TestObj.tan",
);
my $obj = $registry->construct(
"t::TestObj",
);
# SCALAR
is( $obj->get_prop_scalar, "123", 'scalar initially' );
my $cb_self;
my $scalar;
$obj->watch_property( scalar =>
on_set => sub { ( $cb_self, $scalar ) = @_ },
);
my $scalar_shadow;
$obj->watch_property( scalar =>
on_updated => sub { $scalar_shadow = $_[1] },
);
is( $scalar_shadow, "123", 'scalar shadow initially' );
$obj->set_prop_scalar( "456" );
is( $obj->get_prop_scalar, "456", 'scalar after set' );
ref_is( $cb_self, $obj, '$cb_self is $obj' );
is( $scalar, "456", '$scalar after set' );
is( $scalar_shadow, "456", 'scalar shadow finally' );
# HASH
is( $obj->get_prop_hash, { one => 1, two => 2, three => 3 }, 'hash initially' );
my $hash;
undef $cb_self;
my ( $h_key, $h_value );
$obj->watch_property( hash =>
on_set => sub { ( $cb_self, $hash ) = @_ },
on_add => sub { ( undef, $h_key, $h_value ) = @_ },
on_del => sub { ( undef, $h_key ) = @_ },
);
my $hash_shadow;
$obj->watch_property( hash =>
on_updated => sub { $hash_shadow = $_[1] },
);
is( $hash_shadow, { one => 1, two => 2, three => 3 }, 'hash shadow initially' );
$obj->set_prop_hash( { four => 4 } );
is( $obj->get_prop_hash, { four => 4 }, 'hash after set' );
ref_is( $cb_self, $obj, '$cb_self is $obj' );
is( $hash, { four => "4" }, '$hash after set' );
$obj->add_prop_hash( five => 5 );
is( $obj->get_prop_hash, { four => 4, five => 5 }, 'hash after add' );
is( $h_key, 'five', '$h_key after add' );
is( $h_value, 5, '$h_value after add' );
$obj->add_prop_hash( five => 6 );
is( $obj->get_prop_hash, { four => 4, five => 6 }, 'hash after add as change' );
is( $h_key, 'five', '$h_key after add as change' );
is( $h_value, 6, '$h_value after add as change' );
$obj->del_prop_hash( 'five' );
is( $obj->get_prop_hash, { four => 4 }, 'hash after del' );
is( $h_key, 'five', '$h_key after del' );
is( $hash_shadow, { four => 4 }, 'hash shadow finally' );
# QUEUE
is( $obj->get_prop_queue, [ 1, 2, 3 ], 'queue initially' );
my $queue;
undef $cb_self;
my ( $q_count, @q_values );
$obj->watch_property( queue =>
on_set => sub { ( $cb_self, $queue ) = @_ },
on_push => sub { shift; @q_values = @_ },
on_shift => sub { ( undef, $q_count ) = @_ },
);
my $queue_shadow;
$obj->watch_property( queue =>
on_updated => sub { $queue_shadow = $_[1] },
);
is( $queue_shadow, [ 1, 2, 3 ], 'queue shadow initially' );
$obj->set_prop_queue( [ 4, 5, 6 ] );
is( $obj->get_prop_queue, [ 4, 5, 6 ], 'queue after set' );
ref_is( $cb_self, $obj, '$cb_self is $obj' );
is( $queue, [ 4, 5, 6 ], '$queue after set' );
$obj->push_prop_queue( 7 );
is( $obj->get_prop_queue, [ 4, 5, 6, 7 ], 'queue after push' );
is( \@q_values, [ 7 ], '@q_values after push' );
$obj->shift_prop_queue;
is( $obj->get_prop_queue, [ 5, 6, 7 ], 'queue after shift' );
is( $q_count, 1, '$q_count after shift' );
$obj->shift_prop_queue( 2 );
is( $obj->get_prop_queue, [ 7 ], 'queue after shift(2)' );
is( $q_count, 2, '$q_count after shift(2)' );
is( $queue_shadow, [ 7 ], 'queue shadow finally' );
# ARRAY
is( $obj->get_prop_array, [ 1, 2, 3 ], 'array initially' );
my $array;
undef $cb_self;
my ( $a_index, $a_count, @a_values, $a_delta );
$obj->watch_property( array =>
on_set => sub { ( $cb_self, $array ) = @_ },
on_push => sub { shift; @a_values = @_ },
on_shift => sub { ( undef, $a_count ) = @_ },
on_splice => sub { ( undef, $a_index, $a_count, @a_values ) = @_ },
on_move => sub { ( undef, $a_index, $a_delta ) = @_ },
);
my $array_shadow;
$obj->watch_property( array =>
on_updated => sub { $array_shadow = $_[1] },
);
is( $array_shadow, [ 1, 2, 3 ], 'array shadow initially' );
$obj->set_prop_array( [ 4, 5, 6 ] );
is( $obj->get_prop_array, [ 4, 5, 6 ], 'array after set' );
ref_is( $cb_self, $obj, '$cb_self is $obj' );
is( $array, [ 4, 5, 6 ], '$array after set' );
$obj->push_prop_array( 7 );
is( $obj->get_prop_array, [ 4, 5, 6, 7 ], 'array after push' );
is( \@a_values, [ 7 ], '@a_values after push' );
$obj->shift_prop_array;
is( $obj->get_prop_array, [ 5, 6, 7 ], 'array after shift' );
is( $a_count, 1, '$a_count after shift' );
$obj->shift_prop_array( 2 );
is( $obj->get_prop_array, [ 7 ], 'array after shift(2)' );
is( $a_count, 2, '$a_count after shift(2)' );
$obj->splice_prop_array( 0, 0, ( 5, 6 ) );
is( $obj->get_prop_array, [ 5, 6, 7 ], 'array after splice(0,0)' );
is( $a_index, 0, '$a_index after splice(0,0)' );
is( $a_count, 0, '$a_count after splice(0,0)' );
is( \@a_values, [ 5, 6 ], '@a_values after splice(0,0)' );
$obj->splice_prop_array( 2, 1, () );
is( $obj->get_prop_array, [ 5, 6 ], 'array after splice(2,1)' );
is( $a_index, 2, '$a_index after splice(2,1)' );
is( $a_count, 1, '$a_count after splice(2,1)' );
is( \@a_values, [ ], '@a_values after splice(2,1)' );
$obj->move_prop_array( 0, 1 );
is( $obj->get_prop_array, [ 6, 5 ], 'array after move(+1)' );
is( $a_index, 0, '$a_index after move' );
is( $a_delta, 1, '$a_delta after move' );
$obj->set_prop_array( [ 0 .. 9 ] );
$obj->move_prop_array( 3, 2 );
is( $obj->get_prop_array, [ 0, 1, 2, 4, 5, 3, 6, 7, 8, 9 ], 'array after move(+2)' );
$obj->move_prop_array( 5, -2 );
is( $obj->get_prop_array, [ 0 .. 9 ], 'array after move(-2)' );
is( $array_shadow, [ 0 .. 9 ], 'array shadow finally' );
# OBJSET
# Shall have to construct some other TestObj objects to use here, as we can't
# put regular ints in
is( $obj->get_prop_objset, [], 'objset initially' );
my $objset;
undef $cb_self;
my ( $added, $deleted_id );
$obj->watch_property( objset =>
on_set => sub { ( $cb_self, $objset ) = @_ },
on_add => sub { ( undef, $added ) = @_ },
on_del => sub { ( undef, $deleted_id ) = @_ },
);
my $new = $registry->construct( "t::TestObj" );
$obj->set_prop_objset( { $new->id => $new } );
is( $obj->get_prop_objset, [ Test2::Tools::Compare::exact_ref($new) ], 'objset after set' );
ref_is( $cb_self, $obj, '$cb_self is $obj' );
is( $objset, [ Test2::Tools::Compare::exact_ref($new) ], '$objset after set' );
$obj->del_prop_objset( $new );
is( $obj->get_prop_objset, [], 'objset after del' );
is( $deleted_id, $new->id, '$deleted_id after del' );
$obj->add_prop_objset( $new );
is( $obj->get_prop_objset, [ Test2::Tools::Compare::exact_ref($new) ], 'objset after add' );
ref_is( $added, $new, '$added after add' );
done_testing;
|