test_formmethod.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Test cases for formmethod module.
  5. """
  6. from twisted.trial import unittest
  7. from twisted.python import formmethod
  8. class ArgumentTests(unittest.TestCase):
  9. def argTest(self, argKlass, testPairs, badValues, *args, **kwargs):
  10. arg = argKlass("name", *args, **kwargs)
  11. for val, result in testPairs:
  12. self.assertEqual(arg.coerce(val), result)
  13. for val in badValues:
  14. self.assertRaises(formmethod.InputError, arg.coerce, val)
  15. def test_argument(self):
  16. """
  17. Test that corce correctly raises NotImplementedError.
  18. """
  19. arg = formmethod.Argument("name")
  20. self.assertRaises(NotImplementedError, arg.coerce, "")
  21. def testString(self):
  22. self.argTest(formmethod.String, [("a", "a"), (1, "1"), ("", "")], ())
  23. self.argTest(formmethod.String, [("ab", "ab"), ("abc", "abc")], ("2", ""), min=2)
  24. self.argTest(formmethod.String, [("ab", "ab"), ("a", "a")], ("223213", "345x"), max=3)
  25. self.argTest(formmethod.String, [("ab", "ab"), ("add", "add")], ("223213", "x"), min=2, max=3)
  26. def testInt(self):
  27. self.argTest(formmethod.Integer, [("3", 3), ("-2", -2), ("", None)], ("q", "2.3"))
  28. self.argTest(formmethod.Integer, [("3", 3), ("-2", -2)], ("q", "2.3", ""), allowNone=0)
  29. def testFloat(self):
  30. self.argTest(formmethod.Float, [("3", 3.0), ("-2.3", -2.3), ("", None)], ("q", "2.3z"))
  31. self.argTest(formmethod.Float, [("3", 3.0), ("-2.3", -2.3)], ("q", "2.3z", ""),
  32. allowNone=0)
  33. def testChoice(self):
  34. choices = [("a", "apple", "an apple"),
  35. ("b", "banana", "ook")]
  36. self.argTest(formmethod.Choice, [("a", "apple"), ("b", "banana")],
  37. ("c", 1), choices=choices)
  38. def testFlags(self):
  39. flags = [("a", "apple", "an apple"),
  40. ("b", "banana", "ook")]
  41. self.argTest(formmethod.Flags,
  42. [(["a"], ["apple"]), (["b", "a"], ["banana", "apple"])],
  43. (["a", "c"], ["fdfs"]),
  44. flags=flags)
  45. def testBoolean(self):
  46. tests = [("yes", 1), ("", 0), ("False", 0), ("no", 0)]
  47. self.argTest(formmethod.Boolean, tests, ())
  48. def test_file(self):
  49. """
  50. Test the correctness of the coerce function.
  51. """
  52. arg = formmethod.File("name", allowNone=0)
  53. self.assertEqual(arg.coerce("something"), "something")
  54. self.assertRaises(formmethod.InputError, arg.coerce, None)
  55. arg2 = formmethod.File("name")
  56. self.assertIsNone(arg2.coerce(None))
  57. def testDate(self):
  58. goodTests = {
  59. ("2002", "12", "21"): (2002, 12, 21),
  60. ("1996", "2", "29"): (1996, 2, 29),
  61. ("", "", ""): None,
  62. }.items()
  63. badTests = [("2002", "2", "29"), ("xx", "2", "3"),
  64. ("2002", "13", "1"), ("1999", "12","32"),
  65. ("2002", "1"), ("2002", "2", "3", "4")]
  66. self.argTest(formmethod.Date, goodTests, badTests)
  67. def testRangedInteger(self):
  68. goodTests = {"0": 0, "12": 12, "3": 3}.items()
  69. badTests = ["-1", "x", "13", "-2000", "3.4"]
  70. self.argTest(formmethod.IntegerRange, goodTests, badTests, 0, 12)
  71. def testVerifiedPassword(self):
  72. goodTests = {("foo", "foo"): "foo", ("ab", "ab"): "ab"}.items()
  73. badTests = [("ab", "a"), ("12345", "12345"), ("", ""), ("a", "a"), ("a",), ("a", "a", "a")]
  74. self.argTest(formmethod.VerifiedPassword, goodTests, badTests, min=2, max=4)