File Coverage

File:t/10-yaml2o.t
Coverage:96.8%

linestmtbrancondsubpodtimecode
1#!/usr/bin/env perl
2
3
1
1
1
2904
2
25
use strict;
4
1
1
1
5
0
31
use warnings;
5
1
1
1
330
70760
6
use Test::More;
6
1
1
1
463
2453
2
use Test::Exception;
7
1
1
1
747
15289
54
use File::Temp      qw/tempfile/;
8
1
1
1
300
2
70197
use Util::H2O::More qw/yaml2h2o yaml2o/;
9
10
1
116
my $YAML = <<EOYAML;
11---
12database:
13  host:     localhost
14  port:     3306
15  db:       users
16  username: appuser
17  password: 1uggagel2345
18---
19devices:
20  copter1:
21    active:  1
22    macaddr: a0:ff:6b:14:19:6e
23    host:    192.168.0.14
24    port:    80
25  thingywhirl:
26    active:  1
27    macaddr: 00:88:fb:1a:5f:08
28    host:    192.168.0.14
29    port:    80
30EOYAML
31
32
1
2
my $NOT_YAML_OR_FILE = <<EONOTYAML;
33MR DUCKS
34AM R NOT
35AM R 2
36C DEM E D B D WANGS
37L I C
38MR DUCKS
39EONOTYAML
40
41{
42
1
1
4
32
    dies_ok { yaml2h2o $NOT_YAML_OR_FILE } q{yaml2h2o rejects input that is neither a file name nor clearly YAML};
43
44
1
301
    my ( $dbconfig1, $devices1 ) = yaml2h2o $YAML;
45
46
1
2
    is $dbconfig1->database->host,        q{localhost}, q{YAML string parsed and objectified as expected};
47
1
229
    is $devices1->devices->copter1->port, 80,           q{YAML string parsed and objectified as expected};
48
49
1
200
    my ( $fh, $filename ) = tempfile( SUFFIX => '.yaml' );
50
51
1
1
312
3
    print {$fh} $YAML;
52
1
22
    close $fh or die qq{Could not close temporary YAML file '$filename': $!};
53
54
1
2
    my ( $dbconfig2, $devices2 ) = yaml2h2o $filename;
55
56
1
3
    is $dbconfig2->database->host,        q{localhost}, q{YAML file parsed and objectified as expected};
57
1
213
    is $devices2->devices->copter1->port, 80,           q{YAML file parsed and objectified as expected};
58
59
1
203
    my $missing_filename = $filename . q{.does-not-exist};
60
61
1
1
4
22
    dies_ok { yaml2h2o $missing_filename } q{yaml2h2o rejects a one-line file name that does not exist};
62}
63
64{
65
1
1
1
1
1
708
5
21
    dies_ok { yaml2o $NOT_YAML_OR_FILE } q{yaml2o rejects input that is neither a file name nor clearly YAML};
66
67
1
202
    my ( $dbconfig1, $devices1 ) = yaml2o $YAML;
68
69
1
3
    is $dbconfig1->database->host,        q{localhost}, q{YAML string parsed and objectified as expected};
70
1
214
    is $devices1->devices->copter1->port, 80,           q{YAML string parsed and objectified as expected};
71
72
1
209
    my ( $fh, $filename ) = tempfile( SUFFIX => '.yaml' );
73
74
1
1
192
9
    print {$fh} $YAML;
75
1
16
    close $fh or die qq{Could not close temporary YAML file '$filename': $!};
76
77
1
3
    my ( $dbconfig2, $devices2 ) = yaml2o $filename;
78
79
1
3
    is $dbconfig2->database->host,        q{localhost}, q{YAML file parsed and objectified as expected};
80
1
206
    is $devices2->devices->copter1->port, 80,           q{YAML file parsed and objectified as expected};
81}
82
83
1
568
done_testing;