1#!/usr/bin/env perl
2#
3# Test processing of message bodies which have their content stored
4# in an array.  This does not test the reading of the bodies
5# from file.
6#
7
8use strict;
9use warnings;
10
11use Mail::Message::Test;
12use Mail::Message::Body::Lines;
13
14use Test::More tests => 30;
15use IO::Scalar;
16
17# Test to read a Lines from file.
18# Let's fake the file, for simplicity.
19
20my $filedata = <<'SIMULATED_FILE';
21This is a file
22with five lines, and it
23is used to test whether
24the reading into a lines body
25would work (or not)
26SIMULATED_FILE
27
28my $f = IO::Scalar->new(\$filedata);
29
30my $body = Mail::Message::Body::Lines->new(file => $f);
31ok($body,                                        "body from file is true");
32
33is($body->string, $filedata,                     "body strings to data");
34cmp_ok($body->nrLines, "==", 5,                  "body reports 5 lines");
35cmp_ok($body->size, "==", length $filedata,      "body size as data");
36
37my $fakeout;
38my $g = IO::Scalar->new(\$fakeout);
39$body->print($g);
40is($fakeout, $filedata,                          "body prints right data");
41
42my @lines = $body->lines;
43cmp_ok(@lines, "==", 5,                          "body produces five lines");
44
45my @filedata = split /^/, $filedata;
46cmp_ok(@filedata, "==", 5,                       "data 5 lines");
47
48foreach (0..4) { is($lines[$_], $filedata[$_],   "expected line $_") }
49
50# Reading data from lines.
51
52$body = Mail::Message::Body::Lines->new(data => [@filedata]);
53ok($body,                                        "body from array is true");
54
55is($body->string, $filedata,                     "body string is data");
56cmp_ok($body->nrLines, "==", 5,                  "body reports 5 lines");
57cmp_ok($body->size, "==", length $filedata,      "body reports correct size");
58
59$fakeout = '';
60$body->print($g);
61is($fakeout, $filedata,                          "body prints to data");
62
63@lines = $body->lines;
64cmp_ok(@lines, "==", 5,                          "body produces 5 lines");
65foreach (0..4) { is($lines[$_], $filedata[$_],   "body line $_") }
66
67# Test overloading
68
69is("$body", $filedata,                           "stringification");
70@lines = @$body;
71cmp_ok(@lines, "==", 5,                          "overload array-deref");
72foreach (0..4) { is($lines[$_], $filedata[$_],   "overload array $_") }
73