# -*- Mode: Python -*- # GObject-Introspection - a framework for introspecting GObject libraries # Copyright (C) 2012 Dieter Verfaillie # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA # 02110-1301, USA. # ''' test_parser.py Tests ensuring the "parse tree" built by annotationparser.py continues to function correctly. ''' import difflib import os import xml.etree.ElementTree as etree import unittest from giscanner.annotationparser import AnnotationParser from giscanner.ast import Namespace from giscanner.message import MessageLogger def parsed2tree(docblock): parsed = '' if docblock is not None: parsed += '\n' parsed += ' \n' # An identifier name is always required, but we can't trust our # own parser to ensure this when testing so fall back to an empty # string when no name has been parsed... parsed += ' %s\n' % (docblock.name or '', ) if docblock.options.values: parsed += ' \n' for key, value in docblock.options.values: parsed += ' \n' parsed += ' %s\n' % (key, ) if value is not None: options = value.all() parsed += ' \n' for option in options: parsed += ' \n' parsed += ' \n' parsed += ' \n' parsed += ' \n' parsed += ' \n' if docblock.params: parsed += ' \n' for param_name in docblock.params: param = docblock.params.get(param_name) parsed += ' \n' parsed += ' %s\n' % (param_name, ) if param.options.values: parsed += ' \n' for key, value in param.options.values: parsed += ' \n' parsed += ' %s\n' % (key, ) if value is not None: options = value.all() parsed += ' \n' for option in options: parsed += ' \n' parsed += ' \n' parsed += ' \n' parsed += ' \n' if param.comment or param.value: parsed += ' %s\n' % (param.comment or param.value, ) parsed += ' \n' parsed += ' \n' if docblock.comment or docblock.value: parsed += ' %s\n' % (docblock.comment or docblock.value, ) if docblock.tags: parsed += ' \n' for tag_name in docblock.tags: tag = docblock.tags.get(tag_name) parsed += ' \n' parsed += ' %s\n' % (tag_name, ) if tag.options.values: parsed += ' \n' for key, value in tag.options.values: parsed += ' \n' parsed += ' %s\n' % (key, ) if value is not None: options = value.all() parsed += ' \n' for option in options: parsed += ' \n' parsed += ' \n' parsed += ' \n' parsed += ' \n' if tag.comment or tag.value: parsed += ' %s\n' % (tag.comment or tag.value, ) parsed += ' \n' parsed += ' \n' parsed += '' return parsed def expected2tree(docblock): # Note: this sucks, but we can't rely on etree.tostring() to generate useable output :( expected = '' if docblock is not None: expected += '\n' if docblock.find('identifier') is not None: expected += ' \n' # Expecting an identifier name is required, don't bother checking if it's there or not expected += ' %s\n' % (docblock.find('identifier/name').text, ) annotations = docblock.find('identifier/annotations') if annotations is not None: expected += ' \n' for annotation in annotations.findall('annotation'): expected += ' \n' expected += ' %s\n' % (annotation.find('name').text, ) if annotation.find('options') is not None: expected += ' \n' for option in annotation.findall('options/option'): expected += ' \n' expected += ' \n' expected += ' \n' expected += ' \n' expected += ' \n' parameters = docblock.find('parameters') if parameters is not None: expected += ' \n' for parameter in parameters.findall('parameter'): expected += ' \n' expected += ' %s\n' % (parameter.find('name').text, ) annotations = parameter.find('annotations') if annotations is not None: expected += ' \n' for annotation in parameter.findall('annotations/annotation'): expected += ' \n' expected += ' %s\n' % (annotation.find('name').text, ) if annotation.find('options') is not None: expected += ' \n' for option in annotation.findall('options/option'): expected += ' \n' expected += ' \n' expected += ' \n' expected += ' \n' if parameter.find('description') is not None: expected += ' %s\n' % (parameter.find('description').text, ) expected += ' \n' expected += ' \n' description = docblock.find('description') if description is not None: expected += ' %s\n' % (description.text, ) tags = docblock.find('tags') if tags is not None: expected += ' \n' for tag in tags.findall('tag'): expected += ' \n' expected += ' %s\n' % (tag.find('name').text, ) annotations = tag.find('annotations') if annotations is not None: expected += ' \n' for annotation in tag.findall('annotations/annotation'): expected += ' \n' expected += ' %s\n' % (annotation.find('name').text, ) if annotation.find('options') is not None: expected += ' \n' for option in annotation.findall('options/option'): expected += ' \n' expected += ' \n' expected += ' \n' expected += ' \n' if tag.find('description') is not None: expected += ' %s\n' % (tag.find('description').text, ) expected += ' \n' expected += ' \n' expected += '' return expected def create_tests(tests_dir, tests_file): tests_name = os.path.relpath(tests_file[:-4], tests_dir) tests_name = tests_name.replace('/', '.').replace('\\', '.') tests_tree = etree.parse(tests_file).getroot() fix_cdata_elements = tests_tree.findall('test/commentblock') fix_cdata_elements += tests_tree.findall('.//description') for element in fix_cdata_elements: if element.text: element.text = element.text.replace('{{?', '') for counter, test in enumerate(tests_tree.findall('test')): test_name = 'test_%s.%03d' % (tests_name, counter + 1) test_method = TestCommentBlock.__create_test__(test) setattr(TestCommentBlock, test_name, test_method) class TestCommentBlock(unittest.TestCase): @classmethod def __create_test__(cls, testcase): def do_test(self): # Parse GTK-Doc comment block commentblock = testcase.find('commentblock').text parsed_docblock = AnnotationParser().parse_comment_block((commentblock, 'test.c', 1)) parsed_tree = parsed2tree(parsed_docblock).split('\n') # Get expected output expected_docblock = testcase.find('docblock') expected_tree = expected2tree(expected_docblock).split('\n') # Construct a meaningful message msg = 'Parsed DocBlock object tree does not match expected output:\n\n' msg += '%s\n\n' % (commentblock, ) diff = difflib.unified_diff(expected_tree, parsed_tree, 'Expected DocBlock', 'Parsed DocBlock', n=max(len(expected_tree), len(parsed_tree)), lineterm='') for line in diff: msg += '%s\n' % (line, ) # Compare parsed with expected DocBlock tree self.assertEqual(parsed_tree, expected_tree, msg) return do_test if __name__ == '__main__': # Initialize message logger # TODO: at some point it might be a good idea to test warnings emitted # by annotationparser here, instead of having them in tests/warn/annotationparser.h? namespace = Namespace('Test', '1.0') logger = MessageLogger.get(namespace=namespace) logger.enable_warnings(False) # Load test cases from disc tests_dir = os.path.dirname(os.path.abspath(__file__)) for dirpath, dirnames, filenames in os.walk(tests_dir): for filename in filenames: tests_file = os.path.join(dirpath, filename) if os.path.basename(tests_file).endswith('.xml'): create_tests(tests_dir, tests_file) # Run test suite unittest.main()