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
|
#!/usr/bin/perl -T
#
# Test flags conversion of IMAP4 folders.
use strict;
use warnings;
use lib qw(. .. tests);
use Tools;
use Test::More;
use Mail::Transport::IMAP4;
my $mti = 'Mail::Transport::IMAP4';
BEGIN
{ eval { require Mail::IMAPClient };
if($@ =~ m/Can't locate/)
{ plan skip_all => 'requires Mail::IMAPClient';
exit(0);
}
eval { require Digest::HMAC_MD5 };
if($@ =~ m/Can't locate/)
{ plan skip_all => 'requires Digest::HMAC_MD5';
exit(0);
}
plan tests => 65;
}
###
### Checking labels2flags
###
sub expect_flags($$$)
{ my ($got, $expect, $text) = @_;
my $errors = 0;
my %got;
$got{$_}++ for split " ", $got;
if(grep {$_ > 1} values %got)
{ $errors++;
ok(0, "found double, $text");
}
else
{ ok(1, $text);
}
foreach my $e (split " ", $expect)
{ if(delete $got{$e}) { ok(1, "found $e") }
else { $errors++; ok(0, "missing $e") }
}
if(keys %got)
{ ok(0, "got too much: ".join(" ", keys %got));
$errors++;
}
else
{ ok(1, "exact match");
}
if($errors)
{ warn "$errors errors, expected '$expect' got '$got'\n";
}
}
my $flags = $mti->labelsToFlags();
expect_flags($flags, '', "Empty set");
$flags = $mti->labelsToFlags(seen => 1, flagged => 1, old => 1);
expect_flags($flags, '\Seen \Flagged', "No old");
$flags = $mti->labelsToFlags( {seen => 1, flagged => 1, old => 1} );
expect_flags($flags, '\Seen \Flagged', "No old as hash");
$flags = $mti->labelsToFlags(seen => 1, flagged => 1, old => 0);
expect_flags($flags, '\Seen \Flagged \Recent', "No old");
$flags = $mti->labelsToFlags( {seen => 1, flagged => 1, old => 0} );
expect_flags($flags, '\Seen \Flagged \Recent', "No old as hash");
$flags = $mti->labelsToFlags(seen => 1, replied => 1, flagged => 1,
deleted => 1, draft => 1, old => 0, spam => 1);
expect_flags($flags, '\Seen \Answered \Flagged \Deleted \Draft \Recent \Spam',
"show all flags");
$flags = $mti->labelsToFlags(seen => 0, replied => 0, flagged => 0,
deleted => 0, draft => 0, old => 1, spam => 0);
expect_flags($flags, '', "show no flags");
###
### Checking flagsToLabels
###
sub expect_labels($$$)
{ my ($got, $expect, $text) = @_;
my $gotkeys = join " ", %$got;
my $expkeys = join " ", %$expect;
# warn "expected '$expkeys' got '$gotkeys'\n";
# depends on predefined labels
cmp_ok(scalar keys %$got, '==', 7, "$text; nr fields");
foreach my $k (keys %$got)
{ my $g = $got->{$k} || 0;
my $e = $expect->{$k} || 0;
cmp_ok($g, '==', $e, "got $k");
}
foreach my $k (keys %$expect)
{ my $g = $got->{$k} || 0;
my $e = $expect->{$k} || 0;
cmp_ok($g, '==', $e, "expect $k");
}
}
my $labels = $mti->flagsToLabels('REPLACE');
expect_labels $labels, {old => 1}, "flagsToLabels: Empty set";
$labels = $mti->flagsToLabels(REPLACE => qw[\Seen \Flagged] );
expect_labels $labels
, {old => 1, seen => 1, flagged => 1}
, "flagsToLabels: Empty set";
$labels = $mti->flagsToLabels(REPLACE =>
qw[\Seen \Answered \Flagged \Deleted \Draft \Recent \Spam] );
expect_labels $labels
, { seen => 1, replied => 1, flagged => 1, deleted => 1
, draft => 1, spam => 1
}
, "show all labels";
exit 0;
|