File: search_element.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 (394 lines) | stat: -rw-r--r-- 17,388 bytes parent folder | download | duplicates (2)
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
# -*- 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;
use lib "t/lib";

use Data::Dumper;
$Data::Dumper::Indent = 1;

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

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

my $root = $inst->config_root;

ok( $root, "created root" );

my @data = ( [
        'Z',
        "std_id:foo",
        {
            'Z'  => { 'next_step' => { 'Z'  => '' } },
            'X'  => { 'next_step' => { 'X'  => '' } },
            'DX' => { 'next_step' => { 'DX' => '' } } }
    ],
    [
        'ab2', 'warp',
        {
            'ab2' => {
                'next_step' => {
                    'warp2' =>
                        { 'next_class' => { 'SubSlave2' => { 'next_step' => { 'ab2' => '' } } } },
                    'sub_slave' =>
                        { 'next_step' => { 'sub_slave' => { 'next_step' => { 'ab2' => '' } } } } }
            },
            'aa2' => {
                'next_step' => {
                    'warp2' =>
                        { 'next_class' => { 'SubSlave2' => { 'next_step' => { 'aa2' => '' } } } },
                    'sub_slave' =>
                        { 'next_step' => { 'sub_slave' => { 'next_step' => { 'aa2' => '' } } } } }
            },
            'X' => { 'next_step' => { 'X' => '', 'std_id' => { 'next_step' => { 'X' => '' } } } },
            'ac' => { 'next_step' => { 'sub_slave' => { 'next_step' => { 'ac' => '' } } } },
            'Y'  => { 'next_step' => { 'Y'         => '' } },
            'DX' => { 'next_step' => { 'std_id'    => { 'next_step' => { 'DX' => '' } } } },
            'Z'  => {
                'next_step' => {
                    'warp2' =>
                        { 'next_class' => { 'SubSlave2' => { 'next_step' => { 'Z' => '' } } } },
                    'std_id' => { 'next_step' => { 'Z' => '' } },
                    'sub_slave' =>
                        { 'next_step' => { 'sub_slave' => { 'next_step' => { 'Z' => '' } } } } }
            },
            'ab'  => { 'next_step' => { 'sub_slave' => { 'next_step' => { 'ab' => '' } } } },
            'ad2' => {
                'next_step' => {
                    'warp2' =>
                        { 'next_class' => { 'SubSlave2' => { 'next_step' => { 'ad2' => '' } } } },
                    'sub_slave' =>
                        { 'next_step' => { 'sub_slave' => { 'next_step' => { 'ad2' => '' } } } } }
            },
            'ad'  => { 'next_step' => { 'sub_slave' => { 'next_step' => { 'ad' => '' } } } },
            'aa'  => { 'next_step' => { 'sub_slave' => { 'next_step' => { 'aa' => '' } } } },
            'ac2' => {
                'next_step' => {
                    'warp2' =>
                        { 'next_class' => { 'SubSlave2' => { 'next_step' => { 'ac2' => '' } } } },
                    'sub_slave' =>
                        { 'next_step' => { 'sub_slave' => { 'next_step' => { 'ac2' => '' } } } } } }
        }
    ],

    [
        'Z', '!',
        {
            'string_with_def' => { 'next_step' => { 'string_with_def' => '' } },
            'aa2'             => {
                'next_step' => {
                    'slave_y' => {
                        'next_step' => {
                            'warp2' => {
                                'next_class' =>
                                    { 'SubSlave2' => { 'next_step' => { 'aa2' => '' } } }
                            },
                            'sub_slave' => {
                                'next_step' => { 'sub_slave' => { 'next_step' => { 'aa2' => '' } } }
                            } }
                    },
                    'warp' => {
                        'next_class' => {
                            'SlaveY' => {
                                'next_step' => {
                                    'warp2' => {
                                        'next_class' =>
                                            { 'SubSlave2' => { 'next_step' => { 'aa2' => '' } } }
                                    },
                                    'sub_slave' => {
                                        'next_step' =>
                                            { 'sub_slave' => { 'next_step' => { 'aa2' => '' } } } }
                                } } } } }
            },
            'Y' => {
                'next_step' => {
                    'slave_y' => { 'next_step' => { 'Y' => '' } },
                    'warp' => { 'next_class' => { 'SlaveY' => { 'next_step' => { 'Y' => '' } } } } }
            },
            'DX' => {
                'next_step' => {
                    'slave_y' =>
                        { 'next_step' => { 'std_id' => { 'next_step' => { 'DX' => '' } } } },
                    'olist' => { 'next_step' => { 'DX' => '' } },
                    'warp'  => {
                        'next_class' => {
                            'SlaveZ' => { 'next_step' => { 'DX' => '' } },
                            'SlaveY' => {
                                'next_step' => { 'std_id' => { 'next_step' => { 'DX' => '' } } } } }
                    },
                    'std_id' => { 'next_step' => { 'DX' => '' } } }
            },
            'Z' => {
                'next_step' => {
                    'slave_y' => {
                        'next_step' => {
                            'warp2' => {
                                'next_class' => { 'SubSlave2' => { 'next_step' => { 'Z' => '' } } }
                            },
                            'std_id'    => { 'next_step' => { 'Z' => '' } },
                            'sub_slave' => {
                                'next_step' => { 'sub_slave' => { 'next_step' => { 'Z' => '' } } } }
                        }
                    },
                    'olist' => { 'next_step' => { 'Z' => '' } },
                    'warp'  => {
                        'next_class' => {
                            'SlaveZ' => { 'next_step' => { 'Z' => '' } },
                            'SlaveY' => {
                                'next_step' => {
                                    'warp2' => {
                                        'next_class' =>
                                            { 'SubSlave2' => { 'next_step' => { 'Z' => '' } } }
                                    },
                                    'std_id'    => { 'next_step' => { 'Z' => '' } },
                                    'sub_slave' => {
                                        'next_step' =>
                                            { 'sub_slave' => { 'next_step' => { 'Z' => '' } } } } }
                            } }
                    },
                    'std_id' => { 'next_step' => { 'Z' => '' } } }
            },
            'ad2' => {
                'next_step' => {
                    'slave_y' => {
                        'next_step' => {
                            'warp2' => {
                                'next_class' =>
                                    { 'SubSlave2' => { 'next_step' => { 'ad2' => '' } } }
                            },
                            'sub_slave' => {
                                'next_step' => { 'sub_slave' => { 'next_step' => { 'ad2' => '' } } }
                            } }
                    },
                    'warp' => {
                        'next_class' => {
                            'SlaveY' => {
                                'next_step' => {
                                    'warp2' => {
                                        'next_class' =>
                                            { 'SubSlave2' => { 'next_step' => { 'ad2' => '' } } }
                                    },
                                    'sub_slave' => {
                                        'next_step' =>
                                            { 'sub_slave' => { 'next_step' => { 'ad2' => '' } } } }
                                } } } } }
            },
            'tree_macro' => { 'next_step' => { 'tree_macro' => '' } },
            'a_string'   => { 'next_step' => { 'a_string'   => '' } },
            'ad'         => {
                'next_step' => {
                    'slave_y' =>
                        { 'next_step' => { 'sub_slave' => { 'next_step' => { 'ad' => '' } } } },
                    'warp' => {
                        'next_class' => {
                            'SlaveY' => {
                                'next_step' => { 'sub_slave' => { 'next_step' => { 'ad' => '' } } }
                            } } } }
            },
            'ordered_hash' => { 'next_step' => { 'ordered_hash' => '' } },
            'aa'           => {
                'next_step' => {
                    'slave_y' =>
                        { 'next_step' => { 'sub_slave' => { 'next_step' => { 'aa' => '' } } } },
                    'warp' => {
                        'next_class' => {
                            'SlaveY' => {
                                'next_step' => { 'sub_slave' => { 'next_step' => { 'aa' => '' } } }
                            } } } }
            },
            'ac2' => {
                'next_step' => {
                    'slave_y' => {
                        'next_step' => {
                            'warp2' => {
                                'next_class' =>
                                    { 'SubSlave2' => { 'next_step' => { 'ac2' => '' } } }
                            },
                            'sub_slave' => {
                                'next_step' => { 'sub_slave' => { 'next_step' => { 'ac2' => '' } } }
                            } }
                    },
                    'warp' => {
                        'next_class' => {
                            'SlaveY' => {
                                'next_step' => {
                                    'warp2' => {
                                        'next_class' =>
                                            { 'SubSlave2' => { 'next_step' => { 'ac2' => '' } } }
                                    },
                                    'sub_slave' => {
                                        'next_step' =>
                                            { 'sub_slave' => { 'next_step' => { 'ac2' => '' } } } }
                                } } } } }
            },
            'lista'  => { 'next_step' => { 'lista'  => '' } },
            'hash_b' => { 'next_step' => { 'hash_b' => '' } },
            'ab2'    => {
                'next_step' => {
                    'slave_y' => {
                        'next_step' => {
                            'warp2' => {
                                'next_class' =>
                                    { 'SubSlave2' => { 'next_step' => { 'ab2' => '' } } }
                            },
                            'sub_slave' => {
                                'next_step' => { 'sub_slave' => { 'next_step' => { 'ab2' => '' } } }
                            } }
                    },
                    'warp' => {
                        'next_class' => {
                            'SlaveY' => {
                                'next_step' => {
                                    'warp2' => {
                                        'next_class' =>
                                            { 'SubSlave2' => { 'next_step' => { 'ab2' => '' } } }
                                    },
                                    'sub_slave' => {
                                        'next_step' =>
                                            { 'sub_slave' => { 'next_step' => { 'ab2' => '' } } } }
                                } } } } }
            },
            'int_v'        => { 'next_step' => { 'int_v'        => '' } },
            'listb'        => { 'next_step' => { 'listb'        => '' } },
            'yes_no_boolean' => {'next_step' => {'yes_no_boolean' => ''}},
            'my_reference' => { 'next_step' => { 'my_reference' => '' } },
            'X'            => {
                'next_step' => {
                    'slave_y' => {
                        'next_step' => { 'X' => '', 'std_id' => { 'next_step' => { 'X' => '' } } }
                    },
                    'olist' => { 'next_step' => { 'X' => '' } },
                    'warp'  => {
                        'next_class' => {
                            'SlaveZ' => { 'next_step' => { 'X' => '' } },
                            'SlaveY' => {
                                'next_step' =>
                                    { 'X' => '', 'std_id' => { 'next_step' => { 'X' => '' } } } } }
                    },
                    'std_id' => { 'next_step' => { 'X' => '' } } }
            },
            'ac' => {
                'next_step' => {
                    'slave_y' =>
                        { 'next_step' => { 'sub_slave' => { 'next_step' => { 'ac' => '' } } } },
                    'warp' => {
                        'next_class' => {
                            'SlaveY' => {
                                'next_step' => { 'sub_slave' => { 'next_step' => { 'ac' => '' } } }
                            } } } }
            },
            'a_uniline' => { 'next_step' => { 'a_uniline' => '' } },
            'ab'        => {
                'next_step' => {
                    'slave_y' =>
                        { 'next_step' => { 'sub_slave' => { 'next_step' => { 'ab' => '' } } } },
                    'warp' => {
                        'next_class' => {
                            'SlaveY' => {
                                'next_step' => { 'sub_slave' => { 'next_step' => { 'ab' => '' } } }
                            } } } }
            },
            'a_boolean' => {'next_step' => {'a_boolean' => ''}},
            'my_check_list' => { 'next_step' => { 'my_check_list' => '' } },
            'hash_a'        => { 'next_step' => { 'hash_a'        => '' } } } ] );

