Ejemplo n.º 1
0
    def test_only_lower_bound_implies_same_upper_bound(self):
        def func(self_, arg1, arg2, arg3):
            pass

        decorator = arity(3)
        self.assertRaises(UsageError, decorator(func), (self, 1, 2))
        decorator(func)(self, 1, 2, 3)  # Does not raise
        self.assertRaises(UsageError, decorator(func), (self, 1, 2, 3, 4))
Ejemplo n.º 2
0
    def test_different_upper_and_lower_bounds(self):
        def func(self_, arg1, arg2, arg3):
            pass

        decorator = arity(2, 3)
        self.assertRaises(UsageError, decorator(func), (self, 1))
        decorator(func)(self, 1, 2)  # Does not raise
        decorator(func)(self, 1, 2, 3)  # Does not raise
        self.assertRaises(UsageError, decorator(func), (self, 1, 2, 3, 4))
Ejemplo n.º 3
0
    def test_decorator_forwards_arguments(self):
        def func(self_, arg1):
            self.assertEqual(self_, self)
            self.assertEqual(arg1, "foo")
            called[0] = True

        called = [False]
        decorator = arity(1)
        decorator(func)(self, "foo")
        self.assertTrue(called[0])
Ejemplo n.º 4
0
    def test_missing_arguments_are_set_to_None(self):
        def func(self_, arg1, arg2, arg3):
            self.assertEqual(arg1, 1)
            self.assertTrue(arg2 is None)
            self.assertTrue(arg3 is None)
            called[0] = True

        called = [False]
        decorator = arity(1, 3)
        decorator(func)(self, 1)
        self.assertTrue(called[0])
Ejemplo n.º 5
0
    def test_usage_error_messages(self):
        def func(self_):
            pass

        with self.assertRaisesRegexp(UsageError,
                                     "subcommand expects 0 arguments"):
            arity(0)(func)(self, 1)

        with self.assertRaisesRegexp(UsageError,
                                     "subcommand expects 1 argument"):
            arity(1)(func)(self, 1, 2)

        with self.assertRaisesRegexp(UsageError,
                                     "subcommand expects 2 arguments"):
            arity(2)(func)(self, 1, 2, 3)

        with self.assertRaisesRegexp(
                UsageError, "subcommand expects between 1 and 2 arguments"):
            arity(1, 2)(func)(self, 1, 2, 3)