changeset 32:0e49648f8d74

Arbitrary function condition.
author Jeremy Thurgood <firxen@gmail.com>
date Sun, 01 Sep 2013 16:39:13 +0200
parents c62ed518e5c8
children d9b65cf72db4
files nagslang/environment.py nagslang/tests/test_environment.py
diffstat 2 files changed, 31 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/nagslang/environment.py	Sun Sep 01 16:14:35 2013 +0200
+++ b/nagslang/environment.py	Sun Sep 01 16:39:13 2013 +0200
@@ -9,6 +9,11 @@
 
 
 class AllConditions(ProtagonistCondition):
+    """Condition that is met if all provided conditions are met.
+
+    Conditions are evaluated lazily, so the first condition to fail stops
+    processing.
+    """
     def __init__(self, *conditions):
         self.conditions = conditions
 
@@ -20,6 +25,11 @@
 
 
 class AnyCondition(ProtagonistCondition):
+    """Condition that is met if any provided condition is met.
+
+    Conditions are evaluated lazily, so the first condition to pass stops
+    processing.
+    """
     def __init__(self, *conditions):
         self.conditions = conditions
 
@@ -31,16 +41,22 @@
 
 
 class WolfFormCondition(ProtagonistCondition):
+    """Condition that is met if the protagonist is in wolf form.
+    """
     def check(self, protagonist):
         return protagonist.in_wolf_form()
 
 
 class HumanFormCondition(ProtagonistCondition):
+    """Condition that is met if the protagonist is in human form.
+    """
     def check(self, protagonist):
         return protagonist.in_human_form()
 
 
 class ItemRequiredCondition(ProtagonistCondition):
+    """Condition that is met if the protagonist has the required item.
+    """
     def __init__(self, required_item):
         self.required_item = required_item
 
@@ -48,6 +64,16 @@
         return protagonist.has_item(self.required_item)
 
 
+class FunctionCondition(ProtagonistCondition):
+    """Condition that is met if the provided function returns `True`.
+    """
+    def __init__(self, func):
+        self.func = func
+
+    def check(self, protagonist):
+        return self.func(protagonist)
+
+
 class Action(object):
     """Representation of an action that can be performed.
 
--- a/nagslang/tests/test_environment.py	Sun Sep 01 16:14:35 2013 +0200
+++ b/nagslang/tests/test_environment.py	Sun Sep 01 16:39:13 2013 +0200
@@ -88,6 +88,11 @@
         self.assert_met(item, FakeProtagonist(item=True))
         self.assert_not_met(item, FakeProtagonist(item=False))
 
+    def test_function_condition(self):
+        condition = environment.FunctionCondition(lambda p: p.startswith('x'))
+        self.assert_met(condition, 'xxx')
+        self.assert_not_met(condition, 'yyy')
+
 
 class TestActions(TestCase):
     def setUp(self):