From 71d9bb31f91fbd8f4e38e47fce54fd3221b3f8f5 Mon Sep 17 00:00:00 2001 From: Ulf Magnusson Date: Sat, 6 Jun 2015 04:24:26 +0200 Subject: Add some test cases for malformed expressions. Also fix a tokenizer nit: a '!' at the end of a line can be tokenized (though it's meaningless). --- kconfiglib.py | 4 +--- testsuite.py | 37 +++++++++++++++++++++++++------------ 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/kconfiglib.py b/kconfiglib.py index 81659ca..fc5d14f 100644 --- a/kconfiglib.py +++ b/kconfiglib.py @@ -751,9 +751,7 @@ class Config(object): i += 1 elif c == "!": - if i >= len(s): - _tokenization_error(s, filename, linenr) - if s[i] == "=": + if i < len(s) and s[i] == "=": append(T_UNEQUAL) i += 1 else: diff --git a/testsuite.py b/testsuite.py index 1e06854..5ccff6d 100644 --- a/testsuite.py +++ b/testsuite.py @@ -310,8 +310,6 @@ def run_selftests(): # eval() # - # TODO: Stricter syntax checking? - print "Testing eval()..." c = kconfiglib.Config("Kconfiglib/tests/Keval") @@ -321,6 +319,15 @@ def run_selftests(): verify(res == val, "'{0}' evaluated to {1}, expected {2}".format(expr, res, val)) + def verify_eval_bad(expr): + try: + c.eval(expr) + except kconfiglib.Kconfig_Syntax_Error: + pass + else: + fail('eval("{0}") should throw Kconfig_Syntax_Error' + .format(expr)) + # No modules verify_eval("n", "n") verify_eval("m", "n") @@ -395,6 +402,22 @@ def run_selftests(): verify_eval("not_defined_2 = not_defined_2", "y") verify_eval("not_defined_1 != not_defined_2", "y") + # The C implementation's parser can be pretty lax about syntax. Kconfiglib + # sometimes needs to emulate that. Verify that some bad stuff throws + # Kconfig_Syntax_Error at least. + verify_eval_bad("") + verify_eval_bad("&") + verify_eval_bad("|") + verify_eval_bad("!") + verify_eval_bad("(") + verify_eval_bad(")") + verify_eval_bad("=") + verify_eval_bad("(X") + verify_eval_bad("X &&") + verify_eval_bad("&& X") + verify_eval_bad("X ||") + verify_eval_bad("|| X") + # # Text queries # @@ -1734,16 +1757,6 @@ def test_call_all(conf): conf.eval("y && ARCH") - # Syntax error - caught_exception = False - try: - conf.eval("y && && y") - except kconfiglib.Kconfig_Syntax_Error: - caught_exception = True - - verify(caught_exception, - "No exception generated for expression with syntax error") - for s in conf.get_symbols(): s.__str__() s.get_assignable_values() -- cgit v1.2.3