forked from rjbs/Config-INI
/
reader.t
96 lines (79 loc) · 2.09 KB
/
reader.t
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
#!perl -w
use strict;
use IO::File;
use IO::String;
use Test::More tests => 8;
# Check their perl version
use_ok('Config::INI::Reader');
# Try to read in a config
my $hashref = Config::INI::Reader->read_file( 'corpus/simple.ini' );
isa_ok($hashref, 'HASH', "return of Config::INI::Reader->read_file");
# Check the structure of the config
my $expected = {
'_' => {
root => 'something',
},
section => {
one => 'two',
Foo => 'Bar',
this => 'Your Mother!',
blank => '',
moo => 'kooh',
},
'Section Two' => {
'something else' => 'blah',
'remove' => 'whitespace',
},
};
is_deeply($hashref, $expected, 'Config structure matches expected');
# Add some stuff to the trivial config and check write_string() for it
my $Trivial = {};
$Trivial->{_} = { root1 => 'root2' };
$Trivial->{section} = {
foo => 'bar',
this => 'that',
blank => '',
};
$Trivial->{section2} = {
'this little piggy' => 'went to market'
};
my $string = <<END;
root1=root2
[ section ]
blank=
foo=bar
this=that
[section2]
this little piggy=went to market
END
{ # Test read_string
my $hashref = Config::INI::Reader->read_string( $string );
isa_ok($hashref, 'HASH', "return of Config::INI::Reader->read_string");
is_deeply( $hashref, $Trivial, '->read_string returns expected value' );
}
{ # Test read_handle
my $fh = IO::File->new('corpus/simple.ini', 'r');
my $data = do { local $/ = undef; <$fh> };
is_deeply(
Config::INI::Reader->new->read_handle( IO::String->new($data) ),
$expected,
'->read_handle returns expected value'
);
}
#####################################################################
# Bugs that happened we don't want to happen again
{
# Reading in an empty file, or a defined but zero length string, should yield
# a valid, but empty, object.
my $empty = Config::INI::Reader->read_string('');
is_deeply($empty, {}, "an empty string gets an empty hashref");
}
{
# "0" is a valid section name
my $config = Config::INI::Reader->read_string("[0]\nfoo = 1\n");
is_deeply(
$config,
{ 0 => { foo => 1 } },
"we can use 0 as a section name",
);
}