File: 12-dumpfile.t

package info (click to toggle)
libyaml-syck-perl 1.20-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 932 kB
  • sloc: ansic: 3,987; perl: 3,387; makefile: 4
file content (88 lines) | stat: -rw-r--r-- 2,072 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
use t::TestYAML;
use Test::More;
use FindBin '$RealBin';

chdir $RealBin;

unless (-w $RealBin) {
    plan skip_all => "Can't write to $RealBin";
    exit;
}

plan tests => 6;

*::DumpFile = *YAML::Syck::DumpFile;

sub file_contents_is {
    my ($fn, $expected, $test_name) = @_;
    local *FH;
    open FH, $fn or die $!;
    my $contents = do { local $/; <FH> };
    is($contents, $expected, $test_name);
    close FH;
}

my $scalar = 'a simple scalar';
my $expected_yaml = <<YAML;
--- a simple scalar
YAML

# using file name
{
    DumpFile('dumpfile.yml', $scalar);
    file_contents_is('dumpfile.yml', $expected_yaml, 'DumpFile works with filenames');
    unlink 'dumpfile.yml' or die $!;
}

# dump to IO::File
{
    require IO::File;
    my $h = IO::File->new('>dumpfile.yml');
    DumpFile($h, $scalar);
    close $h;
    file_contents_is('dumpfile.yml', $expected_yaml, 'DumpFile works with IO::File');
    unlink 'dumpfile.yml' or die $!;
}

# dump to indirect file handles
SKIP: {
    skip "indirect file handles require 5.6 or later", 1 unless $] >= 5.006000; eval q[

    open(my $h, '>', 'dumpfile.yml');
    DumpFile($h, $scalar);
    close $h;
    file_contents_is('dumpfile.yml', $expected_yaml, 'DumpFile works with indirect file handles');
    unlink 'dumpfile.yml' or die $!;

] }

# dump to ordinary filehandles
{
    local *H;
    open(H, '>dumpfile.yml');
    DumpFile(*H, $scalar);
    close(H);
    file_contents_is('dumpfile.yml', $expected_yaml, 'DumpFile works with ordinary file handles');
    unlink 'dumpfile.yml' or die $!;
}

# dump to ordinary filehandles (refs)
{
    local *H;
    open(H, '>dumpfile.yml');
    DumpFile(\*H, $scalar);
    close(H);
    file_contents_is('dumpfile.yml', $expected_yaml, 'DumpFile works with glob refs');
    unlink 'dumpfile.yml' or die $!;
}

# dump to "in memory" file
SKIP : {
    skip "in-memory files require 5.8 or later", 1 unless $] >= 5.00800; eval q[

    open(my $h, '>', \my $s);
    DumpFile($h, $scalar);
    close($h);
    is($s, $expected_yaml, 'DumpFile works with in-memory files');

] }