File: 005-config-plain.t

package info (click to toggle)
libconfig-record-perl 1.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 212 kB
  • ctags: 14
  • sloc: perl: 516; sh: 34; makefile: 2
file content (287 lines) | stat: -rw-r--r-- 7,141 bytes parent folder | download | duplicates (3)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# -*- perl -*- $Id: 005Config.t,v 1.8 2006/01/27 16:25:50 dan Exp $

use Test::More tests => 57;

BEGIN { use_ok("Config::Record") }

#$| = undef;
use strict;
use warnings;
use Carp qw(confess);
use File::Temp qw(tempfile);
use IO::File;

my $config = <<END;
  name = Foo
  title = "Wizz bang wallop"

  label = "First string " \\
          "split across"
  description = <<EOF
This is a multi-line paragraph.
This is the second line.
And the third
EOF
# Some delibrate blank lines to test parsing...


  eek = ( # Testing an array
    OOhh
    " Aahhh "
    Wizz \\
    Bang
    <<EOF
A long paragraph in
here
EOF
  )
  people = ( # Testing an array of hashes
    {
      forename = John
      surnamne = Doe
    }
    {
      forename = Some
      surname = One
    }
  )
  wizz = { # Testing a hash
    foo = "Elk"
    ooh = "fds"
    eek.wibble = Hurrah
  }
  wibble = { # Testing a hash of hashes
    nice = {
      ooh = (
        weee
        {
          aah = sfd
          oooh = "   Weeee   "
        }
      )
    }
  }
END

my ($fh, $file) = tempfile("tmpXXXXXXX", UNLINK => 1);
print $fh $config;
close $fh;

# First test the constructor with a filename
my $cfg = Config::Record->new(file => $file, debug => ($ENV{TEST_DEBUG} || 0));

# Test plain string
is($cfg->get("name"), "Foo", "Plain string");

# Test quoted string
is($cfg->get("title"), "Wizz bang wallop", "Quoted string");

# Test continuation
is($cfg->get("label"), "First string split across", "Continuation");

# Test here doc
is($cfg->get("description"), <<EOF
This is a multi-line paragraph.
This is the second line.
And the third
EOF
, "Here doc");

# Test array element continuation
is($cfg->get("eek")->[2], "Wizz Bang", "Continuation");

# Test array here doc
is($cfg->get("eek")->[3], "A long paragraph in\nhere\n", "Here doc");

# Test defaults
is($cfg->get("nada", "eek"), "eek", "Defaults");

# Test nested hash/array lookups
ok(defined $cfg->get("wibble/nice"), "Hash key defined");
ok(defined $cfg->get("wibble/nice/ooh"), "Hash, hash key defined");
ok($cfg->get("wibble/nice/ooh", ["oooh"])->[0] eq "weee", "Hash, hash, array value");

# Now test the constructor with a file handle
$fh = IO::File->new($file);
$cfg = Config::Record->new(file => $fh);

# Test plain string
is($cfg->get("name"), "Foo", "Plain string");

# Test quoted string
is($cfg->get("title"), "Wizz bang wallop", "Quoted string");

# Test continuation
is($cfg->get("label"), "First string split across", "Continuation");

# Test here doc
is($cfg->get("description"), <<EOF
This is a multi-line paragraph.
This is the second line.
And the third
EOF
, "Here doc");

# Test array element continuation
is($cfg->get("eek")->[2], "Wizz Bang", "Continuation");

# Test array here doc
is($cfg->get("eek")->[3], "A long paragraph in\nhere\n", "Here doc");

# Test defaults
is($cfg->get("nada", "eek"), "eek", "Defaults");

# Test nested hash/array lookups
ok(defined $cfg->get("wibble/nice"), "Hash key defined");
ok(defined $cfg->get("wibble/nice/ooh"), "Hash, hash key defined");
ok($cfg->get("wibble/nice/ooh", ["oooh"])->[0] eq "weee", "Hash, hash, array value");

ok(ref($cfg->get("people/[0]")) eq "HASH", "people/[0] is a hash");
is($cfg->get("people/[0]/forename"), "John", "First person forename is John");
is($cfg->get("people/[1]/forename"), "Some", "Second person forename is Some");
eval {
  $cfg->get("people/[2]/forename");
};
ok($@, "too many people");

