summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/hxcases/hx_indent.py89
-rw-r--r--test/hxcases/indent-goldens.jsonl25
-rw-r--r--test/hxcases/indent.jsonl25
-rw-r--r--test/hxdiff.zig8
4 files changed, 146 insertions, 1 deletions
diff --git a/test/hxcases/hx_indent.py b/test/hxcases/hx_indent.py
new file mode 100644
index 00000000..c85c9417
--- /dev/null
+++ b/test/hxcases/hx_indent.py
@@ -0,0 +1,89 @@
+#!/usr/bin/env python3
+"""The goldens of indent.jsonl from helix itself, in a pty: $HX (else `hx`
+on PATH) with its grammars and queries ($HELIX_RUNTIME) and its defaults
+(auto-pairs on), LSP and auto-format off. They were made by the reference
+helix (docs/helix-keys.md), 278b24389's `hx` built from the genizah checkout
+with HELIX_RUNTIME its runtime/. Each case's file is named by its "path" (its
+extension picks the language) and holds its "text"; the keys are typed, then
+a marker where the cursor is, then `:wq!`. The marker's place is the cursor,
+and the anchor: every case ends in insert mode with one cursor no wider than
+helix's block (no `a` or `c` over a selection, whose anchor stays behind).
+
+ HX=... HELIX_RUNTIME=... python3 -B test/hxcases/hx_indent.py \
+ test/hxcases/indent.jsonl > test/hxcases/indent-goldens.jsonl
+"""
+import json, os, pty, select, struct, sys, tempfile, time, fcntl, termios
+
+MARK = "ยง"
+KEYS = {"ret": "\r", "esc": "\x1b", "tab": "\t", "backspace": "\x7f", "lt": "<", "gt": ">"}
+
+
+def keys(spec):
+ out, i = [], 0
+ while i < len(spec):
+ if spec[i] == "<":
+ j = spec.index(">", i)
+ out.append(KEYS[spec[i + 1 : j]])
+ i = j + 1
+ else:
+ out.append(spec[i])
+ i += 1
+ return out
+
+
+def drain(fd, secs):
+ end = time.time() + secs
+ while time.time() < end:
+ r, _, _ = select.select([fd], [], [], 0.02)
+ if r:
+ try:
+ os.read(fd, 65536)
+ except OSError:
+ return
+
+
+def run(case, home):
+ path = os.path.join(home, os.path.basename(case["path"]))
+ with open(path, "w") as f:
+ f.write(case["text"])
+ cfg = os.path.join(home, "config.toml")
+ with open(cfg, "w") as f:
+ f.write("[editor]\nauto-format = false\n[editor.lsp]\nenable = false\n")
+ pid, fd = pty.fork()
+ if pid == 0:
+ for v in ("CONFIG", "CACHE", "DATA", "STATE"):
+ os.environ["XDG_%s_HOME" % v] = home
+ os.environ["TERM"] = "xterm-256color"
+ hx = os.environ.get("HX", "hx")
+ os.execvp(hx, [hx, "-c", cfg, path])
+ fcntl.ioctl(fd, termios.TIOCSWINSZ, struct.pack("HHHH", 24, 80, 0, 0))
+ drain(fd, 1.0)
+ for k in keys(case["keys"]) + [MARK, "\x1b"]:
+ os.write(fd, k.encode())
+ drain(fd, 0.25 if k == "\x1b" else 0.05)
+ for k in ":wq!\r":
+ os.write(fd, k.encode())
+ drain(fd, 0.05)
+ for _ in range(100):
+ done, _ = os.waitpid(pid, os.WNOHANG)
+ if done:
+ break
+ drain(fd, 0.05)
+ else:
+ os.kill(pid, 9)
+ os.waitpid(pid, 0)
+ sys.exit("hx did not quit on case " + case["name"])
+ os.close(fd)
+ with open(path) as f:
+ text = f.read()
+ at = text.index(MARK)
+ row = text.count("\n", 0, at)
+ col = len(text[text.rfind("\n", 0, at) + 1 : at].encode())
+ cursor = {"row": row, "col": col}
+ return {"name": case["name"], "text": text.replace(MARK, "", 1), "mode": "insert", "cursor": cursor, "anchor": cursor}
+
+
+with tempfile.TemporaryDirectory() as home:
+ for line in open(sys.argv[1]):
+ if line.strip():
+ print(json.dumps(run(json.loads(line), home), separators=(",", ":"), ensure_ascii=False))
diff --git a/test/hxcases/indent-goldens.jsonl b/test/hxcases/indent-goldens.jsonl
new file mode 100644
index 00000000..8e1b0f21
--- /dev/null
+++ b/test/hxcases/indent-goldens.jsonl
@@ -0,0 +1,25 @@
+{"name":"zig-ret-after-opener","text":"fn f() void {\n \n}\n","mode":"insert","cursor":{"row":1,"col":4},"anchor":{"row":1,"col":4}}
+{"name":"zig-ret-inside-block","text":"fn f() void {\n a();\n \n}\n","mode":"insert","cursor":{"row":2,"col":4},"anchor":{"row":2,"col":4}}
+{"name":"zig-ret-nested-block","text":"fn f() void {\n if (x) {\n \n a();\n }\n}\n","mode":"insert","cursor":{"row":2,"col":8},"anchor":{"row":2,"col":8}}
+{"name":"zig-ret-before-closer","text":"fn f() void {\n a();\n}\n","mode":"insert","cursor":{"row":2,"col":0},"anchor":{"row":2,"col":0}}
+{"name":"zig-ret-between-braces","text":"fn f() void {\n \n}\n","mode":"insert","cursor":{"row":1,"col":4},"anchor":{"row":1,"col":4}}
+{"name":"zig-o","text":"fn f() void {\n a();\n \n}\n","mode":"insert","cursor":{"row":2,"col":4},"anchor":{"row":2,"col":4}}
+{"name":"zig-O-after-opener","text":"fn f() void {\n \n a();\n}\n","mode":"insert","cursor":{"row":1,"col":4},"anchor":{"row":1,"col":4}}
+{"name":"zig-O-before-closer","text":"fn f() void {\n a();\n \n}\n","mode":"insert","cursor":{"row":2,"col":4},"anchor":{"row":2,"col":4}}
+{"name":"zig-typed-closer-stays","text":"fn f() void {\n a();\n }\n}\n","mode":"insert","cursor":{"row":2,"col":5},"anchor":{"row":2,"col":5}}
+{"name":"py-ret-after-colon","text":"def f():\n \n pass\n","mode":"insert","cursor":{"row":1,"col":4},"anchor":{"row":1,"col":4}}
+{"name":"py-ret-inside-block","text":"def f():\n x = 1\n \n y = 2\n","mode":"insert","cursor":{"row":2,"col":4},"anchor":{"row":2,"col":4}}
+{"name":"py-ret-after-last-line","text":"def f():\n x = 1\n\n","mode":"insert","cursor":{"row":2,"col":0},"anchor":{"row":2,"col":0}}
+{"name":"py-ret-after-return","text":"def f():\n return 1\n\n","mode":"insert","cursor":{"row":2,"col":0},"anchor":{"row":2,"col":0}}
+{"name":"py-o","text":"if x:\n \n y()\n","mode":"insert","cursor":{"row":1,"col":4},"anchor":{"row":1,"col":4}}
+{"name":"py-O","text":"if x:\n \n y()\n","mode":"insert","cursor":{"row":1,"col":4},"anchor":{"row":1,"col":4}}
+{"name":"py-ret-between-brackets","text":"x = [\n \n]\n","mode":"insert","cursor":{"row":1,"col":4},"anchor":{"row":1,"col":4}}
+{"name":"rs-ret-after-opener","text":"fn f() {\n \n}\n","mode":"insert","cursor":{"row":1,"col":4},"anchor":{"row":1,"col":4}}
+{"name":"rs-ret-inside-block","text":"fn f() {\n a();\n \n}\n","mode":"insert","cursor":{"row":2,"col":4},"anchor":{"row":2,"col":4}}
+{"name":"rs-ret-before-closer","text":"fn f() {\n a();\n}\n","mode":"insert","cursor":{"row":2,"col":0},"anchor":{"row":2,"col":0}}
+{"name":"rs-ret-between-parens","text":"fn g() {\n f(\n \n );\n}\n","mode":"insert","cursor":{"row":2,"col":8},"anchor":{"row":2,"col":8}}
+{"name":"rs-o","text":"fn f() {\n a();\n \n}\n","mode":"insert","cursor":{"row":2,"col":4},"anchor":{"row":2,"col":4}}
+{"name":"rs-O","text":"fn f() {\n a();\n \n}\n","mode":"insert","cursor":{"row":2,"col":4},"anchor":{"row":2,"col":4}}
+{"name":"txt-ret-copies-indent","text":"a\n b\n \n","mode":"insert","cursor":{"row":2,"col":4},"anchor":{"row":2,"col":4}}
+{"name":"txt-ret-between-braces","text":"a\n x {\n \n }\n","mode":"insert","cursor":{"row":2,"col":8},"anchor":{"row":2,"col":8}}
+{"name":"txt-o","text":"a\n b\n \n","mode":"insert","cursor":{"row":2,"col":4},"anchor":{"row":2,"col":4}}
diff --git a/test/hxcases/indent.jsonl b/test/hxcases/indent.jsonl
new file mode 100644
index 00000000..8330badf
--- /dev/null
+++ b/test/hxcases/indent.jsonl
@@ -0,0 +1,25 @@
+{"name": "zig-ret-after-opener", "path": "/x.zig", "text": "fn f() void {\n}\n", "keys": "A<ret>"}
+{"name": "zig-ret-inside-block", "path": "/x.zig", "text": "fn f() void {\n a();\n}\n", "keys": "jA<ret>"}
+{"name": "zig-ret-nested-block", "path": "/x.zig", "text": "fn f() void {\n if (x) {\n a();\n }\n}\n", "keys": "jA<ret>"}
+{"name": "zig-ret-before-closer", "path": "/x.zig", "text": "fn f() void {\n a();}\n", "keys": "jgli<ret>"}
+{"name": "zig-ret-between-braces", "path": "/x.zig", "text": "fn f() void {}\n", "keys": "gli<ret>"}
+{"name": "zig-o", "path": "/x.zig", "text": "fn f() void {\n a();\n}\n", "keys": "jo"}
+{"name": "zig-O-after-opener", "path": "/x.zig", "text": "fn f() void {\n a();\n}\n", "keys": "jO"}
+{"name": "zig-O-before-closer", "path": "/x.zig", "text": "fn f() void {\n a();\n}\n", "keys": "jjO"}
+{"name": "zig-typed-closer-stays", "path": "/x.zig", "text": "fn f() void {\n a();\n}\n", "keys": "jo}"}
+{"name": "py-ret-after-colon", "path": "/x.py", "text": "def f():\n pass\n", "keys": "A<ret>"}
+{"name": "py-ret-inside-block", "path": "/x.py", "text": "def f():\n x = 1\n y = 2\n", "keys": "jA<ret>"}
+{"name": "py-ret-after-last-line", "path": "/x.py", "text": "def f():\n x = 1\n", "keys": "jA<ret>"}
+{"name": "py-ret-after-return", "path": "/x.py", "text": "def f():\n return 1\n", "keys": "jA<ret>"}
+{"name": "py-o", "path": "/x.py", "text": "if x:\n y()\n", "keys": "o"}
+{"name": "py-O", "path": "/x.py", "text": "if x:\n y()\n", "keys": "jO"}
+{"name": "py-ret-between-brackets", "path": "/x.py", "text": "x = []\n", "keys": "gli<ret>"}
+{"name": "rs-ret-after-opener", "path": "/x.rs", "text": "fn f() {\n}\n", "keys": "A<ret>"}
+{"name": "rs-ret-inside-block", "path": "/x.rs", "text": "fn f() {\n a();\n}\n", "keys": "jA<ret>"}
+{"name": "rs-ret-before-closer", "path": "/x.rs", "text": "fn f() {\n a();}\n", "keys": "jgli<ret>"}
+{"name": "rs-ret-between-parens", "path": "/x.rs", "text": "fn g() {\n f();\n}\n", "keys": "jglhi<ret>"}
+{"name": "rs-o", "path": "/x.rs", "text": "fn f() {\n a();\n}\n", "keys": "jo"}
+{"name": "rs-O", "path": "/x.rs", "text": "fn f() {\n a();\n}\n", "keys": "jjO"}
+{"name": "txt-ret-copies-indent", "path": "/x.txt", "text": "a\n b\n", "keys": "jA<ret>"}
+{"name": "txt-ret-between-braces", "path": "/x.txt", "text": "a\n x {}\n", "keys": "jgli<ret>"}
+{"name": "txt-o", "path": "/x.txt", "text": "a\n b\n", "keys": "jo"}
diff --git a/test/hxdiff.zig b/test/hxdiff.zig
index 5c1bc04e..010cc674 100644
--- a/test/hxdiff.zig
+++ b/test/hxdiff.zig
@@ -201,6 +201,8 @@ pub fn main(init: std.process.Init) !void {
const Case = struct {
name: []const u8,
pane: []const u8 = "file",
+ /// the file pane's name, whose extension picks its grammar
+ path: ?[]const u8 = null,
text: []const u8,
keys: []const u8,
};
@@ -303,7 +305,11 @@ fn runCase(arena: std.mem.Allocator, case: Case, pane_kind: []const u8) !Result
pump(core);
if (std.mem.eql(u8, pane_kind, "file")) {
- _ = try core.setTestFile(case.text);
+ const pane = try core.setTestFile(case.text);
+ if (case.path) |path| {
+ core.gpa.free(pane.file.?.path);
+ pane.file.?.path = try core.gpa.dupe(u8, path);
+ }
} else if (std.mem.eql(u8, pane_kind, "tty")) {
// the harness is the pty here: cook \n to \r\n the way ONLCR would
const cooked_len = std.math.add(usize, case.text.len, std.mem.count(u8, case.text, "\n")) catch return error.OutOfMemory;