process-input script now simply returns commands to be executed

master
Mike Gerwitz 2011-08-24 23:35:02 -04:00
parent 520723e184
commit 23f3a04ef7
2 changed files with 22 additions and 80 deletions

View File

@ -29,22 +29,24 @@ process-input "foo bar" 2>/dev/null <<< "" && {
# test data # test data
scene_opendoor="opendoor" scene_opendoor="opendoor"
doorspeak="Yo. I'm a door." doorspeak="Yo. I'm a door."
multiactions="
MSG foo
WAIT
GO somewhereelse
"
testscene=" testscene="
TYPE input TYPE input
OBJECT door OBJECT door
DEFAULT MSG What about the door? DEFAULT MSG What about the door?
ACTION invalid
ACTION open ACTION open
GO $scene_opendoor GO $scene_opendoor
ACTION unknown ACTION unknown
UNKNOWNCMD foo UNKNOWNCMD foo
ACTION badgo ACTION multi
GO $multiactions
ACTION talk
MSG $doorspeak
ACTION badtalk
MSG
STORY STORY
This is a test scene. This is a test scene.
@ -65,38 +67,19 @@ assert-equal "$( tryinput "open door" )" "GO $scene_opendoor" || {
fail "Should be able to perform known actions on known objects" fail "Should be able to perform known actions on known objects"
} }
# should not allow actions on unknown objects # actions on unknown objects should simply output an empty string
assert-equal "$( tryinput "open idkwhat" )" "MSG Cannot open idkwhat" || { assert-equal "$( tryinput "open idkwhat" )" "" || {
simplefail "Should not be able to perform actions on unknown objects" simplefail "Should not be able to perform actions on unknown objects"
} }
# should not allow unknown actions on known objects # unknown actions on known objects should simply output an empty string
assert-equal "$( tryinput "fondle door" )" "MSG Cannot fondle door" || { assert-equal "$( tryinput "fondle door" )" "" || {
simplefail "Should not be able to perform unknown actions on known objects" simplefail "Should not be able to perform unknown actions on known objects"
} }
# action requires associated command # all commands associated with an action should be returned (left trimmed)
assert-equal "$( tryinput "invalid door" )" "MSG Cannot invalid door" || { expected=$( sed 's/ \+//' <<< "$multiactions" )
simplefail "ACTION directive should require valid command" assert-equal "$( tryinput "multi door" )" "$expected" || {
} fail "All commands for a multi-line action should be returned"
# should fail on unknown command
tryinput "unknown door" 2>/dev/null && {
simplefail "ACTION directive should fail on unknwon command"
}
# GO should require a scene
tryinput "badgo door" 2>/dev/null && {
simplefail "GO command should require a target scene"
}
# MSG should return a message
assert-equal "$( tryinput "talk door" )" "MSG $doorspeak" || {
fail "MSG command should return a message"
}
# MSG command should require an argument
tryinput "badtalk door" 2>/dev/null && {
simplefail "MSG should require output text"
} }

View File

@ -1,11 +1,8 @@
#!/bin/bash #!/bin/bash
# #
# Processes input text and outputs result # Processes input text and outputs commands to be executed as a result
# #
# This script will return one of two commands with an associated argument, which # This script does not actually execute the commands or further validate them.
# should be interpreted as follows:
# - MSG - Output the provided message
# - GO - Go to the scene matching the provided id
# #
# Copyright (C) 2011 Mike Gerwitz # Copyright (C) 2011 Mike Gerwitz
# #
@ -45,8 +42,9 @@ fi
action=$( cut -d' ' -f1 <<< "$words" ) action=$( cut -d' ' -f1 <<< "$words" )
object=$( cut -d' ' -f2 <<< "$words" ) object=$( cut -d' ' -f2 <<< "$words" )
# check for am action on a known object # check for am action on a known object and output the associated commands, left
action_data=$( awk " # trimmed
awk "
/OBJECT $object/,/^$/ { /OBJECT $object/,/^$/ {
found = 1 found = 1
} }
@ -62,43 +60,4 @@ action_data=$( awk "
} }
" <<< "$data" \ " <<< "$data" \
| sed 's/^[ \t]\+//' | sed 's/^[ \t]\+//'
)
# if we were unable to find the associated object/action, then let the user know
# that they've performed an invalid action
if [ ! "$action_data" ]; then
echo "MSG Cannot $words"
exit 0
fi
# what type of command should we execute?
cmd=$( cut -d' ' -f1 <<< "$action_data" )
arg=$( cut -s -d' ' -f2- <<< "$action_data" )
case "$cmd" in
GO)
if [ ! "$arg" ]; then
echo "ERROR: GO command requires target scene" >&2
exit 4
fi
# the action will be to go to the target scene
echo "GO $arg"
;;
MSG)
if [ ! "$arg" ]; then
echo "ERROR: MSG requires an output string" >&2
exit 4
fi
# the MSG action will indicate that a message should be output
echo "MSG $arg"
;;
*)
echo "ERROR: Unknown command '$cmd' for action: $words" >&2
exit 4
;;
esac