From b8618bdc093a0a877693ac0d0bd23e29b08a6fae Mon Sep 17 00:00:00 2001 From: batt Date: Tue, 1 Sep 2009 14:25:21 +0000 Subject: [PATCH] Fix regexp; add tests. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@2844 38d2e660-2303-0410-9eaa-f027e97ec537 --- test/enablecfg.py | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/test/enablecfg.py b/test/enablecfg.py index 22f11dde..d670d364 100755 --- a/test/enablecfg.py +++ b/test/enablecfg.py @@ -48,7 +48,20 @@ whitelist = [ 'RAMP_USE_FLOATING_POINT', ] -pattern = r'\s*#define\s+(\w+)\s+(0)\W?' +tests = [ + '#define T_0 0\n', + '#define T_1 0\n', + ' #define T_2 0 \n', + '#define T_3 0 /* */\n', + '#define T_4 0/* */\n', +] +no_tests = [ + '#define T_1 0A\n', + '#define T_1 0x0A/* */\n', + '#define T_1 0UL /* */\n', + '#define T_1 0UL/* */\n', +] +pattern = r'\s*#define\s+(\w+)\s+(0)(?:\s+|/|$)' def f(match): if match.group(1) in whitelist: @@ -59,7 +72,14 @@ def f(match): ex = match.end(2) - match.end(0) return data[:sx] + '1' + data[ex:] -data = file(sys.argv[1]).read() -data, count = re.subn(pattern, f, data) -if count: - file(sys.argv[1], 'w').write(data) +if len(sys.argv) == 1: + for t in tests: + print t, re.subn(pattern, f, t)[0] + print '-' * 42 + for t in no_tests: + print t, re.subn(pattern, f, t)[0] +else: + data = file(sys.argv[1]).read() + data, count = re.subn(pattern, f, data) + if count: + file(sys.argv[1], 'w').write(data) -- 2.25.1