Skip to content

Commit

Permalink
Merge pull request #99 from twisted/12.go-action-parser
Browse files Browse the repository at this point in the history
Reduce the repetition in the Go action parse expression
  • Loading branch information
mithrandi committed Nov 15, 2020
2 parents e47ed95 + 851f498 commit 53fe250
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 14 deletions.
24 changes: 10 additions & 14 deletions src/imaginary/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ def parseImpl(self, instring, loc, doActions=True):



orLiterals = lambda xs: pyparsing.Or(map(pyparsing.Literal, xs))



class _ActionType(type):
actions = []
def __new__(cls, name, bases, attrs):
Expand Down Expand Up @@ -571,8 +575,7 @@ def targetTaken(player, target, container=None):


class Remove(TargetAction):
expr = ((pyparsing.Literal("remove") |
pyparsing.Literal("take off")) +
expr = (orLiterals(["remove", "take off"]) +
pyparsing.White() +
targetString("target"))

Expand Down Expand Up @@ -641,7 +644,7 @@ def do(self, player, line):
class TakeFrom(ToolAction):
actionName = "take"

expr = ((pyparsing.Literal("get") ^ pyparsing.Literal("take")) +
expr = (orLiterals(["get", "take"]) +
pyparsing.White() +
targetString("target") +
pyparsing.Optional(pyparsing.White() +
Expand Down Expand Up @@ -899,14 +902,9 @@ def do(self, player, line, direction):


class Go(Action):
expr = (
(pyparsing.Literal("go") + pyparsing.White() +
targetString("direction")) |
(pyparsing.Literal("enter") + pyparsing.White() +
targetString("direction")) |
(pyparsing.Literal("exit") + pyparsing.White() +
targetString("direction")) |
DIRECTION_LITERAL)
_goVerbs = orLiterals(["go", "enter", "exit"])
_goForm = _goVerbs + pyparsing.White() + targetString("direction")
expr = _goForm | DIRECTION_LITERAL

actorInterface = iimaginary.IThing

Expand Down Expand Up @@ -1006,9 +1004,7 @@ def do(self, player, line, target):


class Hit(TargetAction):
expr = ((pyparsing.Literal("hit") ^
pyparsing.Literal("attack") ^
pyparsing.Literal("kill")) +
expr = (orLiterals(["hit", "attack", "kill"]) +
pyparsing.White() +
pyparsing.restOfLine.setResultsName("target"))

Expand Down
36 changes: 36 additions & 0 deletions src/imaginary/test/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -783,3 +783,39 @@ def test_helpMultiword(self):
self._test("help foo bar", ["baz"], [])
finally:
Help.helpContentPath = original


def test_hit(self):
"""
The hit action removes some hitpoints from the target.
"""
self._violenceTest("hit")


def test_kill(self):
"""
The hit action has I{kill} as an alias.
"""
self._violenceTest("hit")


def test_attack(self):
"""
The hit action has I{attack} as an alias.
"""
self._violenceTest("hit")


def _violenceTest(self, verb):
fuzzy = self.world.create(u"fuzzy", proper=True)
# Flush the buffer to simplify the real assertion.
self.assertCommandOutput(
None,
["Fuzzy arrives."],
["Fuzzy arrives."],
)
self.assertCommandOutput(
verb + " fuzzy",
["You hit fuzzy for [12345] hitpoints."],
["Test Player hits fuzzy."],
)

0 comments on commit 53fe250

Please sign in to comment.