From 9c5768dfbaef3f2b5a5167a32c71d7259321e239 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dagfinn=20Ilmari=20Manns=C3=A5ker?= Date: Fri, 28 Oct 2016 17:56:32 +0100 Subject: Use a hash for the test mailstore Most of the operations are by ID, only retrieve_by_tags has to iterate over the messages. --- t/lib/Test/Sietima/MailStore.pm | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) (limited to 't') diff --git a/t/lib/Test/Sietima/MailStore.pm b/t/lib/Test/Sietima/MailStore.pm index 2925fd3..030e5fc 100644 --- a/t/lib/Test/Sietima/MailStore.pm +++ b/t/lib/Test/Sietima/MailStore.pm @@ -9,15 +9,15 @@ with 'Sietima::MailStore'; has _mails => ( is => 'rw', - default => sub { +[] }, + default => sub { +{} }, ); -sub clear { shift->_mails([]) } +sub clear { shift->_mails({}) } sub store ($self,$mail,@tags) { my $str = $mail->as_string; my $id = sha1_hex($str); - push $self->_mails->@*, { + $self->_mails->{$id} = { id => $id, mail => $str, tags => { map {$_ => 1;} @tags, }, @@ -27,7 +27,7 @@ sub store ($self,$mail,@tags) { sub retrieve_by_tags ($self,@tags){ my @ret; - for my $m ($self->_mails->@*) { + for my $m (values $self->_mails->%*) { next unless all { $m->{tags}{$_} } @tags; push @ret, { $m->%{id}, @@ -39,8 +39,7 @@ sub retrieve_by_tags ($self,@tags){ } sub retrieve_by_id ($self,$id) { - for my $m ($self->_mails->@*) { - next unless $m->{id} eq $id; + if (my $m = $self->_mails->{$id}) { return Email::MIME->new($m->{mail}); } @@ -48,9 +47,7 @@ sub retrieve_by_id ($self,$id) { } sub remove($self,$id) { - my $idx = first_index { $_->{id} eq $id } $self->_mails->@*; - return unless defined $idx; - splice $self->_mails->@*,$idx,1; + delete $self->_mails->{$id}; return; } -- cgit v1.2.3