Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 185 lines (159 sloc) 5.089 kB
40d5744 @mikel Fixed scanner.rb so that it passes the same tests as scanner.c. Also…
authored
1 require 'test_helper'
08e6952 @trans initial import
trans authored
2 require 'tmail/mailbox'
3 require 'fileutils'
4
215d73a @mikel ==== Fri Oct 26 02:21:13 GMT 2007 Mikel Lindsaar <raasdnil@gmail.com>
authored
5 class MailboxTester < Test::Unit::TestCase
08e6952 @trans initial import
trans authored
6 include FileUtils
7
8 MAILBOX = '_mh'
9 N = 5
10
11 def setup
50ec7e9 @mikel Removed the masses of rm_rf & mkdir messages from the test suite
authored
12 rm_rf(MAILBOX, :verbose => false)
13 mkdir(MAILBOX, :verbose => false)
08e6952 @trans initial import
trans authored
14 N.downto(1) do |i|
15 File.open( "#{MAILBOX}/#{i}", 'w' ) {|f|
16 f.puts 'From: aamine'
17 f.puts 'To: aamine@loveruby.net'
18 f.puts "Subject: #{i}"
19 f.puts ''
20 f.puts 'body'
21 }
22 end
23 @n = N
24
25 @ld = TMail::MhMailbox.new( MAILBOX )
26 end
27
28 def make_mails_older( diff )
29 Dir.entries( MAILBOX ).collect {|n| "#{MAILBOX}/#{n}" }.each do |path|
30 if File.file? path then
31 t = File.mtime(path) - diff
32 File.utime t, t, path
33 end
34 end
35 end
36
37 def teardown
50ec7e9 @mikel Removed the masses of rm_rf & mkdir messages from the test suite
authored
38 rm_rf(MAILBOX, :verbose => false)
08e6952 @trans initial import
trans authored
39 end
40
41 def test_s_new
42 ld = TMail::MhMailbox.new( MAILBOX )
43 assert_instance_of TMail::MhMailbox, ld
44 end
45
46 def test_each_port
47 dir = File.expand_path(MAILBOX)
48 c = 0
49 n = 0
50 TMail::MhMailbox.new( MAILBOX ).each_port do |port|
51 assert_kind_of TMail::FilePort, port
52 assert_equal dir, File.dirname(port.filename)
f51f010 @mikel * Made the package into a GEM!
authored
53 assert_match(/\A\d+\z/, File.basename(port.filename))
08e6952 @trans initial import
trans authored
54 nn = File.basename(port.filename).to_i
55 assert nn > n
56 n = nn
57 c += 1
58 end
59 assert_equal N, c
60 end
61
62 def test_reverse_each_port
63 dir = File.expand_path(MAILBOX)
64 c = 0
65 n = 100000
66 TMail::MhMailbox.new( MAILBOX ).reverse_each_port do |port|
67 assert_kind_of TMail::FilePort, port
68 assert_equal dir, File.dirname(port.filename)
f51f010 @mikel * Made the package into a GEM!
authored
69 assert_match(/\A\d+\z/, File.basename(port.filename))
08e6952 @trans initial import
trans authored
70 nn = File.basename(port.filename).to_i
71 assert nn < n
72 n = nn
73 c += 1
74 end
75 assert_equal N, c
76 end
77
78 def test_new_port
79 port = @ld.new_port
80 assert_kind_of TMail::FilePort, port
81 assert_equal File.expand_path('.') + '/' + MAILBOX,
82 File.dirname(port.filename)
83 assert_equal( (N+1).to_s, File.basename(port.filename) )
84
85 create port
86 end
87
88 def create( port )
89 port.wopen {|f|
90 f.puts 'From: aamine'
91 f.puts 'To: aamine@loveruby.net'
92 f.puts "Subject: #{@n + 1}"
93 f.puts ''
94 f.puts 'body'
95 }
96 @n += 1
97 end
98
99 def test_each_new_port
100 make_mails_older 5
101
102 c = 0
103 @ld.each_new_port do |port|
104 assert_kind_of TMail::FilePort, port
105 c += 1
106 end
107 assert_equal @n, c
108
109 t = Time.now - 2
110 create @ld.new_port
111 c = 0
112 @ld.each_new_port( t ) do |port|
113 assert_kind_of TMail::FilePort, port
114 c += 1
115 end
116 assert_equal 1, c
117
118 make_mails_older 5
119 c = 0
120 @ld.each_new_port do |port|
121 assert_kind_of TMail::FilePort, port
122 c += 1
123 end
124 assert_equal 0, c
125 end
e5f52a9 @mikel Added tests for UNIXMbox.fromaddr method. Also changed behaviour of H…
authored
126
127 def test_unix_mbox_fromaddr_method
128 p = TMail::FilePort.new("#{File.dirname(__FILE__)}/fixtures/mailbox")
129 assert_equal(TMail::UNIXMbox.fromaddr(p), "mikel@return_path.com")
130 end
131
132 def test_unix_mbox_fromaddr_method_missing_return_path
133 p = TMail::FilePort.new("#{File.dirname(__FILE__)}/fixtures/mailbox_without_return_path")
134 assert_equal(TMail::UNIXMbox.fromaddr(p), "mikel@from_address.com")
135 end
136
137 def test_unix_mbox_fromaddr_method_missing_from_address
138 p = TMail::FilePort.new("#{File.dirname(__FILE__)}/fixtures/mailbox_without_from")
139 assert_equal(TMail::UNIXMbox.fromaddr(p), "mike@envelope_sender.com.au")
140 end
141
142 def test_unix_mbox_from_addr_method_missing_all_from_fields_in_the_email
143 p = TMail::FilePort.new("#{File.dirname(__FILE__)}/fixtures/mailbox_without_any_from_or_sender")
144 assert_equal(TMail::UNIXMbox.fromaddr(p), "nobody")
145 end
146
38df9c9 @mikel Added a lot more documentation, getting there
authored
147 def test_opening_mailbox_to_read_does_not_raise_IO_error
148 mailbox = TMail::UNIXMbox.new("#{File.dirname(__FILE__)}/fixtures/mailbox", nil, true)
149 assert_nothing_raised do
150 mailbox.each_port do |port|
151 TMail::Mail.new(port)
152 end
153 end
154 end
155
156 def test_reading_correct_number_of_emails_from_a_mailbox
157 mailbox = TMail::UNIXMbox.new("#{File.dirname(__FILE__)}/fixtures/mailbox", nil, true)
158 @emails = []
159 mailbox.each_port { |m| @emails << TMail::Mail.new(m) }
160 assert_equal(4, @emails.length)
161 end
162
163 def test_truncating_a_mailbox_to_zero_if_it_is_opened_with_readonly_false
164 filename = "#{File.dirname(__FILE__)}/fixtures/mailbox"
8567557 @mikel Applied patch #18515 to remove ftools from test case (Charles Lowe)
authored
165 FileUtils.copy(filename, "#{filename}_test")
38df9c9 @mikel Added a lot more documentation, getting there
authored
166 filename = "#{filename}_test"
167 mailbox = TMail::UNIXMbox.new(filename, nil, false)
168 @emails = []
169 mailbox.each_port { |m| @emails << TMail::Mail.new(m) }
170 assert_equal(4, @emails.length)
171 assert_equal('', File.read(filename))
172 File.delete(filename)
173 end
174
27239fe @mikel Closed #20724 - TMail Mailbox would fail if the From line had an out …
authored
175 def test_fromline2time_should_return_nil_on_out_of_range_dates
176 filename = "#{File.dirname(__FILE__)}/fixtures/mailbox"
177 FileUtils.copy(filename, "#{filename}_test")
178 filename = "#{filename}_test"
179 line = "From mikel mikel March 24 01:02:03 1900"
180 mailbox = TMail::UNIXMbox.new(filename, nil, false)
181 assert_equal(nil, mailbox.send(:fromline2time, line))
182 end
183
08e6952 @trans initial import
trans authored
184 end
Something went wrong with that request. Please try again.