Bash · 1228 bytes Raw Blame History
1 #!/bin/sh
2 TEST_PREFIX="[defun]"
3 . "$(cd "$(dirname "$0")" && pwd)/test_harness.sh"
4
5 # defun is a fortsh-specific alternate syntax for function definitions
6
7 section "1. defun basic"
8 check_exit "defun defines a function" 'defun greet echo hello; greet' "0"
9 check_output "defun function produces output" 'defun greet echo hello; greet' "hello"
10 check_exit "defun with no args shows usage" 'defun 2>/dev/null; true' "0"
11
12 section "2. defun with arguments"
13 check_output "defun function receives args" 'defun say echo; say hello world' "hello world"
14 check_output "defun function uses dollar-1" 'defun greet echo "hi $1"; greet alice' "hi alice"
15
16 section "3. defun overwrite and unset"
17 check_output "defun overwrites existing function" 'defun f echo old; defun f echo new; f' "new"
18 check_output "unset -f removes defun function" 'defun f echo test; unset -f f; f 2>/dev/null; echo $?' "127"
19 check_output "type shows defun as function" 'defun myfunc echo hi; type myfunc 2>/dev/null | head -1' "myfunc is a function"
20
21 section "4. defun edge cases"
22 check_output "defun with special chars in body" 'defun f echo "hello world"; f' "hello world"
23 check_output "defun called multiple times" 'defun f echo ok; f; f; f' "ok
24 ok
25 ok"
26
27 print_summary