Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Parser Testing

Paul McGuire edited this pageJan 19, 2020 ·5 revisions

Pyparsing 3.0 will introduce some new methods in apyparsing_testing namespace, to add assertions for easy validation of ParseResults and ParseExceptions. The API is still undergoing some revisions, but hopefully these methods will make it easier to write pyparsing parser unit tests using a consistent style and test API.

(These classes were backported to pyparsing 2.4.6 so that you can write automated unit tests of your parsing apps before upgrading to pyparsing 3.0.)

  • Assert methods defined as part of thepyparsing_test.TestParseResultsAsserts mixin class. Add these methods to yourown unittest.TestCase classes using:

    from pyparsing import pyparsing_test as pptclass MyTestCase(ppt.TestParseResultsAsserts, unittest.TestCase):    ... test case class body ...

    Assert methods:

    • assertParseResultsEquals(self, result, expected_list=None, expected_dict=None, msg=None)

      General purpose assert that can compare theParseResults object returned from callingparseString,searchString orscanString to an expected list and/or expected dict.

    • assertParseAndCheckList(self, expr, test_string, expected_list, msg=None, verbose=True)

      Convenience method to take a pyparsing expression and test string, runexpr.parseString(test_string),and compare the parsed tokens to an expected list. Ifverbose is True, the parsed results are shownusingdump().

    • assertParseAndCheckDict(self, expr, test_string, expected_dict, msg=None, verbose=True)

      Convenience method to take a pyparsing expression and test string, runexpr.parseString(test_string),and compare the named results to an expected dict. Ifverbose is True, the parsed results are shownusingdump().

    • assertRunTestResults(self, run_tests_report, expected_parse_results=None, msg=None)

      Unit test assertion to evaluate output ofParserElement.runTests(). If a list oflist-dict tuples is given as theexpected_parse_results argument, then these are zippedwith the report tuples returned by runTests and evaluated usingassertParseResultsEquals.Finally, asserts that the overallrunTests() success value is True.

    • assertRaisesParseException(self, exc_type=ParseException, msg=None)

      Contexgt manager to be used for negative test cases that should raise aParseException,similar tounittest.TestCase.assertRaises.

  • reset_pyparsing_context context manager, to restore pyparsingconfig settings

    Context manager to be used when writing unit tests that modify pyparsing config values:

    • packrat parsing
    • default whitespace characters.
    • default keyword characters
    • literal string auto-conversion class
    • __diag__ settings

    Example:

        with reset_pyparsing_context():        # temporarily change literal-inlining to use Suppress instead of Literal        ParserElement.inlineLiteralsUsing(Suppress)

Clone this wiki locally


[8]ページ先頭

©2009-2025 Movatter.jp