File: iterator.t

package info (click to toggle)
libconfig-model-perl 2.155-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,172 kB
  • sloc: perl: 15,117; makefile: 19
file content (160 lines) | stat: -rw-r--r-- 4,467 bytes parent folder | download | duplicates (4)
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
# -*- cperl -*-

use ExtUtils::testlib;
use Test::More;
use Test::Memory::Cycle;
use Config::Model;
use Config::Model::Tester::Setup qw/init_test/;
use Config::Model::Value;

use strict;
use warnings;
use lib "t/lib";

my ($model, $trace) = init_test();

my @models = $model->load( Master => 'Config/Model/models/Master.pl' );

is_deeply(
    \@models,
    [qw/SubSlave2 SubSlave X_base_class2 X_base_class SlaveZ SlaveY Master/],
    "check list of model declared in t/big_model.pm (taking order into account)"
);

$model->augment_config_class(
    name    => 'Master',
    element => [
        warn_if => {
            type          => 'leaf',
            value_type    => 'string',
            warn_if_match => { 'foo' => { fix => '$_ = uc;' } },
        },
        warn_unless => {
            type              => 'leaf',
            value_type        => 'string',
            warn_unless_match => { foo => { msg => '', fix => '$_ = "foo".$_;' } },
        },
    ] );

my $inst = $model->instance(
    root_class_name => 'Master',
    instance_name   => 'test1'
);
ok( $inst, "created dummy instance" );

my $root = $inst->config_root;

my $step = qq!
warn_if=foobar
std_id:ab X=Bv -
std_id:ab2 -
std_id:bc X=Av -
std_id:"a b" X=Av -
std_id:"a b.c" X=Av -
tree_macro=mXY
hash_a:toto=toto_value
hash_a:titi=titi_value
hash_a:"ti ti"="ti ti value"
ordered_hash:z=1
ordered_hash:y=2
ordered_hash:x=3
lista=a,b,c,d
olist:0 X=Av -
olist:1 X=Bv -
my_reference="titi"
warp warp2 aa2="foo bar"
!;

$Config::Model::Value::nowarning = 1;
ok( $root->load( step => $step ), "set up data in tree" );

my @expected = (
    [ '',     'lista' ],
    [ '',     'lista:0' ],
    [ 'back', 'lista:1' ],
    [ '',     'lista:0' ],
    [ 'for',  'lista' ],
    [ '',     'lista:0' ],
    [ '',     'lista:1' ],
    [ '',     'lista:2' ],
    [ '',     'lista:3' ],
    [ '',     'hash_a' ],
    [ '',     'hash_a:"ti ti"' ],
    [ '',     'hash_a:titi' ],
    [ '',     'hash_a:toto' ],
    [ '',     'tree_macro' ],
    [ '',     'a_string' ],
    [ 'back', 'int_v' ],
    [ '',     'a_string' ],
    [ '',     'tree_macro' ],
    [ '',     'hash_a:toto' ],
    [ 'for',  'hash_a:titi' ],
    [ '',     'hash_a:toto' ],
    [ '',     'tree_macro' ],
    [ '',     'a_string' ],
    [ '',     'int_v' ],
    [ 'back', 'warn_if' ],
    [ 'bail', 'int_v' ],
);

my $steer = sub {
    my ( $iter, $item )   = @_;
    my ( $dir,  $expect ) = @$item;
    $iter->bail_out    if $dir eq 'bail';
    $iter->go_forward  if $dir eq 'for';
    $iter->go_backward if $dir eq 'back';
    return @$item;
};

my $leaf_element_cb = sub {
    my ( $iter, $data_r, $node, $element, $index, $leaf_object ) = @_;
    print "test: leaf_element_cb called for ", $leaf_object->location, "\n"
        if $trace;
    my ( $dir, $expect ) = $steer->( $iter, shift @expected );
    is( $leaf_object->location, $expect, "leaf_element_cb got $expect and '$dir'" );
};

my $int_cb = sub {
    my ( $iter, $data_r, $node, $element, $index, $leaf_object ) = @_;
    print "test: int_cb called for ", $leaf_object->location, "\n"
        if $trace;
    my ( $dir, $expect ) = $steer->( $iter, shift @expected );
    is( $leaf_object->location, $expect, "int_cb got $expect and '$dir'" );
};

my $hash_element_cb = sub {
    my ( $iter, $data_r, $node, $element, @keys ) = @_;
    print "test: hash_element_cb called for ", $node->location, " element $element\n"
        if $trace;
    my $obj = $node->fetch_element($element);
    my ( $dir, $expect ) = $steer->( $iter, shift @expected );
    is( $obj->location, $expect, "hash_element_cb got $expect and '$dir'" );
};

my $list_element_cb = sub {
    my ( $iter, $data_r, $node, $element, @idx ) = @_;
    print "test: list_element_cb called for ", $node->location, " element $element\n"
        if $trace;
    my $obj = $node->fetch_element($element);
    my ( $dir, $expect ) = $steer->( $iter, shift @expected );
    is( $obj->location, $expect, "list_element_cb got $expect and '$dir'" );
};

my $iterator = $inst->iterator(
    leaf_cb                => $leaf_element_cb,
    integer_value_cb       => $int_cb,
    hash_element_cb        => $hash_element_cb,
    list_element_cb        => $list_element_cb,
    call_back_on_warning   => 1,
    call_back_on_important => 1,
);
ok( $iterator, "created iterator helper" );

$iterator->start;

is_deeply( \@expected, [], "iterator explored all items" );

memory_cycle_ok($model, "memory cycle");

done_testing;