$cfg->set("people/[2]", { "forename" => "Bob", "surname" => "Man" });
ok(ref($cfg->get("people/[2]")) eq "HASH", "people/[2] is a hash");
is($cfg->get("people/[2]/forename"), "Bob", "Third person forename is Bob");

eval {
  # Root element should be a hash!
  $cfg->get("[0]");
};
ok($@, "root should be a hash");

# Now lets get a view

my $subcfg = $cfg->view("people/[2]");

is($subcfg->get("forename"), "Bob", "Got forename from view");
is($subcfg->get("surname"), "Man", "Got surname from view");

$subcfg->set("address", [{ "street" => "Long road",
			  "phone" => [
				      "123",
				      "456",
				      ],
			  "city" => "London" },
			 { "street" => "Other road",
			   "phone" => [
				       "513",
				      ],
			   "city" => "London" }]);

is($subcfg->get("address/[0]/street"), "Long road", "Street is long road");
is($subcfg->get("address/[0]/phone/[0]"), "123", "First phone number is 123");

is($subcfg->get("address/[1]/street"), "Other road", "Street is other road");

# Check the original config was altered too

is($cfg->get("people/[2]/address/[0]/street"), "Long road", "Street is long road");
is($cfg->get("people/[2]/address/[0]/phone/[0]"), "123", "First phone number is 123");

is($cfg->get("people/[2]/address/[1]/street"), "Other road", "Street is other road");


# Now test a view or two that fail
eval {
  $cfg->view("people");
};
ok($@, "getting view of people failed");
eval {
  $cfg->view("people/[1]/forename");
};
ok($@, "getting view of people/[1]/forename failed");

# Test with empty constructor & load method

$cfg = Config::Record->new();

# Shouldn't be anything there yet
eval "$cfg->get('name')";
ok($@ ? 1 : 0, "No defaults");

# Lets set an option
$cfg->set("name" => "Blah");
is($cfg->get("name"), "Blah", "Set option");

# Now load the config record
$fh = IO::File->new($file);
$cfg->load($fh);

# Test plain string - should have overwritten 'Blah'
is($cfg->get("name"), "Foo", "Reload plain string");

# Test quoted string
is($cfg->get("title"), "Wizz bang wallop", "Reloaded quoted string");

# Test defaults
is($cfg->get("nada", "eek"), "eek", "Reloaded defaults");

# Test compound paths
is($cfg->get("wizz/foo"), "Elk", "Compound paths");

# Test '.' in key names
is($cfg->get("wizz/eek.wibble"), "Hurrah", "Compound paths with .");


# Now write it out to another file....
my ($fh2, $file2) = tempfile("tmpXXXXXXX", UNLINK => 1);
$fh2->close;
$cfg->save($file2);

# ...and then read it back in
my $cfg2 = Config::Record->new(file => $file2);

# Test plain string
is($cfg2->get("name"), "Foo", "Saved plain string");

# Test quoted string
is($cfg2->get("title"), "Wizz bang wallop", "Saved quoted string");

# Test continuation
is($cfg->get("label"), "First string split across", "Continuation");

# Test here doc
is($cfg->get("description"), <<EOF
This is a multi-line paragraph.
This is the second line.
And the third
EOF
, "Here doc");

# Test array element continuation
is($cfg->get("eek")->[2], "Wizz Bang", "Continuation");

# Test array here doc
is($cfg->get("eek")->[3], "A long paragraph in\nhere\n", "Here doc");

# Test defaults
is($cfg2->get("nada", "eek"), "eek", "Saved defaults");

# Test nested hash/array lookups
ok(defined $cfg2->get("wibble/nice"), "Hash key defined");
ok(defined $cfg2->get("wibble/nice/ooh"), "Hash, hash key defined");
ok($cfg2->get("wibble/nice/ooh", ["oooh"])->[0] eq "weee", "Hash, hash, array value");

# Now recursively compare entire hash
is_deeply($cfg->record, $cfg2->record, "Entire hash");


# Finally test the constructor with bogus ref

my $bogus = {};
bless $bogus, "Bogus";
eval "Config::Record->new(file => $bogus)";
ok($@ ? 1 : 0, "Bogus constructor");


exit 0;


# Local Variables:
# mode: cperl
# End: