Add tests for has-argument and parse-argument functions

This commit is contained in:
2013-06-26 21:46:47 +02:00
parent ab7a7a10dd
commit f1d2dec645

51
test/lib/arguments-test.sh Executable file
View File

@@ -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()"