diff --git a/test/common b/test/common index 0d61ec2..4c140cc 100644 --- a/test/common +++ b/test/common @@ -1,5 +1,16 @@ #!/bin/bash +# init the "game" and profile +export GSGP_ID=gsgptest +export GSGP_TITLE="Test Suite" +export GSGP_PROFILE=TEST + +. "$( dirname $0 )/../util/common" + +# annihilate profile to start fresh +rm -rf "$GSGP_PROFILE_PATH" +mkdir -p "$GSGP_PROFILE_PATH" + lastgiven='' lastexpected='' @@ -24,10 +35,15 @@ assert-eq() } -fail() +simplefail() { echo "FAILURE: $1" >&2 +} + + +fail() +{ + simplefail "$1" echo "Expected '$lastexpected', but received '$lastgiven'" >&2 echo >&2 } - diff --git a/test/test-inv b/test/test-inv new file mode 100755 index 0000000..e219fa6 --- /dev/null +++ b/test/test-inv @@ -0,0 +1,54 @@ +#!/bin/bash + +mypath=$( dirname $0 ) +. "$mypath/common" + +add="$mypath/../util/inv-add" +count="$mypath/../util/inv-count" + +# 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" +} + +# let's create our dummy item +itemfile="$mypath/../items/_foo" +cat > "$itemfile" <<'EOF' +NAME Test Foobar +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-equal $( $count _foo ) 1 || { + fail "Should add item to inventory" +} + +# 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-equal $( $count _foo ) 2 || { + fail "Items should be appended to inventory" +} + +# now let's try to add multiple +$add 2:_foo +assert-equal $( $count _foo ) 4 || { + fail "Should be able to add multiple items to inventory" +} + +# we no longer need our test item +rm "$itemfile" + diff --git a/util/common b/util/common index 7095e64..c979675 100644 --- a/util/common +++ b/util/common @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash if [ ! "$GSGP_ID" ]; then echo "ID unavailable" >&2 @@ -9,3 +9,9 @@ if [ ! "$GSGP_PROFILE" ]; then echo "Profile unavailable" >&2 exit 2 fi + +# path to current profile +export GSGP_PROFILE_PATH="$HOME/.$GSGP_ID/profiles/$GSGP_PROFILE" + +# create path in case it does not yet exist +mkdir -p "$GSGP_PROFILE_PATH" 2>/dev/null diff --git a/util/common-inv b/util/common-inv index 5ee0f67..bec2070 100644 --- a/util/common-inv +++ b/util/common-inv @@ -1,4 +1,24 @@ #!/bin/bash +# +# Common inventory commands +# +# Copyright (C) 2011 Mike Gerwitz +# +# This file is part of gsgp. This program is free software: you can +# redistribute it and/or modify it under the terms of the GNU General Public +# License as published by the Free Software Foundation, either version 3 of +# the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +## + +. "$mypath/common" itemdir="$( dirname $0 )/../items" diff --git a/util/getinvpath b/util/getinvpath new file mode 100755 index 0000000..1380c1b --- /dev/null +++ b/util/getinvpath @@ -0,0 +1,6 @@ +#!/bin/bash + +. "$( dirname $0 )/common" + +echo "$GSGP_PROFILE_PATH/_inv" + diff --git a/util/inv-add b/util/inv-add new file mode 100755 index 0000000..cc6fbdf --- /dev/null +++ b/util/inv-add @@ -0,0 +1,63 @@ +#!/bin/bash +# +# Adds an item to the inventory, given the item tag +# +# Copyright (C) 2011 Mike Gerwitz +# +# This file is part of gsgp. This program is free software: you can +# redistribute it and/or modify it under the terms of the GNU General Public +# License as published by the Free Software Foundation, either version 3 of +# the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +## + +mypath=$( dirname $0 ) + +. "$mypath/common-inv" + +if [ $# -lt 1 ]; then + echo "Usage: $0 ITEM" +fi + +# we should be provided with the item "tag", which identifies the item itself as +# well as additional metadata +tag="$1" + +inv_path=$( $mypath/getinvpath ) +item_path="$mypath/../items" +item_id=$( inv-item-id "$tag" ) +item_count=$( inv-item-count "$tag" ) +item_file="$item_path/$item_id" +inv_item_file="$inv_path/$item_id" + +# use -gt rather than -lt to ensure we'll still fail if $item_count is not an +# integer +[ $item_count -gt 0 ] || { + echo "Item count required" >&2 + exit 1 +} + +# does the item we're attempting to access actually exist? +[ -r "$item_file" ] || { + echo "Item does not exist" >&2 + exit 1 +} + +# attempt to create the inventory in case it does not exist +mkdir -p "$inv_path" 2>/dev/null + +# currently unused +attr='' + +# append item(s) to inventory +for i in $( seq 1 $item_count ); do + echo "$sttr" >> "$inv_item_file" +done + diff --git a/util/inv-count b/util/inv-count new file mode 100755 index 0000000..6fd8440 --- /dev/null +++ b/util/inv-count @@ -0,0 +1,40 @@ +#!/bin/bash +# +# Counts number of items in the inventory +# +# Copyright (C) 2011 Mike Gerwitz +# +# This file is part of gsgp. This program is free software: you can +# redistribute it and/or modify it under the terms of the GNU General Public +# License as published by the Free Software Foundation, either version 3 of +# the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +mypath=$( dirname $0 ) + +. "$mypath/common-inv" + +if [ $# -lt 1 ]; then + echo "Usage: $0 ITEM" +fi + +# we should be provided with the id, not the tag +item_id="$1" +inv_item_file="$( $mypath/getinvpath )/$item_id" + +# if the file does not exit, then we clearly have 0 of the item in our inventory +if [ ! -r "$inv_item_file" ]; then + echo 0 + exit +fi + +# count the number of items in the inventory +wc -l < "$inv_item_file" || echo 0 +