From a72025c385495fd1040c8a6d84f8ec72a96bc109 Mon Sep 17 00:00:00 2001 From: Johan Holmberg Date: Mon, 15 Jun 2015 23:15:23 +0200 Subject: [PATCH] 'file_match' now handles multi line patterns --- python/CMDTEST_example.py | 9 ++++++++- python/cmdtest.py | 24 +++++++++++++++++++----- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/python/CMDTEST_example.py b/python/CMDTEST_example.py index 3aeec63..573880f 100644 --- a/python/CMDTEST_example.py +++ b/python/CMDTEST_example.py @@ -90,6 +90,10 @@ class TC_example(TestCase): self.create_file("abc.txt", [ 'detta är abc.txt', 'räksmörgås', + ' aaa', + ' bbb', + ' ccc', + ' ddd', ], encoding='utf-16') with self.cmd("cat abc.txt") as c: c.stdout_equal([ @@ -102,7 +106,10 @@ class TC_example(TestCase): 'detta är abc.txtx', 'räksmörgås', ], 'utf-16') - c.file_match("abc.txt", "tt", 'utf-16') + c.file_match("abc.txt", [ + "xbb", + "ccc", + ], 'utf-16') with self.cmd("true") as c: pass diff --git a/python/cmdtest.py b/python/cmdtest.py index d155819..7b29bfe 100755 --- a/python/cmdtest.py +++ b/python/cmdtest.py @@ -39,6 +39,14 @@ ORIG_CWD = os.getcwd() #---------------------------------------------------------------------- +def subranges(n, arr): + arr = list(arr) + for i in range(0,len(arr)-n+1): + yield arr[i:i+n] + +def to_list(arg): + return arg if isinstance(arg, list) else [arg] + def to_content(lines): return ''.join(line + "\n" for line in lines) @@ -137,6 +145,15 @@ class ExpectPattern: self.encoding = encoding self.pattern = pattern + def _match(self, lines): + patterns = to_list(self.pattern) + for some_lines in subranges(len(patterns), lines): + for pattern, line in zip(patterns, some_lines): + if not re.search(pattern, line): + break + else: + return True + def check(self, name, actual_bytes): try: actual_lines = actual_bytes.lines(self.encoding) @@ -144,15 +161,12 @@ class ExpectPattern: actual_lines = [""] ok = False else: - ok = False - for line in actual_lines: - if re.search(self.pattern, line): - ok = True + ok = self._match(actual_lines) if not ok: print("--- ERROR:", name) error_show(name, "actual:", actual_lines) - error_show(name, "expect:", ['PATTERN: ' + self.pattern]) + error_show(name, "expect:", ['PATTERN:'] + to_list(self.pattern)) self.result._nerrors += 1 #----------------------------------------------------------------------