From 2bf35dee559b93009f156ba1cfe00bb568582857 Mon Sep 17 00:00:00 2001 From: glyh Date: Fri, 5 Apr 2024 10:43:49 +0800 Subject: [PATCH] add some macro tests --- spec/ysh-macros.test.sh | 58 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 spec/ysh-macros.test.sh diff --git a/spec/ysh-macros.test.sh b/spec/ysh-macros.test.sh new file mode 100644 index 0000000000..288024d3d4 --- /dev/null +++ b/spec/ysh-macros.test.sh @@ -0,0 +1,58 @@ +## our_shell: ysh +## oils_failures_allowed: 10000 + +#### Unless syntax sugar +macro unless (; cond ;; block) { + return (quote { + if (not (unquote cond)) { + (unquote block) + } + }) +} + +unless (1 > 2) { + echo rocks +} +## status: 0 +## STDOUT: +rocks +## END + +#### withMutex syntax sugar +proc mutexCreate() { + var mutexPath = $(mktemp) +} +macro withMutex (; ...mutexesWithBlk) { + if (len(mutexesWithBlk) == 1) { + return (mutexesWithBlk[0]) + } else { + var mutexesRest = mutexesWithBlk[1:] + var mutexCurrent = mutexesWithBlk[0] + return ( + quote { + { + if flock $fd { + unquote (withMutex (...mutexesRest)) + } else { + echo "Failed to grab the lock `$mutexCurrent`" + exit 1 + } + } {fd}<$mutexCurrent + } + ) + } +} + +var lock1 = mutexCreate +var lock2 = mutexCreate +var lock3 = mutexCreate +withMutex (lock1, lock2, lock3) { + echo rocks +} +## status: 0 +## STDOUT: +rocks +## END + + +