File: hash_id_of_node.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 (138 lines) | stat: -rw-r--r-- 3,498 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
# -*- cperl -*-

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

use warnings;
use strict;

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

my @element = (
    # Value constructor args are passed in their specific array ref
    cargo => { type => 'node', config_class_name => 'Slave' },
);

# minimal set up to get things working
$model->create_config_class(
    name    => "Master",
    element => [
        'plain_hash' => {
            type => 'hash',
            class => 'Config::Model::HashId',    # default
            index_type => 'integer',

            @element
        },
        'bounded_hash' => {
            type => 'hash',
            class => 'Config::Model::HashId',    # default
            index_type => 'integer',

            # hash boundaries
            min    => 1,
            max    => 123,
            max_nb => 2,
            @element
        },
        'hash_with_default_and_init' => {
            type              => 'hash',
            index_type        => 'string',
            default_with_init => {
                'def_1' => 'X=Av Y=Bv',
                'def_2' => 'Y=Av Z=Cv'
            },
            @element
        },
    ],
);

$model->create_config_class(
    name    => "Slave",
    element => [
        [qw/X Y Z/] => {
            type       => 'leaf',
            value_type => 'enum',
            choice     => [qw/Av Bv Cv/]
        },
    ] );

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

my $root = $inst->config_root;

my $b = $root->fetch_element('bounded_hash');
ok( $b, "bounded hash created" );

is( $b->name, 'Master bounded_hash id', "check hash id name" );

my $b1 = $b->fetch_with_id(1);
isa_ok( $b1, 'Config::Model::Node', "fetched element id 1" );

is( $b1->config_class_name, 'Slave', 'check config_class_name' );

my $h_with_def = $root->fetch_element('hash_with_default_and_init');
my $res        = [ $h_with_def->fetch_all_indexes ];

#print Dumper( $res ) ;

is_deeply( $res, [qw/def_1 def_2/], 'check default items' );

#print $root->dump_tree ;
is(
    $root->dump_tree,
    'bounded_hash:1 -
hash_with_default_and_init:def_1
  X=Av
  Y=Bv -
hash_with_default_and_init:def_2
  Y=Av
  Z=Cv - -
', "check default items with children setup"
);

is( $h_with_def->fetch_with_id('def_1')->index_value, 'def_1', 'check index_value prior to move' );

$h_with_def->move( 'def_1', 'moved_1' );

is( $h_with_def->fetch_with_id('moved_1')->index_value, 'moved_1', 'check index_value after move' );

$res = [ $h_with_def->fetch_all_indexes ];
is_deeply( $res, [qw/def_2 moved_1/], 'check moved items keys' );

#print $root->dump_tree ;
is(
    $root->dump_tree,
    'bounded_hash:1 -
hash_with_default_and_init:def_2
  Y=Av
  Z=Cv -
hash_with_default_and_init:moved_1
  X=Av
  Y=Bv - -
', "check moved items with children setup"
);

$root->load("plain_hash:2 X=Av Y=Av Z=Cv");
my $ph = $root->fetch_element('plain_hash');
ok( $ph->copy( 2, 3 ), "node copy in hash" );
is( $ph->fetch_with_id(2)->dump_tree, $ph->fetch_with_id(3)->dump_tree, "compare copied values" );

ok( $ph->move( 2, 4 ), "node move in hash" );
is(
    $ph->fetch_with_id(4)->dump_tree,
    $ph->fetch_with_id(3)->dump_tree,
    "compare copied then moved values"
);

is_deeply( [ $ph->fetch_all_indexes ], [ 3, 4 ], "compare indexes after move" );

memory_cycle_ok($model, "memory cycle");
done_testing;