File: 006-config-quoted.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 (350 lines) | stat: -rw-r--r-- 9,029 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# -*- perl -*- $Id: 005Config.t,v 1.8 2006/01/27 16:25:50 dan Exp $

use Test::More tests => 68;

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

#$| = undef;
use strict;
use warnings;
use Carp qw(confess);
use Test::Harness;
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
    }
  )
  array = (
    (
      sub1
    )
    (
      sub2
    )
  )
  wizz = { # Testing a hash
    foo = "Elk"
    ooh = "fds"
    eek.wibble = Hurrah
  }
  wibble = { # Testing a hash of hashes
    nice = {
      ooh = (
        weee
        {
          aah = sfd
          oooh = "   Weeee   "
        }
      )
    }
  }
  # Testing spaces in keys
  "  quoted one  " = wizz
  "  quoted \\\\ two  " = "  wizz  "
  "quoted \\" three" = (
     yeah
  )
  "quoted \\\\\\" four" = {
    ooh = ahh
  }
  "quoted \\" five" = {
     "[0]" = notarray
     data = (
        {
          "sub1/key" = hello
          "sub2\\\\/key" = world
        }
        wizz
        {
           "here [2] key" = (
              one
              two
              three
           )
        }
     )
     "other \\\\/" = {
        sub = one
     }
  }
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},
			      features => {quotedkeys => 1 });

# 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,
			   debug => $ENV{TEST_DEBUG},
			   features => {quotedkeys => 1});

# 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");

# Test keys with spaces in them

is($cfg->get('  quoted one  '), "wizz", "Quoted key + value");
is($cfg->get('  quoted \ two  '), "  wizz  ", "Quoted key + value");
is_deeply($cfg->get('quoted " three'), ["yeah"], "Quoted key + array");
is_deeply($cfg->get('quoted \" four'), {"ooh" => "ahh" }, "Quoted key + hash with spaces");

# Testing keys with / and [] in them
is($cfg->get('quoted " five/\\[0\\]/'), "notarray", "Special path chars one");
is($cfg->get('quoted " five/data/[0]/sub1\\/key'), "hello", "Special path chars two");
is($cfg->get('quoted " five/data/[0]/sub2\\\\/key'), "world", "Special path chars three");
is($cfg->get('quoted " five/data/[1]'), "wizz", "Special path chars four");
is($cfg->get('quoted " five/data/[2]/here [2] key/[0]'), "one", "Special path chars five");
is($cfg->get('quoted " five/data/[2]/here [2] key/[1]'), "two", "Special path chars six");
is_deeply($cfg->get('quoted " five/other \\\\/'), {"sub" => "one"}, "Special path chars seven");

# 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(debug => $ENV{TEST_DEBUG},
			   features => {quotedkeys => 1});

# 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(debug => $ENV{TEST_DEBUG},
			       file => $file2, features => {quotedkeys => 1});

# 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
use Data::Dumper;
#warn Dumper($cfg->record);
#warn Dumper($cfg2->record);
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: