head.test 1.4 KB

123456789101112131415161718192021222324252627282930313233
  1. #!/bin/bash
  2. [ -f testing.sh ] && . testing.sh
  3. #testing "name" "command" "result" "infile" "stdin"
  4. testing "head, stdin" "head -n 1 && echo yes" "one\nyes\n" "" "one\ntwo"
  5. testing "head, stdin via -" "head -n 1 - && echo yes" "one\nyes\n" "" "one\ntwo"
  6. testing "head, file" "head input -n 1 && echo yes" "one\nyes\n" "one\ntwo" ""
  7. testing "-number" "head -2 input && echo yes" "one\ntwo\nyes\n" \
  8. "one\ntwo\nthree\nfour" ""
  9. testing "head, default lines" "head" "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n" "" "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12"
  10. # coreutils & busybox name stdin as "standard input", toybox uses "-"
  11. testing "-v file" "head -v -n 1 input" "==> input <==\none\n" "one\ntwo\n" ""
  12. testing "-v stdin" "head -v -n 1 | sed 's/==> standard input <==/==> - <==/'" \
  13. "==> - <==\none\n" "" "one\ntwo\n"
  14. testing "file and stdin" "head -n 1 input - | sed 's/==> standard input <==/==> - <==/'" \
  15. "==> input <==\none\n\n==> - <==\nfoo\n" "one\ntwo\n" "foo\nbar\n"
  16. echo "foo
  17. bar
  18. baz" > file1
  19. testing "head, multiple files" "head -n 2 input file1" "==> input <==\none\ntwo\n\n==> file1 <==\nfoo\nbar\n" "one\ntwo\nthree\n" ""
  20. testing "-q, multiple files" "head -q -n 2 input file1" "one\ntwo\nfoo\nbar\n" \
  21. "one\ntwo\nthree\n" ""
  22. rm file1
  23. testing "-c 3" "head -c 3" "one" "" "one\ntwo"
  24. testing "-c bigger than input" "head -c 3" "a" "" "a"
  25. testing "-c 3 -n 1" "head -c 3 -n 1" "one\n" "" "one\ntwo"
  26. testing "-n 1 -c 3" "head -n 1 -c 3" "one" "" "one\ntwo"