Пример #1
0
    def test_call(self):
        template = m.TemplateFunction("func")

        def check_int(a):
            if not isinstance(a, int):
                # Mock pybind11 errors.
                raise TypeError("incompatible function arguments: int")
            return "int"

        def check_float(a):
            if not isinstance(a, float):
                # Mock pybind11 errors.
                raise TypeError("incompatible function arguments: float")
            return "float"

        template.add_instantiation(int, check_int)
        template.add_instantiation(float, check_float)

        with self.assertRaises(TypeError) as cm:
            template()
        self.assertIn("without argument", str(cm.exception))

        self.assertEqual(template(0), "int")
        self.assertEqual(template(0.), "float")
        with self.assertRaises(TypeError) as cm:
            template("hello")
        self.assertIn("incompatible function arguments for template",
                      str(cm.exception))
Пример #2
0
    def test_function(self):
        template = m.TemplateFunction("func")

        template.add_instantiation(int, dummy_a)
        template.add_instantiation(float, dummy_b)

        self.assertEqual(template[int](), 1)
        self.assertIn("<function func[int] ", str(template[int]))
        self.assertEqual(template[float](), 2)
        self.assertEqual(str(template), "<TemplateFunction {}.func>".format(
            _TEST_MODULE))