-
Notifications
You must be signed in to change notification settings - Fork 36
/
test_mbox.rb
147 lines (126 loc) · 3.55 KB
/
test_mbox.rb
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
138
139
140
141
142
143
144
145
146
147
require 'test_helper'
require 'tmail/mailbox'
require 'fileutils'
class MailboxTester < Test::Unit::TestCase
include FileUtils
MAILBOX = '_mh'
N = 5
def setup
rm_rf MAILBOX
mkdir MAILBOX
N.downto(1) do |i|
File.open( "#{MAILBOX}/#{i}", 'w' ) {|f|
f.puts 'From: aamine'
f.puts 'To: aamine@loveruby.net'
f.puts "Subject: #{i}"
f.puts ''
f.puts 'body'
}
end
@n = N
@ld = TMail::MhMailbox.new( MAILBOX )
end
def make_mails_older( diff )
Dir.entries( MAILBOX ).collect {|n| "#{MAILBOX}/#{n}" }.each do |path|
if File.file? path then
t = File.mtime(path) - diff
File.utime t, t, path
end
end
end
def teardown
rm_rf MAILBOX
end
def test_s_new
ld = TMail::MhMailbox.new( MAILBOX )
assert_instance_of TMail::MhMailbox, ld
end
def test_each_port
dir = File.expand_path(MAILBOX)
c = 0
n = 0
TMail::MhMailbox.new( MAILBOX ).each_port do |port|
assert_kind_of TMail::FilePort, port
assert_equal dir, File.dirname(port.filename)
assert_match(/\A\d+\z/, File.basename(port.filename))
nn = File.basename(port.filename).to_i
assert nn > n
n = nn
c += 1
end
assert_equal N, c
end
def test_reverse_each_port
dir = File.expand_path(MAILBOX)
c = 0
n = 100000
TMail::MhMailbox.new( MAILBOX ).reverse_each_port do |port|
assert_kind_of TMail::FilePort, port
assert_equal dir, File.dirname(port.filename)
assert_match(/\A\d+\z/, File.basename(port.filename))
nn = File.basename(port.filename).to_i
assert nn < n
n = nn
c += 1
end
assert_equal N, c
end
def test_new_port
port = @ld.new_port
assert_kind_of TMail::FilePort, port
assert_equal File.expand_path('.') + '/' + MAILBOX,
File.dirname(port.filename)
assert_equal( (N+1).to_s, File.basename(port.filename) )
create port
end
def create( port )
port.wopen {|f|
f.puts 'From: aamine'
f.puts 'To: aamine@loveruby.net'
f.puts "Subject: #{@n + 1}"
f.puts ''
f.puts 'body'
}
@n += 1
end
def test_each_new_port
make_mails_older 5
c = 0
@ld.each_new_port do |port|
assert_kind_of TMail::FilePort, port
c += 1
end
assert_equal @n, c
t = Time.now - 2
create @ld.new_port
c = 0
@ld.each_new_port( t ) do |port|
assert_kind_of TMail::FilePort, port
c += 1
end
assert_equal 1, c
make_mails_older 5
c = 0
@ld.each_new_port do |port|
assert_kind_of TMail::FilePort, port
c += 1
end
assert_equal 0, c
end
def test_unix_mbox_fromaddr_method
p = TMail::FilePort.new("#{File.dirname(__FILE__)}/fixtures/mailbox")
assert_equal(TMail::UNIXMbox.fromaddr(p), "mikel@return_path.com")
end
def test_unix_mbox_fromaddr_method_missing_return_path
p = TMail::FilePort.new("#{File.dirname(__FILE__)}/fixtures/mailbox_without_return_path")
assert_equal(TMail::UNIXMbox.fromaddr(p), "mikel@from_address.com")
end
def test_unix_mbox_fromaddr_method_missing_from_address
p = TMail::FilePort.new("#{File.dirname(__FILE__)}/fixtures/mailbox_without_from")
assert_equal(TMail::UNIXMbox.fromaddr(p), "mike@envelope_sender.com.au")
end
def test_unix_mbox_from_addr_method_missing_all_from_fields_in_the_email
p = TMail::FilePort.new("#{File.dirname(__FILE__)}/fixtures/mailbox_without_any_from_or_sender")
assert_equal(TMail::UNIXMbox.fromaddr(p), "nobody")
end
end