Exemple #1
0
    def test_base(self):
        template = m.TemplateBase("BaseTpl")
        self.assertEqual(str(template),
                         "<TemplateBase {}.BaseTpl>".format(_TEST_MODULE))

        # Single arguments.
        template.add_instantiation(int, 1)
        self.assertEqual(template[int], 1)
        self.assertEqual(template.get_instantiation(int), (1, (int, )))
        self.assertEqual(template.get_param_set(1), {(int, )})
        self.assertTrue(template.is_instantiation(1))
        self.assertFalse(template.is_instantiation(10))
        # Duplicate parameters.
        self.assertRaises(RuntimeError,
                          lambda: template.add_instantiation(int, 4))

        # Invalid parameters.
        self.assertRaises(RuntimeError, lambda: template[float])
        # New instantiation.
        template.add_instantiation(float, 2)
        self.assertEqual(template[float], 2)

        # Default instantiation.
        self.assertEqual(template[None], 1)
        self.assertEqual(template.get_instantiation(), (1, (int, )))

        # Multiple arguments.
        template.add_instantiation((int, int), 3)
        self.assertEqual(template[int, int], 3)
        # Duplicate instantiation.
        template.add_instantiation((float, float), 1)
        self.assertEqual(template.get_param_set(1), {(int, ), (float, float)})
        # Nested getitem indices.
        self.assertEqual(template[(int, int)], 3)
        self.assertEqual(template[[int, int]], 3)

        # List instantiation.
        def instantiation_func(param):
            return 100 + len(param)

        dummy_a = (str, ) * 5
        dummy_b = (str, ) * 10
        template.add_instantiations(instantiation_func, [dummy_a, dummy_b])
        self.assertEqual(template[dummy_a], 105)
        self.assertEqual(template[dummy_b], 110)

        # Ensure that we can only call this once.
        dummy_c = (str, ) * 7
        with self.assertRaises(RuntimeError):
            template.add_instantiations(instantiation_func, [dummy_c])
Exemple #2
0
    def test_base(self):
        template = m.TemplateBase("BaseTpl")
        self.assertEquals(str(template), "<TemplateBase __main__.BaseTpl>")

        # Single arguments.
        template.add_instantiation(int, 1)
        self.assertEquals(template[int], 1)
        self.assertEquals(template.get_instantiation(int), (1, (int, )))
        self.assertEquals(template.get_param_set(1), {(int, )})
        self.assertTrue(m.is_instantiation_of(1, template))
        self.assertFalse(m.is_instantiation_of(10, template))
        # Duplicate parameters.
        self.assertRaises(RuntimeError,
                          lambda: template.add_instantiation(int, 4))

        # Invalid parameters.
        self.assertRaises(RuntimeError, lambda: template[float])
        # New instantiation.
        template.add_instantiation(float, 2)
        self.assertEquals(template[float], 2)

        # Default instantiation.
        self.assertEquals(template[None], 1)
        self.assertEquals(template.get_instantiation(), (1, (int, )))

        # Multiple arguments.
        template.add_instantiation((int, int), 3)
        self.assertEquals(template[int, int], 3)
        # Duplicate instantiation.
        template.add_instantiation((float, float), 1)
        self.assertEquals(template.get_param_set(1), {(int, ), (float, float)})
        # Nested getitem indices.
        self.assertEquals(template[(int, int)], 3)
        self.assertEquals(template[[int, int]], 3)

        # List instantiation.
        def instantiation_func(param):
            return 100 + len(param)

        dummy_a = (str, ) * 5
        dummy_b = (str, ) * 10
        template.add_instantiations(instantiation_func, [dummy_a, dummy_b])
        self.assertEquals(template[dummy_a], 105)
        self.assertEquals(template[dummy_b], 110)