#!/bin/bash

[ -f testing.sh ] && . testing.sh

#testing "name" "command" "result" "infile" "stdin"

testing "xargs" "xargs && echo yes" "hello\nyes\n" "" "hello"
testing "spaces" "xargs" \
	"one two three four\n" "" "one two\tthree  \nfour\n\n"

testing "-n 0" "xargs -n 0 2>/dev/null || echo ok" "ok\n" \
	"" "one \ntwo\n three"
testing "-n 2" "xargs -n 2" "one two\nthree\n" "" "one \ntwo\n three"
testing "-n exact match" "xargs -n 3" "one two three\n" "" "one two three"
testing "xargs2" "xargs -n2" "one two\nthree four\nfive\n" "" \
	"one two three four five"
testing "-s too long" "xargs -s 9 echo 2>/dev/null || echo ok" \
	"one\ntwo\nok\n" "" "one two three"
testing "-s 13" "xargs -s 13 echo" "one two\nthree\n" "" "one \ntwo\n three"
testing "-s 12" "xargs -s 12 echo" "one\ntwo\nthree\n" "" "one \ntwo\n three"

touch one two three
testing "command -opt" "xargs -n2 ls -1" "one\ntwo\nthree\n" "" \
	"one two three"
rm one two three

testing "-0 -n1" "printf 'a\0b\0c\0d\0e\0f' | xargs -0 -n1 echo _" "_ a\n_ b\n_ c\n_ d\n_ e\n_ f\n" "" ""
testing "-0 -n2" "printf 'a\0b\0c\0d\0e\0f' | xargs -0 -n2 echo _" "_ a b\n_ c d\n_ e f\n" "" ""

testing "-t" "xargs -t 2>stderr ; cat stderr ; rm stderr" "one two\necho one two \n" "" "one\ntwo\n"

testing "-E END" "xargs -E END" "a b\n" "" "a\nb\nEND\nc\nd\n"

testing "-r" "xargs -r echo x" "" "" ""

# TODO: what exactly is -x supposed to do? why does coreutils output "one"?
#testing "-x" "xargs -x -s 9 || echo expected" "one\nexpected\n" "" "one\ntwo\nthree"

# TODO: test for -L? what exactly is the difference between -n and -L?

#testing "-n exact match"
#testing "-s exact match"
#testing "-s 0"
#testing "-s impossible"

# xargs command_not_found - returns 127
# xargs false - returns 1