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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
|
# -*- cperl -*-
use warnings FATAL => qw(all);
use ExtUtils::testlib;
use Test::More;
use Test::Exception;
use Test::Warn ;
use Test::Differences ;
use Test::Memory::Cycle;
use Config::Model;
use Config::Model::AnyId;
use Log::Log4perl qw(:easy :levels) ;
BEGIN { plan tests => 107; }
use strict;
my $arg = shift || '';
my $log = 0 ;
my $trace = $arg =~ /t/ ? 1 : 0 ;
$log = 1 if $arg =~ /l/;
my $home = $ENV{HOME} || "";
my $log4perl_user_conf_file = "$home/.log4config-model";
if ($log and -e $log4perl_user_conf_file ) {
Log::Log4perl::init($log4perl_user_conf_file);
}
else {
Log::Log4perl->easy_init($log ? $WARN: $ERROR);
}
my $model = Config::Model -> new ( ) ;
Config::Model::Exception::Any->Trace(1) if $arg =~ /e/;
ok(1,"compiled");
my @element = (
# Value constructor args are passed in their specific array ref
cargo => {
type => 'leaf',
value_type => 'string'
},
);
# minimal set up to get things working
$model->create_config_class(
name => "Master",
element => [
bounded_list => {
type => 'list',
list_class => 'Config::Model::ListId', # default
max => 123,
cargo => {
type => 'leaf',
value_type => 'string'
},
},
plain_list => { type => 'list', @element },
list_with_auto_created_id => {
type => 'list',
auto_create_ids => 4,
@element
},
olist => {
type => 'list',
cargo => {
type => 'node',
config_class_name => 'Slave'
},
},
list_with_default_with_init_leaf => {
type => 'list',
default_with_init => {
0 => 'def_1 stuff',
1 => 'def_2 stuff'
},
@element,
},
list_with_default_with_init_node => {
type => 'list',
default_with_init => {
0 => 'X=Bv Y=Cv',
1 => 'X=Av'
},
cargo => {
type => 'node',
config_class_name => 'Slave'
},
},
map {
("list_with_".$_."_duplicates" => { type => 'list', duplicates => $_ , @element, },);
} qw/warn allow forbid suppress/ ,
]
);
$model->create_config_class(
name => "Bogus",
element => [
list_with_wrong_auto_create => {
type => 'list',
auto_create_ids => ['foo'],
@element
},
list_with_wrong_duplicates => {
type => 'list',
duplicates => 'forbid',
cargo => {
type => 'node',
config_class_name => 'Slave'
},
},
list_with_yada_duplicates => {
type => 'list',
duplicates => 'yada' ,
@element,
},
]
);
$model->create_config_class(
name => "Slave",
element => [
[qw/X Y Z/] => {
type => 'leaf',
value_type => 'enum',
choice => [qw/Av Bv Cv/]
},
]
);
ok(1,"config classes created") ;
my $inst = $model->instance(
root_class_name => 'Master',
instance_name => 'test1'
);
ok( $inst, "created dummy instance" );
$inst->initial_load_stop ;
my $root = $inst->config_root;
eq_or_diff( [ $root->fetch_element('olist')->fetch_all_indexes ],
[], "check index list of empty list" );
is($inst->needs_save,0,"verify instance needs_save status after creation") ;
my $b = $root->fetch_element('bounded_list');
ok( $b, "bounded list created" );
is($inst->needs_save,0,"verify instance needs_save status after element creation") ;
is( $b->fetch_with_id(1)->store('foo'), 'foo', "stored in 1" );
is( $b->fetch_with_id(0)->store('baz'), 'baz', "stored in 0" );
is( $b->fetch_with_id(2)->store('bar'), 'bar', "stored in 2" );
is($inst->needs_save,3,"verify instance needs_save status after storing into element") ;
print join("\n", $inst->list_changes("\n")),"\n" if $trace;
throws_ok { $b->fetch_with_id(124)->store('baz'); }
qr/Index 124 > max_index limit 123/, 'max error caught';
my $bogus_root = $model->instance( root_class_name => 'Bogus' )->config_root;
throws_ok { $bogus_root->fetch_element('list_with_wrong_auto_create'); }
qr/Wrong auto_create argument for list/, 'wrong auto_create caught';
eq_or_diff( [ $b->fetch_all_indexes ], [ 0, 1, 2 ], "check ids" );
$b->delete(1);
is( $b->fetch_with_id(1)->fetch, undef, "check deleted id" );
is( $b->index_type, 'integer', 'check list index_type' );
is( $b->max_index, 123, 'check list max boundary' );
$b->push( 'toto', 'titi' );
is( $b->fetch_with_id(2)->fetch, 'bar', "check last item of table" );
is( $b->fetch_with_id(3)->fetch, 'toto', "check pushed toto item" );
is( $b->fetch_with_id(4)->fetch, 'titi', "check pushed titi item" );
$b->push_x(
values => [ 'toto', 'titi' ],
check => 'no',
annotation => ['toto comment']
);
is( $b->fetch_with_id(5)->fetch, 'toto', "check pushed toto item with push_x" );
is( $b->fetch_with_id(5)->annotation,
'toto comment', "check pushed toto annotation with push_x" );
is( $b->fetch_with_id(6)->fetch, 'titi', "check pushed titi item with push_x" );
$b->push_x(
values => 'toto2',
check => 'no',
annotation => 'toto2 comment'
);
is( $b->fetch_with_id(7)->fetch, 'toto2', "check pushed toto2 item with push_x" );
is( $b->fetch_with_id(7)->annotation,
'toto2 comment', "check pushed toto2 annotation with push_x" );
my @all = $b->fetch_all_values;
eq_or_diff( \@all, [qw/baz bar toto titi toto titi toto2/], "check fetch_all_values" );
my $lac = $root->fetch_element('list_with_auto_created_id');
eq_or_diff(
[ $lac->fetch_all_indexes ],
[ 0 .. 3 ],
"check list_with_auto_created_id"
);
map { is( $b->fetch_with_id($_)->index_value, $_, "Check index value $_" ); }
( 0 .. 4 );
$b->move( 3, 4 );
is( $b->fetch_with_id(3)->fetch, undef, "check after move idx 3 in 4" );
is( $b->fetch_with_id(4)->fetch, 'toto', "check after move idx 3 in 4" );
map {
is( $b->fetch_with_id($_)->index_value, $_, "Check moved index value $_" );
} ( 0 .. 4 );
$b->fetch_with_id(3)->store('titi');
$b->swap( 3, 4 );
map {
is( $b->fetch_with_id($_)->index_value, $_,
"Check swapped index value $_" );
} ( 0 .. 4 );
is( $b->fetch_with_id(3)->fetch, 'toto', "check value after swap" );
is( $b->fetch_with_id(4)->fetch, 'titi', "check value after swap" );
$b->remove(3);
is( $b->fetch_with_id(3)->fetch, 'titi', "check after remove" );
# test move swap with node list
my $ol = $root->fetch_element('olist');
my @set = ( [qw/X Av/], [qw/X Bv/], [qw/Y Av/], [qw/Z Cv/], [qw/Z Av/], );
my $i = 0;
foreach my $item (@set) {
my ( $e, $v ) = @$item;
$ol->fetch_with_id( $i++ )->fetch_element($e)->store($v);
}
$inst->clear_changes ;
$ol->move( 3, 4 );
is($inst->needs_save,1,"verify instance needs_save status after move") ;
print scalar $inst->list_changes,"\n" if $trace ;
$inst->clear_changes ;
is( $ol->fetch_with_id(3)->fetch_element('Z')->fetch,
undef, "check after move idx 3 in 4" );
is( $ol->fetch_with_id(4)->fetch_element('Z')->fetch,
'Cv', "check after move idx 3 in 4" );
map {
is( $ol->fetch_with_id($_)->index_value, $_, "Check moved index value $_" );
} ( 0 .. 4 );
$ol->swap( 0, 2 );
is($inst->needs_save,1,"verify instance needs_save status after move") ;
print scalar $inst->list_changes,"\n" if $trace ;
$inst->clear_changes ;
is( $ol->fetch_with_id(0)->fetch_element('X')->fetch,
undef, "check after move idx 0 in 2" );
is( $ol->fetch_with_id(0)->fetch_element('Y')->fetch, 'Av',
"check after move" );
is( $ol->fetch_with_id(2)->fetch_element('Y')->fetch,
undef, "check after move" );
is( $ol->fetch_with_id(2)->fetch_element('X')->fetch, 'Av',
"check after move" );
map {
is( $ol->fetch_with_id($_)->index_value, $_, "Check moved index value $_" );
} ( 0 .. 4 );
print $root->dump_tree( experience => 'beginner' ) if $trace;
is( $ol->fetch_with_id(0)->fetch_element('X')->fetch,
undef, "check before move" );
$ol->remove(0);
print $root->dump_tree( experience => 'beginner' ) if $trace;
is( $ol->fetch_with_id(0)->fetch_element('X')->fetch, 'Bv',
"check after move" );
# test store
my @test = (
[ a1 => ['a1'] ],
[ '"a","b"' => [qw/a b/] ],
[ 'a,b' => [qw/a b/] ],
[ '"a\"a",b' => [qw/a"a b/] ],
[ '"a,a",b' => [ 'a,a', 'b' ] ],
[ '",a1"' => [',a1'] ],
);
foreach my $l (@test) {
$b->load( $l->[0] );
eq_or_diff( [ $b->fetch_all_values ], $l->[1], "test store $l->[0]" );
}
throws_ok { $b->load('a,,b'); } "Config::Model::Exception::Load",
"fails load 'a,,b'";
# test preset mode
$inst->preset_start;
my $pl = $root->fetch_element('plain_list');
$pl->fetch_with_id(0)->store('prefoo');
$pl->fetch_with_id(1)->store('prebar');
$inst->preset_stop;
ok( 1, "filled preset values" );
eq_or_diff(
[ $pl->fetch_all_values ],
[ 'prefoo', 'prebar' ],
"check that preset values are read"
);
$pl->fetch_with_id(2)->store('bar');
eq_or_diff(
[ $pl->fetch_all_values ],
[ 'prefoo', 'prebar', 'bar' ],
"check that values are read"
);
eq_or_diff( [ $pl->fetch_all_values( mode => 'custom' ) ],
['bar'], "check that custom values are read" );
# test default_with_init on leaf
my $lwdwil = $root->fetch_element('list_with_default_with_init_leaf');
# note: calling fetch_all_indexes is required to trigger creation of default_with_init keys
eq_or_diff([$lwdwil->fetch_all_indexes],[0,1],"check default keys");
is($lwdwil->fetch_with_id(0)->fetch,'def_1 stuff',"test default_with_init leaf 0") ;
is($lwdwil->fetch_with_id(1)->fetch,'def_2 stuff',"test default_with_init leaf 1") ;
# test default_with_init on node
my $lwdwin = $root->fetch_element('list_with_default_with_init_node');
eq_or_diff([$lwdwin->fetch_all_indexes],[0,1],"check default keys");
is($lwdwin->fetch_with_id(0)->fetch_element('X')->fetch,'Bv',"test default_with_init node 0") ;
is($lwdwin->fetch_with_id(0)->fetch_element('Y')->fetch,'Cv',"test default_with_init node 0") ;
is($lwdwin->fetch_with_id(1)->fetch_element('X')->fetch,'Av',"test default_with_init node 0") ;
throws_ok { $bogus_root->fetch_element('list_with_wrong_duplicates'); } "Config::Model::Exception::Model",
"fails duplicates with node cargo";
throws_ok { $bogus_root->fetch_element('list_with_yada_duplicates'); } "Config::Model::Exception::Model",
"fails yada duplicates";
foreach my $what (qw/forbid warn suppress/) {
my $lwd = $root->fetch_element('list_with_'.$what.'_duplicates');
$lwd->push(qw/string1 string2/);
$lwd->push('string1'); # does not trigger duplicate issues, yet
$lwd->push('string1'); # does not trigger duplicate issues, yet
# there we go
if ($what eq 'forbid') {
is($lwd->needs_check, 1, "verify needs_check is true") ;
throws_ok { $lwd->fetch_all_values ; } "Config::Model::Exception::WrongValue",
"fails forbidden duplicates" ;
is($lwd->needs_check, 0, "verify needs_check after fetch_all_values") ;
throws_ok { $lwd->fetch_all_values ; } "Config::Model::Exception::WrongValue",
"fails forbidden duplicates even if needs_check is false" ;
is($lwd->needs_check, 0, "verify again needs_check after fetch_all_values") ;
$lwd->delete(2) ;
is($lwd->needs_check, 1, "verify needs_check after list content modif") ;
}
elsif ($what eq 'warn') {
warnings_like { $lwd->fetch_all_values ; } qr/Duplicated/ ,
"warns with duplicated values" ;
is($lwd->has_fixes, 2,"check nb of fixes") ;
$inst->apply_fixes ;
warnings_like { $lwd->fetch_all_values ; } [] , # no warning accepted
"no longer warns with duplicated values" ;
}
else {
$lwd->check_content ;
}
is ($lwd->fetch_with_id(0)->fetch,'string1',
"check that original values is untouched after $what duplicates");
}
# test preset clear stuff
# done after auto_create_ids tests, because preset_clear or layered_clear
# also clean up auto_create_ids (if there's no data in there)
$pl->clear ;
eq_or_diff( [ $pl->fetch_all_indexes ], [ ] ,"check that preset stuff was cleared");
$inst->preset_start;
$pl->fetch_with_id(0)->store('prefoo');
$pl->fetch_with_id(1)->store('prebar');
$inst->preset_stop;
eq_or_diff( [ $pl->fetch_all_indexes ], [0,1] ,"check preset indexes");
$pl->fetch_with_id(1)->store('bar');
$inst->preset_clear ;
eq_or_diff( [ $pl->fetch_all_indexes ], [0] ,"check that only preset stuff was cleared");
is($pl->fetch_with_id(0)->fetch,'bar',"check that bar was moved from 1 to 0");
# test layered stuff
$pl->clear ;
$inst->layered_start;
$pl->fetch_with_id(0)->store('prefoo');
$pl->fetch_with_id(1)->store('prebar');
$inst->layered_stop;
eq_or_diff( [ $pl->fetch_all_indexes ], [0,1] ,"check layered indexes");
$pl->fetch_with_id(1)->store('bar');
$inst->layered_clear ;
eq_or_diff( [ $pl->fetch_all_indexes ], [0] ,"check that only layered stuff was cleared");
is($pl->fetch_with_id(0)->fetch,'bar',"check that bar was moved from 1 to 0");
$pl->clear ;
memory_cycle_ok($model,"memory cycles");
|