diff --git a/test/lib/arguments-test.sh b/test/lib/arguments-test.sh new file mode 100755 index 0000000..a812193 --- /dev/null +++ b/test/lib/arguments-test.sh @@ -0,0 +1,51 @@ +#! /usr/bin/env bash +source "../assert.sh" + +source "../../src/lib/arguments.sh" + + +# +# has-argument() +# + +# Returns 1 when it does not have argument. +assert_raises 'has-argument help h -v -f --debug' 1 + +# Returns 0 when any form of the argument is present. +assert_raises 'has-argument help h -v --help' 0 +assert_raises 'has-argument help h -v --help=wtf' 0 +assert_raises 'has-argument help h -v -h' 0 +assert_raises 'has-argument help h -v -h=wtf' 0 + +assert_end "has-argument()" + + +# +# parse-argument() +# + +# Echos nothing and returns 1 when argument is not present. +assert 'parse-argument file f -v --debug' "" +assert_raises 'parse-argument file f -v --debug' 1 + +# echos value of argument and returns 0 when any form of argument is present. +assert 'parse-argument file f -v --file foo/bar.txt' "foo/bar.txt" +assert_raises 'parse-argument file f -v --file foo/bar.txt' 0 +assert 'parse-argument file f -v --file=foo/bar.txt' "foo/bar.txt" +assert_raises 'parse-argument file f -v --file=foo/bar.txt' 0 +assert 'parse-argument file f -v -f foo/bar.txt' "foo/bar.txt" +assert_raises 'parse-argument file f -v -f foo/bar.txt' 0 +assert 'parse-argument file f -v -f=foo/bar.txt' "foo/bar.txt" +assert_raises 'parse-argument file f -v -f=foo/bar.txt' 0 + +# Value with a space. +assert 'parse-argument file f -v --file foo\ bar.txt' "foo bar.txt" +assert 'parse-argument file f -v --file "foo bar.txt"' "foo bar.txt" +assert "parse-argument file f -v --file 'foo bar.txt'" "foo bar.txt" + +assert 'parse-argument file f -v --file=foo\ bar.txt' "foo bar.txt" +assert 'parse-argument file f -v --file="foo bar.txt"' "foo bar.txt" +assert "parse-argument file f -v --file='foo bar.txt'" "foo bar.txt" + + +assert_end "parse-argument()"