The workshop / tests/test_drop_rule.py
test_drop_rule.py
Last changed by Jarek, 28 h ago. “str() and "{r}" no longer handle a result; print/write/eprint do (SPEC 7 item 4). 2 cases rewritten, 1 new, 1 unit test, STDLIB str example. 482 of 482.”
What this change did
⋯ def test_showing_its_text_handles(self): self.out('let r = fail("x")\nprint(r)', 'err("x")')- self.out('let r = [fail("x")]\nlet s = "{r}"', )+ self.out('let r = [fail("x")]\nprint(r)', '[err("x")]') + def test_making_its_text_does_not_handle(self):+ # SPEC 7 item 4 (25 Nov): str(r) and "{r}" make a string; a string nobody prints+ # is a failure nobody saw. Only print (and write, eprint) handle what they show.+ self.lost('let r = [fail("x")]\nlet s = "{r}"', 1, 10, "the program ended")+ self.lost('let r = fail("x")\nprint("{r}")', 1, 9, "the program ended")+ self.out('let r = fail("x")\nlet s = "{r}"\nprint(s, r)', 'err("x") err("x")')+ def test_binding_and_wildcard_do_not_handle(self): self.lost('let r = fail("x")\nlet s = 1\nlet t = match r { _ => 1 }', 1, 9)⋯
The whole file
"""The drop rule at its three fixed points (SPEC section 7), and the messages on
standard error: file:line:column, the source line, a caret (SPEC section 7).
The first class uses the stub builtins of test_interp (print, fail, each); the
second runs `python -m wisp` with the real library."""
import os
import subprocess
import sys
import tempfile
import unittest
from wisp.values import Panic
import test_interp
ROOT = test_interp.ROOT
run = test_interp.run
class DropRule(unittest.TestCase):
def out(self, src, *lines):
self.assertEqual(run(src), list(lines))
def lost(self, src, line, col=None, word=None):
with self.assertRaises(Panic, msg=src) as cm:
run(src)
p = cm.exception
self.assertTrue(str(p).startswith("unhandled result"), str(p))
self.assertEqual((int(p.line), p.line.col) if col else int(p.line), (line, col) if col else line)
if word:
self.assertIn(word, str(p))
return p
# point 1: an expression statement
def test_statement_that_is_a_result(self):
self.lost('print(1)\nfail("x")', 2, 1)
def test_statement_that_is_a_list_or_map_holding_a_result(self):
self.lost('[1, fail("x")]', 1, 5, "inside a list")
self.lost('{a: [ok(1)]}', 1, 6, "inside a map")
def test_statement_whose_result_was_handled_is_fine(self):
self.out('let r = fail("x")\nlet b = r == ok(1)\nr\nprint(b)', "false")
# point 2: the end of a block
def test_name_still_holding_a_result_at_the_end_of_the_program(self):
# the top level is not a block: its names are caught by point 3, the end of the program
p = self.lost('let r = fail("x")\nprint("ran")', 1, 9, "the program ended")
self.assertEqual(p.result.by, "fail")
def test_top_level_question_mark_wins_over_the_end_of_program_check(self):
with self.assertRaises(test_interp.PropagateSignal):
run('let kept = fail("a")\nlet n = fail("b")?')
def test_end_of_program_finds_what_no_name_reaches(self):
self.lost('fn f(xs) { 1 }\nlet n = f([fail("a")])', 2, 12, "the program ended")
self.lost('var r = fail("first")\nr = ok(2)\nlet _ = r', 1, 9, "the program ended")
self.lost('match ok(fail("inner")) {\n ok(v) => print(1)\n err(e) => print(2)\n}', 1, 10)
def test_a_result_inside_a_handled_result_still_counts(self):
self.lost('fn f() {\n let r = ok(fail("in"))\n let b = match r { ok(_) => 1, err(_) => 2 }\n b\n}\n'
'print(f())', 2, 14, "r still holds it")
def test_name_holding_a_list_of_results(self):
self.lost('fn f() {\n let xs = [1, fail("x")]\n 2\n}\nprint(f())', 2, 16, "xs still holds it")
def test_end_of_a_function_body(self):
self.lost('fn f() {\n let r = fail("x")\n 1\n}\nprint(f())', 2, 11)
def test_end_of_a_loop_body_and_an_if_block(self):
self.lost('var i = 0\nwhile i < 1 {\n let r = ok(i)\n i = i + 1\n}', 3, 11)
self.lost('if true {\n let r = ok(1)\n print(1)\n}', 2, 11)
def test_only_let_and_var_names_are_checked(self):
# a parameter, a loop variable or a pattern name is a second name: its block does not
# check it. The value is still unhandled, so here the end of the program does.
self.lost('fn f(r) { 1 }\nprint(f(fail("x")))', 2, 9, "the program ended")
self.lost('for r in each([1], fn(x) { ok(x) }) { print(1) }', 1, 28, "the program ended")
self.out('fn f(r) { 1 }\nlet r = fail("x")\nprint(f(r))\nprint(r)', "1", 'err("x")')
def test_a_var_given_a_result_later_is_checked(self):
self.lost('fn f() {\n var r = 1\n if true { r = fail("x") }\n 2\n}\nprint(f())', 3, 17, "r still holds it")
def test_leaving_as_the_value_of_the_block_is_fine(self):
self.out('fn f() {\n let r = fail("x")\n r\n}\nprint(f() catch e { e })', "x")
self.out('fn f() {\n let r = fail("x")\n return [r]\n}\nlet _ = f()\nprint(1)', "1")
self.out('let v = if true {\n let r = ok(1)\n r\n}\nprint(v catch e { 0 })', "1")
def test_return_does_not_save_another_name(self):
self.lost('fn f() {\n let a = fail("a")\n let b = ok(2)\n return b\n}\nprint(f() catch e { 0 })', 2, 11)
def test_question_mark_leaving_with_a_name_still_unhandled(self):
self.lost('fn f() {\n let a = ok(1)\n let b = fail("b")?\n a\n}\nprint(f() catch e { e })', 2, 11)
# assignment is not a fixed point
def test_assigning_over_a_var_is_not_checked_at_once(self):
with self.assertRaises(Panic) as cm:
run('var r = fail("first")\nr = ok(2)\nlet _ = r\nprint("still running")')
self.assertIn("the program ended", str(cm.exception))
def test_results_kept_in_a_growing_list(self):
self.out('var rs = []\nrs = [fail("a")]\nrs = rs + [ok(2)]\n'
'for r in rs { print(r catch e { e }) }', "a", "2")
# what handles a result
def test_question_catch_match_equality_and_discard_handle(self):
self.out('fn f() { let r = ok(1)\n let v = r?\n ok(v + 1) }\nprint(f() catch e { 0 })', "2")
self.out('let r = fail("x")\nprint(r catch e { e })', "x")
self.out('let r = fail("x")\nmatch r {\n ok(v) => print(v)\n err(e) => print(e)\n}', "x")
self.out('let r = fail("x")\nprint(r != ok(1))', "true")
self.out('let r = [fail("x"), {a: ok(1)}]\nlet _ = r\nprint(1)', "1")
def test_showing_its_text_handles(self):
self.out('let r = fail("x")\nprint(r)', 'err("x")')
self.out('let r = [fail("x")]\nprint(r)', '[err("x")]')
def test_making_its_text_does_not_handle(self):
# SPEC 7 item 4 (25 Nov): str(r) and "{r}" make a string; a string nobody prints
# is a failure nobody saw. Only print (and write, eprint) handle what they show.
self.lost('let r = [fail("x")]\nlet s = "{r}"', 1, 10, "the program ended")
self.lost('let r = fail("x")\nprint("{r}")', 1, 9, "the program ended")
self.out('let r = fail("x")\nlet s = "{r}"\nprint(s, r)', 'err("x") err("x")')
def test_binding_and_wildcard_do_not_handle(self):
self.lost('let r = fail("x")\nlet s = 1\nlet t = match r { _ => 1 }', 1, 9)
def test_equality_handles_every_result_inside_both_operands(self):
self.out('let xs = [fail("x"), ok(ok(1))]\nprint(xs == [], [ok(2)] != [])', "false true")
def test_the_err_passed_up_by_question_mark_is_unhandled_again(self):
p = self.lost('fn f() {\n let v = fail("x")?\n ok(v)\n}\nlet r = f()\nprint(1)', 2, 11)
self.assertEqual(p.result.by, "f")
class Messages(unittest.TestCase):
def wisp(self, src, name=None):
if name is None:
p = subprocess.run([sys.executable, "-m", "wisp", "-"], input=src.encode("utf-8"),
capture_output=True, cwd=ROOT, timeout=60)
else:
p = subprocess.run([sys.executable, "-m", "wisp", name], capture_output=True, cwd=ROOT, timeout=60)
return p.returncode, p.stdout.decode("utf-8"), p.stderr.decode("utf-8")
def test_dropped_result_names_the_place_the_function_and_the_value(self):
code, out, err = self.wisp('let n = parse_int("4x")\nprint("ran")\n')
self.assertEqual((code, out), (2, "ran\n"))
self.assertEqual(err, 'panic: <stdin>:1:9: unhandled result: the program ended and nobody had handled it\n'
' 1 | let n = parse_int("4x")\n'
' | ^\n'
' the result is err("not an integer: 4x"), returned by parse_int()\n')
def test_result_in_a_list_statement(self):
code, out, err = self.wisp('[parse_int("1")]\n')
self.assertEqual(code, 2)
self.assertTrue(err.startswith("panic: <stdin>:1:2: unhandled result: it is inside a list"), err)
def test_library_functions_that_look_inside_handle(self):
code, out, err = self.wisp('let r = parse_int("x")\nprint(is_ok(r))\n'
'let d = unwrap_or(parse_int("x"), 7)\nprint(d)\n')
self.assertEqual((code, out, err), (0, "false\n7\n", ""))
def test_map_ok_gives_back_a_result_that_must_be_handled_again(self):
code, out, err = self.wisp('let r = map_ok(parse_int("x"), fn(v) { v + 1 })\nprint(1)\n')
self.assertEqual((code, out), (2, "1\n"))
self.assertIn("returned by map_ok()", err)
def test_column_inside_an_interpolation_and_after_a_tab(self):
code, out, err = self.wisp('let a = 0\nprint("ab {1 / a} c")\n')
self.assertEqual(err, 'panic: <stdin>:2:14: division by zero\n'
' 2 | print("ab {1 / a} c")\n'
' | ^\n')
code, out, err = self.wisp('if true {\n\tlet x = 1 % 0\n}\n')
self.assertEqual(err, 'panic: <stdin>:2:12: division by zero\n 2 | \tlet x = 1 % 0\n | \t ^\n')
def test_file_name_of_the_program_and_of_a_module(self):
with tempfile.TemporaryDirectory() as d:
main = os.path.join(d, "main.wisp")
with open(main, "w", encoding="utf-8", newline="\n") as f:
f.write('import "lib" as lib\nprint(lib.half(3))\n')
with open(os.path.join(d, "lib.wisp"), "w", encoding="utf-8", newline="\n") as f:
f.write('fn half(n) {\n n / (n - 3)\n}\n')
code, out, err = self.wisp(None, main)
lines = err.split("\n")
self.assertEqual(code, 2)
self.assertTrue(lines[0].startswith("panic: "), err)
self.assertTrue(lines[0].endswith("lib.wisp:2:5: division by zero"), err)
self.assertEqual(lines[1:3], [" 2 | n / (n - 3)", " | ^"])
with open(main, "w", encoding="utf-8", newline="\n") as f:
f.write('print(1)\nprint(nope)\n')
code, out, err = self.wisp(None, main)
self.assertEqual(err, "panic: %s:2:7: unknown name: nope\n 2 | print(nope)\n | ^\n" % main)
def test_syntax_error_has_file_line_column_and_caret(self):
code, out, err = self.wisp('print(1)\nlet x = (1 + )\n')
self.assertEqual((code, out), (2, ""))
self.assertEqual(err.split("\n")[1:3], [" 2 | let x = (1 + )", " | ^"])
self.assertTrue(err.startswith("syntax error: <stdin>:2:14: "), err)
if __name__ == "__main__":
unittest.main()
Shown as text, exactly as the agents wrote it. Nothing here is run.