Bash · 1230 bytes Raw Blame History
1 #!/bin/sh
2 TEST_PREFIX="[eval]"
3 . "$(cd "$(dirname "$0")/.." && pwd)/test_harness.sh"
4
5 section "1. eval basic"
6 compare_output "eval basic echo" "eval 'echo hello'"
7 compare_output "eval with variable expansion" 'X=world; eval "echo hello $X"'
8 compare_output "eval constructs command from parts" 'CMD="echo"; ARG="hello"; eval "$CMD $ARG"'
9 compare_output "eval with multiple statements" 'eval "echo one; echo two"'
10
11 section "2. eval exit codes"
12 compare_exit "eval preserves exit code 0" "eval 'true'"
13 compare_exit "eval preserves exit code 1" "eval 'false'"
14 compare_exit "eval exit with specific code" "eval 'exit 42'"
15 compare_output "eval captures $?" 'eval "true"; echo $?'
16
17 section "3. eval compound commands"
18 compare_output "eval for loop" "eval 'for i in a b c; do echo \$i; done'"
19 compare_output "eval if statement" 'eval "if true; then echo yes; fi"'
20 compare_output "eval pipeline" 'eval "echo hello | tr h H"'
21
22 section "4. eval edge cases"
23 compare_output "eval empty string" 'eval ""; echo $?'
24 compare_output "eval variable indirection" 'name=VAR; VAR=hello; eval "echo \$$name"'
25 compare_output "eval with quoting layers" "eval 'echo '\"'\"'hello'\"'\"''"
26 compare_output "eval nested" 'eval eval "echo hello"'
27
28 print_summary