Re-write the rfc822 tests to use PyUnit.

Update to reflect using "" as the default value for the second parameter
to the get() method.
This commit is contained in:
Fred Drake 2001-05-22 19:38:31 +00:00
parent 233226e977
commit 876dc70b2e
1 changed files with 152 additions and 128 deletions

View File

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