Skip to content

Commit

Permalink
feat(tests, repl): add first repl test using pexpect
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperFola committed Jan 25, 2025
1 parent faa3338 commit 741bbec
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions tests/repl/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import pexpect
import sys
import os


def get_repl():
executable = None
for p in ["./arkscript", "cmake-build-debug/arkscript", "build/arkscript"]:
if os.path.exists(p):
executable = p
break
if executable is None:
print("Couldn't find a valid path to 'arkscript' executable")
sys.exit(1)

child = pexpect.spawn(executable, timeout=2, encoding="utf-8")
child.logfile = sys.stdout
return child


def test(prompt: str, command: str, expected: str):
try:
repl.expect(prompt)
repl.send(command)
if expected is not None:
repl.expect(expected)
except pexpect.ExceptionPexpect:
print(f"Tried to match '{prompt}{command.strip()}' with '{expected}' but got {repl.after}")
raise


repl = get_repl()

# repl headers
repl.expect(r"ArkScript REPL -- Version [0-9]+\.[0-9]+\.[0-9]+-[a-f0-9]{8} \[LICENSE: Mozilla Public License 2\.0\]")
repl.expect(r"Type \"quit\" to quit\. Try \"help\" for more information")

# completion
test("main:001> ", "(le", "let")
repl.send("t a 5)\r\n")

test("main:002> ", "(print a)\r\n", "5")

test("main:003> ", "(im", "port")
repl.sendcontrol("c")

test("main:003> ", "(let foo (fun (bar) (* bar 7))) (print (foo 7))\r\n", "49")

test("main:004> ", "# just a comment\r\n", "main:005> ")

if repl.isalive():
repl.sendline("quit")
repl.close()

0 comments on commit 741bbec

Please sign in to comment.