1use strict;
2use warnings;
3use Test::More tests => 106;
4do "t/lib/helpers.pl";
5
6BEGIN { use_ok('YAML::AppConfig') }
7
8# TEST: Object creation from a file, string, and object.
9{
10    test_object_creation( string => slurp("t/data/normal.yaml") );
11    my $app = test_object_creation( file => "t/data/normal.yaml" );
12    test_object_creation( object => $app );
13}
14
15sub test_object_creation {
16    my $app = YAML::AppConfig->new( @_ );
17    ok( $app, "Checking object creation." );
18
19    # Test obvious accessors are there.
20    for my $method (qw(food man good is i_like and)) {
21        ok( $app->can("get_$method"), "Testing $method getter" );
22        ok( $app->can("set_$method"), "Testing $method setter" );
23    }
24
25    # Test we didn't get some strays.
26    for my $not_method (qw(yummy like or)) {
27        ok( !$app->can("get_$not_method"), "Testing !$not_method getter" );
28        ok( !$app->can("set_$not_method"), "Testing !$not_method getter" );
29    }
30
31    # Test our wild things
32    ok( !$app->can('get_somtin!^^^wild'),
33        "Checking we can't do get_somtin!^^^wild" );
34    ok( !$app->can('set_somtin!^^^wild'),
35        "Checking we can't do set_somtin!^^^wild" );
36    is( $app->get('somtin!^^^wild'), 'cows', "Retrieving somtin!^^^wild" );
37    ok( !$app->can('get_12_foo'), "Checking we can't do get_12_foo" );
38    ok( !$app->can('set_12_foo'), "Checking we can't do set_12_foo" );
39    is( $app->get('12_foo'), 'blah', "Retrieving 12_foo" );
40
41    # Test the values
42    is( $app->get_food,  'oh',   "food test" );
43    is( $app->get_man,  'yeah', "man test" );
44    is( $app->get_good, 'it',   "good test" );
45    is( $app->get_is,   'good', "is test" );
46    is_deeply( $app->get_i_like, [ 'tofu', 'spatetee', 'ice cream' ],
47        "i_like test" );
48    is_deeply(
49        $app->get_and,
50        {
51            yummy => 'stuff', like => 'popcorn',
52            'or' => [qw(candy fruit bars)]
53        },
54        "and test"
55    );
56
57    return $app;
58}
59
60# TEST: Two objects don't clober each others methods.
61{
62    my $bar = "get_bar";
63
64    my $app1 = YAML::AppConfig->new( string => "---\nfoo: 1\nbar: 2\n" );
65    ok( $app1, "Checking object creation." );
66    ok( $app1->can("get_foo"), "Checking that \$app1 has get_foo method." );
67    ok( $app1->can("get_bar"), "Checking that \$app1 has get_bar method." );
68    is( $app1->$bar, 2, "Checking bar1 is ok." );
69
70    my $app2 = YAML::AppConfig->new( string => "---\nqux: 3\nbar: 5\n" );
71    ok( $app2, "Checking object creation." );
72    ok( $app2->can("get_qux"), "Checking that \$app2 has get_qux method." );
73    ok( $app2->can("get_bar"), "Checking that \$app2 has get_bar method." );
74    is( $app2->$bar, 5, "Checking bar2 is ok." );
75
76    # Make sure we didn't clober each other.
77    is( $app1->$bar, 2, "Checking that value of bar1 in app1 is the same." );
78    is( $app2->$bar, 5, "Checking that value of bar2 in app1 is the same." );
79}
80
81# TEST: Make sure we don't get warnings on undef. values
82{
83    my $app = YAML::AppConfig->new( string => "---\nnovalue:\n" );
84    ok($app, "Object created");
85    local $SIG{__WARN__} = sub { die };
86    eval { $app->get_novalue };
87    ok( !$@, "Getting a setting with no value does not produce warnings." );
88}
89