Пример #1
0
def test_print_and_get_capabilities_by_ext(root_parser: RootParser):
    """ Tests that the declared capabilities by extension are correct """

    c = root_parser.get_capabilities_by_ext(strict_type_matching=False)
    print('\n' + str(len(c)) + ' Root parser capabilities by ext:')
    assert len(c) == 13

    cdict = to_str_coll(c)

    # dump(cdict, 'reference_capabilities_by_ext.json')
    assert cdict == load('reference_capabilities_by_ext.json')

    root_parser.print_capabilities_by_ext(strict_type_matching=False)
class AllTests(TestCase):
    def setUp(self):
        """
        Creates the root parser to be used in most tests
        :return:
        """
        self.root_parser = RootParser()

    def test_a_root_parser_capabilities(self):
        """
        Tests that we can print the capabilities of the root parser: registered parsers and converters, supported
        extensions and types, etc.
        :return:
        """
        p = self.root_parser.get_all_parsers(strict_type_matching=False)
        print('\n' + str(len(p)) + ' Root parser parsers:')
        pprint(p)

        print('Testing option hints for parsing chain')
        print(p[0].options_hints())

        c = self.root_parser.get_all_conversion_chains()
        print('\n' + str(len(c[0]) + len(c[2])) + ' Root parser converters:')
        pprint(c)
        e = self.root_parser.get_all_supported_exts()
        print('\n' + str(len(e)) + ' Root parser supported extensions:')
        pprint(e)
        t = self.root_parser.get_all_supported_types_pretty_str()
        print('\n' + str(len(t)) + ' Root parser supported types:')
        pprint(t)
        print('\nRoot parser parsers by extensions:')
        self.root_parser.print_capabilities_by_ext(strict_type_matching=False)
        print('\nRoot parser parsers by types:')
        self.root_parser.print_capabilities_by_type(strict_type_matching=False)
        return

    def test_b_root_parser_any(self):
        """
        Tests that we can ask the rootparser for its capabilities to parse a given type
        :return:
        """
        # print
        self.root_parser.print_capabilities_for_type(typ=Any)

        # details
        res = self.root_parser.find_all_matching_parsers(
            strict=False, desired_type=AnyObject, required_ext='.cfg')
        match_generic, match_approx, match_exact = res[0]
        self.assertEquals(len(match_generic), 0)
        self.assertEquals(len(match_approx), 0)

    def test_objects_support(self):
        """
        Tests all the supported ways to parse a simple object
        :return:
        """

        # Then define the simple class representing your test case
        class ExecOpTest(object):
            def __init__(self, x: float, y: float, op: str,
                         expected_result: float):
                self.x = x
                self.y = y
                self.op = op
                self.expected_result = expected_result

            def __str__(self):
                return self.__repr__()

            def __repr__(self):
                return str(self.x) + ' ' + self.op + ' ' + str(
                    self.y) + ' =? ' + str(self.expected_result)

        # create the parser and parse a single file
        e = parse_item(fix_path('./test_data/objects/test_diff_1'), ExecOpTest)
        pprint(e)

        # parse all of them
        e = parse_collection(fix_path('./test_data/objects'), ExecOpTest)
        pprint(e)

    def test_collections(self):
        """
        Tests all the supported ways to parse collections
        :return:
        """
        l = parse_item(
            fix_path('./test_data/collections'),
            Tuple[Dict[str, int], List[int], Set[int], Tuple[str, int, str]])
        print(l)