File: final-hooks.t

package info (click to toggle)
libconfig-mvp-perl 2.101650-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 228 kB
  • ctags: 59
  • sloc: perl: 595; makefile: 2
file content (76 lines) | stat: -rw-r--r-- 1,259 bytes parent folder | download | duplicates (7)
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
use strict;
use warnings;

use Test::More;

use lib 't/lib';

{
  package FAsm;
  use Moose;
  extends 'Config::MVP::Assembler';

  has notes => (
    is  => 'ro',
    isa => 'ArrayRef',
    default => sub { [] },
  );

  has '+section_class'  => (default => 'FAsm::Sec');
}

{
  package FAsm::Sec;
  use Moose;
  extends 'Config::MVP::Section';

  my $i = 0;

  after finalize => sub {
    my ($self) = @_;
    push @{ $self->sequence->assembler->notes }, [ $self->name, $i++ ];
  };
}

my $asm = FAsm->new;

# I wish I had an existing simple way to say "just a name, no package" here.
# -- rjbs, 2010-05-11
$asm->begin_section(strict => 'S1');
$asm->add_value(foo => 10);

is_deeply($asm->notes, [], "no notes to start with");

$asm->change_section(strict => 'S2');

is_deeply($asm->notes, [ [ S1 => 0 ] ], "finalize one section, get notes!");

$asm->end_section;

$asm->change_section(strict => 'S3');

is_deeply(
  $asm->notes,
  [
    [ S1 => 0 ],
    [ S2 => 1 ],
  ],
  "ending section is as good as changing",
);

$asm->change_section(strict => 'S4');

$asm->finalize;

is_deeply(
  $asm->notes,
  [
    [ S1 => 0 ],
    [ S2 => 1 ],
    [ S3 => 2 ],
    [ S4 => 3 ],
  ],
  "finalize the assembler after more sections, more notes!"
);

done_testing;