Initial import to Tizen
[profile/ivi/python-twisted.git] / twisted / test / test_formmethod.py
1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
3
4
5 """
6 Test cases for formmethod module.
7 """
8
9 from twisted.trial import unittest
10
11 from twisted.python import formmethod
12
13
14 class ArgumentTestCase(unittest.TestCase):
15
16     def argTest(self, argKlass, testPairs, badValues, *args, **kwargs):
17         arg = argKlass("name", *args, **kwargs)
18         for val, result in testPairs:
19             self.assertEqual(arg.coerce(val), result)
20         for val in badValues:
21             self.assertRaises(formmethod.InputError, arg.coerce, val)
22     
23     def testString(self):
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)
28     
29     def testInt(self):
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)
32
33     def testFloat(self):
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", ""),
36                      allowNone=0)
37
38     def testChoice(self):
39         choices = [("a", "apple", "an apple"),
40                    ("b", "banana", "ook")]
41         self.argTest(formmethod.Choice, [("a", "apple"), ("b", "banana")],
42                      ("c", 1), choices=choices)
43
44     def testFlags(self):
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"]),
50                      flags=flags)
51
52     def testBoolean(self):
53         tests =  [("yes", 1), ("", 0), ("False", 0), ("no", 0)]
54         self.argTest(formmethod.Boolean, tests, ())
55
56     def testDate(self):
57         goodTests = { 
58             ("2002", "12", "21"): (2002, 12, 21),
59             ("1996", "2", "29"): (1996, 2, 29),
60             ("", "", ""): None,
61             }.items()
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)
66
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
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)
76
77