Commit 876dc70b authored by Fred Drake's avatar Fred Drake

Re-write the rfc822 tests to use PyUnit.

Update to reflect using "" as the default value for the second parameter
to the get() method.
parent 233226e9
from test_support import verbose, verify import rfc822
import rfc822, sys import sys
import test_support
import unittest
try: try:
from cStringIO import StringIO from cStringIO import StringIO
except ImportError: except ImportError:
from StringIO import StringIO from StringIO import StringIO
def test(msg, results):
fp = StringIO()
fp.write(msg)
fp.seek(0)
m = rfc822.Message(fp)
i = 0
class MessageTestCase(unittest.TestCase):
def create_message(self, msg):
return rfc822.Message(StringIO(msg))
def test_get(self):
msg = self.create_message(
'To: "last, first" <userid@foo.net>\n\ntest\n')
self.assert_(msg.get("to") == '"last, first" <userid@foo.net>')
self.assert_(msg.get("TO") == '"last, first" <userid@foo.net>')
self.assert_(msg.get("No-Such-Header") == "")
self.assert_(msg.get("No-Such-Header", "No-Such-Value")
== "No-Such-Value")
def test_setdefault(self):
msg = self.create_message(
'To: "last, first" <userid@foo.net>\n\ntest\n')
self.assert_(not msg.has_key("New-Header"))
self.assert_(msg.setdefault("New-Header", "New-Value") == "New-Value")
self.assert_(msg.setdefault("New-Header", "Different-Value")
== "New-Value")
self.assert_(msg["new-header"] == "New-Value")
self.assert_(msg.setdefault("Another-Header") == "")
self.assert_(msg["another-header"] == "")
def check(self, msg, results):
"""Check addresses and the date."""
m = self.create_message(msg)
i = 0
for n, a in m.getaddrlist('to') + m.getaddrlist('cc'): for n, a in m.getaddrlist('to') + m.getaddrlist('cc'):
if verbose:
print 'name:', repr(n), 'addr:', repr(a)
try: try:
mn, ma = results[i][0], results[i][1] mn, ma = results[i][0], results[i][1]
except IndexError: except IndexError:
...@@ -22,69 +46,70 @@ def test(msg, results): ...@@ -22,69 +46,70 @@ def test(msg, results):
continue continue
i = i + 1 i = i + 1
if mn == n and ma == a: if mn == n and ma == a:
if verbose: pass
print ' [matched]'
else: else:
if verbose:
print ' [no match]'
print 'not found:', repr(n), repr(a) print 'not found:', repr(n), repr(a)
out = m.getdate('date') out = m.getdate('date')
if out: if out:
if verbose: self.assertEqual(out,
print 'Date:', m.getheader('date') (1999, 1, 13, 23, 57, 35, 0, 0, 0),
if out == (1999, 1, 13, 23, 57, 35, 0, 0, 0): "date conversion failed")
if verbose:
print ' [matched]'
else: # Note: all test cases must have the same date (in various formats),
if verbose: # or no date!
print ' [no match]'
print 'Date conversion failed:', out def test_basic(self):
self.check(
# Note: all test cases must have the same date (in various formats), 'Date: Wed, 13 Jan 1999 23:57:35 -0500\n'
# or no date! 'From: Guido van Rossum <guido@CNRI.Reston.VA.US>\n'
'To: "Guido van\n'
test('''Date: Wed, 13 Jan 1999 23:57:35 -0500 '\t : Rossum" <guido@python.org>\n'
From: Guido van Rossum <guido@CNRI.Reston.VA.US> 'Subject: test2\n'
To: "Guido van '\n'
\t : Rossum" <guido@python.org> 'test2\n',
Subject: test2 [('Guido van\n\t : Rossum', 'guido@python.org')])
test2 self.check(
''', [('Guido van\n\t : Rossum', 'guido@python.org')]) 'From: Barry <bwarsaw@python.org\n'
'To: guido@python.org (Guido: the Barbarian)\n'
test('''From: Barry <bwarsaw@python.org 'Subject: nonsense\n'
To: guido@python.org (Guido: the Barbarian) 'Date: Wednesday, January 13 1999 23:57:35 -0500\n'
Subject: nonsense '\n'
Date: Wednesday, January 13 1999 23:57:35 -0500 'test',
[('Guido: the Barbarian', 'guido@python.org')])
test''', [('Guido: the Barbarian', 'guido@python.org'),
]) self.check(
'From: Barry <bwarsaw@python.org\n'
test('''From: Barry <bwarsaw@python.org 'To: guido@python.org (Guido: the Barbarian)\n'
To: guido@python.org (Guido: the Barbarian) 'Cc: "Guido: the Madman" <guido@python.org>\n'
Cc: "Guido: the Madman" <guido@python.org> 'Date: 13-Jan-1999 23:57:35 EST\n'
Date: 13-Jan-1999 23:57:35 EST '\n'
'test',
test''', [('Guido: the Barbarian', 'guido@python.org'), [('Guido: the Barbarian', 'guido@python.org'),
('Guido: the Madman', 'guido@python.org') ('Guido: the Madman', 'guido@python.org')
]) ])
test('''To: "The monster with self.check(
the very long name: Guido" <guido@python.org> 'To: "The monster with\n'
Date: Wed, 13 Jan 1999 23:57:35 -0500 ' the very long name: Guido" <guido@python.org>\n'
'Date: Wed, 13 Jan 1999 23:57:35 -0500\n'
test''', [('The monster with\n the very long name: Guido', '\n'
'test',
[('The monster with\n the very long name: Guido',
'guido@python.org')]) 'guido@python.org')])
test('''To: "Amit J. Patel" <amitp@Theory.Stanford.EDU> self.check(
CC: Mike Fletcher <mfletch@vrtelecom.com>, 'To: "Amit J. Patel" <amitp@Theory.Stanford.EDU>\n'
"'string-sig@python.org'" <string-sig@python.org> 'CC: Mike Fletcher <mfletch@vrtelecom.com>,\n'
Cc: fooz@bat.com, bart@toof.com ' "\'string-sig@python.org\'" <string-sig@python.org>\n'
Cc: goit@lip.com 'Cc: fooz@bat.com, bart@toof.com\n'
Date: Wed, 13 Jan 1999 23:57:35 -0500 'Cc: goit@lip.com\n'
'Date: Wed, 13 Jan 1999 23:57:35 -0500\n'
test''', [('Amit J. Patel', 'amitp@Theory.Stanford.EDU'), '\n'
'test',
[('Amit J. Patel', 'amitp@Theory.Stanford.EDU'),
('Mike Fletcher', 'mfletch@vrtelecom.com'), ('Mike Fletcher', 'mfletch@vrtelecom.com'),
("'string-sig@python.org'", 'string-sig@python.org'), ("'string-sig@python.org'", 'string-sig@python.org'),
('', 'fooz@bat.com'), ('', 'fooz@bat.com'),
...@@ -92,53 +117,52 @@ test''', [('Amit J. Patel', 'amitp@Theory.Stanford.EDU'), ...@@ -92,53 +117,52 @@ test''', [('Amit J. Patel', 'amitp@Theory.Stanford.EDU'),
('', 'goit@lip.com'), ('', 'goit@lip.com'),
]) ])
# This one is just twisted. I don't know what the proper result should be, def test_twisted(self):
# but it shouldn't be to infloop, which is what used to happen! # This one is just twisted. I don't know what the proper
test('''To: <[smtp:dd47@mail.xxx.edu]_at_hmhq@hdq-mdm1-imgout.companay.com> # result should be, but it shouldn't be to infloop, which is
Date: Wed, 13 Jan 1999 23:57:35 -0500 # what used to happen!
self.check(
test''', [('', ''), 'To: <[smtp:dd47@mail.xxx.edu]_at_hmhq@hdq-mdm1-imgout.companay.com>\n'
'Date: Wed, 13 Jan 1999 23:57:35 -0500\n'
'\n'
'test',
[('', ''),
('', 'dd47@mail.xxx.edu'), ('', 'dd47@mail.xxx.edu'),
('', '_at_hmhq@hdq-mdm1-imgout.companay.com') ('', '_at_hmhq@hdq-mdm1-imgout.companay.com'),
]) ])
# This exercises the old commas-in-a-full-name bug, which should be doing the def test_commas_in_full_name(self):
# right thing in recent versions of the module. # This exercises the old commas-in-a-full-name bug, which
test('''To: "last, first" <userid@foo.net> # should be doing the right thing in recent versions of the
# module.
test''', [('last, first', 'userid@foo.net'), self.check(
]) 'To: "last, first" <userid@foo.net>\n'
'\n'
test('''To: (Comment stuff) "Quoted name"@somewhere.com 'test',
[('last, first', 'userid@foo.net')])
test''', [('Comment stuff', '"Quoted name"@somewhere.com'),
]) def test_quoted_name(self):
self.check(
test('''To: : 'To: (Comment stuff) "Quoted name"@somewhere.com\n'
Cc: goit@lip.com '\n'
Date: Wed, 13 Jan 1999 23:57:35 -0500 'test',
[('Comment stuff', '"Quoted name"@somewhere.com')])
test''', [('', 'goit@lip.com')])
def test_bogus_to_header(self):
self.check(
test('''To: guido@[132.151.1.21] 'To: :\n'
'Cc: goit@lip.com\n'
foo''', [('', 'guido@[132.151.1.21]')]) 'Date: Wed, 13 Jan 1999 23:57:35 -0500\n'
'\n'
'test',
msg = rfc822.Message(StringIO('''To: "last, first" <userid@foo.net> [('', 'goit@lip.com')])
test def test_addr_ipquad(self):
''')) self.check(
verify(msg.get("to") == '"last, first" <userid@foo.net>') 'To: guido@[132.151.1.21]\n'
verify(msg.get("TO") == '"last, first" <userid@foo.net>') '\n'
verify(msg.get("No-Such-Header") is None) 'foo',
verify(msg.get("No-Such-Header", "No-Such-Value") == "No-Such-Value") [('', 'guido@[132.151.1.21]')])
verify(not msg.has_key("New-Header"))
verify(msg.setdefault("New-Header", "New-Value") == "New-Value") test_support.run_unittest(MessageTestCase)
verify(msg.setdefault("New-Header", "Different-Value") == "New-Value")
verify(msg["new-header"] == "New-Value")
verify(msg.setdefault("Another-Header") == "")
verify(msg["another-header"] == "")
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment