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
scene_opendoor="opendoor"
doorspeak="Yo. I'm a door."
multiactions="
MSG foo
WAIT
GO somewhereelse
"
testscene="
TYPE input
OBJECT door
DEFAULT MSG What about the door?
ACTION invalid
ACTION open
GO $scene_opendoor
ACTION unknown
UNKNOWNCMD foo
ACTION badgo
GO
ACTION talk
MSG $doorspeak
ACTION badtalk
MSG
ACTION multi
$multiactions
STORY
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"
}
# should not allow actions on unknown objects
assert-equal "$( tryinput "open idkwhat" )" "MSG Cannot open idkwhat" || {
# actions on unknown objects should simply output an empty string
assert-equal "$( tryinput "open idkwhat" )" "" || {
simplefail "Should not be able to perform actions on unknown objects"
}
# should not allow unknown actions on known objects
assert-equal "$( tryinput "fondle door" )" "MSG Cannot fondle door" || {
# unknown actions on known objects should simply output an empty string
assert-equal "$( tryinput "fondle door" )" "" || {
simplefail "Should not be able to perform unknown actions on known objects"
}
# action requires associated command
assert-equal "$( tryinput "invalid door" )" "MSG Cannot invalid door" || {
simplefail "ACTION directive should require valid command"
}
# 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"
# all commands associated with an action should be returned (left trimmed)
expected=$( sed 's/ \+//' <<< "$multiactions" )
assert-equal "$( tryinput "multi door" )" "$expected" || {
fail "All commands for a multi-line action should be returned"
}

View File

@ -1,11 +1,8 @@
#!/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
# should be interpreted as follows:
# - MSG - Output the provided message
# - GO - Go to the scene matching the provided id
# This script does not actually execute the commands or further validate them.
#
# Copyright (C) 2011 Mike Gerwitz
#
@ -45,8 +42,9 @@ fi
action=$( cut -d' ' -f1 <<< "$words" )
object=$( cut -d' ' -f2 <<< "$words" )
# check for am action on a known object
action_data=$( awk "
# check for am action on a known object and output the associated commands, left
# trimmed
awk "
/OBJECT $object/,/^$/ {
found = 1
}
@ -62,43 +60,4 @@ action_data=$( awk "
}
" <<< "$data" \
| 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