-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement test code for Sisimai::Mail::Memory
- Loading branch information
1 parent
cf66e00
commit d3e73ea
Showing
1 changed file
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
require 'spec_helper' | ||
require 'sisimai/mail/memory' | ||
|
||
describe Sisimai::Mail::Memory do | ||
samplemailbox = [ | ||
'./set-of-emails/mailbox/mbox-0', | ||
'./set-of-emails/maildir/bsd/email-sendmail-01.eml' | ||
] | ||
let(:mailobj) { Sisimai::Mail::Memory.new(samples) } | ||
let(:mockobj) { Sisimai::Mail::Memory.new(invalid) } | ||
emailcontents = nil | ||
|
||
describe 'class method' do | ||
describe '.new' do | ||
context 'mbox file exists' do | ||
messagehandle = File.open(samplemailbox[0], 'r') | ||
emailcontents = messagehandle.read | ||
messagehandle.close | ||
let(:samples) { emailcontents } | ||
subject { mailobj } | ||
it 'returns Sisimai::Mail::Memory object' do | ||
is_expected.to be_a Sisimai::Mail::Memory | ||
end | ||
end | ||
|
||
context 'argument is not a String' do | ||
let(:invalid) { 1 } | ||
it 'raises RuntimeError' do | ||
expect { mockobj }.to raise_error(RuntimeError) | ||
end | ||
end | ||
|
||
context 'argument is empty' do | ||
let(:invalid) { '' } | ||
it 'raises RuntimeError' do | ||
expect { mockobj }.to raise_error(RuntimeError) | ||
end | ||
end | ||
|
||
context 'wrong number of arguments' do | ||
it 'raises ArgumentError' do | ||
expect { Sisimai::Mail::Memory.new }.to raise_error(ArgumentError) | ||
expect { Sisimai::Mail::Memory.new(nil, nil) }.to raise_error(ArgumentError) | ||
end | ||
end | ||
end | ||
end | ||
|
||
describe 'instance method' do | ||
samplemailbox.each do |b| | ||
messagehandle = File.open(samplemailbox[0], 'r') | ||
emailcontents = messagehandle.read | ||
messagehandle.close | ||
let(:samples) { emailcontents } | ||
before do | ||
mailobj.read | ||
end | ||
describe '#size' do | ||
subject { mailobj.size } | ||
it 'returns email size' do | ||
is_expected.to be_a Integer | ||
is_expected.to be > 90000 | ||
end | ||
end | ||
describe '#data' do | ||
subject { mailobj.data } | ||
it 'returns Array' do | ||
is_expected.to be_a Array | ||
end | ||
end | ||
describe '#offset' do | ||
subject { mailobj.offset } | ||
it 'returns valid offset size' do | ||
is_expected.to be_a Integer | ||
is_expected.to be > 0 | ||
is_expected.to be < mailobj.size | ||
end | ||
end | ||
|
||
describe '#read' do | ||
mailbox = Sisimai::Mail::Memory.new(emailcontents) | ||
emindex = 0 | ||
hasread = 0 | ||
|
||
while r = mailbox.read do | ||
emindex += 1 | ||
mailtxt = r | ||
hasread += mailtxt.size | ||
subject { mailtxt } | ||
|
||
it 'returns valid email text' do | ||
is_expected.to be_a String | ||
is_expected.to match(/From:\s*/) | ||
expect(mailtxt.size).to be > 0 | ||
end | ||
example 'current position is larger than 0' do | ||
expect(mailbox.offset).to be > 0 | ||
end | ||
end | ||
|
||
example "the number of emails is #{emindex}" do | ||
expect(emindex).to be == 37 | ||
end | ||
example 'loaded size is larger than 0' do | ||
expect(hasread).to be > 0 | ||
end | ||
end | ||
end | ||
|
||
end | ||
end | ||
|