| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Test::Email; | 
| 2 | 1 |  |  | 1 |  | 872203 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 32 |  | 
| 3 | 1 |  |  | 1 |  | 4 | use warnings; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 33 |  | 
| 4 |  |  |  |  |  |  |  | 
| 5 | 1 |  |  | 1 |  | 5 | use Test::Builder; | 
|  | 1 |  |  |  |  | 11 |  | 
|  | 1 |  |  |  |  | 17 |  | 
| 6 | 1 |  |  | 1 |  | 13 | use MIME::Parser; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 25 |  | 
| 7 | 1 |  |  | 1 |  | 5 | use Carp 'croak'; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 51 |  | 
| 8 |  |  |  |  |  |  |  | 
| 9 | 1 |  |  | 1 |  | 4 | use base 'MIME::Entity'; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 1505 |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | our $VERSION = '0.04'; | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | my $TEST = Test::Builder->new(); | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | my $DEBUG = 0; | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | sub ok { | 
| 18 | 0 |  |  | 0 | 1 | 0 | my ($self, $test_href, $desc) = @_; | 
| 19 |  |  |  |  |  |  |  | 
| 20 | 0 |  |  |  |  | 0 | my $pass = $self->_run_tests($test_href); | 
| 21 |  |  |  |  |  |  |  | 
| 22 | 0 |  |  |  |  | 0 | my $ok = $TEST->ok($pass, $desc); | 
| 23 |  |  |  |  |  |  |  | 
| 24 | 0 |  |  |  |  | 0 | return $ok; | 
| 25 |  |  |  |  |  |  | } | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | sub header_ok { | 
| 28 | 1 |  |  | 1 | 0 | 46 | my ($self, $header_name, $argument, $description) = @_; | 
| 29 |  |  |  |  |  |  |  | 
| 30 | 1 |  |  |  |  | 6 | my $value = $self->head()->get($header_name); | 
| 31 | 1 |  |  |  |  | 35 | chomp($value); | 
| 32 |  |  |  |  |  |  |  | 
| 33 | 1 |  |  |  |  | 5 | my $pass = $TEST->ok($value eq $argument, $description); | 
| 34 |  |  |  |  |  |  |  | 
| 35 | 1 |  |  |  |  | 644 | return $pass; | 
| 36 |  |  |  |  |  |  | } | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | sub header_like { | 
| 39 | 1 |  |  | 1 | 0 | 6003 | my ($self, $header_name, $argument, $description) = @_; | 
| 40 |  |  |  |  |  |  |  | 
| 41 | 1 |  |  |  |  | 5 | my $value = $self->head()->get($header_name); | 
| 42 | 1 |  |  |  |  | 33 | chomp($value); | 
| 43 |  |  |  |  |  |  |  | 
| 44 | 1 |  |  |  |  | 7 | my $pass = $TEST->like($value, $argument, $description); | 
| 45 |  |  |  |  |  |  |  | 
| 46 | 1 |  |  |  |  | 433 | return $pass; | 
| 47 |  |  |  |  |  |  | } | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | sub header_is { | 
| 50 | 1 |  |  | 1 | 0 | 8 | my ($self, $header_name, $argument, $description) = @_; | 
| 51 |  |  |  |  |  |  |  | 
| 52 | 1 |  |  |  |  | 4 | my $value = $self->head()->get($header_name); | 
| 53 | 1 |  |  |  |  | 36 | chomp($value); | 
| 54 |  |  |  |  |  |  |  | 
| 55 | 1 |  |  |  |  | 8 | my $pass = $TEST->is_eq($value, $argument, $description); | 
| 56 |  |  |  |  |  |  |  | 
| 57 | 1 |  |  |  |  | 342 | return $pass; | 
| 58 |  |  |  |  |  |  | } | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | sub body_ok { | 
| 61 | 1 |  |  | 1 | 0 | 9 | my ($self, $argument, $description) = @_; | 
| 62 |  |  |  |  |  |  |  | 
| 63 | 1 |  |  |  |  | 3 | my $body = join '', @{ $self->body() }; | 
|  | 1 |  |  |  |  | 6 |  | 
| 64 |  |  |  |  |  |  |  | 
| 65 | 1 |  |  |  |  | 630 | $body =~ s/\n+$//; | 
| 66 | 1 |  |  |  |  | 4 | $argument  =~ s/\n+$//; | 
| 67 |  |  |  |  |  |  |  | 
| 68 | 1 |  |  |  |  | 5 | my $pass = $TEST->ok($body eq $argument, $description); | 
| 69 |  |  |  |  |  |  |  | 
| 70 | 1 |  |  |  |  | 268 | return $pass; | 
| 71 |  |  |  |  |  |  | } | 
| 72 |  |  |  |  |  |  |  | 
| 73 |  |  |  |  |  |  | sub body_like { | 
| 74 | 1 |  |  | 1 | 0 | 9 | my ($self, $argument, $description) = @_; | 
| 75 |  |  |  |  |  |  |  | 
| 76 | 1 |  |  |  |  | 2 | my $body = join '', @{ $self->body() }; | 
|  | 1 |  |  |  |  | 11 |  | 
| 77 |  |  |  |  |  |  |  | 
| 78 | 1 |  |  |  |  | 643 | $body =~ s/\n+$//; | 
| 79 | 1 |  |  |  |  | 3 | $argument  =~ s/\n+$//; | 
| 80 |  |  |  |  |  |  |  | 
| 81 | 1 |  |  |  |  | 4 | my $pass = $TEST->like($body, $argument, $description); | 
| 82 |  |  |  |  |  |  |  | 
| 83 | 1 |  |  |  |  | 355 | return $pass; | 
| 84 |  |  |  |  |  |  | } | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  | sub body_is { | 
| 87 | 1 |  |  | 1 | 0 | 7 | my ($self, $argument, $description) = @_; | 
| 88 |  |  |  |  |  |  |  | 
| 89 | 1 |  |  |  |  | 3 | my $body = join '', @{ $self->body() }; | 
|  | 1 |  |  |  |  | 5 |  | 
| 90 |  |  |  |  |  |  |  | 
| 91 | 1 |  |  |  |  | 680 | $body =~ s/\n+$//; | 
| 92 | 1 |  |  |  |  | 3 | $argument  =~ s/\n+$//; | 
| 93 |  |  |  |  |  |  |  | 
| 94 | 1 |  |  |  |  | 5 | my $pass = $TEST->is_eq($body, $argument, $description); | 
| 95 |  |  |  |  |  |  |  | 
| 96 | 1 |  |  |  |  | 647 | return $pass; | 
| 97 |  |  |  |  |  |  | } | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | # run all tests against this email, return success | 
| 100 |  |  |  |  |  |  | sub _run_tests { | 
| 101 | 0 |  |  | 0 |  |  | my ($self, $test_href) = @_; | 
| 102 |  |  |  |  |  |  |  | 
| 103 | 0 |  |  |  |  |  | for my $key (keys %$test_href) { | 
| 104 | 0 |  |  |  |  |  | my $passed = $self->_test($key, $test_href->{$key}); | 
| 105 | 0 | 0 |  |  |  |  | if (!$passed) { | 
| 106 | 0 |  |  |  |  |  | return 0; | 
| 107 |  |  |  |  |  |  | } | 
| 108 |  |  |  |  |  |  | } | 
| 109 |  |  |  |  |  |  |  | 
| 110 | 0 |  |  |  |  |  | return 1; | 
| 111 |  |  |  |  |  |  | } | 
| 112 |  |  |  |  |  |  |  | 
| 113 |  |  |  |  |  |  | my %test_for = ( | 
| 114 |  |  |  |  |  |  | header	=>	\&_test_header, | 
| 115 |  |  |  |  |  |  | body	=>	\&_test_body, | 
| 116 |  |  |  |  |  |  | ); | 
| 117 |  |  |  |  |  |  |  | 
| 118 |  |  |  |  |  |  | # perform one test against one email | 
| 119 |  |  |  |  |  |  | sub _test { | 
| 120 | 0 |  |  | 0 |  |  | my ($self, $key, $test) = @_; | 
| 121 |  |  |  |  |  |  |  | 
| 122 | 0 |  |  |  |  |  | _debug("in _test($self, $key, $test)"); | 
| 123 |  |  |  |  |  |  |  | 
| 124 | 0 | 0 |  |  |  |  | if (my $test_cref = $test_for{$key}) { | 
| 125 | 0 |  |  |  |  |  | return $test_cref->($self, $test); | 
| 126 |  |  |  |  |  |  | } | 
| 127 |  |  |  |  |  |  | else { | 
| 128 | 0 |  |  |  |  |  | return $test_for{header}->($self, $key, $test); | 
| 129 |  |  |  |  |  |  | } | 
| 130 |  |  |  |  |  |  | } | 
| 131 |  |  |  |  |  |  |  | 
| 132 |  |  |  |  |  |  | sub _test_header { | 
| 133 | 0 |  |  | 0 |  |  | my ($self, $header, $test) = @_; | 
| 134 |  |  |  |  |  |  |  | 
| 135 | 0 |  |  |  |  |  | _debug("in _test_header($self, $header, $test)"); | 
| 136 |  |  |  |  |  |  |  | 
| 137 | 0 |  | 0 |  |  |  | my $value = $self->head()->get($header) || ''; | 
| 138 | 0 |  |  |  |  |  | chomp($value); | 
| 139 |  |  |  |  |  |  |  | 
| 140 | 0 |  |  |  |  |  | return _do_test($value, $test); | 
| 141 |  |  |  |  |  |  | } | 
| 142 |  |  |  |  |  |  |  | 
| 143 |  |  |  |  |  |  | sub _test_body { | 
| 144 | 0 |  |  | 0 |  |  | my ($self, $test) = @_; | 
| 145 |  |  |  |  |  |  |  | 
| 146 | 0 |  |  |  |  |  | _debug("in _test_body($self, $test)"); | 
| 147 |  |  |  |  |  |  |  | 
| 148 | 0 |  |  |  |  |  | my $body = join '', @{ $self->body() }; | 
|  | 0 |  |  |  |  |  |  | 
| 149 | 0 |  |  |  |  |  | return _do_test($body, $test); | 
| 150 |  |  |  |  |  |  | } | 
| 151 |  |  |  |  |  |  |  | 
| 152 |  |  |  |  |  |  | sub _do_test { | 
| 153 | 0 |  |  | 0 |  |  | my ($thing, $test) = @_; | 
| 154 |  |  |  |  |  |  |  | 
| 155 | 0 |  |  |  |  |  | _debug("Testing '$thing' against $test"); | 
| 156 |  |  |  |  |  |  |  | 
| 157 | 0 |  |  |  |  |  | my $type = ref $test; | 
| 158 | 0 | 0 |  |  |  |  | if ($type eq 'Regexp') { | 
|  |  | 0 |  |  |  |  |  | 
| 159 | 0 |  |  |  |  |  | return $thing =~ $test; | 
| 160 |  |  |  |  |  |  | } | 
| 161 |  |  |  |  |  |  | elsif ($type eq '') { | 
| 162 | 0 |  |  |  |  |  | $thing =~ s/\n+$//; | 
| 163 | 0 |  |  |  |  |  | $test  =~ s/\n+$//; | 
| 164 | 0 |  |  |  |  |  | return $thing eq $test; | 
| 165 |  |  |  |  |  |  | } | 
| 166 |  |  |  |  |  |  | else { | 
| 167 | 0 |  |  |  |  |  | croak "I don't know how to test for this type: '$type'"; | 
| 168 |  |  |  |  |  |  | } | 
| 169 |  |  |  |  |  |  | } | 
| 170 |  |  |  |  |  |  |  | 
| 171 |  |  |  |  |  |  | sub _debug { | 
| 172 | 0 |  |  | 0 |  |  | my ($msg) = @_; | 
| 173 | 0 | 0 |  |  |  |  | warn $msg."\n" if $DEBUG; | 
| 174 |  |  |  |  |  |  | } | 
| 175 |  |  |  |  |  |  |  | 
| 176 |  |  |  |  |  |  | 1; | 
| 177 |  |  |  |  |  |  | __END__ |