summaryrefslogtreecommitdiff
path: root/t/08_readmember_record_sep.t
blob: 5522dfddecdae9f614661b8527b9670a552e543e (plain)
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
#!/usr/bin/perl

use strict;

BEGIN {
    $|  = 1;
    $^W = 1;
}
use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
use Archive::Zip::MemberRead;
use File::Spec;

use Test::More;

BEGIN {
    if ($^O eq 'MSWin32') {
        plan(skip_all => 'Ignoring failing tests on Win32');
    } else {
        plan(tests => 13);
    }
}
use t::common;

SCOPE: {
    my $filename = File::Spec->catfile(TESTDIR, "member_read_xml_like1.zip");
    my $zip = new Archive::Zip;

    # TEST
    isa_ok($zip, "Archive::Zip", "Testing that \$zip is an Archive::Zip");

    my $data = <<"EOF";
One Line
Two Lines
</tag>
Three Lines
Four Lines
Five Lines
</tag>
Quant
Bant
</tag>
Zapta
EOF

    $zip->addString($data, "string.txt");
    $zip->writeToFileNamed($filename);

    {
        # Testing for normal line-based reading.
        my $member = $zip->memberNamed("string.txt");
        my $fh     = $member->readFileHandle();

        # TEST
        ok($fh, "Filehandle is valid");

        # TEST
        is($fh->getline(), "One Line",
            "Testing the first line in a normal read.");

        # TEST
        is($fh->getline(), "Two Lines",
            "Testing the second line in a normal read.");
    }

    {
        # Testing for setting the input record separator of the Perl
        # global variable.

        local $/ = "</tag>\n";

        my $member = $zip->memberNamed("string.txt");
        my $fh     = $member->readFileHandle();

        # TEST
        ok($fh, "Filehandle is valid");

        # TEST
        is(
            $fh->getline(),
            "One Line\nTwo Lines\n",
            "Testing the first \"line\" when \$/ is set."
        );

        # TEST
        is(
            $fh->getline(),
            "Three Lines\nFour Lines\nFive Lines\n",
            "Testing the second \"line\" when \$/ is set."
        );
    }

    {
        # Testing for setting input_record_separator in the filehandle.

        my $member = $zip->memberNamed("string.txt");
        my $fh     = $member->readFileHandle();

        # TEST
        ok($fh, "Filehandle is valid");

        $fh->input_record_separator("</tag>\n");

        # TEST
        is(
            $fh->getline(),
            "One Line\nTwo Lines\n",
            "Testing the first line when input_record_separator is set."
        );

        # TEST
        is(
            $fh->getline(),
            "Three Lines\nFour Lines\nFive Lines\n",
            "Testing the second line when input_record_separator is set."
        );
    }
    {
        # Test setting both input_record_separator in the filehandle
        # and in Perl.

        local $/ = "</t";

        my $member = $zip->memberNamed("string.txt");
        my $fh     = $member->readFileHandle();

        # TEST
        ok($fh, "Filehandle is valid");

        $fh->input_record_separator(" ");

        # TEST
        is($fh->getline(), "One",
            "Testing the first \"line\" in a both set read");

        # TEST
        is($fh->getline(), "Line\nTwo",
            "Testing the second \"line\" in a both set read.");
    }
}