2 # Copyright (C) 2001-2005, Parrot Foundation.
9 use Test::More tests => 69;
13 t/configure/032-data.t - tests Parrot::Configure::Data
17 prove t/configure/032-data.t
21 Regressions tests for the L<Parrote::Configure::Data> class.
25 BEGIN { use Parrot::Configure::Data; }
28 'Parrot::Configure::Data', qw(
43 my $pcd = Parrot::Configure::Data->new;
45 isa_ok( $pcd, 'Parrot::Configure::Data' );
50 my $pcd = Parrot::Configure::Data->new;
52 is( $pcd->get('a'), undef, "->get() unset value returns undef in scalar context" );
53 is( ( $pcd->get('a') ), undef, "->get() unset value returns undef in list context" );
55 my @values = $pcd->get(qw(a b c));
58 eq_array( \@values, [ undef, undef, undef ] ),
59 "->get() multiple unset value returns undef"
64 my $pcd = Parrot::Configure::Data->new;
66 my $self = $pcd->set( 'a' => 1 );
68 # ->set() should return itself
69 isa_ok( $self, 'Parrot::Configure::Data' );
70 is( $pcd->get('a'), 1, "->get() returns proper value after ->set()" );
74 my $pcd = Parrot::Configure::Data->new;
82 isa_ok( $self, 'Parrot::Configure::Data' );
84 my @values = $pcd->get(qw(a b c));
86 is_deeply( \@values, [ 1, 2, 3 ], "->get() returns proper multiple values after ->set()" );
92 my $pcd = Parrot::Configure::Data->new;
94 my $self = $pcd->add( '', 'a' => 1 );
96 # ->add() should return itself
97 isa_ok( $self, 'Parrot::Configure::Data' );
98 is( $pcd->get('a'), 1, "->get() returns proper value after ->add()" );
102 my $pcd = Parrot::Configure::Data->new;
104 my $self = $pcd->add(
111 isa_ok( $self, 'Parrot::Configure::Data' );
113 my @values = $pcd->get(qw(a b c));
115 is_deeply( \@values, [ 1, 2, 3 ], "->get() returns proper multiple values after ->add()" );
119 my $pcd = Parrot::Configure::Data->new;
121 $pcd->add( '', 'a' => 1 );
122 $pcd->add( '', 'a' => 1 );
124 is( $pcd->get('a'), 11, "->get() returns proper value after ->add()->add()" );
128 my $pcd = Parrot::Configure::Data->new;
130 $pcd->add( '', 'a' => 1 );
131 $pcd->add( ' - ', 'a' => 1 );
133 is( $pcd->get('a'), "1 - 1", "->get() returns proper value after ->add()->add()" );
139 my $pcd = Parrot::Configure::Data->new;
141 my @keys = $pcd->keys;
143 is_deeply( \@keys, [], "->keys() returns nothing if no keys are set" );
147 my $pcd = Parrot::Configure::Data->new;
155 # keys is unordered so the result needs to be sorted. eq_set() could be
156 # used here instead but it doesn't provide useful diagnostics
157 my @keys = sort $pcd->keys;
159 is_deeply( \@keys, [qw(a b c)], "->keys() returns all set keys" );
165 my $pcd = Parrot::Configure::Data->new;
167 my $data = $pcd->dump( q{c}, q{*PConfig} );
169 like( $data, qr/\%PConfig = \(\);/, "->dump() returns nothing if no keys are set" );
173 my $pcd = Parrot::Configure::Data->new;
181 my $data = $pcd->dump( q{c}, q{*PConfig} );
185 qr/PConfig = \(\s*'a' => 1,\s*'b' => 2,\s*'c' => 3\s*\);/s,
186 "->dump() returns the proper values"
193 my $pcd = Parrot::Configure::Data->new;
195 my $self = $pcd->clean;
197 # ->clean() should return itself
198 isa_ok( $self, 'Parrot::Configure::Data' );
202 my $pcd = Parrot::Configure::Data->new;
204 $pcd->set( TEMP_FOO => '' );
205 my $self = $pcd->clean;
207 is( $pcd->get('TEMP_FOO'), undef, "->clean() removed TEMP_FOO" );
211 my $pcd = Parrot::Configure::Data->new;
218 my $self = $pcd->clean;
219 my @values = $pcd->get(qw(TEMP_FOO TEMP_BAR TEMP_BAZ));
221 ok( eq_array( \@values, [ undef, undef, undef ] ), "->clean() removed multiple TEMP_* keys" );
227 my $pcd = Parrot::Configure::Data->new;
229 my $self = $pcd->settrigger( 'foo', 'bar', sub { } );
231 # ->settrigger() should return itself
232 isa_ok( $self, 'Parrot::Configure::Data' );
236 my $pcd = Parrot::Configure::Data->new;
239 $pcd->settrigger( 'foo', 'bar', sub { $flag = 1 } );
241 is( $flag, 0, "->settrigger() doesn't activate a callback" );
245 my $pcd = Parrot::Configure::Data->new;
248 $pcd->settrigger( 'foo', 'bar', sub { $flag = 1 } );
251 is( $flag, 0, "->get() doesn't activate the callback" );
255 my $pcd = Parrot::Configure::Data->new;
258 $pcd->settrigger( 'foo', 'bar', sub { $flag = 1 } );
259 $pcd->set( foo => 'bar' );
261 is( $flag, 1, "->set() activates the callback" );
265 my $pcd = Parrot::Configure::Data->new;
268 $pcd->settrigger( 'foo', 'bar', sub { $flag = 1 } );
269 $pcd->add( '', foo => 'bar' );
271 is( $flag, 1, "->add() activates the callback" );
275 my $pcd = Parrot::Configure::Data->new;
279 $pcd->settrigger( 'foo', 'bar', sub { $flag1 = 1 } );
280 $pcd->settrigger( 'foo', 'baz', sub { $flag2 = 1 } );
282 is( $flag1, 0, "->settrigger() doesn't activate a stacked callback" );
283 is( $flag2, 0, "->settrigger() doesn't activate a stacked callback" );
287 my $pcd = Parrot::Configure::Data->new;
291 $pcd->settrigger( 'foo', 'bar', sub { $flag1 = 1 } );
292 $pcd->settrigger( 'foo', 'baz', sub { $flag2 = 1 } );
295 is( $flag1, 0, "->get() doesn't activate the stacked callback" );
296 is( $flag2, 0, "->get() doesn't activate the stacked callback" );
300 my $pcd = Parrot::Configure::Data->new;
304 $pcd->settrigger( 'foo', 'bar', sub { $flag1 = 1 } );
305 $pcd->settrigger( 'foo', 'baz', sub { $flag2 = 1 } );
306 $pcd->set( foo => 'bar' );
308 is( $flag1, 1, "->set() activates the stacked callback" );
309 is( $flag2, 1, "->set() activates the stacked callback" );
313 my $pcd = Parrot::Configure::Data->new;
317 $pcd->settrigger( 'foo', 'bar', sub { $flag1 = 1 } );
318 $pcd->settrigger( 'foo', 'baz', sub { $flag2 = 1 } );
319 $pcd->add( '', foo => 'bar' );
321 is( $flag1, 1, "->add() activates the stacked callback" );
322 is( $flag2, 1, "->add() activates the stacked callback" );
328 my $pcd = Parrot::Configure::Data->new;
330 my @triggers = $pcd->gettriggers('foo');
332 is( scalar @triggers, 0, "->gettriggers() returns the proper number of triggers" );
336 my $pcd = Parrot::Configure::Data->new;
338 $pcd->set( foo => 'bar' );
339 my @triggers = $pcd->gettriggers('foo');
341 is( scalar @triggers, 0, "->gettriggers() returns the proper number of triggers" );
345 my $pcd = Parrot::Configure::Data->new;
348 $pcd->settrigger( 'foo', 'bar', sub { $flag = 1 } );
349 my @triggers = $pcd->gettriggers('foo');
351 is( $flag, 0, "->gettriggers() doesn't activate the callback" );
352 is( scalar @triggers, 1, "->gettriggers() returns the proper number of triggers" );
356 my $pcd = Parrot::Configure::Data->new;
360 $pcd->settrigger( 'foo', 'bar', sub { $flag1 = 1 } );
361 $pcd->settrigger( 'foo', 'baz', sub { $flag2 = 1 } );
362 my @triggers = $pcd->gettriggers('foo');
364 is( $flag1, 0, "->gettriggers() doesn't activate the stacked callback" );
365 is( $flag2, 0, "->gettriggers() doesn't activate the stacked callback" );
366 is( scalar @triggers, 2, "->gettriggers() returns the proper number of triggers" );
372 my $pcd = Parrot::Configure::Data->new;
374 is( $pcd->gettrigger('foo'),
375 undef, "->gettrigger() unset value returns undef in scalar context" );
376 is( ( $pcd->gettrigger('foo') ),
377 undef, "->gettrigger() unset value returns undef in list context" );
381 my $pcd = Parrot::Configure::Data->new;
384 $pcd->settrigger( 'foo', 'bar', sub { $flag = 1 } );
385 my $trigger = $pcd->gettrigger( 'foo', 'bar' );
387 is( $flag, 0, "->gettrigger() doesn't activate the callback" );
388 is( ref $trigger, 'CODE', "->gettrigger() returns a code ref" );
392 my $pcd = Parrot::Configure::Data->new;
396 $pcd->settrigger( 'foo', 'bar', sub { $flag1 = 1 } );
397 $pcd->settrigger( 'foo', 'baz', sub { $flag2 = 1 } );
398 my $trigger = $pcd->gettrigger( 'foo', 'bar' );
400 is( $flag1, 0, "->gettrigger() doesn't activate the stacked callback" );
401 is( $flag2, 0, "->gettrigger() doesn't activate the stacked callback" );
402 is( ref $trigger, 'CODE', "->gettrigger() returns a code ref" );
404 is( $flag1, 1, "->gettrigger() returned the correct callback" );
405 is( $flag2, 0, "->gettrigger() returned the correct callback" );
409 my $pcd = Parrot::Configure::Data->new;
413 $pcd->settrigger( 'foo', 'bar', sub { $flag1 = 1 } );
414 $pcd->settrigger( 'foo', 'baz', sub { $flag2 = 1 } );
415 my $trigger = $pcd->gettrigger( 'foo', 'baz' );
417 is( $flag1, 0, "->gettrigger() doesn't activate the stacked callback" );
418 is( $flag2, 0, "->gettrigger() doesn't activate the stacked callback" );
419 is( ref $trigger, 'CODE', "->gettrigger() returns a code ref" );
421 is( $flag1, 0, "->gettrigger() returned the correct callback" );
422 is( $flag2, 1, "->gettrigger() returned the correct callback" );
428 my $pcd = Parrot::Configure::Data->new;
430 my $self = $pcd->deltrigger( 'foo', 'bar' );
432 is( $self, undef, '->deltrigger() returns undef on failure' );
436 my $pcd = Parrot::Configure::Data->new;
439 $pcd->settrigger( 'foo', 'bar', sub { $flag = 1 } );
440 my $self = $pcd->deltrigger( 'foo', 'bar' );
442 # ->deltrigger() should return itself on success
443 isa_ok( $self, 'Parrot::Configure::Data' );
447 my $pcd = Parrot::Configure::Data->new;
450 $pcd->settrigger( 'foo', 'bar', sub { $flag = 1 } );
451 $pcd->deltrigger( 'foo', 'bar' );
453 is( $pcd->gettrigger( 'foo', 'bar' ), undef, "->deltrigger() removed the callback" );
457 my $pcd = Parrot::Configure::Data->new;
461 $pcd->settrigger( 'foo', 'bar', sub { $flag1 = 1 } );
462 $pcd->settrigger( 'foo', 'baz', sub { $flag2 = 1 } );
463 $pcd->deltrigger( 'foo', 'bar' );
465 is( $pcd->gettrigger( 'foo', 'bar' ), undef, "->deltrigger() removed the stacked callback" );
466 is( ref $pcd->gettrigger( 'foo', 'baz' ),
467 'CODE', "->deltrigger() removed the stacked callback" );
471 my $pcd = Parrot::Configure::Data->new;
475 $pcd->settrigger( 'foo', 'bar', sub { $flag1 = 1 } );
476 $pcd->settrigger( 'foo', 'baz', sub { $flag2 = 1 } );
477 $pcd->deltrigger( 'foo', 'baz' );
479 is( $pcd->gettrigger( 'foo', 'baz' ), undef, "->deltrigger() removed the stacked callback" );
480 is( ref $pcd->gettrigger( 'foo', 'bar' ),
481 'CODE', "->deltrigger() removed the stacked callback" );
485 my $pcd = Parrot::Configure::Data->new;
488 $pcd->settrigger( 'foo', 'bar', sub { $flag = 1 } );
489 $pcd->deltrigger( 'foo', 'bar' );
491 is( $flag, 0, "->deltrigger() doesn't activate the callback" );
495 my $pcd = Parrot::Configure::Data->new;
498 $pcd->settrigger( 'foo', 'bar', sub { $flag = 1 } );
499 $pcd->deltrigger( 'foo', 'bar' );
501 is( $flag, 0, "->deltrigger() doesn't activate the callback" );
505 my $pcd = Parrot::Configure::Data->new;
509 $pcd->settrigger( 'foo', 'bar', sub { $flag1 = 1 } );
510 $pcd->settrigger( 'foo', 'baz', sub { $flag2 = 1 } );
511 $pcd->deltrigger( 'foo', 'bar' );
513 is( $flag1, 0, "->deltrigger() doesn't activate the stacked callback" );
514 is( $flag2, 0, "->deltrigger() doesn't activate the stacked callback" );
518 my $pcd = Parrot::Configure::Data->new;
522 $pcd->settrigger( 'foo', 'bar', sub { $flag1 = 1 } );
523 $pcd->settrigger( 'foo', 'baz', sub { $flag2 = 1 } );
524 $pcd->deltrigger( 'foo', 'baz' );
526 is( $flag1, 0, "->deltrigger() doesn't activate the stacked callback" );
527 is( $flag2, 0, "->deltrigger() doesn't activate the stacked callback" );
532 # cperl-indent-level: 4
535 # vim: expandtab shiftwidth=4: