]> git.cworth.org Git - sup/commitdiff
add unit tests for mbox parsing that currently fail OMG TEST-DRIVEN DEVELOPMENT
authorWilliam Morgan <wmorgan-sup@masanjin.net>
Sat, 26 Apr 2008 21:25:27 +0000 (14:25 -0700)
committerWilliam Morgan <wmorgan-sup@masanjin.net>
Sat, 26 Apr 2008 21:38:51 +0000 (14:38 -0700)
test/test_mbox_parsing.rb [new file with mode: 0644]

diff --git a/test/test_mbox_parsing.rb b/test/test_mbox_parsing.rb
new file mode 100644 (file)
index 0000000..070b152
--- /dev/null
@@ -0,0 +1,114 @@
+#!/usr/bin/ruby
+
+require 'test/unit'
+require 'sup'
+require 'stringio'
+
+include Redwood
+
+class TestMessage < Test::Unit::TestCase
+  def setup
+  end
+
+  def teardown
+  end
+
+  def test_normal_headers
+    h = MBox.read_header StringIO.new(<<EOS)
+From: Bob <bob@bob.com>
+To: Sally <sally@sally.com>
+EOS
+
+    assert_equal "Bob <bob@bob.com>", h["From"]
+    assert_equal "Sally <sally@sally.com>", h["To"]
+    assert_nil h["Message-Id"]
+  end
+
+  ## this is shitty behavior in retrospect, but it's built in now.
+  def test_message_id_stripping
+    h = MBox.read_header StringIO.new("Message-Id: <one@bob.com>\n")
+    assert_equal "one@bob.com", h["Message-Id"]
+
+    h = MBox.read_header StringIO.new("Message-Id: one@bob.com\n")
+    assert_equal "one@bob.com", h["Message-Id"]
+  end
+
+  def test_multiline
+    h = MBox.read_header StringIO.new(<<EOS)
+From: Bob <bob@bob.com>
+Subject: one two three
+  four five six
+To: Sally <sally@sally.com>
+References: seven
+  eight
+Seven: Eight
+EOS
+
+    assert_equal "one two three four five six", h["Subject"]
+    assert_equal "Sally <sally@sally.com>", h["To"]
+    assert_equal "seven eight", h["References"]
+  end
+
+  def test_ignore_spacing
+    variants = [
+      "Subject:one two  three   end\n",
+      "Subject:    one two  three   end\n",
+      "Subject:   one two  three   end    \n",
+    ]
+    variants.each do |s|
+      h = MBox.read_header StringIO.new(s)
+      assert_equal "one two  three   end", h["Subject"]
+    end
+  end
+
+  def test_message_id_ignore_spacing
+    variants = [
+      "Message-Id:     <one@bob.com>       \n",
+      "Message-Id:      one@bob.com        \n",
+      "Message-Id:<one@bob.com>       \n",
+      "Message-Id:one@bob.com       \n",
+    ]
+    variants.each do |s|
+      h = MBox.read_header StringIO.new(s)
+      assert_equal "one@bob.com", h["Message-Id"]
+    end
+  end
+
+  def test_ignore_empty_lines
+    variants = [
+      "",
+      "Message-Id:       \n",
+      "Message-Id:\n",
+    ]
+    variants.each do |s|
+      h = MBox.read_header StringIO.new(s)
+      assert_nil h["Message-Id"]
+    end
+  end
+
+  def test_detect_end_of_headers
+    h = MBox.read_header StringIO.new(<<EOS)
+From: Bob <bob@bob.com>
+
+To: a dear friend
+EOS
+  assert_equal "Bob <bob@bob.com>", h["From"]
+  assert_nil h["To"]
+
+  h = MBox.read_header StringIO.new(<<EOS)
+From: Bob <bob@bob.com>
+\r
+To: a dear friend
+EOS
+  assert_equal "Bob <bob@bob.com>", h["From"]
+  assert_nil h["To"]
+
+  h = MBox.read_header StringIO.new(<<EOS)
+From: Bob <bob@bob.com>
+\r\n\r
+To: a dear friend
+EOS
+  assert_equal "Bob <bob@bob.com>", h["From"]
+  assert_nil h["To"]
+  end
+end