File: 01-defaults.t

package info (click to toggle)
libconfig-onion-perl 1.007-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 200 kB
  • sloc: perl: 151; makefile: 12
file content (51 lines) | stat: -rw-r--r-- 1,430 bytes parent folder | download | duplicates (6)
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
use strict;
use warnings;

use Test::Exception;
use Test::More;

use Config::Onion;

# GH5: don't die if config is empty
{
  lives_ok { Config::Onion->new->get } q(empty config isn't fatal);
}

# construct bare, then add/retrieve default values
{
  my $cfg = Config::Onion->new;
  isa_ok($cfg, 'Config::Onion', 'construct bare config');
  $cfg->set_default(foo => 1);
  is($cfg->get->{foo}, 1, 'retrieve single value');
  is_deeply($cfg->get, { foo => 1 }, 'retrieve full config');
}

# construct by setting default values
{
  my $cfg = Config::Onion->set_default(bar => 2);
  isa_ok($cfg, 'Config::Onion', 'construct with set_default');
  is($cfg->get->{bar}, 2, 'retrieve value after set_default construction');
}

# override existing defaults with new defaults
{
  my $cfg = Config::Onion->set_default(foo => 3);
  $cfg->set_default(bar => 'baz');
  is($cfg->get->{foo}, 3, 'merge defaults preserves old values');
  is($cfg->get->{bar}, 'baz', 'merge defaults sets new values');
  $cfg->set_default(foo => 'new');
  is($cfg->get->{foo}, 'new', 'merge defaults overwrites old values');
}

# accept defaults as either hash or hashref(s)
{
  my $cfg = Config::Onion->set_default({ a => 1 });
  is($cfg->get->{a}, 1, 'set defaults with hashref');
  $cfg->set_default({ b => 2 }, { c => 3 }, d => 4);
  is_deeply( $cfg->get, { a => 1, b => 2, c => 3, d => 4 },
    'set defaults with mixed hashrefs and hash'
  );
}

done_testing;