From 7dae98803a6fc5d08041d1387e2e0d83fc0eb0ed Mon Sep 17 00:00:00 2001 From: Ulf Magnusson Date: Wed, 22 Aug 2018 22:08:06 +0200 Subject: Add a generic node iterator Suggested by Mitja Horvat (pinkfluid) in https://github.com/ulfalizer/Kconfiglib/pull/50. Kconfig.node_iter() iterates through all menu nodes in the menu tree in Kconfig order. This saves scripts the trouble of implementing their own tree walking code. Have node_iter() take a 'unique_syms' flag that can be enabled to only include symbols defined in multiple locations once. This is often what you want when generating output (and is used by write_config()). Order is still preserved. Piggyback a fix to a syntax error test comment. Parsing has been tightened up now. --- testsuite.py | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) (limited to 'testsuite.py') diff --git a/testsuite.py b/testsuite.py index eef8389..1e3d89a 100644 --- a/testsuite.py +++ b/testsuite.py @@ -464,9 +464,7 @@ def run_selftests(): fail('expected eval_string("{}") to throw KconfigError, ' "didn't".format(expr)) - # The C implementation's parser can be pretty lax about syntax. Kconfiglib - # sometimes needs to emulate that. Verify that some bad stuff throws - # KconfigError at least. + # Verify that some bad stuff throws KconfigError's verify_eval_bad("") verify_eval_bad("&") verify_eval_bad("|") @@ -1061,6 +1059,37 @@ g else: fail("'rsource' with missing file did not raise exception") + + print("Testing Kconfig.node_iter()") + + # Reuse tests/Klocation. The node_iter(unique_syms=True) case already gets + # plenty of testing from write_config() as well. + + c = Kconfig("tests/Klocation", warn=False) + + verify_equal( + [node.item.name for node in c.node_iter() + if isinstance(node.item, Symbol)], + ["SINGLE_DEF", "MULTI_DEF", "HELP_1", "HELP_2", "HELP_3", "MULTI_DEF", + "MULTI_DEF", "MENU_HOOK", "COMMENT_HOOK"] + 10*["MULTI_DEF"]) + + verify_equal( + [node.item.name for node in c.node_iter(True) + if isinstance(node.item, Symbol)], + ["SINGLE_DEF", "MULTI_DEF", "HELP_1", "HELP_2", "HELP_3", "MENU_HOOK", + "COMMENT_HOOK"]) + + verify_equal( + [node.prompt[0] for node in c.node_iter() + if not isinstance(node.item, Symbol)], + ["choice", "menu", "comment"]) + + verify_equal( + [node.prompt[0] for node in c.node_iter(True) + if not isinstance(node.item, Symbol)], + ["choice", "menu", "comment"]) + + # Get rid of custom 'srctree' from Klocation test os.environ.pop("srctree", None) -- cgit v1.2.3