Bash · 915 bytes Raw Blame History
1 #!/bin/sh
2 TEST_PREFIX="[shift]"
3 . "$(cd "$(dirname "$0")" && pwd)/test_harness.sh"
4
5 section "1. shift basic"
6 compare_output "shift removes first param" 'set -- a b c; shift; echo $1'
7 compare_output "shift updates count" 'set -- a b c; shift; echo $#'
8 compare_output "shift N removes N params" 'set -- a b c d e; shift 2; echo $1'
9 compare_output "shift all params" 'set -- a b c; shift 3; echo $#'
10 compare_output "shift preserves remaining" 'set -- a b c d; shift 2; echo "$@"'
11
12 section "2. shift edge cases"
13 compare_exit "shift past available params fails" 'set -- a; shift 5 2>/dev/null'
14 compare_output "shift 0 is no-op" 'set -- a b c; shift 0; echo $1'
15 compare_output "shift in loop" 'set -- a b c; while [ $# -gt 0 ]; do echo $1; shift; done'
16 compare_output "shift with no params" 'set --; shift 2>/dev/null; echo $#'
17 compare_output "multiple shifts" 'set -- a b c d e; shift; shift; echo $1 $#'
18
19 print_summary