1#!/usr/bin/perl -wT
2
3use strict;
4use warnings;
5use lib 't/lib';
6
7use Test::More tests => 9;
8
9use TAP::Parser::YAMLish::Writer;
10
11my $out = [
12    "---",
13    "bill-to:",
14    "  address:",
15    "    city: \"Royal Oak\"",
16    "    lines: \"458 Walkman Dr.\\nSuite #292\\n\"",
17    "    postal: 48046",
18    "    state: MI",
19    "  family: Dumars",
20    "  given: Chris",
21    "comments: \"Late afternoon is best. Backup contact is Nancy Billsmer \@ 338-4338\\n\"",
22    "date: 2001-01-23",
23    "invoice: 34843",
24    "product:",
25    "  -",
26    "    description: Basketball",
27    "    price: 450.00",
28    "    quantity: 4",
29    "    sku: BL394D",
30    "  -",
31    "    description: \"Super Hoop\"",
32    "    price: 2392.00",
33    "    quantity: 1",
34    "    sku: BL4438H",
35    "tax: 251.42",
36    "total: 4443.52",
37    "...",
38];
39
40my $in = {
41    'bill-to' => {
42        'given'   => 'Chris',
43        'address' => {
44            'city'   => 'Royal Oak',
45            'postal' => '48046',
46            'lines'  => "458 Walkman Dr.\nSuite #292\n",
47            'state'  => 'MI'
48        },
49        'family' => 'Dumars'
50    },
51    'invoice' => '34843',
52    'date'    => '2001-01-23',
53    'tax'     => '251.42',
54    'product' => [
55        {   'sku'         => 'BL394D',
56            'quantity'    => '4',
57            'price'       => '450.00',
58            'description' => 'Basketball'
59        },
60        {   'sku'         => 'BL4438H',
61            'quantity'    => '1',
62            'price'       => '2392.00',
63            'description' => 'Super Hoop'
64        }
65    ],
66    'comments' =>
67      "Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338\n",
68    'total' => '4443.52'
69};
70
71my @buf1 = ();
72my @buf2 = ();
73my $buf3 = '';
74
75my @destination = (
76    {   name        => 'Array reference',
77        destination => \@buf1,
78        normalise   => sub { return \@buf1 },
79    },
80    {   name        => 'Closure',
81        destination => sub { push @buf2, shift },
82        normalise => sub { return \@buf2 },
83    },
84    {   name        => 'Scalar',
85        destination => \$buf3,
86        normalise   => sub {
87            my @ar = split( /\n/, $buf3 );
88            return \@ar;
89        },
90    },
91);
92
93for my $dest (@destination) {
94    my $name = $dest->{name};
95    ok my $yaml = TAP::Parser::YAMLish::Writer->new, "$name: Created";
96    isa_ok $yaml, 'TAP::Parser::YAMLish::Writer';
97
98    $yaml->write( $in, $dest->{destination} );
99    my $got = $dest->{normalise}->();
100    is_deeply $got, $out, "$name: Result matches";
101}
102