-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_examples.py
More file actions
102 lines (87 loc) · 3.63 KB
/
Copy pathtest_examples.py
File metadata and controls
102 lines (87 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env python3
"""Her örnek koşuyor mu, ve başındaki yorum gerçekten onun çıktısı mı.
`examples/` bir öğretme dizini, ve bir öğretme dizininin en kötü hâli
*eskimiş* olanıdır: komut değişir, çıktı değişir, yorum olduğu yerde kalır, ve
dili ilk kez okuyan kişi çalışmayan bir şeyi kopyalar.
Her dosyanın başındaki yorum bloğu komutu ve beklenen çıktıyı taşıyor:
// $ zerdali run 01-hello.zd
// merhaba, Zerdali
Bu test onları ayrıştırıp gerçekten koşturuyor. Yani örneklerdeki her satır
ölçülüyor, ve bir daha elle güncellenmesi gereken bir yer değil.
Run: python3 test_examples.py
"""
import glob
import os
import re
import subprocess
import sys
HERE = os.path.dirname(os.path.abspath(__file__))
EXAMPLES = os.path.join(HERE, "examples")
def blocks(text: str):
"""Yorumdaki her `$ ...` ve onu izleyen çıktı satırları."""
lines = [l[2:].lstrip() if l.startswith("//") else None
for l in text.splitlines()]
out, cmd, expect = [], None, []
for line in lines:
if line is None:
break
if line.startswith("$ "):
if cmd:
out.append((cmd, expect))
cmd, expect = line[2:], []
elif cmd is not None:
if line == "":
if expect:
out.append((cmd, expect)); cmd, expect = None, []
else:
expect.append(line)
if cmd:
out.append((cmd, expect))
return out
def run(cmd: str):
# `zerdali` yerine bu ağacın yorumlayıcısı, ve `examples/` içinden: yorumdaki
# komut kullanıcının yazacağı komut, koşturulan onun bu depodaki karşılığı.
line = cmd.replace("zerdali ", f"{sys.executable} ../zerdali.py ", 1)
# Boru varsa kabuk: bir örnekteki komut kullanıcının yazacağı komut, ve
# kullanıcı `| tail -1` yazıyor. Kendi metnimiz, dışarıdan gelen bir girdi
# değil.
if "|" in line:
r = subprocess.run(line, cwd=EXAMPLES, capture_output=True, timeout=300,
shell=True)
else:
r = subprocess.run(line.split(), cwd=EXAMPLES, capture_output=True,
timeout=300)
return (r.stdout + r.stderr).decode("utf-8", "replace")
def main() -> int:
bad, checked = [], 0
for path in sorted(glob.glob(os.path.join(EXAMPLES, "*.zd"))):
name = os.path.basename(path)
text = open(path, encoding="utf-8").read()
# Her örnek denetimden geçmeli, yorumu olsun olmasın.
out = run(f"zerdali check {name}")
if "0 error(s)" not in out:
bad.append(f"{name}: check -> {out.strip().splitlines()[0]}")
continue
for cmd, expect in blocks(text):
if not expect:
continue
checked += 1
got = run(cmd)
# Yorumda `...` ile kısaltılmış satırlar var: baş kısmı eşleşsin.
for want in expect:
head = want.split("...")[0].rstrip()
if head not in got:
bad.append(f"{name}: `{cmd}`\n bekleniyor: {want}\n"
f" görülen: {got.strip().splitlines()[0] if got.strip() else '(boş)'}")
break
print()
for b in bad:
print(f"FAIL {b}")
n = len(glob.glob(os.path.join(EXAMPLES, "*.zd")))
if bad:
print(f"\n{len(bad)} örnek yorumundaki komut ya da çıktı gerçekle uyuşmuyor")
return 1
print(f"{n} örnek denetleniyor; {checked} komut, hepsi yorumundaki çıktıyı veriyor")
return 0
if __name__ == "__main__":
raise SystemExit(main())