File: 300body-file.t

package info (click to toggle)
libmail-message-perl 3.017-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,632 kB
  • sloc: perl: 11,156; makefile: 4
file content (85 lines) | stat: -rw-r--r-- 2,773 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/env perl
#
# Test processing of message bodies which have their content stored
# in a file.
#

use strict;
use warnings;

use Mail::Message::Test;
use Mail::Message::Body::File;

use Test::More tests => 33;
use IO::Scalar;

# Test to read a Lines from file.
# Let's fake the file, for simplicity.

my $filedata = <<'SIMULATED_FILE';
This is a file
with five lines, and it
is used to test whether
the reading into a lines body
would work (or not)
SIMULATED_FILE

# Test script has Unix line endings (LF) even under Windows.
# Replace LF by CRLF if running under Windows,
# so the file is truly a Windows file:
$filedata =~ s/\n/\r\n/gs if $crlf_platform;

my $f = IO::Scalar->new(\$filedata);

my $body = Mail::Message::Body::File->new(file => $f);
ok($body,                                           'body creation from file');
is($body->string, $filedata,                        'stringify');
cmp_ok($body->nrLines, "==", 5,                     'nr lines');

# Mail::Message::Body::File::size() substracts 1 per line (for CR) on Windows
my $body_length = length $filedata;
$body_length -= $body->nrLines if $crlf_platform;
cmp_ok($body->size, "==", $body_length,             'size');

my $fakeout;
my $g = IO::Scalar->new(\$fakeout);
$body->print($g);
is($fakeout, $filedata,                             'print');

my @lines = $body->lines;
cmp_ok(@lines, "==", 5,                             'count of lines');
my @filedata = split /^/, $filedata;
cmp_ok(@filedata, "==", 5,                          'count expected lines');
foreach (0..4) { is($lines[$_], $filedata[$_],      "line $_") }

# Reading data from lines.

$body = Mail::Message::Body::File->new(data => [@filedata]);
ok($body,                                           'creation from array of lines');
is($body->string, $filedata,                        'data');
cmp_ok($body->nrLines, "==", 5,                     'nr lines');
cmp_ok($body->size, "==", $body_length,             'size');

$fakeout = '';
$body->print($g);
is($fakeout, $filedata,                             'result print');

@lines = $body->lines;
cmp_ok(@lines, "==", 5,                             'count of lines');
foreach (0..4) { is($lines[$_], $filedata[$_],      "line $_") }

# Test overloading

is("$body", $filedata,                              'overloaded stringification');
@lines = @$body;
ok(@lines,                                          'overloaded ref array');
cmp_ok(@lines, "==", 5,                             'count of lines');
foreach (0..4) { is($lines[$_], $filedata[$_],      "line $_") }

# Test cleanup

my $filename = $body->tempFilename;
ok(-f $filename,                                    'filename exists');
undef $body;
ok(! -f $filename,                                  'file cleaned up');