File: testmboxreader.rb

package info (click to toggle)
ruby-rubymail 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,056 kB
  • sloc: ruby: 6,061; makefile: 7
file content (161 lines) | stat: -rw-r--r-- 4,920 bytes parent folder | download | duplicates (5)
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env ruby
#--
#   Copyright (c) 2002 Matt Armstrong.  All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
#    this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. The name of the author may not be used to endorse or promote products
#    derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
# NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

require 'test/testbase'
require 'rmail/mailbox/mboxreader'

class TextRMailMBoxReader < TestBase

  def test_mbox_s_new
    r = RMail::Mailbox::MBoxReader.new("")
    assert_instance_of(RMail::Mailbox::MBoxReader, r)
  end

  def test_mbox_simple
    data_as_file("mbox.simple") { |f|
      mbox = RMail::Mailbox::MBoxReader.new(f)

      chunk = mbox.read(nil)
      assert_equal("From foo@bar  Wed Nov 27 12:27:32 2002\nmessage1\n",
                   chunk)
      chunk = mbox.read(nil)
      assert_nil(chunk)
      mbox.next

      chunk = mbox.read(nil)
      assert_equal("From foo@bar  Wed Nov 27 12:27:36 2002\nmessage2\n",
                   chunk)
      chunk = mbox.read(nil)
      assert_nil(chunk)
      mbox.next

      chunk = mbox.read(nil)
      assert_equal("From foo@bar  Wed Nov 27 12:27:40 2002\nmessage3\n",
                   chunk)
      chunk = mbox.read(nil)
      assert_nil(chunk)

      mbox.next
      chunk = mbox.read(nil)
      assert_nil(chunk)
    }
  end

  def test_mbox_odd
    data_as_file("mbox.odd") { |f|
      mbox = RMail::Mailbox::MBoxReader.new(f)

      chunk = mbox.read(nil)
      assert_equal("From foo@bar  Wed Nov 27 12:27:36 2002\nmessage1\n",
                   chunk)
      chunk = mbox.read(nil)
      assert_nil(chunk)
      mbox.next

      chunk = mbox.read(nil)
      assert_equal("From foo@bar  Wed Nov 27 12:27:40 2002\nmessage2\n",
                   chunk)
      chunk = mbox.read(nil)
      assert_nil(chunk)

      mbox.next
      chunk = mbox.read(nil)
      assert_nil(chunk)
    }
  end

  def t_chunksize_helper(reader, expected)
    chunk = reader.read(nil)
    assert_equal(expected, chunk)
  end

  def test_mbox_chunksize
    1.upto(80) { |chunk_size|
      data_as_file("mbox.simple") { |f|
        mbox = RMail::Mailbox::MBoxReader.new(f)

        mbox.chunk_size = chunk_size

        t_chunksize_helper(mbox,
           "From foo@bar  Wed Nov 27 12:27:32 2002\nmessage1\n")
        mbox.next
        t_chunksize_helper(mbox,
           "From foo@bar  Wed Nov 27 12:27:36 2002\nmessage2\n")
        mbox.next
        t_chunksize_helper(mbox,
           "From foo@bar  Wed Nov 27 12:27:40 2002\nmessage3\n")
        mbox.next

        chunk = mbox.read
        assert_nil(chunk)
      }
    }
  end

  def t_randomly_randomize(template)
    count = rand(10)
    messages = []
    0.upto(count) { |i|
      messages << template[0, 5 + rand(template.length - 5)] + "\n"
    }
    mbox_string = messages.join("\n")
    return [ messages, mbox_string ]
  end

  def t_randomly_parse_messages(reader)
    messages = []
    while message = reader.read(nil)
      messages << message
      reader.next
    end
    return messages
  end

  def t_randomly_parse_helper(chunk_size, messages, mbox_string)
    reader = RMail::Mailbox::MBoxReader.new(mbox_string)
    reader.chunk_size = chunk_size unless chunk_size.nil?

    messages2 = t_randomly_parse_messages(reader)
    assert_equal(messages, messages2)
  end

  def test_mbox_randomly
    5.times {
      template = ("From foo@bar\n" +
                  "text1\ntext2\ntext3\ntexttexttexttexttext4\n" +
                  (("abcdefghijklmnopqrstuvwxyz" * rand(5)) + "\n") * rand(20))

      messages, mbox_string = t_randomly_randomize(template)

      1.upto(200) { |chunk_size|
        t_randomly_parse_helper(chunk_size, messages, mbox_string)
      }
      t_randomly_parse_helper(nil, messages, mbox_string)
    }
  end

end