diff options
author | Přemysl Eric Janouch <p@janouch.name> | 2025-01-02 23:29:50 +0100 |
---|---|---|
committer | Přemysl Eric Janouch <p@janouch.name> | 2025-01-06 08:30:14 +0100 |
commit | e40d56152d68aa7fcf05b551e08c94d498ca9163 (patch) | |
tree | a7e3d6ca3dcf739e03bd3213029b531c5595b0fc /tools/wdye/test.lua | |
parent | 21379d4c02e85ae82c1010ca3b91dceb7dfee514 (diff) | |
download | liberty-e40d56152d68aa7fcf05b551e08c94d498ca9163.tar.gz liberty-e40d56152d68aa7fcf05b551e08c94d498ca9163.tar.xz liberty-e40d56152d68aa7fcf05b551e08c94d498ca9163.zip |
Add an Expect-like tool
This is to provide an Expect utility with a minimal dependency tree
for C-based projects. It also addresses some Tcl Expect design issues,
as perceived by me.
Diffstat (limited to 'tools/wdye/test.lua')
-rw-r--r-- | tools/wdye/test.lua | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/tools/wdye/test.lua b/tools/wdye/test.lua new file mode 100644 index 0000000..5e2fe36 --- /dev/null +++ b/tools/wdye/test.lua @@ -0,0 +1,24 @@ +for k, v in pairs(wdye) do _G[k] = v end + +-- The terminal echoes back, we don't want to read the same stuff twice. +local cat = spawn {"sh", "-c", "cat > /dev/null", environ={TERM="xterm"}} +assert(cat, "failed to spawn process") +assert(cat.term.key_left, "bad terminfo") + +cat:send("Hello\r") +local m = expect(cat:exact {"Hello\r", function (p) return p[0] end}) +assert(m == "Hello\r", "exact match failed, or value expansion mismatch") + +local t = table.pack(expect(timeout {.5, 42})) +assert(#t == 1 and t[1] == 42, "timeout match failed, or value mismatch") + +cat:send("abc123\r") +expect(cat:regex {"A(.*)3", nocase=true, function (p) + assert(p[0] == "abc123", "wrong regex group #0") + assert(p[1] == "bc12", "wrong regex group #1") +end}) + +-- Send EOF (^D), test method chaining. +cat:send("Closing...\r"):send("\004") +local v = expect(cat:eof {true}, + cat:default {.5, function (p) error "expected EOF, got a timeout" end}) |