my @items    = $root->model_searcher->get_searchable_elements;
my @expected = qw/DX X Y Z a_boolean a_string a_uniline aa aa2 ab ab2 ac ac2 ad ad2
    hash_a hash_b int_v lista listb my_check_list
    my_reference ordered_hash string_with_def tree_macro yes_no_boolean/;

is_deeply( \@items, \@expected, "list of searchable items" );

foreach my $item (@data) {
    next unless @$item == 3;
    my $node = $root->grab( $item->[1] );
    my $model_searcher = $node->model_searcher->prepare( element => $item->[0] );

    is_deeply( $model_searcher->{data},
        $item->[2],
        "verify search data on " . $node->config_class_name . "($item->[0],$item->[1])" )
        || print Dumper $model_searcher->{data};
}

my $model_searcher = $root->model_searcher->prepare( element => 'X' );
$root->load("tree_macro=XZ");

my $step = $model_searcher->next_step();
is_deeply( $step, [qw/olist slave_y std_id warp/], 'check first step' );

my $obj = $model_searcher->choose('warp');
is( $obj->name, 'warp', 'check chosen object' );

my $target = $model_searcher->auto_choose( sub { }, sub { } );
is( $target->name, 'warp X', 'check auto chosen object for X' );

$step = $model_searcher->next_step();
is_deeply( $step, [], 'check that no more steps are left' );

