Example #1
0
    def test_merge(self):
        metadata1 = FunctionMetadata("main", "Desc of main.\n",
                                     "Returns 0 on success", {
                                         "argc": ("argc desc", None),
                                         "argv": ("argv desc", None)
                                     }, {"opt":
                                         ("opt desc", int, 88)}, {"self", "A"})

        metadata2 = FunctionMetadata(
            "process", "Desc of process.\n", "Returns the processed stuff.", {
                "argc": ("argc desc from process", int),
                "to_process": ("to_process desc", int)
            }, {"opt2": ("opt2 desc", str, "hello")}, {"self", "B"})

        metadata3 = FunctionMetadata("nodesc", "", "", {}, {})

        merged_metadata = FunctionMetadata.merge(metadata1, metadata2,
                                                 metadata3)

        self.assertEqual(merged_metadata.name,
                         "<Merged signature of 'main', 'process', 'nodesc'>")
        self.assertEqual(merged_metadata.desc, "Desc of process.\n")
        self.assertEqual(merged_metadata.retval_desc,
                         "Returns the processed stuff.")
        self.assertEqual(
            merged_metadata.non_optional_params, {
                "argc": ("argc desc from process", int),
                "argv": ("argv desc", None),
                "to_process": ("to_process desc", int)
            })
        self.assertEqual(merged_metadata.optional_params, {
            "opt": ("opt desc", int, 88),
            "opt2": ("opt2 desc", str, "hello")
        })
        self.assertEqual(merged_metadata.omit, frozenset({"self", "A", "B"}))
Example #2
0
 def get_metadata(cls):
     metadata = FunctionMetadata(
         'run',
         optional_params=cls.get_optional_params(),
         non_optional_params=cls.get_non_optional_params())
     metadata.desc = inspect.getdoc(cls)
     return metadata
