1# -*- cperl -*-
2
3use warnings;
4
5use ExtUtils::testlib;
6use Test::More;
7use Test::Warn;
8use Test::Memory::Cycle;
9use Config::Model;
10use Config::Model::Tester::Setup qw/init_test/;
11
12use strict;
13use lib "t/lib";
14
15use Test::Log::Log4perl;
16
17my ($model, $trace) = init_test();
18
19$model->create_config_class(
20    name    => "WarnMaster",
21    element => [
22        warn_if => {
23            type          => 'leaf',
24            value_type    => 'string',
25            warn_if_match => { 'foo' => { fix => '$_ = uc;' } },
26        },
27        warn_unless => {
28            type              => 'leaf',
29            value_type        => 'string',
30            warn_unless_match => { foo => { msg => '', fix => '$_ = "foo".$_;' } },
31        },
32    ] );
33
34my $messager ;
35my $inst = $model->instance(
36    root_class_name => 'WarnMaster',
37    instance_name   => 'test1',
38    root_dir        => 'foobar',
39    on_message_cb   => sub { $messager = shift;},
40);
41ok( $inst, "created dummy instance" );
42
43ok( $model->instance(name => 'test1'), "check that instance can be retrieved by name");
44
45$inst->show_message('hello');
46is($messager,'hello',"test show_message_cb");
47
48isa_ok( $inst->config_root, 'Config::Model::Node', "test config root class" );
49
50is( $inst->data('test'), undef, "test empty private data ..." );
51$inst->data( 'test', 'coucou' );
52is( $inst->data('test'), 'coucou', "retrieve private data" );
53
54is( $inst->root_dir->stringify,  'foobar', "test config root directory" );
55
56# test if fixes can be applied through the instance
57my $root = $inst->config_root;
58my $wip  = $root->fetch_element('warn_if');
59my $wup  = $root->fetch_element('warn_unless');
60
61my $wt = Test::Log::Log4perl->get_logger("User");
62Test::Log::Log4perl->start(ignore_priority => "info");
63$wt->warn(qr/should not match/);
64$wt->warn(qr/should match/);
65$wip->store('foobar');
66$wup->store('bar');
67Test::Log::Log4perl->end("test warn_if and warn_unless condition (instance test)");
68
69is( $inst->has_warning, 2, "check warning count at instance level" );
70$inst->apply_fixes;
71is( $wup->fetch,        'foobar', "test if fixes were applied (instance test)" );
72is( $wup->fetch,        'foobar', "test if fixes were applied (instance test)" );
73is( $inst->has_warning, 0,        "check cleared warning count at instance level" );
74
75my $binst = $model->instance(
76    root_class_name => 'Master',
77    instance_name   => 'test2'
78);
79ok( $binst, "created dummy instance" );
80
81my $root2 = $binst->config_root;
82
83my $step =
84      'std_id:ab X=Bv - std_id:bc X=Av - a_string="toto tata" '
85    . 'lista=a,b,c,d olist:0 X=Av - olist:1 X=Bv - listb=b,c,d '
86    . '! hash_a:X2=x hash_a:Y2=xy  hash_b:X3=xy my_check_list=X2,X3';
87
88ok( $root2->load( step => $step ), "set up data in tree with '$step'" );
89
90is( $binst->has_warning, 0, "test has_warning with big model" );
91
92memory_cycle_ok($model, "memory cycles");
93
94done_testing;
95