File: basic.t

package info (click to toggle)
libmoosex-configuration-perl 0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 148 kB
  • sloc: perl: 266; makefile: 2
file content (129 lines) | stat: -rw-r--r-- 2,242 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
use strict;
use warnings;
use autodie;

use Test::More 0.88;

use File::Temp qw( tempdir );
use Path::Class qw( dir file );

{
    package Conf;

    use Moose;
    use MooseX::Configuration;
    use MooseX::Types::Moose qw( ArrayRef Int Num Str );

    has root_key_a => (
        is       => 'ro',
        isa      => Str,
        key      => 'a',
        required => 1,
    );

    has root_key_b => (
        is            => 'ro',
        isa           => Int,
        key           => 'b',
        default       => 'value of b',
        documentation => 'This is the b key',
    );

    has foo_key_c => (
        is            => 'ro',
        isa           => Num,
        section       => 'foo',
        key           => 'c',
        documentation => 'This is the c key',
    );

    has foo_key_d => (
        is            => 'ro',
        isa           => Num,
        section       => 'foo',
        key           => 'd',
        default       => 42,
        documentation => 'This is the d key',
    );

    has not_config => (
        is  => 'ro',
        isa => ArrayRef,
    );
}

{
    ok(
        Conf->new( root_key_a => 'x' ),
        'can create a Conf object without reading a config file'
    );
}

my $tempdir = dir( tempdir( CLEANUP => 1 ) );

{
    my $file = $tempdir->file('test1.conf');

    open my $fh, '>', $file;
    print {$fh} <<'EOF';
a = Foo
b = 42

[foo]
c = 4.2
EOF
    close $fh;

    my $conf = Conf->new( config_file => $file );

    is(
        $conf->root_key_a(), 'Foo',
        'got root_key_a from config file'
    );

    is(
        $conf->root_key_b(), 42,
        'got root_key_b from config file'
    );

    is(
        $conf->foo_key_c(), 4.2,
        'got foo_key_c from config file'
    );

    my $buffer = q{};
    open $fh, '>', \$buffer;

    $conf->write_config_file(
        generated_by => 'Test code',
        file         => $fh,
    );

    my $expect = <<'EOF';
; Test code

; This configuration key is required.
a = Foo

; This is the b key
; Defaults to "value of b"
b = 42

[foo]
; This is the c key
c = 4.2

; This is the d key
; Defaults to 42
; d =

EOF

    is(
        $buffer,
        $expect,
        'write_file generates expected file'
    );
}

done_testing();