File: 317encode-eol.t

package info (click to toggle)
libmail-message-perl 3.019-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,620 kB
  • sloc: perl: 10,810; makefile: 4
file content (57 lines) | stat: -rw-r--r-- 1,607 bytes parent folder | download
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
#!/usr/bin/env perl
#
# Encoding and Decoding of Base64
# Could use some more tests....
#

use strict;
use warnings;

use Mail::Message::Test;
use Mail::Message::Body::Lines;

use Test::More;

use Time::HiRes 'time';
use Scalar::Util 'refaddr';

my $trace = 0;
my $large = $trace ? 100_000 : 1000;  # make larger for benchmarking

sub random_line($$$)
{	my ($avg_length, $number, $eol) = @_;
    my $line = substr "abcdefghijklmnopqrstuvwxyz0123456789!@#$%^*", 0, rand($avg_length *2);
	$line .= $eol->($line);
}

my @lines;
push @lines, random_line(20, $large, sub {(length $_[0]) % 2 ? "\012" : "\015\012"})
    while @lines < $large;
ok scalar @lines, 'created '.@lines.' lines';
my $i0 = grep ! m/\015\012$/, @lines;
cmp_ok $i0, '<', scalar @lines, "... $i0 do not end on CRLF";

my $body  = Mail::Message::Body::Lines->new
  ( mime_type => 'text/plain'
  , data      => \@lines
  );
is $body->mimeType, 'text/plain', '... text body';

my $s2   = time;
my $b2   = $body->eol("CRLF");
ok defined $b2, sprintf "Convert to CRLF in %.3fs", time - $s2;

ok refaddr $body != refaddr $b2, '... new body';
isa_ok $b2, 'Mail::Message::Body', '...';
isa_ok $b2, 'Mail::Message::Body::Lines', '...';
my $l2   = $b2->lines;
ok refaddr $l2 != refaddr \@lines, '... new set of lines';
cmp_ok scalar @$l2, '==', scalar @lines, '... same number of lines';
cmp_ok scalar(grep m!\015\012$!, @$l2), '==', scalar @$l2, '... all end with CRLF';

my $s3   = time;
my $b3   = $b2->eol("CRLF");
ok defined $b3, sprintf 'already perfect CRLF in %.3fs', time - $s3;
ok refaddr $b2 == refaddr $b3, '... same body';

done_testing;