| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
use Test::More tests => 19; |
| 7 |
|
| 8 |
use Config::IniFiles; |
| 9 |
|
| 10 |
use lib "./t/lib"; |
| 11 |
|
| 12 |
use Config::IniFiles::TestPaths; |
| 13 |
|
| 14 |
my %ini; |
| 15 |
my ( $ini, $value ); |
| 16 |
my (@value); |
| 17 |
|
| 18 |
# Get files from the 't' directory, portably |
| 19 |
t_unlink("test05.ini"); |
| 20 |
|
| 21 |
# Test 1 |
| 22 |
# Tying a hash. |
| 23 |
# TEST |
| 24 |
ok( |
| 25 |
( |
| 26 |
tie %ini, 'Config::IniFiles', |
| 27 |
( -file => t_file("test.ini"), -default => 'test1', -nocase => 1 ) |
| 28 |
), |
| 29 |
"Tie to test.ini was successful." |
| 30 |
); |
| 31 |
|
| 32 |
tied(%ini)->SetFileName( t_file("test05.ini") ); |
| 33 |
tied(%ini)->SetWriteMode("0666"); |
| 34 |
|
| 35 |
# Test 2 |
| 36 |
# Retrieve scalar value |
| 37 |
$value = $ini{test1}{one}; |
| 38 |
|
| 39 |
# TEST |
| 40 |
is( $value, 'value1', "Value is value1" ); |
| 41 |
|
| 42 |
# Test 3 |
| 43 |
# Retrieve array reference |
| 44 |
$value = $ini{test1}{mult}; |
| 45 |
|
| 46 |
# TEST |
| 47 |
is( ref($value), 'ARRAY', "test1/mult is an array." ); |
| 48 |
|
| 49 |
# Test 4 |
| 50 |
# Creating a scalar value using tied hash |
| 51 |
$ini{'test2'}{'seven'} = 'value7'; |
| 52 |
tied(%ini)->RewriteConfig; |
| 53 |
tied(%ini)->ReadConfig; |
| 54 |
$value = $ini{'test2'}{'seven'}; |
| 55 |
|
| 56 |
# TEST |
| 57 |
is( $value, 'value7', "test2/seven is value7" ); |
| 58 |
|
| 59 |
# Test 5 |
| 60 |
# Deleting a scalar value using tied hash |
| 61 |
delete $ini{test2}{seven}; |
| 62 |
tied(%ini)->RewriteConfig; |
| 63 |
tied(%ini)->ReadConfig; |
| 64 |
$value = ''; |
| 65 |
$value = $ini{test2}{seven}; |
| 66 |
|
| 67 |
# TEST |
| 68 |
ok( !defined($value), "test2/seven does not exist" ); |
| 69 |
|
| 70 |
# Test 6 |
| 71 |
# Testing default values using tied hash |
| 72 |
# TEST |
| 73 |
is( $ini{test2}{three}, 'value3', "test2/three is equal to value3" ); |
| 74 |
|
| 75 |
# Test 7 |
| 76 |
# Case insensitivity in a hash parameter |
| 77 |
# TEST |
| 78 |
is( $ini{test2}{FOUR}, 'value4', "test2/FOUR is value4 - case insensitivity" ); |
| 79 |
|
| 80 |
# Test 8 |
| 81 |
# Case insensitivity in a hash section |
| 82 |
# TEST |
| 83 |
is( $ini{TEST2}{four}, 'value4', "TEST2/four is value4 - case insensitivity" ); |
| 84 |
|
| 85 |
# Test 9 |
| 86 |
# Listing section names using keys |
| 87 |
$ini = Config::IniFiles->new( |
| 88 |
-file => t_file("test.ini"), |
| 89 |
-default => 'test1', |
| 90 |
-nocase => 1 |
| 91 |
); |
| 92 |
$ini->SetFileName( t_file("test05b.ini") ); |
| 93 |
{ |
| 94 |
my @S1 = $ini->Sections; |
| 95 |
my @S2 = keys %ini; |
| 96 |
|
| 97 |
# TEST |
| 98 |
is_deeply( \@S1, \@S2, "All sections OK." ); |
| 99 |
} |
| 100 |
|
| 101 |
# Test 10 |
| 102 |
# Listing parameter names using keys |
| 103 |
{ |
| 104 |
my @S1 = sort { $a cmp $b } $ini->Parameters('test1'); |
| 105 |
my @S2 = sort { $a cmp $b } keys %{ $ini{test1} }; |
| 106 |
|
| 107 |
# TEST |
| 108 |
is_deeply( \@S1, \@S2, "All keys of section 'test1' are OK." ); |
| 109 |
} |
| 110 |
|
| 111 |
# Test 11 |
| 112 |
# Copying a section using tied hash |
| 113 |
my %bak = %{ $ini{test2} }; |
| 114 |
|
| 115 |
# TEST |
| 116 |
is( $bak{six}, "value6", "Copied value is OK." ); |
| 117 |
|
| 118 |
# Test 12 |
| 119 |
# Deleting a whole section using tied hash |
| 120 |
delete $ini{test2}; |
| 121 |
$value = $ini{test2}; |
| 122 |
|
| 123 |
# TEST |
| 124 |
ok( ( !$value ), "test2 section was deleted" ); |
| 125 |
|
| 126 |
# Test 13 |
| 127 |
# Creating a section and parameters using a hash |
| 128 |
$ini{newsect} = {}; |
| 129 |
%{ $ini{newsect} } = %bak; |
| 130 |
$value = $ini{newsect}{four} || ''; |
| 131 |
|
| 132 |
# TEST |
| 133 |
is( $value, 'value4', "Creating a section and parameters using a hash" ); |
| 134 |
|
| 135 |
# Test 14 |
| 136 |
# Checking use of default values for newly created section |
| 137 |
$value = $ini{newsect}{one}; |
| 138 |
|
| 139 |
# TEST |
| 140 |
is( $value, "value1", |
| 141 |
"Checking use of default values for newly created section" ); |
| 142 |
|
| 143 |
# Test 15 |
| 144 |
# print "Store new section in hash ........ "; |
| 145 |
tied(%ini)->RewriteConfig; |
| 146 |
tied(%ini)->ReadConfig; |
| 147 |
$value = $ini{newsect}{four}; |
| 148 |
|
| 149 |
# TEST |
| 150 |
is( $value, 'value4', "Store new section in hash" ); |
| 151 |
|
| 152 |
# Test 16 |
| 153 |
# Writing 2 line multivalue and returning it |
| 154 |
$ini{newsect} = {}; |
| 155 |
$ini{test1}{multi_2} = [ 'line 1', 'line 2' ]; |
| 156 |
tied(%ini)->RewriteConfig; |
| 157 |
tied(%ini)->ReadConfig; |
| 158 |
@value = @{ $ini{test1}{multi_2} }; |
| 159 |
|
| 160 |
# TEST |
| 161 |
is_deeply( |
| 162 |
\@value, |
| 163 |
[ 'line 1', 'line 2' ], |
| 164 |
"Writing 2 line multivalue and returning it" |
| 165 |
); |
| 166 |
|
| 167 |
# Test 17 |
| 168 |
# Getting a default value not in the file |
| 169 |
tie %ini, 'Config::IniFiles', |
| 170 |
( -file => t_file("test.ini"), -default => 'default', -nocase => 1 ); |
| 171 |
$ini{default}{cassius} = 'clay'; |
| 172 |
$value = $ini{test1}{cassius}; |
| 173 |
|
| 174 |
# TEST |
| 175 |
is( $value, 'clay', "Getting a default value not in the file" ); |
| 176 |
|
| 177 |
# Test 18 |
| 178 |
# Setting value to number of elements in array |
| 179 |
my @thing = ( "one", "two", "three" ); |
| 180 |
$ini{newsect}{five} = @thing; |
| 181 |
$value = $ini{newsect}{five}; |
| 182 |
|
| 183 |
# TEST |
| 184 |
is( $value, 3, "Setting value to number of elements in array" ); |
| 185 |
|
| 186 |
# Test 19 |
| 187 |
# Setting value to number of elements in array |
| 188 |
@thing = ("one"); |
| 189 |
$ini{newsect}{five} = @thing; |
| 190 |
$value = $ini{newsect}{five}; |
| 191 |
|
| 192 |
# TEST |
| 193 |
is( $value, 1, "Testing that value is 1." ); |
| 194 |
|
| 195 |
# Clean up when we're done |
| 196 |
t_unlink("test05.ini"); |
| 197 |
|