388 lines
22 KiB
Python
388 lines
22 KiB
Python
"""Tests for the Mojo corpus pipeline. Offline: no test touches the network.
|
|
|
|
python3 -m unittest discover -s scripts/mojo/tests (or: pixi run mojo-test)
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import io
|
|
import json
|
|
import stat
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
import zipfile
|
|
from pathlib import Path
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
|
|
|
from mojocorpus import common, emit_ts, godbolt, verify # noqa: E402
|
|
from mojocorpus.common import Pin, PipelineError, Source, dialect_flags, load_pin, read_json # noqa: E402
|
|
from mojocorpus.fetch import _clean_names, extract_subtree, fetch_all, find_root # noqa: E402
|
|
from mojocorpus.harvest import detect_local_zips # noqa: E402
|
|
from mojocorpus.processors import diagnostics, editor, messages, release, skill, syntax, tokens # noqa: E402
|
|
|
|
REPO = common.REPO
|
|
|
|
|
|
def make_zip(path: Path, files: dict[str, str], links: dict[str, str] | None = None) -> Path:
|
|
with zipfile.ZipFile(path, "w") as zf:
|
|
for name, text in files.items():
|
|
zf.writestr(name, text)
|
|
for name, target in (links or {}).items():
|
|
info = zipfile.ZipInfo(name)
|
|
info.external_attr = (stat.S_IFLNK | 0o777) << 16
|
|
zf.writestr(info, target)
|
|
return path
|
|
|
|
|
|
def tiny_pin(**refs) -> Pin:
|
|
return Pin(version="1.1.0", compiler="mojo_1_1_0", base="https://godbolt.org", concurrency=2, timeout=30,
|
|
sources={
|
|
"modular": Source("modular", "modular/modular", refs.get("modular", common.UNRESOLVED),
|
|
["stdlib/std/prelude/__init__.mojo", "docs/site/releases"], "Apache-2.0"),
|
|
"mojo-syntax": Source("mojo-syntax", "modular/mojo-syntax", refs.get("mojo-syntax", common.UNRESOLVED),
|
|
["syntaxes/mojo.syntax.json"], "MIT"),
|
|
},
|
|
newer=[{"id": "match", "pattern": r"^\s*(?:comptime\s+)?__match\b", "why": "nightly"}],
|
|
older=[{"id": "fn", "pattern": r"^\s*fn\s+\w", "why": "removed"}])
|
|
|
|
|
|
MODULAR_FILES = {"stdlib/std/prelude/__init__.mojo": "from std.builtin import Int\n",
|
|
"docs/site/releases/v1.1.0.md": "## Removed\n\n- `fn` is gone.\n"}
|
|
|
|
|
|
# --------------------------------------------------------------------------
|
|
class FetchTests(unittest.TestCase):
|
|
def test_root_is_found_by_markers_whatever_the_wrapping_directory(self):
|
|
markers = ["stdlib/std/prelude/__init__.mojo", "docs/site/releases"]
|
|
for prefix in ("Mojo/", "modular-0123abc/mojo/", ""):
|
|
names = [prefix + n for n in MODULAR_FILES] + ["modular-0123abc/README.md"]
|
|
self.assertEqual(find_root(names, markers), prefix, prefix)
|
|
|
|
def test_a_directory_marker_needs_no_directory_entry_in_the_zip(self):
|
|
self.assertEqual(find_root(["a/docs/site/releases/v1.md", "a/stdlib/std/prelude/__init__.mojo"],
|
|
["stdlib/std/prelude/__init__.mojo", "docs/site/releases"]), "a/")
|
|
|
|
def test_missing_markers_name_the_markers(self):
|
|
with self.assertRaisesRegex(PipelineError, "prelude"):
|
|
find_root(["x/README.md"], ["stdlib/std/prelude/__init__.mojo"])
|
|
|
|
def test_path_traversal_is_refused_and_junk_and_links_are_skipped(self):
|
|
with tempfile.TemporaryDirectory() as d:
|
|
bad = make_zip(Path(d) / "bad.zip", {"../evil.txt": "x"})
|
|
with zipfile.ZipFile(bad) as zf, self.assertRaisesRegex(PipelineError, "escapes"):
|
|
_clean_names(zf)
|
|
ok = make_zip(Path(d) / "ok.zip", {"a/f.txt": "x", "__MACOSX/a/._f.txt": "y", "a/.DS_Store": "z"},
|
|
links={"a/link": "/etc/passwd"})
|
|
with zipfile.ZipFile(ok) as zf:
|
|
self.assertEqual([i.filename for i in _clean_names(zf)], ["a/f.txt"])
|
|
|
|
def test_only_the_marked_subtree_is_extracted(self):
|
|
with tempfile.TemporaryDirectory() as d:
|
|
z = make_zip(Path(d) / "m.zip", {**{"top/mojo/" + k: v for k, v in MODULAR_FILES.items()}, "top/max/big.bin": "x" * 100})
|
|
root, prefix = extract_subtree(z, ["stdlib/std/prelude/__init__.mojo", "docs/site/releases"], Path(d) / "out")
|
|
self.assertEqual(prefix, "top/mojo")
|
|
self.assertTrue((root / "docs/site/releases/v1.1.0.md").exists())
|
|
self.assertFalse((Path(d) / "out/top/max").exists())
|
|
|
|
def test_local_zip_lock_records_hash_and_stands_in_for_a_resolved_ref(self):
|
|
with tempfile.TemporaryDirectory() as d:
|
|
d = Path(d)
|
|
z = make_zip(d / "Mojo.zip", {"Mojo/" + k: v for k, v in MODULAR_FILES.items()})
|
|
lock = d / "lock.json"
|
|
pin = tiny_pin(modular="mojo/v1.1.0")
|
|
wd = d / "wd"; wd.mkdir()
|
|
fetch_all(pin, wd, {"modular": z}, only={"modular"}, lock_path=lock)
|
|
entry = read_json(lock)["sources"]["modular"]
|
|
self.assertEqual(entry["kind"], "local")
|
|
self.assertEqual(entry["sha256"], common.sha256_file(z))
|
|
self.assertEqual(entry["standsInFor"], "mojo/v1.1.0")
|
|
self.assertEqual(entry["pinnedRef"], "local-override")
|
|
self.assertEqual(entry["root"], "Mojo")
|
|
|
|
def test_an_unresolved_ref_with_a_local_lock_insists_on_the_same_zip(self):
|
|
with tempfile.TemporaryDirectory() as d:
|
|
d = Path(d)
|
|
z = make_zip(d / "Mojo.zip", {"Mojo/" + k: v for k, v in MODULAR_FILES.items()})
|
|
lock = d / "lock.json"
|
|
pin = tiny_pin()
|
|
(d / "wd1").mkdir(); (d / "wd2").mkdir()
|
|
fetch_all(pin, d / "wd1", {"modular": z}, only={"modular"}, lock_path=lock)
|
|
with self.assertRaisesRegex(PipelineError, "local zip"):
|
|
fetch_all(pin, d / "wd2", {}, only={"modular"}, lock_path=lock)
|
|
|
|
def test_an_unresolved_ref_without_a_local_zip_explains_what_to_do(self):
|
|
with tempfile.TemporaryDirectory() as d:
|
|
with self.assertRaisesRegex(PipelineError, "UNRESOLVED"):
|
|
fetch_all(tiny_pin(), Path(d), {}, only={"modular"}, lock_path=Path(d) / "lock.json")
|
|
|
|
def test_local_dir_matches_zips_by_markers_and_leaves_resolved_sources_to_github(self):
|
|
with tempfile.TemporaryDirectory() as d:
|
|
d = Path(d)
|
|
make_zip(d / "Mojo.zip", {"Mojo/" + k: v for k, v in MODULAR_FILES.items()})
|
|
make_zip(d / "syntax.zip", {"mojo-syntax-main/syntaxes/mojo.syntax.json": "{}"})
|
|
make_zip(d / "other.zip", {"x/readme.txt": "?"})
|
|
self.assertEqual(sorted(detect_local_zips(tiny_pin(), d)), ["modular", "mojo-syntax"])
|
|
resolved = tiny_pin(modular="mojo/v1.1.0")
|
|
self.assertEqual(sorted(detect_local_zips(resolved, d)), ["mojo-syntax"])
|
|
self.assertEqual(sorted(detect_local_zips(resolved, d, include_resolved=True)), ["modular", "mojo-syntax"])
|
|
|
|
|
|
# --------------------------------------------------------------------------
|
|
class PinTests(unittest.TestCase):
|
|
def test_the_committed_pin_loads_and_is_1_1_0(self):
|
|
pin = load_pin()
|
|
self.assertEqual(pin.version, "1.1.0")
|
|
self.assertEqual(pin.compiler, "mojo_1_1_0")
|
|
self.assertEqual(pin.sources["modular"].ref, "mojo/v1.1.0")
|
|
self.assertTrue(pin.newer and pin.older)
|
|
|
|
def test_dialect_markers_match_code_not_comments_or_strings(self):
|
|
pin = tiny_pin()
|
|
self.assertEqual(dialect_flags("fn main():\n pass\n", pin), {"older": ["fn"]})
|
|
self.assertEqual(dialect_flags('# fn main() was the old way\ndef main():\n print("fn x")\n', pin), {})
|
|
self.assertEqual(dialect_flags('def f():\n """\nfn g(): docstring\n"""\n', pin), {})
|
|
self.assertEqual(dialect_flags("def f(x: Int):\n comptime __match x:\n", pin), {"newer": ["match"]})
|
|
|
|
|
|
# --------------------------------------------------------------------------
|
|
class ProcessorTests(unittest.TestCase):
|
|
def test_annotations_resolve_to_the_lines_they_name(self):
|
|
src = "\n".join([
|
|
"def f():", # 1
|
|
" x = y # expected-error {{use of unknown declaration 'y'}}", # 2
|
|
" # expected-error @below {{first}}", # 3
|
|
" # expected-note @below {{second, same line}}", # 4
|
|
"", # 5
|
|
" z()", # 6
|
|
" # expected-error @+2 {{two down}}", # 7
|
|
" a()", # 8
|
|
" b()", # 9
|
|
" c()", # 10
|
|
" # expected-warning @above {{c is odd}}", # 11
|
|
" d() # expected-error-re {{value of type '{{.*}}' cannot}}", # 12
|
|
])
|
|
got = [(a["kind"], a["line"], a["message"], a["regex"]) for a in diagnostics.parse_annotations(src)]
|
|
self.assertEqual(got, [
|
|
("error", 2, "use of unknown declaration 'y'", False),
|
|
("error", 6, "first", False),
|
|
("note", 6, "second, same line", False),
|
|
("error", 9, "two down", False),
|
|
("warning", 10, "c is odd", False),
|
|
("error", 12, "value of type '{{.*}}' cannot", True),
|
|
])
|
|
|
|
def test_the_pass_comes_from_the_verify_run_line(self):
|
|
self.assertEqual(diagnostics.pass_of(["%parse-mojo %s | FileCheck %s", "%parse-mojo %s | kgen-opt -check-lifetimes -verify-diagnostics"]), "lifetimes")
|
|
self.assertEqual(diagnostics.pass_of(["%parse-mojo-isolated %s -verify-diagnostics"]), "parser")
|
|
|
|
def test_messages_keep_wording_placeholders_and_fixits(self):
|
|
cpp = '''
|
|
} else if (p.consumeIfSoftIdentifier("read")) {
|
|
p.emitError(loc, "'read' was removed; use 'imm'")
|
|
<< FixIt::replaceToken(loc, "imm");
|
|
}
|
|
emitError(loc) << "use of uninitialized value '" << name << "'";
|
|
diag.attachNote(other) << "declared here";
|
|
'''
|
|
got = [(m["severity"], m["message"], m["fixits"]) for m in messages.scan(cpp)]
|
|
self.assertEqual(got, [
|
|
("error", "'read' was removed; use 'imm'", [{"kind": "replaceToken", "text": "imm"}]),
|
|
("error", "use of uninitialized value '{}'", []),
|
|
("note", "declared here", []),
|
|
])
|
|
|
|
def test_token_kinds_and_prelude_parse(self):
|
|
tk = tokens.parse_token_kinds('TOK_KEYWORD(def)\nTOK_KEYWORD(fn) // Mojo extension.\nTOK_KEYWORD(__mlir_region)\n'
|
|
'TOK_PUNCTUATION(minus_greater, "->")\nTOK_LITERAL(integer) // 42\n')
|
|
self.assertEqual([k["word"] for k in tk["keywords"]], ["def", "fn", "__mlir_region"])
|
|
self.assertEqual(tk["keywords"][1]["note"], "Mojo extension.")
|
|
self.assertTrue(tk["keywords"][2]["internal"])
|
|
self.assertEqual(tk["punctuation"], [{"name": "minus_greater", "spelling": "->"}])
|
|
pre = tokens.parse_prelude("from std.a import (\n Int,\n String, # text\n)\nfrom std.b import input, print\nfrom std.c import x as y\n")
|
|
self.assertEqual(pre, ["Int", "String", "input", "print", "y"])
|
|
self.assertIsNotNone(tokens.REMOVED.search('''emitError(getToken().getLoc(), "'fn' has been removed; use 'def' instead")'''))
|
|
|
|
def test_syntax_assertions_become_cases(self):
|
|
text = '# SYNTAX TEST "source.mojo" "x"\nvar foo = 1\n# <- storage.modifier\n# ^^^ variable.other - keyword\n'
|
|
cases = syntax.parse_cases(text, "t.mojo")
|
|
self.assertEqual([(c["slice"], c["scopes"], c["notScopes"]) for c in cases],
|
|
[("v", ["storage.modifier"], []), ("foo", ["variable.other"], ["keyword"])])
|
|
self.assertEqual(syntax.parse_cases("x = 1\n# <~~- a\n", "t")[0]["slice"], "=") # two '~' shift to column 2
|
|
|
|
def test_release_sections_and_skill_table(self):
|
|
md = "## Removed {#removed}\n\n- Removed `fn`. Use `def`.\n- Removed\n `alias`, too.\n\n## Fixed\n\n- A bug.\n"
|
|
secs = release.sections(md)
|
|
self.assertEqual([(s["title"], s["anchor"], s["bullets"]) for s in secs],
|
|
[("Removed", "removed", ["Removed `fn`.", "Removed `alias`, too."]), ("Fixed", "", ["A bug."])])
|
|
rows = skill.table_after("## Removed syntax\n\n| Old | New |\n|---|---|\n| `fn` | `def` |\n\n## Next\n", "Removed syntax")
|
|
self.assertEqual(rows, [["Old", "New"], ["`fn`", "`def`"]])
|
|
|
|
def test_vscode_json_with_comments_and_trailing_commas(self):
|
|
self.assertEqual(editor.loads_jsonc('{\n // c\n "a": "//not a comment", /* b */ "x": [1, 2,],\n}'),
|
|
{"a": "//not a comment", "x": [1, 2]})
|
|
|
|
|
|
# --------------------------------------------------------------------------
|
|
class GodboltTests(unittest.TestCase):
|
|
def test_run_requests_match_mojo_quest(self):
|
|
b = godbolt.request_body("def main(): pass", "run", stdin="1\n")
|
|
self.assertEqual(b["options"]["compilerOptions"], {"executorRequest": True, "skipAsm": True})
|
|
self.assertEqual(b["options"]["executeParameters"], {"args": [], "stdin": "1\n"})
|
|
self.assertEqual(b["lang"], "mojo")
|
|
self.assertTrue(godbolt.request_body("x", "both")["options"]["filters"]["execute"])
|
|
with self.assertRaises(PipelineError):
|
|
godbolt.request_body("x", "jit")
|
|
|
|
def test_answers_normalise_to_one_shape(self):
|
|
run = godbolt.normalise("run", {"buildResult": {"code": 0, "stderr": [{"text": "warning: w"}]}, "didExecute": True,
|
|
"code": 0, "stdout": [{"text": "hi"}, {"text": "there"}], "stderr": [], "timedOut": False})
|
|
self.assertEqual((run["compiled"], run["executed"], run["exitCode"], run["stdout"], run["buildStderr"]),
|
|
(True, True, 0, "hi\nthere", "warning: w"))
|
|
both = godbolt.normalise("both", {"code": 0, "stderr": [], "asm": [{"text": "add:", "source": None}, {"text": " ret", "source": {"line": 2}}],
|
|
"execResult": {"didExecute": True, "code": 0, "stdout": [{"text": "5"}], "stderr": []}})
|
|
self.assertEqual(both["asm"], [{"text": "add:", "line": None}, {"text": " ret", "line": 2, "file": None}])
|
|
marked = godbolt.normalise("asm", {"code": 0, "asm": [{"text": " lea", "source": {"file": "/lib/io.mojo", "line": 218, "mainsource": False}}]})
|
|
self.assertEqual(marked["asm"], [{"text": " lea", "line": 218, "file": "/lib/io.mojo", "main": False}])
|
|
self.assertEqual((both["stdout"], both["returnedAsm"], both["returnedExec"]), ("5", True, True))
|
|
|
|
|
|
# --------------------------------------------------------------------------
|
|
class VerifyTests(unittest.TestCase):
|
|
def item(self, **kw):
|
|
base = dict(id="t/x", corpus="t", path=Path("x.mojo"), source="def main():\n pass\n", expect={"kind": "pass", "check": {}})
|
|
base.update(kw)
|
|
return verify.Item(**base)
|
|
|
|
@staticmethod
|
|
def golden(**r):
|
|
res = {"compiled": True, "executed": True, "exitCode": 0, "stdout": "", "stderr": "", "timedOut": False, "buildStderr": ""}
|
|
res.update(r)
|
|
return {"result": res}
|
|
|
|
def test_output_is_normalised_like_mojo_quest(self):
|
|
self.assertEqual(verify.normalize_output("a \r\nb\t\n\n\n"), "a\nb")
|
|
|
|
def test_pass_and_fail_expectations(self):
|
|
it = self.item(expect={"kind": "pass", "check": {"stdout": "3"}})
|
|
self.assertEqual(verify.judge(it, self.golden(stdout="3\n"))[0], "pass")
|
|
self.assertEqual(verify.judge(it, self.golden(stdout="4"))[0], "fail")
|
|
self.assertEqual(verify.judge(it, self.golden(compiled=False))[1], "did not compile")
|
|
self.assertIn("exit code 1", verify.judge(it, self.golden(exitCode=1))[1])
|
|
self.assertEqual(verify.judge(it, None)[0], "unverified")
|
|
starter = self.item(expect={"kind": "fail", "check": {"stdout": "3"}})
|
|
self.assertEqual(verify.judge(starter, self.golden(stdout="2"))[0], "pass")
|
|
self.assertEqual(verify.judge(starter, self.golden(stdout="3"))[0], "fail")
|
|
|
|
def test_source_patterns_use_javascript_flags(self):
|
|
it = self.item(source='def f():\n """Doc."""\n', expect={"kind": "pass", "check": {"patterns": ['"""[\\s\\S]+?"""'], "flags": ""}})
|
|
self.assertEqual(verify.judge(it, self.golden())[0], "pass")
|
|
it2 = self.item(source="DEF f", expect={"kind": "pass", "check": {"patterns": ["def"], "flags": "i"}})
|
|
self.assertEqual(verify.judge(it2, self.golden())[0], "pass")
|
|
|
|
def test_compile_errors_must_appear_at_their_lines(self):
|
|
it = self.item(expect={"kind": "compile-error", "messages": [
|
|
{"line": 3, "message": "use of uninitialized value 'x'", "regex": False},
|
|
{"line": 5, "message": "value of type '{{[A-Za-z]+}}' cannot be", "regex": True}]})
|
|
stderr = ("/app/example.mojo:3:11: error: use of uninitialized value 'x'\n"
|
|
"/app/example.mojo:5:1: error: value of type 'Loud' cannot be implicitly copied\n")
|
|
self.assertEqual(verify.judge(it, self.golden(compiled=False, buildStderr=stderr))[0], "pass")
|
|
self.assertEqual(verify.judge(it, self.golden(compiled=False, buildStderr=stderr.replace(":3:", ":4:")))[0], "fail")
|
|
self.assertEqual(verify.judge(it, self.golden(compiled=True))[0], "fail")
|
|
|
|
def test_golden_keys_change_with_anything_that_changes_the_answer(self):
|
|
a = self.item()
|
|
k = a.key("mojo_1_1_0")
|
|
self.assertEqual(k, self.item().key("mojo_1_1_0"))
|
|
self.assertNotEqual(k, a.key("mojo_1_2_0"))
|
|
self.assertNotEqual(k, self.item(stdin="1").key("mojo_1_1_0"))
|
|
self.assertNotEqual(k, self.item(args="-O0").key("mojo_1_1_0"))
|
|
self.assertNotEqual(k, self.item(mode="asm").key("mojo_1_1_0"))
|
|
self.assertNotEqual(k, self.item(source="def main():\n pass \n").key("mojo_1_1_0"))
|
|
|
|
def test_headers_set_stdin_mode_args_and_expectations(self):
|
|
h = verify.parse_header("# probe: q\n# stdin: 1\\n2\\n\n# mode: both\n# args: -O0\n# expect-stdout: a\n# expect-stdout: b\ndef main(): pass\n# probe: not a header\n")
|
|
self.assertEqual((h["probe"], h["stdin"], h["mode"], h["args"], h["expect-stdout"]), ("q", "1\n2\n", "both", "-O0", ["a", "b"]))
|
|
with tempfile.TemporaryDirectory() as d:
|
|
p = Path(d) / "p.mojo"
|
|
p.write_text("# expect-stdout: 3\ndef main():\n print(3)\n")
|
|
self.assertEqual(verify.header_item(p, "local", Path(d)).expect, {"kind": "pass", "check": {"stdout": "3"}})
|
|
p.write_text("# expect: sometimes\n")
|
|
with self.assertRaises(PipelineError):
|
|
verify.header_item(p, "local", Path(d))
|
|
|
|
|
|
# --------------------------------------------------------------------------
|
|
class CommittedCorpusTests(unittest.TestCase):
|
|
"""The corpus in the repository is whole: every file a manifest names exists, ids are unique."""
|
|
|
|
KINDS = {"pass", "fail", "compile-error", "observe", "skip"}
|
|
|
|
def test_manifests_are_consistent(self):
|
|
corpus = common.CORPUS
|
|
if not (corpus / "report.json").exists():
|
|
self.skipTest("no harvested corpus")
|
|
for name in ("quest", "manual", "diagnostics"):
|
|
mf = read_json(corpus / name / "manifest.json")
|
|
ids = [i["id"] for i in mf["items"]]
|
|
self.assertEqual(len(ids), len(set(ids)), name)
|
|
for it in mf["items"]:
|
|
self.assertTrue((corpus / name / it["file"]).exists(), it["file"])
|
|
self.assertIn(it["expect"]["kind"], self.KINDS, it["id"])
|
|
self.assertIn("path", it["provenance"], it["id"])
|
|
|
|
def test_every_probe_and_local_program_parses_and_asks_a_question(self):
|
|
items = verify.collect(["probes", "local"])
|
|
self.assertGreaterEqual(len(items), 60)
|
|
for it in items:
|
|
self.assertTrue(it.question, f"{it.id} has no '# probe:' line")
|
|
self.assertRegex(it.source, r"(?m)^(def|fn) main\(", it.id) # the fn probe keeps fn on purpose
|
|
|
|
def test_status_judges_every_item_without_the_network(self):
|
|
doc = verify.status(load_pin(), write=False)
|
|
verdicts = {"pass", "fail", "observed", "skipped", "unverified"}
|
|
self.assertTrue(all(r["verdict"] in verdicts for r in doc["items"]))
|
|
|
|
def test_strict_status_holds_our_corpora_and_reports_upstream(self):
|
|
doc = {"items": [
|
|
{"id": "manual/a", "corpus": "manual", "verdict": "fail", "why": "newer syntax"},
|
|
{"id": "local/b", "corpus": "local", "verdict": "pass", "why": ""},
|
|
{"id": "stress/c", "corpus": "stress", "verdict": "fail", "why": "printed 3"},
|
|
]}
|
|
self.assertEqual([r["id"] for r in verify.strict_failures(doc)], ["stress/c"])
|
|
self.assertEqual([r["id"] for r in verify.strict_failures(doc, verify.CORPORA_ALL)], ["manual/a", "stress/c"])
|
|
# the committed goldens: everything we wrote passes or was observed
|
|
committed = verify.status(load_pin(), write=False)
|
|
self.assertEqual(verify.strict_failures(committed), [])
|
|
|
|
def test_generated_typescript_matches_the_pin_and_corpus(self):
|
|
with tempfile.TemporaryDirectory() as d:
|
|
emit_ts.emit(load_pin(), Path(d), syntax_dir=Path(d))
|
|
for f in ("pin.ts", "tokens.ts", "editor.ts", "programs.ts", "stdlib.ts", "types.ts"):
|
|
committed = (common.GENERATED_TS / f).read_text(encoding="utf-8")
|
|
self.assertEqual((Path(d) / f).read_text(encoding="utf-8"), committed,
|
|
f"src/mojo/generated/{f} is stale; run `python3 scripts/mojo/corpus.py emit`")
|
|
self.assertEqual((Path(d) / "mojo.xml").read_text(encoding="utf-8"), (emit_ts.SYNTAX_DIR / "mojo.xml").read_text(encoding="utf-8"),
|
|
"library/syntax/mojo.xml is stale; run `python3 scripts/mojo/corpus.py emit`")
|
|
|
|
def test_the_quarto_syntax_is_well_formed_with_the_1_1_0_words(self):
|
|
import xml.etree.ElementTree as ET
|
|
root = ET.parse(emit_ts.SYNTAX_DIR / "mojo.xml").getroot()
|
|
lists = {l.get("name"): [i.text for i in l] for l in root.iter("list")}
|
|
self.assertIn("def", lists["keywords"])
|
|
self.assertNotIn("fn", lists["keywords"])
|
|
self.assertEqual(sorted(lists["removed"]), ["fn", "read"])
|
|
self.assertTrue({"mut", "out", "deinit", "imm", "var", "ref"} <= set(lists["conventions"]))
|
|
names = {c.get("name") for c in root.iter("context")}
|
|
for c in root.iter("context"):
|
|
for rule in c:
|
|
ctx = rule.get("context")
|
|
if ctx and not ctx.startswith("#"):
|
|
self.assertIn(ctx, names, f"a rule in {c.get('name')} names a missing context {ctx}")
|
|
for kw in root.iter("keyword"):
|
|
self.assertIn(kw.get("String"), lists, f"keyword rule names a missing list {kw.get('String')}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|