Began adding test scripts and inventory common

master
Mike Gerwitz 2011-08-22 19:04:28 -04:00
parent 3407ef95f9
commit a320badb3a
4 changed files with 104 additions and 0 deletions

0
items/.empty 100644
View File

33
test/common 100644
View File

@ -0,0 +1,33 @@
#!/bin/bash
lastgiven=''
lastexpected=''
last()
{
lastgiven="$1"
lastexpected="$2"
}
assert-equal()
{
last "$1" "$2"
[ "$1" == "$2" ]
}
assert-eq()
{
last "$1" "$2"
[ $1 -eq $2 ]
}
fail()
{
echo "FAILURE: $1" >&2
echo "Expected '$lastexpected', but received '$lastgiven'" >&2
echo >&2
}

View File

@ -0,0 +1,37 @@
#!/bin/bash
mypath=$( dirname $0 )
. "$mypath/common"
. "$mypath/../util/common-inv"
#expected values
eid=_foo
ename="Large Ugly Foobar"
ecount=5
# test item tag
item="$ecount:$eid"
# let's write a temporary item for testing
itemfile="$mypath/../items/_foo"
cat > $itemfile <<EOF
NAME $ename
EOF
assert-equal "$( inv-item-id "$item" )" "$eid" || {
fail "Unable to retrieve item id"
}
assert-eq $( inv-item-count "$item" ) $ecount || {
fail "Unable to retrieve item count"
}
assert-equal "$( inv-item-name "$item" )" "$ename" || {
fail "Unable to retrieve item name"
}
# we're done with our test item
rm $itemfile

34
util/common-inv 100644
View File

@ -0,0 +1,34 @@
#!/bin/bash
itemdir="$( dirname $0 )/../items"
##
# Retrieve name of item from item descriptor file
#
# @param string item tag
#
# @output full item name
##
inv-item-name()
{
local id=$( inv-item-id "$1" )
# we're looking for the NAME line, in the format of "NAME <full name>"
cat "$itemdir/$id" <<< "$1" \
| grep '^NAME ' \
| cut -d' ' -f2-
}
inv-item-id()
{
cut -d':' -f2 <<< "$1"
}
inv-item-count()
{
cut -d':' -f1 <<< "$1"
}