Changeset 32:0e49648f8d74


Ignore:
Timestamp:
Sep 1, 2013, 2:39:13 PM (7 years ago)
Author:
Jeremy Thurgood <firxen@…>
Branch:
default
Message:

Arbitrary function condition.

Location:
nagslang
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • nagslang/environment.py

    r28 r32  
    1010
    1111class AllConditions(ProtagonistCondition):
     12    """Condition that is met if all provided conditions are met.
     13
     14    Conditions are evaluated lazily, so the first condition to fail stops
     15    processing.
     16    """
    1217    def __init__(self, *conditions):
    1318        self.conditions = conditions
     
    2126
    2227class AnyCondition(ProtagonistCondition):
     28    """Condition that is met if any provided condition is met.
     29
     30    Conditions are evaluated lazily, so the first condition to pass stops
     31    processing.
     32    """
    2333    def __init__(self, *conditions):
    2434        self.conditions = conditions
     
    3242
    3343class WolfFormCondition(ProtagonistCondition):
     44    """Condition that is met if the protagonist is in wolf form.
     45    """
    3446    def check(self, protagonist):
    3547        return protagonist.in_wolf_form()
     
    3749
    3850class HumanFormCondition(ProtagonistCondition):
     51    """Condition that is met if the protagonist is in human form.
     52    """
    3953    def check(self, protagonist):
    4054        return protagonist.in_human_form()
     
    4256
    4357class ItemRequiredCondition(ProtagonistCondition):
     58    """Condition that is met if the protagonist has the required item.
     59    """
    4460    def __init__(self, required_item):
    4561        self.required_item = required_item
     
    4763    def check(self, protagonist):
    4864        return protagonist.has_item(self.required_item)
     65
     66
     67class FunctionCondition(ProtagonistCondition):
     68    """Condition that is met if the provided function returns `True`.
     69    """
     70    def __init__(self, func):
     71        self.func = func
     72
     73    def check(self, protagonist):
     74        return self.func(protagonist)
    4975
    5076
  • nagslang/tests/test_environment.py

    r28 r32  
    8989        self.assert_not_met(item, FakeProtagonist(item=False))
    9090
     91    def test_function_condition(self):
     92        condition = environment.FunctionCondition(lambda p: p.startswith('x'))
     93        self.assert_met(condition, 'xxx')
     94        self.assert_not_met(condition, 'yyy')
     95
    9196
    9297class TestActions(TestCase):
Note: See TracChangeset for help on using the changeset viewer.