'file_match' now handles multi line patterns

This commit is contained in:
Johan Holmberg 2015-06-15 23:15:23 +02:00
parent 58260c5a21
commit a72025c385
2 changed files with 27 additions and 6 deletions

View File

@ -90,6 +90,10 @@ class TC_example(TestCase):
self.create_file("abc.txt", [ self.create_file("abc.txt", [
'detta är abc.txt', 'detta är abc.txt',
'räksmörgås', 'räksmörgås',
' aaa',
' bbb',
' ccc',
' ddd',
], encoding='utf-16') ], encoding='utf-16')
with self.cmd("cat abc.txt") as c: with self.cmd("cat abc.txt") as c:
c.stdout_equal([ c.stdout_equal([
@ -102,7 +106,10 @@ class TC_example(TestCase):
'detta är abc.txtx', 'detta är abc.txtx',
'räksmörgås', 'räksmörgås',
], 'utf-16') ], '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: with self.cmd("true") as c:
pass pass

View File

@ -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): def to_content(lines):
return ''.join(line + "\n" for line in lines) return ''.join(line + "\n" for line in lines)
@ -137,6 +145,15 @@ class ExpectPattern:
self.encoding = encoding self.encoding = encoding
self.pattern = pattern 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): def check(self, name, actual_bytes):
try: try:
actual_lines = actual_bytes.lines(self.encoding) actual_lines = actual_bytes.lines(self.encoding)
@ -144,15 +161,12 @@ class ExpectPattern:
actual_lines = ["<CAN'T DECODE AS " + self.encoding + ">"] actual_lines = ["<CAN'T DECODE AS " + self.encoding + ">"]
ok = False ok = False
else: else:
ok = False ok = self._match(actual_lines)
for line in actual_lines:
if re.search(self.pattern, line):
ok = True
if not ok: if not ok:
print("--- ERROR:", name) print("--- ERROR:", name)
error_show(name, "actual:", actual_lines) 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 self.result._nerrors += 1
#---------------------------------------------------------------------- #----------------------------------------------------------------------