File: multi_warp_value.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 (152 lines) | stat: -rw-r--r-- 4,331 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
# -*- cperl -*-

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

use strict;
use warnings;

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

my @m1 = qw/A1 B1/;
my @m2 = qw/A2 B2 C2/;
my @m3 = qw/A3 B3/;
my @rules;

foreach my $c1 (@m1) {
    foreach my $c2 (@m2) {
        foreach my $c3 (@m3) {
            push @rules, [ $c1, $c2, $c3 ], { default => "m$c1$c2$c3" };
        }
    }
}

# minimal set up to get things working
my $model_data = {
    name      => 'Master',
    'element' => [
        macro1 => {
            type       => 'leaf',
            value_type => 'enum',
            choice     => \@m1
        },
        macro2 => {
            type       => 'leaf',
            value_type => 'enum',
            choice     => \@m2
        },
        macro3 => {
            type       => 'leaf',
            value_type => 'enum',
            choice     => \@m3
        },
        m1 => {
            type       => 'leaf',
            value_type => 'string',
            'warp'     => {
                follow => [ '- macro1', ' - macro2', '- macro3' ],
                rules  => \@rules
            }
        },
        'm2' => {
            type       => 'leaf',
            value_type => 'string',
            default    => 'unsatisfied',
            'warp'     => {
                follow  => [ '- macro1', ' - macro2', '- macro3' ],
                'rules' => [
                    [ 'A1', 'A2', 'A3' ] => { default => '3xA' },
                    [ 'B1', [ 'B2', 'C2' ], 'B3' ] => { default => '3x[BC]' },
                ]
            },
        },
        'm3' => {
            type       => 'leaf',
            value_type => 'string',
            default    => 'unsatisfied',
            'warp'     => {
                follow  => '- macro2',
                'rules' => [
                    [ 'B2', 'A2' ] => { default => 'A2 B2 rule' },
                    'C2' => { default => 'C2 rule' },
                ]
            },
        },
        'm4' => {
            type       => 'leaf',
            value_type => 'string',
            default    => 'unsatisfied',
            'warp'     => {
                follow => {
                    m1 => '- macro1',
                    m2 => ' - macro2',
                    m3 => '- macro3'
                },
                'rules' => [
                    '$m1 eq "A1" and $m2 eq "A2" and $m3 eq "A3"' => { default => '3xA' },
                    '($m1 eq "B1") and ($m2 eq "B2" or $m2 eq "C2") and ($m3 eq "B3")' =>
                        { default => '3x[BC]' },
                ]
            },
        },
    ] };

my $copy = dclone $model_data ;

$model->create_config_class(%$copy);

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

my $root = $inst->config_root;

use Config::Model::Warper;

eq_or_diff( [ Config::Model::Warper::_dclone_key('foo') ],
    ['foo'], "Test _dclone_key (single key)" );

#use Devel::TraceCalls;
#trace_calls {Class => "Config::Model::Value",};
#trace_calls {Class => "Config::Model::WarpedThing",};

foreach my $c1 (@m1) {
    ok( $root->load("macro1=$c1"), "Setting Root macro1 to $c1" );
    foreach my $c2 (@m2) {
        ok( $root->load("macro2=$c2"), "Setting Root macro2 to $c2" );
        foreach my $c3 (@m3) {
            ok( $root->load("macro3=$c3"), "Setting Root macro3 to $c3" );

            my $vm1 = $root->grab_value('m1');
            is( $vm1, "m$c1$c2$c3", "Reading Root slot m1: $vm1" );

            my $index = "$c1$c2$c3";
            my $m2_val =
                  $index eq 'A1A2A3'    ? '3xA'
                : $index =~ /B1[BC]2B3/ ? '3x[BC]'
                :                         'unsatisfied';
            is( $root->grab_value('m2'), $m2_val, "Reading Root slot m2" );
            is( $root->grab_value('m4'), $m2_val, "Reading Root slot m4" );
        }
    }
}

my @test =
    ( [ "macro2=A2", "A2 B2 rule" ], [ "macro2=C2", "C2 rule" ], [ "macro2=B2", "A2 B2 rule" ], );

foreach my $u_test (@test) {
    my ( $load, $exp ) = @$u_test;
    $root->load($load);
    is( $root->grab_value('m3'), $exp, "test m3 with $load" );
}

memory_cycle_ok($model, "memory cycle");

done_testing;