[cage] Fix pgegrep, which was merely an innocent bystander in the Great Namespace...
[parrot.git] / t / configure / 032-data.t
blob43dcc5fc92466e82df1f52ca13c9e9cc2e39c5fc
1 #!perl
2 # Copyright (C) 2001-2005, Parrot Foundation.
3 # $Id$
5 use strict;
6 use warnings;
8 use lib qw( lib );
9 use Test::More tests => 69;
11 =head1 NAME
13 t/configure/032-data.t - tests Parrot::Configure::Data
15 =head1 SYNOPSIS
17     prove t/configure/032-data.t
19 =head1 DESCRIPTION
21 Regressions tests for the L<Parrote::Configure::Data> class.
23 =cut
25 BEGIN { use Parrot::Configure::Data; }
27 can_ok(
28     'Parrot::Configure::Data', qw(
29         new
30         get
31         set
32         keys
33         dump
34         clean
35         settrigger
36         gettriggers
37         gettrigger
38         deltrigger
39         )
43     my $pcd = Parrot::Configure::Data->new;
45     isa_ok( $pcd, 'Parrot::Configure::Data' );
48 # ->get() / ->set()
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));
57     ok(
58         eq_array( \@values, [ undef, undef, undef ] ),
59         "->get() multiple unset value returns undef"
60     );
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;
76     my $self = $pcd->set(
77         'a' => 1,
78         'b' => 2,
79         'c' => 3,
80     );
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()" );
89 # ->add()
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(
105         '',
106         'a' => 1,
107         'b' => 2,
108         'c' => 3,
109     );
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()" );
136 # ->keys()
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;
149     $pcd->set(
150         'a' => 1,
151         'b' => 2,
152         'c' => 3,
153     );
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" );
162 # ->dump()
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;
175     $pcd->add(
176         '',
177         'a' => 1,
178         'b' => 2,
179         'c' => 3,
180     );
181     my $data = $pcd->dump( q{c}, q{*PConfig} );
183     like(
184         $data,
185         qr/PConfig = \(\s*'a' => 1,\s*'b' => 2,\s*'c' => 3\s*\);/s,
186         "->dump() returns the proper values"
187     );
190 # ->clean()
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;
213     $pcd->set(
214         TEMP_FOO => '',
215         TEMP_BAR => '',
216         TEMP_BAZ => '',
217     );
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" );
224 # ->settrigger()
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;
238     my $flag = 0;
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;
247     my $flag = 0;
248     $pcd->settrigger( 'foo', 'bar', sub { $flag = 1 } );
249     $pcd->get('foo');
251     is( $flag, 0, "->get() doesn't activate the callback" );
255     my $pcd = Parrot::Configure::Data->new;
257     my $flag = 0;
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;
267     my $flag = 0;
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;
277     my $flag1 = 0;
278     my $flag2 = 0;
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;
289     my $flag1 = 0;
290     my $flag2 = 0;
291     $pcd->settrigger( 'foo', 'bar', sub { $flag1 = 1 } );
292     $pcd->settrigger( 'foo', 'baz', sub { $flag2 = 1 } );
293     $pcd->get('foo');
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;
302     my $flag1 = 0;
303     my $flag2 = 0;
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;
315     my $flag1 = 0;
316     my $flag2 = 0;
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" );
325 # ->gettriggers()
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;
347     my $flag = 0;
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;
358     my $flag1 = 0;
359     my $flag2 = 0;
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" );
369 # ->gettrigger()
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;
383     my $flag = 0;
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;
394     my $flag1 = 0;
395     my $flag2 = 0;
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" );
403     &$trigger;
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;
411     my $flag1 = 0;
412     my $flag2 = 0;
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" );
420     &$trigger;
421     is( $flag1, 0, "->gettrigger() returned the correct callback" );
422     is( $flag2, 1, "->gettrigger() returned the correct callback" );
425 # ->deltrigger()
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;
438     my $flag = 0;
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;
449     my $flag = 0;
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;
459     my $flag1 = 0;
460     my $flag2 = 0;
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;
473     my $flag1 = 0;
474     my $flag2 = 0;
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;
487     my $flag = 0;
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;
497     my $flag = 0;
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;
507     my $flag1 = 0;
508     my $flag2 = 0;
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;
520     my $flag1 = 0;
521     my $flag2 = 0;
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" );
530 # Local Variables:
531 #   mode: cperl
532 #   cperl-indent-level: 4
533 #   fill-column: 100
534 # End:
535 # vim: expandtab shiftwidth=4: