116 lines
2.9 KiB
Bash
Executable File
116 lines
2.9 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
mypath=$( dirname $0 )
|
|
. "$mypath/common"
|
|
|
|
add="$mypath/../util/inv-add"
|
|
count="$mypath/../util/inv-count"
|
|
invrm="$mypath/../util/inv-rm"
|
|
invlist="$mypath/../util/inv-list"
|
|
getpath="$mypath/../util/getinvpath"
|
|
|
|
# the item _foo does not exist
|
|
$add 1:_foo 2>/dev/null && {
|
|
simplefail "Should not be permitted to add unknown items to inventory"
|
|
}
|
|
|
|
# count should be 0 if we have none of the item in the inventory
|
|
assert-equal $( $count _foo ) 0 || {
|
|
fail "Should return 0 if item is not in inventory"
|
|
}
|
|
|
|
# dummy item names
|
|
itemname1="Test Foobar"
|
|
itemname2="Test Foobar Baz"
|
|
|
|
# let's create our dummy items
|
|
itemfile="$mypath/../items/_foo"
|
|
itemfile2="$mypath/../items/_foo2"
|
|
cat > "$itemfile" <<EOF
|
|
NAME $itemname1
|
|
EOF
|
|
cat > "$itemfile2" <<EOF
|
|
NAME $itemname2
|
|
EOF
|
|
|
|
# now, let's give that another shot.
|
|
$add 1:_foo 2>/dev/null || {
|
|
simplefail "Should be able to add item to inventory if it exists"
|
|
}
|
|
|
|
# one should have been added
|
|
assert-eq $( $count _foo ) 1 || {
|
|
fail "Should add item to inventory"
|
|
}
|
|
|
|
# should be able to get location of file
|
|
assert-eq $( cat $( $getpath _foo ) | wc -l ) 1 || {
|
|
fail "Should be able to manually access file"
|
|
}
|
|
|
|
# and inventory path
|
|
assert-equal $( dirname "$( $getpath _foo )" ) $( $getpath ) || {
|
|
fail "Should return full inventory path if no item is provided"
|
|
}
|
|
|
|
# number should be required
|
|
$add _foo 2>/dev/null && {
|
|
simplefail "Item quantity should be required"
|
|
}
|
|
|
|
# let's try adding another and ensure it's appended
|
|
$add 1:_foo
|
|
assert-eq $( $count _foo ) 2 || {
|
|
fail "Items should be appended to inventory"
|
|
}
|
|
|
|
# now let's try to add multiple
|
|
$add 2:_foo
|
|
assert-eq $( $count _foo ) 4 || {
|
|
fail "Should be able to add multiple items to inventory"
|
|
}
|
|
|
|
# add a different item
|
|
$add 2:_foo2
|
|
assert-eq $( $count _foo2 ) 2 || {
|
|
fail "Distinct items have separate counts"
|
|
}
|
|
|
|
# ensure both are listed in the inventory
|
|
list=$( $invlist )
|
|
assert-contains "^_foo $itemname1$" "$list" \
|
|
&& assert-contains "^_foo2 $itemname2" "$list" || {
|
|
simplefail "Inventory list should contain both items"
|
|
}
|
|
|
|
# shouldn't contain any others
|
|
assert-eq $( wc -l <<< "$list" ) 2 || {
|
|
fail "Inventory list should only contain items in inventory"
|
|
}
|
|
|
|
# remove one item
|
|
$invrm _foo2
|
|
assert-eq $( $count _foo2 ) 1 || {
|
|
fail "Should be able to remove a single item from the inventory"
|
|
}
|
|
|
|
# remove the other
|
|
$invrm _foo2
|
|
assert-eq $( $count _foo2 ) 0 || {
|
|
fail "Should be able to remove all of one item from inventory"
|
|
}
|
|
|
|
# the file should be removed if there are no more of the item in the inventory
|
|
assert-no-exist "$( $getpath _foo2 )" || {
|
|
fail "Item inventory file should not exist when item count is 0"
|
|
}
|
|
|
|
# can we remove an item that's not in the inventory?
|
|
$invrm _foo2 2>/dev/null && {
|
|
fail "Should not be able to remove an item that isn't in the inventory"
|
|
}
|
|
|
|
# we no longer need our test item
|
|
rm "$itemfile" "$itemfile2"
|
|
|