From: Fred L. D. <fd...@us...> - 2003-07-11 05:11:03
|
Update of /cvsroot/cvs-syncmail/syncmail/tests In directory sc8-pr-cvs1:/tmp/cvs-serv17549 Modified Files: Tag: new-config-branch test_options.py Log Message: Rework the tests to use the unit testing framework. Index: test_options.py =================================================================== RCS file: /cvsroot/cvs-syncmail/syncmail/tests/Attic/test_options.py,v retrieving revision 1.1.2.1 retrieving revision 1.1.2.2 diff -u -d -r1.1.2.1 -r1.1.2.2 --- test_options.py 11 Jul 2003 04:26:36 -0000 1.1.2.1 +++ test_options.py 11 Jul 2003 05:11:00 -0000 1.1.2.2 @@ -4,92 +4,110 @@ import os import sys +import unittest from cStringIO import StringIO import syncmail -def eq(a, b, msg=None): - if msg is None: - msg = "%s != %s" % (`a`, `b`) - if not a == b: - raise AssertionError(msg) +class SimpleOptionTests(unittest.TestCase): -def raises(exctype, f, *args, **kw): - try: - apply(f, args, kw) - except exctype: - pass - else: - raise AssertionError("expected exception") + def test_replacer(self): + VARS = {"FOO": "<whack!>"} -VARS = {"FOO": "<whack!>"} + # "variable" replacement + replace = syncmail.Replacer(VARS) -# "variable" replacement -replace = syncmail.Replacer(VARS) + self.assertEqual(replace("abc$FOO-def${SPLAT}"), "abc<whack!>-def") + self.assertEqual(replace("$FOO"), "<whack!>") + self.assertEqual(replace("$ FOO"), "$ FOO") + self.assertEqual(replace("${FOO}"), "<whack!>") + self.assertEqual(replace(" ${FOO} "), " <whack!> ") -eq(replace("abc$FOO-def${SPLAT}"), "abc<whack!>-def") -eq(replace("$FOO"), "<whack!>") -eq(replace("$ FOO"), "$ FOO") -eq(replace("${FOO}"), "<whack!>") -eq(replace(" ${FOO} "), " <whack!> ") + def test_OptionLookup_cascade(self): + dicts = [ + {'common': '1', 'first': 'one'}, + {'common': '2', 'second': 'two'}, + {'common': '3', 'third': 'three'}, + {'common': '4', 'fourth': 'four'}, + ] + options = syncmail.OptionLookup(dicts) + self.assertEqual(options.get('common'), '1') + self.assertEqual(options.get('first'), 'one') + self.assertEqual(options.get('second'), 'two') + self.assertEqual(options.get('third'), 'three') + self.assertEqual(options.get('fourth'), 'four') + self.assertEqual(options.get('missing'), None) + self.assertEqual(options.get('common'), '1') + self.assertEqual(options.get('third'), 'three') -# load_cmdline() -eq(syncmail.load_cmdline([]), ({}, [])) + def test_OptionLookup_replacements(self): + dicts = [ + {"foo": "bar", + "branch": "$BRANCH", + "hostname": "$HOSTNAME"}, + ] + options = syncmail.OptionLookup(dicts, "my-branch") + self.assertEqual(options.get("branch"), "my-branch") + host = os.environ.get("HOSTNAME", syncmail.getfqdn()) + self.assertEqual(options.get("hostname"), host) -sys.stderr = StringIO() -sys.stdout = sys.stderr -try: - raises(SystemExit, - syncmail.load_cmdline, ['-qq']) - raises(SystemExit, - syncmail.load_cmdline, ['--config=filename', '--no-config']) - raises(SystemExit, - syncmail.load_cmdline, ['--no-config', '--no-config']) -finally: - sys.stderr = sys.__stderr__ - sys.stdout = sys.__stdout__ + def test_load_cmdline_empty(self): + self.assertEqual(syncmail.load_cmdline([]), ({}, [])) -# load_configuration() -config = syncmail.load_configuration([]) -eq(config.contextlines, 2) -eq(config.verbose, 1) -eq(config.smtp_server, syncmail.MAILHOST) -eq(config.smtp_port, syncmail.MAILPORT) + def test_load_configuration_empty(self): + config = syncmail.load_configuration([]) + self.assertEqual(config.contextlines, 2) + self.assertEqual(config.verbose, 1) + self.assertEqual(config.smtp_server, syncmail.MAILHOST) + self.assertEqual(config.smtp_port, syncmail.MAILPORT) -config = syncmail.load_configuration(['-q', '--mailhost=smtp.example.com']) -eq(config.verbose, 0) -eq(config.smtp_server, "smtp.example.com") -eq(config.smtp_port, syncmail.MAILPORT) + def test_load_configuration_simple(self): + config = syncmail.load_configuration( + ['-q', '--mailhost=smtp.example.com']) + self.assertEqual(config.verbose, 0) + self.assertEqual(config.smtp_server, "smtp.example.com") + self.assertEqual(config.smtp_port, syncmail.MAILPORT) -config = syncmail.load_configuration(['--mailhost=smtp.example.com:8025']) -eq(config.smtp_server, "smtp.example.com") -eq(config.smtp_port, 8025) + def test_load_configuration_smtp_address(self): + config = syncmail.load_configuration( + ['--mailhost=smtp.example.com:8025']) + self.assertEqual(config.smtp_server, "smtp.example.com") + self.assertEqual(config.smtp_port, 8025) -config = syncmail.load_configuration(['--mailhost=:8025']) -eq(config.smtp_server, syncmail.MAILHOST) -eq(config.smtp_port, 8025) + config = syncmail.load_configuration(['--mailhost=:8025']) + self.assertEqual(config.smtp_server, syncmail.MAILHOST) + self.assertEqual(config.smtp_port, 8025) -# OptionLookup -dicts = [ - {'common': '1', 'first': 'one'}, - {'common': '2', 'second': 'two'}, - {'common': '3', 'third': 'three'}, - {'common': '4', 'fourth': 'four'}, - ] -options = syncmail.OptionLookup(dicts) -eq(options.get('common'), '1') -eq(options.get('first'), 'one') -eq(options.get('second'), 'two') -eq(options.get('third'), 'three') -eq(options.get('fourth'), 'four') -eq(options.get('missing'), None) -eq(options.get('common'), '1') -eq(options.get('third'), 'three') -options = syncmail.OptionLookup([{"foo": "bar", - "branch": "$BRANCH", - "hostname": "$HOSTNAME"}], "my-branch") -eq(options.get("branch"), "my-branch") -eq(options.get("hostname"), os.environ.get("HOSTNAME", syncmail.getfqdn())) +class UsageErrorTests(unittest.TestCase): + + def setUp(self): + sys.stderr = StringIO() + sys.stdout = sys.stderr + + def tearDown(self): + sys.stderr = sys.__stderr__ + sys.stdout = sys.__stdout__ + + def test_simple_option_twice(self): + self.assertRaises(SystemExit, syncmail.load_cmdline, + ['-qq']) + + def test_competing_options(self): + self.assertRaises(SystemExit, syncmail.load_cmdline, + ['--config=filename', '--no-config']) + + def test_double_negation(self): + self.assertRaises(SystemExit, syncmail.load_cmdline, + ['--no-config', '--no-config']) + + +def test_suite(): + suite = unittest.makeSuite(SimpleOptionTests) + suite.addTest(unittest.makeSuite(UsageErrorTests)) + return suite + +if __name__ == "__main__": + unittest.main(defaultTest="test_suite") |