File: hash_id_of_node.t

package info (click to toggle)
libconfig-model-perl 2.021-3%2Bdeb7u1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,104 kB
  • sloc: perl: 20,550; makefile: 11
file content (150 lines) | stat: -rw-r--r-- 3,795 bytes parent folder | download
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
# -*- cperl -*-

use warnings FATAL => qw(all);

use ExtUtils::testlib;
use Test::More tests => 18;
use Test::Memory::Cycle;
use Config::Model ;
use Data::Dumper ;
use Log::Log4perl qw(:easy :levels) ;

use strict;

my $arg = shift || '';
my ($log,$show) = (0) x 2 ;

my $trace = $arg =~ /t/ ? 1 : 0 ;
$::debug            = 1 if $arg =~ /d/;
$log                = 1 if $arg =~ /l/;
$show               = 1 if $arg =~ /s/;

Log::Log4perl->easy_init($log ? $TRACE: $WARN);

Config::Model::Exception::Any->Trace(1) if $arg =~ /e/;

ok(1,"Compilation done");

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
my $model = Config::Model->new() ;
$model ->create_config_class 
  (
   name => "Master",
   element 
   => [ 
       'plain_hash'
       => { type => 'hash',
	    # hash_class constructor args are all keys of this hash
	    # except type and class
	    hash_class => 'Config::Model::HashId', # default
	    index_type  => 'integer',

	    @element
	  },
        'bounded_hash'
       => { type => 'hash',
	    # hash_class constructor args are all keys of this hash
	    # except type and class
	    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);