~ubuntu-branches/ubuntu/trusty/horae/trusty

« back to all changes in this revision

Viewing changes to 0CPAN/Config-IniFiles-2.39/t/05hash.t

  • Committer: Bazaar Package Importer
  • Author(s): Carlo Segre
  • Date: 2008-02-23 23:13:02 UTC
  • mfrom: (2.1.2 hardy)
  • Revision ID: james.westby@ubuntu.com-20080223231302-mnyyxs3icvrus4ke
Tags: 066-3
Apply patch to athena_parts/misc.pl for compatibility with 
perl-tk 804.28.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
use strict;
2
 
use Test;
3
 
use Config::IniFiles;
4
 
 
5
 
BEGIN { plan tests => 19 }
6
 
 
7
 
my %ini;
8
 
my ($ini, $value);
9
 
my (@value);
10
 
 
11
 
# Get files from the 't' directory, portably
12
 
chdir('t') if ( -d 't' );
13
 
unlink "test05.ini";
14
 
 
15
 
 
16
 
# Test 1
17
 
# Tying a hash.
18
 
ok( tie %ini, 'Config::IniFiles', ( -file => "test.ini", -default => 'test1', -nocase => 1 ) );
19
 
tied(%ini)->SetFileName("test05.ini");
20
 
tied(%ini)->SetWriteMode("0666");
21
 
 
22
 
# Test 2
23
 
# Retrieve scalar value
24
 
$value = $ini{test1}{one};
25
 
ok($value eq 'value1');
26
 
 
27
 
# Test 3
28
 
# Retrieve array reference
29
 
$value = $ini{test1}{mult};
30
 
ok(ref $value eq 'ARRAY'); 
31
 
 
32
 
# Test 4
33
 
# Creating a scalar value using tied hash
34
 
$ini{'test2'}{'seven'} = 'value7';
35
 
tied(%ini)->RewriteConfig;
36
 
tied(%ini)->ReadConfig;
37
 
$value = $ini{'test2'}{'seven'};
38
 
ok($value eq 'value7');
39
 
 
40
 
# Test 5
41
 
# Deleting a scalar value using tied hash
42
 
delete $ini{test2}{seven};
43
 
tied(%ini)->RewriteConfig;
44
 
tied(%ini)->ReadConfig;
45
 
$value='';
46
 
$value = $ini{test2}{seven};
47
 
ok(! defined ($value));
48
 
 
49
 
# Test 6
50
 
# Testing default values using tied hash
51
 
ok( $ini{test2}{three} eq 'value3' );
52
 
 
53
 
# Test 7
54
 
# Case insensitivity in a hash parameter
55
 
ok( $ini{test2}{FOUR} eq 'value4' );
56
 
 
57
 
# Test 8
58
 
# Case insensitivity in a hash section
59
 
ok( $ini{TEST2}{four} eq 'value4' );
60
 
 
61
 
# Test 9
62
 
# Listing section names using keys
63
 
$value = 1;
64
 
$ini = new Config::IniFiles( -file => "test.ini" );
65
 
$ini->SetFileName("test05b.ini");
66
 
my @S1 = $ini->Sections;
67
 
my @S2 = keys %ini;
68
 
foreach (@S1) {
69
 
        unless( (grep "$_", @S2) &&
70
 
                (grep "$_", qw( test1 test2 [w]eird characters ) ) ) {
71
 
                $value = 0;
72
 
                last;
73
 
        }
74
 
}
75
 
ok( $value );
76
 
 
77
 
# Test 10
78
 
# Listing parameter names using keys
79
 
$value = 1;
80
 
@S1 = $ini->Parameters('test1');
81
 
@S2 = keys %{$ini{test1}};
82
 
foreach (@S1) {
83
 
        unless( (grep "$_", @S2) &&
84
 
                (grep "$_", qw( three two one ) ) ) {
85
 
                $value = 0;
86
 
                last;
87
 
        }
88
 
}
89
 
ok($value);
90
 
 
91
 
# Test 11
92
 
# Copying a section using tied hash
93
 
my %bak = %{$ini{test2}};
94
 
$value = $bak{six} || '';
95
 
ok( $value eq 'value6' );
96
 
 
97
 
# Test 12
98
 
# Deleting a whole section using tied hash
99
 
delete $ini{test2};
100
 
$value = $ini{test2};
101
 
ok(not $value);
102
 
 
103
 
# Test 13
104
 
# Creating a section and parameters using a hash
105
 
$ini{newsect} = {};
106
 
%{$ini{newsect}} = %bak;
107
 
$value = $ini{newsect}{four} || '';
108
 
ok( $value eq 'value4' );
109
 
 
110
 
# Test 14
111
 
# Checking use of default values for newly created section
112
 
$value = $ini{newsect}{one};
113
 
ok( $value eq 'value1' );
114
 
 
115
 
# Test 15
116
 
# print "Store new section in hash ........ ";
117
 
tied(%ini)->RewriteConfig;
118
 
tied(%ini)->ReadConfig;
119
 
$value = $ini{newsect}{four} || '';
120
 
ok( $value eq 'value4' );
121
 
 
122
 
# Test 16
123
 
# Writing 2 line multilvalue and returing it
124
 
$ini{newsect} = {};
125
 
$ini{test1}{multi_2} = ['line 1', 'line 2'];
126
 
tied(%ini)->RewriteConfig;
127
 
tied(%ini)->ReadConfig;
128
 
@value = @{$ini{test1}{multi_2}};
129
 
ok( (@value == 2) 
130
 
    && ($value[0] eq 'line 1')
131
 
    && ($value[1] eq 'line 2')
132
 
  );
133
 
 
134
 
# Test 17
135
 
# Getting a default value not in the file
136
 
tie %ini, 'Config::IniFiles', ( -file => "test.ini", -default => 'default', -nocase => 1 );
137
 
$ini{default}{cassius} = 'clay';
138
 
$value = $ini{test1}{cassius};
139
 
ok( $value eq 'clay' );
140
 
 
141
 
# Test 18
142
 
# Setting value to number of elements in array
143
 
my @thing = ("one", "two", "three");
144
 
$ini{newsect}{five} = @thing;
145
 
$value = $ini{newsect}{five};
146
 
ok($value == 3);
147
 
 
148
 
# Test 19
149
 
# Setting value to number of elements in array
150
 
@thing = ("one");
151
 
$ini{newsect}{five} = @thing;
152
 
$value = $ini{newsect}{five};
153
 
ok($value == 1);
154
 
 
155
 
# Clean up when we're done
156
 
unlink "test05.ini";
157