← Lesson
Tests
Joeven
Run
Reset
Python loads on first run
import json def add(a, b): return a + b def goal_satisfied(state): answer = state.get("answer") return state.get("done") is True and isinstance(answer, str) and len(answer) > 0 def parse_action(text): try: data = json.loads(text) except json.JSONDecodeError: return {"ok": False, "error": "bad_json"} tool = data.get("tool") args = data.get("args") if not tool or not isinstance(args, dict): return {"ok": False, "error": "shape"} return {"ok": True, "tool": tool, "args": args} def run_tests(): tests = [ ("tool_add", lambda: add(2, 3) == 5), ("goal_yes", lambda: goal_satisfied({"done": True, "answer": "ok"}) is True), ("goal_no_flag", lambda: goal_satisfied({"done": False, "answer": "ok"}) is False), ("goal_empty", lambda: goal_satisfied({"done": True, "answer": ""}) is False), ("parse_ok", lambda: parse_action('{"tool": "add", "args": {"a": 1}}')["ok"] is True), ("parse_bad", lambda: parse_action("not json")["ok"] is False), ("parse_no_args", lambda: parse_action('{"tool": "add"}')["ok"] is False), ] passed = 0 failed = 0 for name, fn in tests: try: assert fn() is True print(name, "PASS") passed += 1 except AssertionError: print(name, "FAIL") failed += 1 except Exception as exc: print(name, "ERROR", type(exc).__name__, exc) failed += 1 print("passed", passed, "failed", failed) return failed run_tests()
Run to execute this in your browser. Nothing is sent to a server.