# no user choice to look for aa
$root->load("tree_macro=XY");
$model_searcher = $root->model_searcher->prepare( element => 'aa' );
$model_searcher->choose('warp');
$target = $model_searcher->auto_choose( sub { }, sub { } );
is( $target->name, 'warp sub_slave aa', 'check auto chosen object for aa' );

# try choose_next
$model_searcher = $root->model_searcher->prepare( element => 'aa' );
$model_searcher->choose('warp');
$step = $model_searcher->next_choice();
is_deeply( $step, [], 'check that no more steps are left after next_choice' );
$target = $model_searcher->current_object;
is( $target->name, 'warp sub_slave aa', 'check chosen object for aa' );

$model_searcher = $root->model_searcher->prepare( element => 'DX' );
$root->load("tree_macro=XZ");
my $cb1 = sub {
    my $object = shift;
    is( $object->config_class_name, 'Master', 'check object of element call-back (DX))' );
    is_deeply( [@_], [qw/olist slave_y std_id warp/], 'check param of element call-back (DX)' );
    return 'warp';
};

$target = $model_searcher->auto_choose( $cb1, sub { } );
is( $target->name, 'warp DX', 'check auto chosen object for DX (warp)' );

# restart and try through olist
$model_searcher->reset;
$target = $model_searcher->auto_choose( sub { 'olist' }, sub { return 1; } );

is( $target->name, 'olist:1 DX', 'check auto_choose target for DX (olist)' );

# restart and try through std_d
$model_searcher->reset;
$target = $model_searcher->auto_choose( sub { 'std_id' }, sub { return 'foo'; } );

is( $target->name, 'std_id:foo DX', 'check auto_choose target for DX (std_id)' );

# restart and try through std_d with next_choice
$model_searcher->reset;
$step = $model_searcher->next_choice();
is_deeply( $step, [qw/olist slave_y std_id warp/], 'next_choice 1' );

$model_searcher->choose('std_id');
ok( 1, "std_id choice done" );

#print $root->dump_tree(full_dump =>1) ;

$step = $model_searcher->next_choice();
is_deeply( $step, [], 'next_choice 2' );

is( $model_searcher->current_object->name, 'std_id:foo DX', 'next_choice target' );
memory_cycle_ok($model, "memory cycle");

done_testing;