Skip to content

Commit

Permalink
runtest: support carriage returns in tests.
Browse files Browse the repository at this point in the history
This allows carriage returns to be specified in tests which allows
multiline tests to be written i.e. by using Ctrl-V Enter to put
a literal carriage return (^M) into the test definition. The effect of
this is to send a single string to the REPL that contains newlines
which normally can't be done because readline behavior will split it
into two separate lines.
  • Loading branch information
kanaka committed Dec 10, 2018
1 parent 76adfab commit 18616b1
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions runtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def log(data, end='\n'):
help="Disable optional tests that follow a ';>>> optional=True'")
parser.set_defaults(optional=True)

parser.add_argument('test_file', type=argparse.FileType('r'),
parser.add_argument('test_file', type=str,
help="a test file formatted as with mal test data")
parser.add_argument('mal_cmd', nargs="*",
help="Mal implementation command line. Use '--' to "
Expand Down Expand Up @@ -148,14 +148,14 @@ def read_to_prompt(self, prompts, timeout):
buf = self.buf[0:match.start()]
self.buf = self.buf[end:]
self.last_prompt = prompt
return buf
return buf.replace("^M", "\r")
return None

def writeline(self, str):
def _to_bytes(s):
return bytes(s, "utf-8") if IS_PY_3 else s

self.stdin.write(_to_bytes(str + "\n"))
self.stdin.write(_to_bytes(str.replace('\r', '\x16\r') + "\n"))

def cleanup(self):
#print "cleaning up"
Expand All @@ -169,7 +169,8 @@ def cleanup(self):
class TestReader:
def __init__(self, test_file):
self.line_num = 0
self.data = test_file.read().split('\n')
f = open(test_file, newline='') if IS_PY_3 else open(test_file)
self.data = f.read().split('\n')
self.soft = False
self.deferrable = False
self.optional = False
Expand Down Expand Up @@ -292,7 +293,7 @@ def assert_prompt(runner, prompts, timeout):

if t.form == None: continue

log("TEST: %s -> [%s,%s]" % (t.form, repr(t.out), t.ret), end='')
log("TEST: %s -> [%s,%s]" % (repr(t.form), repr(t.out), t.ret), end='')

# The repeated form is to get around an occasional OS X issue
# where the form is repeated.
Expand Down Expand Up @@ -349,7 +350,7 @@ def assert_prompt(runner, prompts, timeout):
%3d: failing tests
%3d: passing tests
%3d: total tests
""" % (args.test_file.name, soft_fail_cnt, fail_cnt,
""" % (args.test_file, soft_fail_cnt, fail_cnt,
pass_cnt, test_cnt)
log(results)

Expand Down

0 comments on commit 18616b1

Please sign in to comment.