Ejemplo n.º 1
0
    def testNone(self):
        def func_none(date=None):
            return date

        func_none = argcheck(datetime.datetime)(func_none)
        func_none()
        func_none(None)
        self.assertRaises(TypeError, func_none, True)
        self.assertRaises(TypeError, func_none, date=True)

        def func_none2(s, date=None, date2=None):
            return date

        func_none2 = argcheck(str, datetime.datetime,
                              datetime.datetime)(func_none2)
        func_none2('foo')
        func_none2('bar', None)
        func_none2('baz', None, None)
        func_none2(s='foo')
        func_none2(s='bar', date=None)
        func_none2(s='baz', date=None, date2=None)
        self.assertRaises(TypeError, func_none2, 'noggie', True)
        self.assertRaises(TypeError, func_none2, 'boogie', None, True)
        self.assertRaises(TypeError, func_none2, s='noggie', date2=True)
        self.assertRaises(TypeError,
                          func_none2,
                          s='boogie',
                          date=None,
                          date2=True)
Ejemplo n.º 2
0
    def testDefault(self):
        f1 = lambda a, b=1: None
        d1 = argcheck(int, int)(f1)
        self.assertRaises(TypeError, d1, 'f')

        f2 = lambda a, b='str': None
        self.assertRaises(TypeError, argcheck, f2)
Ejemplo n.º 3
0
    def testDefault(self):
        f1 = lambda a, b=1: None
        d1 = argcheck(int, int)(f1)
        self.assertRaises(TypeError, d1, 'f')

        f2 = lambda a, b='str': None
        self.assertRaises(TypeError, argcheck, f2)
Ejemplo n.º 4
0
 def testDisable(self):
     argcheck.disable()
     def func(s):
         pass
     func = argcheck(str)(func)
     func(10)
     argcheck.enable()
Ejemplo n.º 5
0
 def testTwoArgs(self):
     f = argcheck(str, int)(lambda s, i: None)
     f('str', 1)
     self.assertRaises(TypeError, f, 1, 1)         # first incorret
     self.assertRaises(TypeError, f, 'str', 'str') # second incorrect
     self.assertRaises(TypeError, f, 1, 'str')     # swapped
     self.assertRaises(TypeError, f, 1)            # too few
     self.assertRaises(TypeError, f, 'str', 1, 1)  # too many
Ejemplo n.º 6
0
 def testTwoArgs(self):
     f = argcheck(str, int)(lambda s, i: None)
     f('str', 1)
     self.assertRaises(TypeError, f, 1, 1)  # first incorret
     self.assertRaises(TypeError, f, 'str', 'str')  # second incorrect
     self.assertRaises(TypeError, f, 1, 'str')  # swapped
     self.assertRaises(TypeError, f, 1)  # too few
     self.assertRaises(TypeError, f, 'str', 1, 1)  # too many
Ejemplo n.º 7
0
 def testNumber(self):
     def func(n):
         return n
     func = argcheck(number)(func)
     self.assertEqual(func(0), 0)
     self.assertEqual(func(0L), 0L)
     self.assertEqual(func(0.0), 0.0)
     self.assertEqual(func(Decimal(0)), Decimal(0))
Ejemplo n.º 8
0
    def testDisable(self):
        argcheck.disable()

        def func(s):
            pass

        func = argcheck(str)(func)
        func(10)
        argcheck.enable()
Ejemplo n.º 9
0
    def testNumber(self):
        def func(n):
            return n

        func = argcheck(number)(func)
        self.assertEqual(func(0), 0)
        self.assertEqual(func(0L), 0L)
        self.assertEqual(func(0.0), 0.0)
        self.assertEqual(func(decimal.Decimal(0)), decimal.Decimal(0))
Ejemplo n.º 10
0
    def testUserDefined(self):
        class Payment(object):
            pass

        def pay(payment, description):
            pass
        pay = argcheck(Payment, str)(pay)
        pay(Payment(), 'foo')
        self.assertRaises(TypeError, 'bar', 'bar')
        self.assertRaises(TypeError, Payment(), Payment())
Ejemplo n.º 11
0
    def testUserDefined(self):
        class Payment(object):
            pass

        def pay(payment, description):
            pass

        pay = argcheck(Payment, str)(pay)
        pay(Payment(), 'foo')
        self.assertRaises(TypeError, 'bar', 'bar')
        self.assertRaises(TypeError, Payment(), Payment())
Ejemplo n.º 12
0
        class Test:
            def method1(self, foo, bar):
                return foo + bar

            method1 = argcheck(int, int)(method1)

            def method2(self, a, b, c, d, e, f, g=0.0):
                return g

            method2 = argcheck(Custom, int, datetime.datetime, int, int, float,
                               float)(method2)

            def method3(self, s, date=None, date2=None):
                return

            method3 = argcheck(str, datetime.datetime,
                               datetime.datetime)(method3)

            def method4(self, n):
                return n

            method4 = argcheck(percent)(method4)
