function.sh 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/bin/sh
  2. source $(dirname "$0")/test-commons.inc
  3. # Syntax ok?
  4. fn() { echo $* }
  5. # Can we invoke that?
  6. if not test "$(fn 1)" = 1 { fail cannot invoke "'fn 1'" }
  7. if not test "$(fn 1 2)" = "1 2" { fail cannot invoke "'fn 1 2'" }
  8. # With explicit argument names?
  9. fn(a) { echo $a }
  10. # Can we invoke that?
  11. if not test "$(fn 1)" = 1 { fail cannot invoke "'fn 1'" with explicit names }
  12. if not test "$(fn 1 2)" = 1 { fail cannot invoke "'fn 1 2'" with explicit names and extra arguments }
  13. # FIXME: Re-enable this when we have something akin to 'try'
  14. # or when not-enough-args isn't a hard failure.
  15. # Can it fail?
  16. # if fn 2>/dev/null {
  17. # fail "'fn'" with an explicit argument is not failing with not enough args
  18. # exit 1
  19. # }
  20. # $0 in function should be its name
  21. fn() { echo $0 }
  22. if not test "$(fn)" = fn { fail '$0' in function not equal to its name }
  23. # Ensure ARGV does not leak from inner frames.
  24. fn() {
  25. fn2 1 2 3
  26. echo $*
  27. }
  28. fn2() { }
  29. if not test "$(fn foobar)" = "foobar" { fail 'Frames are somehow messed up in nested functions' }
  30. fn(xfoo) { }
  31. xfoo=1
  32. fn 2
  33. if not test $xfoo -eq 1 { fail 'Functions overwrite parent scopes' }
  34. echo PASS