Example #3
0
 def test_construction(self):
     self.assertRaises(TypeError, FunctionMetadata, 5)
     self.assertRaises(TypeError, FunctionMetadata, "name", desc=5)
     self.assertRaises(TypeError, FunctionMetadata, "name", retval_desc=5)
     self.assertRaises(TypeError, FunctionMetadata, "name", non_optional_params=5)
     self.assertRaises(TypeError, FunctionMetadata, "name", optional_params=5)
     self.assertRaises(TypeError, FunctionMetadata.from_function, 5)
     self.assertRaises(TypeError, FunctionMetadata.from_function, self.test_construction, 5)
     self.check_function_metadata_data_set(FunctionMetadata("name"), "name")
    def test_merge(self):
        metadata1 = FunctionMetadata('main', 'Desc of main.\n',
                                     'Returns 0 on success', {
                                         'argc': ('argc desc', None),
                                         'argv': ('argv desc', None)
                                     }, {'opt': ('opt desc', int, 88)},
                                     {'self', 'A'}, {'test1'})

        metadata2 = FunctionMetadata(
            'process', 'Desc of process.\n', 'Returns the processed stuff.', {
                'argc': ('argc desc from process', int),
                'to_process': ('to_process desc', int)
            }, {'opt2': ('opt2 desc', str, 'hello')}, {'self', 'B'}, {'test2'})

        metadata3 = FunctionMetadata('nodesc', '', '', {}, {})

        merged_metadata = FunctionMetadata.merge(metadata1, metadata2,
                                                 metadata3)

        self.assertEqual(merged_metadata.name,
                         "<Merged signature of 'main', 'process', 'nodesc'>")
        self.assertEqual(merged_metadata.desc, 'Desc of process.\n')
        self.assertEqual(merged_metadata.retval_desc,
                         'Returns the processed stuff.')
        self.assertEqual(
            merged_metadata.non_optional_params, {
                'argc': ('argc desc from process', int),
                'argv': ('argv desc', None),
                'to_process': ('to_process desc', int)
            })
        self.assertEqual(merged_metadata.optional_params, {
            'opt': ('opt desc', int, 88),
            'opt2': ('opt2 desc', str, 'hello')
        })
        self.assertEqual(merged_metadata.omit, frozenset({'self', 'A', 'B'}))
        self.assertEqual(merged_metadata.deprecated_params,
                         frozenset({'test1', 'test2'}))
    def test_add_deprecated_param(self):
        uut = FunctionMetadata(
            "test",
            non_optional_params={'not_optional': ('desc', str)},
            optional_params={'optional': ('desc2', str, 'default')})

        uut.add_deprecated_param('optional', 'old_optional')
        uut.add_deprecated_param('not_optional', 'old_not_optional')

        self.assertEqual(uut.non_optional_params,
                         {'not_optional': ('desc', str)})
        self.assertEqual(uut.optional_params,
                         {'optional': ('desc2', str, 'default'),
                          'old_optional': ('desc2', str, 'default'),
                          'old_not_optional': ('desc', str, None)})
    def test_from_function(self):
        uut = FunctionMetadata.from_function(self.test_from_function)
        self.check_function_metadata_data_set(uut, "test_from_function")
        # setattr on bound methods will fail, vars() will use the dict from
        # the unbound method which is ok.
        vars(self.test_from_function)["__metadata__"] = (
            FunctionMetadata("t"))
        uut = FunctionMetadata.from_function(self.test_from_function)
        self.check_function_metadata_data_set(uut, "t")

        uut = FunctionMetadata.from_function(TestClass(5, 5).__init__)
        self.check_function_metadata_data_set(
            uut,
            "__init__",
            desc="Description",
            retval_desc="ret",
            non_optional_params={
                "param1": (uut.str_nodesc, None),
                "param2": ("d", None)
            },
            optional_params={
                "param3": (uut.str_nodesc + " ("
                           + uut.str_optional.format("5") + ")",
                           None, 5),
                "param4": ("p4 desc ("
                           + uut.str_optional.format("6") + ")", int, 6)})

        uut = FunctionMetadata.from_function(TestClass(5, 5).__init__,
                                             omit={"param3", "param2"})
        self.check_function_metadata_data_set(
            uut,
            "__init__",
            desc="Description",
            retval_desc="ret",
            non_optional_params={
                "param1": (uut.str_nodesc,
                           None)
            },
            optional_params={
                "param4": ("p4 desc (" + uut.str_optional.format("6") + ")",
                           int,
                           6)})
    def test_from_function(self):
        uut = FunctionMetadata.from_function(self.test_from_function)
        self.check_function_metadata_data_set(uut, 'test_from_function')
        # setattr on bound methods will fail, vars() will use the dict from
        # the unbound method which is ok.
        vars(self.test_from_function)['__metadata__'] = (FunctionMetadata('t'))
        uut = FunctionMetadata.from_function(self.test_from_function)
        self.check_function_metadata_data_set(uut, 't')

        uut = FunctionMetadata.from_function(TestClass(5, 5).__init__)
        self.check_function_metadata_data_set(
            uut,
            '__init__',
            desc='Description',
            retval_desc='ret',
            non_optional_params={
                'param1': (uut.str_nodesc, None),
                'param2': ('d', None)
            },
            optional_params={
                'param3':
                (uut.str_nodesc + ' (' + uut.str_optional.format('5') + ')',
                 None, 5),
                'param4':
                ('p4 desc (' + uut.str_optional.format('6') + ')', int, 6)
            })

        uut = FunctionMetadata.from_function(TestClass(5, 5).__init__,
                                             omit={'param3', 'param2'})
        self.check_function_metadata_data_set(
            uut,
            '__init__',
            desc='Description',
            retval_desc='ret',
            non_optional_params={'param1': (uut.str_nodesc, None)},
            optional_params={
                'param4':
                ('p4 desc (' + uut.str_optional.format('6') + ')', int, 6)
            })
 def test_construction(self):
     self.check_function_metadata_data_set(FunctionMetadata('name'), 'name')