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
|
# -*- cperl -*-
use ExtUtils::testlib;
use Test::More;
use Test::Memory::Cycle;
use Test::Differences;
use Config::Model;
use Config::Model::Tester::Setup qw/init_test/;
use warnings;
use strict;
use lib "t/lib";
my ($model, $trace) = init_test();
my $inst = $model->instance(
root_class_name => 'Master',
model_file => 'dump_load_model.pl',
instance_name => 'test1'
);
ok( $inst, "created dummy instance" );
my $root = $inst->config_root;
ok( $root, "Config root created" );
$inst->preset_start;
$root->fetch_element( name => 'hidden_string', accept_hidden => 1 )->store('hidden value');
my $step = 'std_id:ab X=Bv ' . '! lista:=a,b listb:=b ';
ok( $root->load( step => $step ), "preset data in tree with '$step'" );
$inst->preset_stop;
$step =
'std_id:ab X=Bv - std_id:bc X=Av - std_id:"b d " X=Av '
. '- a_string="toto \"titi\" tata" another_string="foobar" a_string2=dod@foo.com '
. 'a_string_to_test_newline="foo\nbar\\\\nbaz" '
. 'lista:=a,b,c,d olist:0 X=Av - olist:1 X=Bv - listb:=b,"c c2",d listc:="dod@foo.com" '
. '! hash_a:X2=x hash_a:Y2=xy hash_b:X3=xy my_check_list=X2,X3 hash_b:comment_test="#"'
. ' hash_a:"~"="~/bar" ';
ok( $root->load( step => $step ), "set up data in tree" );
eq_or_diff(
[ sort $root->fetch_element('std_id')->fetch_all_indexes ],
[ 'ab', 'b d ', 'bc' ],
"check std_id keys"
);
eq_or_diff( [ sort $root->fetch_element('lista')->fetch_all_values( mode => 'custom' ) ],
[qw/c d/], "check lista custom values" );
my $cds = $root->dump_tree;
print "cds string:\n$cds" if $trace;
subtest "test round trip" => sub {
my $load_inst = $model->instance(
root_class_name => 'Master',
model_file => 'dump_load_model.pl',
instance_name => 'round_trip'
);
my $round_root = $load_inst->config_root;
ok($round_root->load($cds), "test round trip: load");
my $new_cds = $round_root->dump_tree;
eq_or_diff(
[ split /\n/, $new_cds ],
[ split /\n/, $cds ],
"test round trip: dump "
)
};
my $orig_expect = <<'EOF' ;
std_id:ab -
std_id:"b d "
X=Av -
std_id:bc
X=Av -
lista:=c,d
listb:="c c2",d
listc:="dod@foo.com"
hash_a:X2=x
hash_a:Y2=xy
hash_a:"~"="~/bar"
hash_b:X3=xy
hash_b:comment_test="#"
olist:0
X=Av -
olist:1
X=Bv -
a_string="toto \"titi\" tata"
a_string2=dod@foo.com
a_string_to_test_newline="foo
bar\\nbaz"
another_string=foobar
my_check_list=X2,X3 -
EOF
$cds =~ s/\s+\n/\n/g;
eq_or_diff(
[ split /\n/, $cds ],
[ split /\n/, $orig_expect ],
"check dump of only customized values "
);
$cds = $root->dump_tree( mode => 'user' );
print "cds string:\n$cds" if $trace;
my $expect = <<'EOF' ;
std_id:ab
X=Bv
DX=Dv -
std_id:"b d "
X=Av
DX=Dv -
std_id:bc
X=Av
DX=Dv -
lista:=a,b,c,d
listb:=b,"c c2",d
listc:="dod@foo.com"
hash_a:X2=x
hash_a:Y2=xy
hash_a:"~"="~/bar"
hash_b:X3=xy
hash_b:comment_test="#"
olist:0
X=Av
DX=Dv -
olist:1
X=Bv
DX=Dv -
string_with_def="yada yada"
a_uniline="yada yada"
a_string="toto \"titi\" tata"
a_string2=dod@foo.com
a_string_to_test_newline="foo
bar\\nbaz"
another_string=foobar
int_v=10
my_check_list=X2,X3 -
EOF
$cds =~ s/\s+\n/\n/g;
eq_or_diff( [ split /\n/, $cds ], [ split /\n/, $expect ], "check dump of all values " );
my $listb = $root->fetch_element('listb');
$listb->clear;
$cds = $root->dump_tree( mode => 'user' );
print "cds string:\n$cds" if $trace;
$expect = <<'EOF' ;
std_id:ab
X=Bv
DX=Dv -
std_id:"b d "
X=Av
DX=Dv -
std_id:bc
X=Av
DX=Dv -
lista:=a,b,c,d
listc:="dod@foo.com"
hash_a:X2=x
hash_a:Y2=xy
hash_a:"~"="~/bar"
hash_b:X3=xy
hash_b:comment_test="#"
olist:0
X=Av
DX=Dv -
olist:1
X=Bv
DX=Dv -
string_with_def="yada yada"
a_uniline="yada yada"
a_string="toto \"titi\" tata"
a_string2=dod@foo.com
a_string_to_test_newline="foo
bar\\nbaz"
another_string=foobar
int_v=10
my_check_list=X2,X3 -
EOF
$cds =~ s/\s+\n/\n/g;
eq_or_diff(
[ split /\n/, $cds ],
[ split /\n/, $expect ],
"check dump of all values after listb is cleared"
);
# check empty strings
my $a_s = $root->fetch_element('a_string');
$a_s->store("");
$expect = <<'EOF' ;
std_id:ab
X=Bv
DX=Dv -
std_id:"b d "
X=Av
DX=Dv -
std_id:bc
X=Av
DX=Dv -
lista:=a,b,c,d
listc:="dod@foo.com"
hash_a:X2=x
hash_a:Y2=xy
hash_a:"~"="~/bar"
hash_b:X3=xy
hash_b:comment_test="#"
olist:0
X=Av
DX=Dv -
olist:1
X=Bv
DX=Dv -
string_with_def="yada yada"
a_uniline="yada yada"
a_string=""
a_string2=dod@foo.com
a_string_to_test_newline="foo
bar\\nbaz"
another_string=foobar
int_v=10
my_check_list=X2,X3 -
EOF
$cds = $root->dump_tree( mode => 'user' );
print "cds string:\n$cds" if $trace;
$cds =~ s/\s+\n/\n/g;
eq_or_diff(
[ split /\n/, $cds ],
[ split /\n/, $expect ],
"check dump of all values after a_string is set to ''"
);
# check preset values
$cds = $root->dump_tree( mode => 'preset' );
print "cds string:\n$cds" if $trace;
$expect = <<'EOF' ;
std_id:ab
X=Bv -
std_id:"b d " -
std_id:bc -
lista:=a,b
olist:0 -
olist:1 - -
EOF
$cds =~ s/\s+\n/\n/g;
eq_or_diff( [ split /\n/, $cds ], [ split /\n/, $expect ], "check dump of all preset values" );
# shake warp stuff
my $tm = $root->fetch_element('tree_macro');
for ( qw/XY XZ mXY XY mXY XZ/ ) { $tm->store($_); }
$cds = $root->dump_tree( mode => 'user', skip_auto_write => 'cds_file' );
print "cds string:\n$cds" if $trace;
like( $cds, qr/hidden value/, "check that hidden value is shown (macro=XZ)" );
# check that list of undef is not shown
for ( 0 .. 3 ) { $listb->fetch_with_id($_)->store(undef) }
$cds = $root->dump_tree( mode => 'user' );
print "Empty listb dump:\n$cds" if $trace;
unlike( $cds, qr/listb/, "check that listb containing undef values is not shown" );
# reload test
my $reload_root = $model->instance(
root_class_name => 'Master',
instance_name => 'reload_test'
)->config_root;
$reload_root->load($orig_expect);
my $reloaded_dump = $reload_root->dump_tree;
eq_or_diff(
[ split /\n/, $reloaded_dump ],
[ split /\n/, $orig_expect ],
"check dump of tree load with dump result"
);
# annotation tests
my $root2 = $model->instance(
root_class_name => 'Master',
instance_name => 'test2'
)->config_root;
$step = ' std_id:ab#std_id_ab_note
X=Bv X#std_id_ab_X_note
- std_id#std_id_note std_id:bc X=Av X#std_id_bc_X_note '
. '- a_string="toto \"titi\" tata" a_string#a_string_note another_string="foobar"'
. 'lista#lista_note lista:=a,b,c,d lista:1#lista_1_note olist#o_list_note olist:0#olist_0_note X=Av - olist:1#olist1_c X=Bv - listb:=b,"c c2",d '
. '! hash_a:X2=x#hash_a_X2 hash_a:Y2=xy#"hash_a Y2 note" hash_b:X3=xy#hash_b_X3
my_check_list=X2,X3 plain_object#"plain comment" aa2=aa2_value';
ok( $root2->load( step => $step ), "set up data in tree annotation" );
is( $root2->fetch_element('std_id')->annotation, 'std_id_note', "check annotation for std_id" );
is( $root2->grab('std_id:ab')->annotation, 'std_id_ab_note', "check annotation for std_id:ab" );
is( $root2->grab('olist:0')->annotation, 'olist_0_note', "check annotation for olist:0" );
my $expect_count = scalar grep { /#/ } split //, $step;
$cds = $root2->dump_tree( mode => 'user' );
print "Dump with annotations:\n$cds" if $trace;
is( ( scalar grep { /#/ } split //, $cds ),
$expect_count, "check that $expect_count annotations are found" );
my $root3 = $model->instance(
root_class_name => 'Master',
instance_name => 'test3'
)->config_root;
ok(
$root3->load( step => $cds ),
"set up data in tree with dumped data+annotation"
);
my $cds2 = $root3->dump_tree( mode => 'user' );
print "Dump second instance with annotations:\n$cds2" if $trace;
is( $cds2, $cds, "check both dumps" );
memory_cycle_ok( $model, "memory cycles" );
done_testing;
|