1
# Copyright (c) 2001-2004 Twisted Matrix Laboratories.
2
# See LICENSE for details.
6
Test cases for formmethod module.
9
from twisted.trial import unittest
11
from twisted.python import formmethod
14
class ArgumentTestCase(unittest.TestCase):
16
def argTest(self, argKlass, testPairs, badValues, *args, **kwargs):
17
arg = argKlass("name", *args, **kwargs)
18
for val, result in testPairs:
19
self.assertEquals(arg.coerce(val), result)
21
self.assertRaises(formmethod.InputError, arg.coerce, val)
24
self.argTest(formmethod.String, [("a", "a"), (1, "1"), ("", "")], ())
25
self.argTest(formmethod.String, [("ab", "ab"), ("abc", "abc")], ("2", ""), min=2)
26
self.argTest(formmethod.String, [("ab", "ab"), ("a", "a")], ("223213", "345x"), max=3)
27
self.argTest(formmethod.String, [("ab", "ab"), ("add", "add")], ("223213", "x"), min=2, max=3)
30
self.argTest(formmethod.Integer, [("3", 3), ("-2", -2), ("", None)], ("q", "2.3"))
31
self.argTest(formmethod.Integer, [("3", 3), ("-2", -2)], ("q", "2.3", ""), allowNone=0)
34
self.argTest(formmethod.Float, [("3", 3.0), ("-2.3", -2.3), ("", None)], ("q", "2.3z"))
35
self.argTest(formmethod.Float, [("3", 3.0), ("-2.3", -2.3)], ("q", "2.3z", ""),
39
choices = [("a", "apple", "an apple"),
40
("b", "banana", "ook")]
41
self.argTest(formmethod.Choice, [("a", "apple"), ("b", "banana")],
42
("c", 1), choices=choices)
45
flags = [("a", "apple", "an apple"),
46
("b", "banana", "ook")]
47
self.argTest(formmethod.Flags,
48
[(["a"], ["apple"]), (["b", "a"], ["banana", "apple"])],
49
(["a", "c"], ["fdfs"]),
52
def testBoolean(self):
53
tests = [("yes", 1), ("", 0), ("False", 0), ("no", 0)]
54
self.argTest(formmethod.Boolean, tests, ())
58
("2002", "12", "21"): (2002, 12, 21),
59
("1996", "2", "29"): (1996, 2, 29),
62
badTests = [("2002", "2", "29"), ("xx", "2", "3"),
63
("2002", "13", "1"), ("1999", "12","32"),
64
("2002", "1"), ("2002", "2", "3", "4")]
65
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)
72
def testVerifiedPassword(self):
73
goodTests = {("foo", "foo"): "foo", ("ab", "ab"): "ab"}.items()
74
badTests = [("ab", "a"), ("12345", "12345"), ("", ""), ("a", "a"), ("a",), ("a", "a", "a")]
75
self.argTest(formmethod.VerifiedPassword, goodTests, badTests, min=2, max=4)