From 80dd9297a5602a31b5d94d10fecf9c5c6d600dac Mon Sep 17 00:00:00 2001 From: Toilal Date: Sun, 18 Oct 2015 23:07:43 +0200 Subject: [PATCH] Add RemoveMatchRule and AppendMatchRule classes --- rebulk/rules.py | 25 +++++++++++++++++++++++ rebulk/test/default_rules_module.py | 24 ++++++++++++++++++++++ rebulk/test/test_rules.py | 31 +++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 rebulk/test/default_rules_module.py diff --git a/rebulk/rules.py b/rebulk/rules.py index 9ae1ab6..18b2826 100644 --- a/rebulk/rules.py +++ b/rebulk/rules.py @@ -8,6 +8,7 @@ from itertools import groupby import six +from .utils import is_iterable @six.add_metaclass(ABCMeta) @@ -70,6 +71,30 @@ def __repr__(self): return self.name if self.name else self.__class__.__name__ +class RemoveMatchRule(Rule): # pylint: disable=abstract-method + """ + Remove matches returned by then + """ + def then(self, matches, when_response, context): + if is_iterable(when_response): + for match in when_response: + matches.remove(match) + else: + matches.remove(when_response) + + +class AppendMatchRule(Rule): # pylint: disable=abstract-method + """ + Append matches returned by then + """ + def then(self, matches, when_response, context): + if is_iterable(when_response): + for match in when_response: + matches.append(match) + else: + matches.append(when_response) + + class Rules(list): """ list of rules ready to execute. diff --git a/rebulk/test/default_rules_module.py b/rebulk/test/default_rules_module.py new file mode 100644 index 0000000..a1f0516 --- /dev/null +++ b/rebulk/test/default_rules_module.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# pylint: disable=no-self-use, pointless-statement, missing-docstring, invalid-name +from ..match import Match +from ..rules import RemoveMatchRule, AppendMatchRule + + +class RuleRemove0(RemoveMatchRule): + def when(self, matches, context): + return matches[0] + + +class RuleAppend0(AppendMatchRule): + def when(self, matches, context): + return Match(5, 10) + +class RuleRemove1(RemoveMatchRule): + def when(self, matches, context): + return [matches[0]] + + +class RuleAppend1(AppendMatchRule): + def when(self, matches, context): + return [Match(5, 10)] diff --git a/rebulk/test/test_rules.py b/rebulk/test/test_rules.py index 061fd33..0bc0cb2 100644 --- a/rebulk/test/test_rules.py +++ b/rebulk/test/test_rules.py @@ -1,6 +1,7 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- # pylint: disable=no-self-use, pointless-statement, missing-docstring, invalid-name +from rebulk.test.default_rules_module import RuleRemove0, RuleAppend0, RuleAppend1, RuleRemove1 from ..rules import Rules from ..match import Matches, Match @@ -53,6 +54,36 @@ def test_rule_when(): assert matches[1] == Match(3, 4) +def test_default_rules(): + rules = Rules(RuleRemove0) + + matches = Matches([Match(1, 2)]) + rules.execute_all_rules(matches, {}) + + assert len(matches) == 0 + + rules = Rules(RuleAppend0) + + matches = Matches([Match(1, 2)]) + rules.execute_all_rules(matches, {}) + + assert len(matches) == 2 + + rules = Rules(RuleRemove1) + + matches = Matches([Match(1, 2)]) + rules.execute_all_rules(matches, {}) + + assert len(matches) == 0 + + rules = Rules(RuleAppend1) + + matches = Matches([Match(1, 2)]) + rules.execute_all_rules(matches, {}) + + assert len(matches) == 2 + + def test_rule_module(): rules = Rules(rm)