Ejemplo n.º 13
0
    def testNone(self):
        def func_none(date=None):
            return date
        func_none = argcheck(datetime.datetime)(func_none)
        func_none()
        func_none(None)
        self.assertRaises(TypeError, func_none, True)
        self.assertRaises(TypeError, func_none, date=True)

        def func_none2(s, date=None, date2=None):
            return date
        func_none2 = argcheck(str, datetime.datetime,
                              datetime.datetime)(func_none2)
        func_none2('foo')
        func_none2('bar', None)
        func_none2('baz', None, None)
        func_none2(s='foo')
        func_none2(s='bar', date=None)
        func_none2(s='baz', date=None, date2=None)
        self.assertRaises(TypeError, func_none2, 'noggie', True)
        self.assertRaises(TypeError, func_none2, 'boogie', None, True)
        self.assertRaises(TypeError, func_none2, s='noggie', date2=True)
        self.assertRaises(TypeError, func_none2, s='boogie',
                          date=None, date2=True)
Ejemplo n.º 14
0
    def testNone(self):
        def func_none(date=None):
            return date

        func_none = argcheck(datetime.datetime)(func_none)
        func_none()
        func_none(None)
        self.assertRaises(TypeError, func_none, True)
        self.assertRaises(TypeError, func_none, date=True)

        def func_none2(s, date=None, date2=None):
            return date

        func_none2 = argcheck(str, datetime.datetime, datetime.datetime)(func_none2)
        func_none2("foo")
        func_none2("bar", None)
        func_none2("baz", None, None)
        func_none2(s="foo")
        func_none2(s="bar", date=None)
        func_none2(s="baz", date=None, date2=None)
        self.assertRaises(TypeError, func_none2, "noggie", True)
        self.assertRaises(TypeError, func_none2, "boogie", None, True)
        self.assertRaises(TypeError, func_none2, s="noggie", date2=True)
        self.assertRaises(TypeError, func_none2, s="boogie", date=None, date2=True)
Ejemplo n.º 15
0
 def testPercent(self):
     def func(n):
         return n
     func = argcheck(percent)(func)
     self.assertEqual(func(50), 50)
     self.assertEqual(func(50L), 50L)
     self.assertEqual(func(50.0), 50.0)
     self.assertEqual(func(Decimal(50)), Decimal(50))
     self.assertRaises(ValueError, func, -1)
     self.assertRaises(ValueError, func, -1L)
     self.assertRaises(ValueError, func, -1.0)
     self.assertRaises(ValueError, func, Decimal(-1))
     self.assertRaises(ValueError, func, 101)
     self.assertRaises(ValueError, func, 101L)
     self.assertRaises(ValueError, func, 101.0)
     self.assertRaises(ValueError, func, Decimal(101))
Ejemplo n.º 16
0
    def testPercent(self):
        def func(n):
            return n

        func = argcheck(percent)(func)
        self.assertEqual(func(50), 50)
        self.assertEqual(func(50L), 50L)
        self.assertEqual(func(50.0), 50.0)
        self.assertEqual(func(decimal.Decimal(50)), decimal.Decimal(50))
        self.assertRaises(ValueError, func, -1)
        self.assertRaises(ValueError, func, -1L)
        self.assertRaises(ValueError, func, -1.0)
        self.assertRaises(ValueError, func, decimal.Decimal(-1))
        self.assertRaises(ValueError, func, 101)
        self.assertRaises(ValueError, func, 101L)
        self.assertRaises(ValueError, func, 101.0)
        self.assertRaises(ValueError, func, decimal.Decimal(101))
Ejemplo n.º 17
0
 def testOneArg(self):
     f = argcheck(str)(lambda s: None)
     f('str')
     self.assertRaises(TypeError, f, None)
     self.assertRaises(TypeError, f, 1)
Ejemplo n.º 18
0
 def testErrorHandling(self):
     self.assertRaises(TypeError, argcheck(str), True)
     self.assertRaises(TypeError, argcheck(int), lambda **x: None)
     self.assertRaises(TypeError, argcheck(int), lambda: None)
     self.assertRaises(TypeError, argcheck(int), lambda x='str': None)
Ejemplo n.º 19
0
 def testVarArgs(self):
     f = argcheck(int)(lambda *v: None)
     f(1)
     f(1, 'str')
     f(1, 2, 3)
Ejemplo n.º 20
0
 def testErrorHandling(self):
     self.assertRaises(TypeError, argcheck(str), True)
     self.assertRaises(TypeError, argcheck(int), lambda **x: None)
     self.assertRaises(TypeError, argcheck(int), lambda: None)
     self.assertRaises(TypeError, argcheck(int), lambda x='str': None)
Ejemplo n.º 21
0
 def testVarArgs(self):
     f = argcheck(int)(lambda *v: None)
     f(1)
     f(1, 'str')
     f(1, 2, 3)
Ejemplo n.º 22
0
 def testOneArg(self):
     f = argcheck(str)(lambda s: None)
     f('str')
     self.assertRaises(TypeError, f, None)
     self.assertRaises(TypeError, f, 1)