From c4cafcecb1f50e38275b7d82852fd33b63964fb2 Mon Sep 17 00:00:00 2001 From: Kitson Kelly Date: Fri, 10 Aug 2018 10:38:32 -0700 Subject: Support wildcard matching of output in tests --- tools/util.py | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) (limited to 'tools/util.py') diff --git a/tools/util.py b/tools/util.py index 0a6f6a071..9e29cf742 100644 --- a/tools/util.py +++ b/tools/util.py @@ -1,5 +1,4 @@ -# Copyright 2018 Ryan Dahl -# All rights reserved. MIT License. +# Copyright 2018 the Deno authors. All rights reserved. MIT license. import os import shutil import stat @@ -133,3 +132,33 @@ def build_path(): return os.environ["DENO_BUILD_PATH"] else: return os.path.join(root_path, "out", build_mode()) + + +# Returns True if the expected matches the actual output, allowing variation +# from actual where expected has the wildcard (e.g. matches /.*/) +def pattern_match(pattern, string, wildcard="[WILDCARD]"): + if len(pattern) == 0: + return string == 0 + if pattern == wildcard: + return True + + parts = str.split(pattern, wildcard) + + if len(parts) == 1: + return pattern == string + + if string.startswith(parts[0]): + string = string[len(parts[0]):] + else: + return False + + for i in range(1, len(parts)): + if i == (len(parts) - 1): + if parts[i] == "" or parts[i] == "\n": + return True + found = string.find(parts[i]) + if found < 0: + return False + string = string[(found + len(parts[i])):] + + return len(string) == 0 -- cgit v1.2.3