From c6e1704f0018c309f7bdb638298c20d83cc6e709 Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Sun, 29 Mar 2020 18:34:08 +0200 Subject: [PATCH] pexpect test for commandline --current-process It was not clear to me hwo this behaves when there are comments. Include a friendly helper to compute control characters. No functional change. --- build_tools/pexpect_helper.py | 22 ++++++++++++++++++++++ tests/pexpects/commandline.py | 27 ++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/build_tools/pexpect_helper.py b/build_tools/pexpect_helper.py index fdd6a2192..68d14ba62 100644 --- a/build_tools/pexpect_helper.py +++ b/build_tools/pexpect_helper.py @@ -341,3 +341,25 @@ class SpawnedProc(object): "LIGHTCYAN": ansic(96), "WHITE": ansic(97), } + + +def control(char: str) -> str: + """ Returns the char sent when control is pressed along the given key. """ + assert len(char) == 1 + char = char.lower() + if ord("a") <= ord(char) <= ord("z"): + return chr(ord(char) - ord("a") + 1) + return chr({ + "@": 0, + "`": 0, + "[": 27, + "{": 27, + "\\": 28, + "|": 28, + "]": 29, + "}": 29, + "^": 30, + "~": 30, + "_": 31, + "?": 127, + }[char]) diff --git a/tests/pexpects/commandline.py b/tests/pexpects/commandline.py index 79f95aab3..eab95106c 100644 --- a/tests/pexpects/commandline.py +++ b/tests/pexpects/commandline.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -from pexpect_helper import SpawnedProc +from pexpect_helper import SpawnedProc, control sp = SpawnedProc() send, sendline, sleep, expect_prompt, expect_re, expect_str = ( @@ -59,3 +59,28 @@ send("\b" * 64) # Commandline works when run on its own (#8807). sendline("commandline whatever") expect_re("prompt [0-9]+>whatever") + +# Test --current-process output +send(control("u")) +sendline(r"bind \cb 'set tmp (commandline --current-process)'") +expect_prompt() +send("echo process1; echo process2") +send(control("a")) +send(control("b")) +send(control("k")) +sendline("echo first process is <$tmp>") +expect_str("first process is ") + +send("echo process # comment") +send(control("a")) +send(control("b")) +send(control("k")) +sendline('echo "process extent is <$tmp>"') +expect_str("process extent is ") + +sendline(r"bind \cb 'set tmp (commandline --current-process | count)'") +sendline(r'commandline "echo line1 \\" "# comment" "line2"') +send(control("b")) +send(control("u") * 6) +sendline('echo "process spans $tmp lines"') +expect_str("process spans 3 lines")