File: writer.t

package info (click to toggle)
libtoml-tiny-perl 0.20-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,792 kB
  • sloc: perl: 1,173; makefile: 10
file content (165 lines) | stat: -rw-r--r-- 4,509 bytes parent folder | download | duplicates (2)
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
use strict;
use warnings;

use DateTime ();
use Test2::V0;
use TOML::Tiny qw(from_toml to_toml);
use TOML::Tiny::Writer ();

my $src = do{ local $/; <DATA> };

subtest basics => sub{
  my $data = from_toml($src);
  my $toml = to_toml($data);
  my $got  = from_toml($toml);
  is $got, $data, 'to_toml <=> from_toml';
};

#-------------------------------------------------------------------------------
# Adapted from DateTime::Format::RFC3339.
#-------------------------------------------------------------------------------
subtest 'rfc3339 datetimes' => sub{
  my $dt;

  $dt = DateTime->new(year => 2002, month => 7, day => 1, hour => 13, minute => 50, second => 5, time_zone => 'UTC');
  is TOML::Tiny::Writer::strftime_rfc3339($dt), '2002-07-01T13:50:05Z', 'UTC';

  $dt = DateTime->new(year => 2002, month => 7, day => 1, hour => 13, minute => 50, second => 5, time_zone => 'Europe/London');
  is TOML::Tiny::Writer::strftime_rfc3339($dt), '2002-07-01T13:50:05+01:00', 'positive offset';

  $dt = DateTime->new(year => 2002, month => 1, day => 1, hour => 13, minute => 50, second => 5, time_zone => 'Europe/London');
  is TOML::Tiny::Writer::strftime_rfc3339($dt), '2002-01-01T13:50:05+00:00', 'zero offset';

  $dt = DateTime->new(year => 2002, month => 1, day => 1, hour => 13, minute => 50, second => 5, time_zone => 'America/New_York');
  is TOML::Tiny::Writer::strftime_rfc3339($dt), '2002-01-01T13:50:05-05:00', 'negative offset';

  $dt = DateTime->new(year => 1880, month => 1, day => 1, hour => 0, minute => 0, second => 0, time_zone => 'America/New_York');
  is TOML::Tiny::Writer::strftime_rfc3339($dt), '1880-01-01T04:56:02Z', 'offset with non-integral minutes';

  $dt = DateTime->new(year => 2002, month => 1, day => 1, hour => 13, minute => 50, second => 5, time_zone => 'floating');
  is TOML::Tiny::Writer::strftime_rfc3339($dt), '2002-01-01T13:50:05', 'floating time zone';
};

subtest 'oddballs and regressions' => sub{
  subtest 'strings that look like numbers' => sub{
    my $data = {
      is_inf  => 'inf',
      not_inf => 'to infinity and beyond',
      is_nan  => 'nan',
      not_nan => 'no nan here',
    };

    is to_toml({a => 'no inf here'}), 'a="no inf here"', '"inf" present in string is string';
    is to_toml({a => 'no nan here'}), 'a="no nan here"', '"nan" present in string is string';
  };

   subtest 'strings that contain DateTime but are not DateTime' => sub {

      my $data = {
        not_a_dt => 'Not a 2024-05-31T17:08:44',
      };

     is to_toml( $data ), 'not_a_dt="Not a 2024-05-31T17:08:44"',
        $data->{not_a_dt};

   };

  subtest 'values which are undefined' => sub {

      my $data = { deep => { in => { an => [ undef ] } } };

      eval { to_toml( $data ) };
      like $@, qr/found undefined value/;

  };


  subtest 'quoted inline table keys' => sub {

      my $data = { q{foo} => [ q{bar}, { q{<=} => 33 } ] } ;
      my $encoded = to_toml( $data );

      my $decoded;
      ok( lives { $decoded = from_toml( $encoded ) },
          'decode succeeded' ) or note $@;
      is ( $decoded, $data, 'round trip successful' );

  };

  subtest 'encoding a non-hashref' => sub {

      eval { to_toml( q{} ) };
      like $@, qr/must be a hashref/, 'scalar';

      eval { to_toml( [] ) };
      like $@, qr/must be a hashref/, 'array';

  };

};

subtest 'to_toml_array' => sub{
    my @to_toml = [1,2,3];
    my $toml = TOML::Tiny::Writer::to_toml_array(\@to_toml, {strict => 1});
    ok($toml, 'no exception in strict mode');
};

done_testing;

__DATA__
# This is a TOML document.

title = "TOML Example"

hetero_array = ["life", "universe", "everything", 42]

[owner]
name = "Tom Preston-Werner"
dob = 1979-05-27T07:32:00-08:00 # First class dates

[database]
server = "192.168.1.1"
ports = [ 8001, 8001, 8002 ]
connection_max = 5000
enabled = true
options = {"quote-keys"=false}

[servers]

  # Indentation (tabs and/or spaces) is allowed but not required
  [servers.alpha]
  ip = "10.0.0.1"
  dc = "eqdc10"

  [servers.beta]
  ip = "10.0.0.2"
  dc = "eqdc10"

[clients]
data = [ ["gamma", "delta"], [1, 2] ]

# Line breaks are OK when inside arrays
hosts = [
  "alpha",
  "omega"
]

[cfg."something with a 'single-quote'".nested]
inner = "forty-one"

[cfg.'something with a "double-quote"'.nested]
inner = "forty-two"

[cfg."something with a 'single-quote' and a \"double-quote\"".nested]
inner = "forty-three"

[[products]]
name = "Hammer"
sku = 738594937

[[products]]

[[products]]
name = "Nail"
sku = 284758393
color = "gray"