Skip to content

Commit

Permalink
Add more unit tests on introspect module
Browse files Browse the repository at this point in the history
  • Loading branch information
Toilal committed Nov 11, 2015
1 parent 69b958e commit cda3bca
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 18 deletions.
6 changes: 2 additions & 4 deletions rebulk/introspector.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Description(object):
Abstract class for a description.
"""
@abstractproperty
def properties(self):
def properties(self): # pragma: no cover
"""
Properties of described object.
:return: all properties that described object can generate grouped by name.
Expand All @@ -34,8 +34,6 @@ def __init__(self, pattern): # pylint:disable=too-many-branches
self.pattern = pattern
self._properties = defaultdict(list)

if pattern.marker or pattern.private:
return
if pattern.properties:
for key, values in pattern.properties.items():
extend_safe(self._properties[key], values)
Expand Down Expand Up @@ -95,7 +93,7 @@ class Introspection(Description):
"""
def __init__(self, rebulk, context=None):
self.patterns = [PatternDescription(pattern) for pattern in rebulk.effective_patterns(context)
if not pattern.private]
if not pattern.private and not pattern.marker]
self.rules = [RuleDescription(rule) for rule in rebulk.effective_rules(context)]

@property
Expand Down
3 changes: 2 additions & 1 deletion rebulk/test/default_rules_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def when(self, matches, context):

class RuleAppend2(Rule):
consequence = [AppendMatch('renamed')]
properties = {'renamed': []}
properties = {'renamed': [None]}
def when(self, matches, context):
return [Match(5, 10)]

Expand All @@ -49,6 +49,7 @@ def when(self, matches, context):

class RuleAppend3(Rule):
consequence = AppendMatch('renamed')
properties = {'renamed': [None]}
def when(self, matches, context):
return [Match(5, 10)]

Expand Down
62 changes: 52 additions & 10 deletions rebulk/test/test_introspector.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# pylint: disable=no-self-use,pointless-statement,missing-docstring,protected-access,invalid-name
from .. import Rebulk
from .. import introspector
from .default_rules_module import RuleAppend2
from .default_rules_module import RuleAppend2, RuleAppend3


def test_string_introspector():
Expand Down Expand Up @@ -53,15 +53,21 @@ def test_string_properties():
assert properties['custom'] == ['One', 1]


def test_pattern_value():
def test_various_pattern():
rebulk = Rebulk()\
.regex('One', 'Two', 'Three', name='first', value="string") \
.string('1', '2', '3', name='second', value="digit") \
.string('4', '5', '6', name='third')
.string('4', '5', '6', name='third') \
.string('private', private=True) \
.functional(lambda string: (0, 5), name='func', value='test') \
.regex('One', 'Two', 'Three', name='regex_name') \
.regex('(?P<one>One)(?P<two>Two)(?P<three>Three)') \
.functional(lambda string: (6, 10), name='func2') \
.string('7', name='third')

introspected = introspector.introspect(rebulk, None)

assert len(introspected.patterns) == 3
assert len(introspected.patterns) == 8
assert len(introspected.rules) == 2

first_properties = introspected.patterns[0].properties
Expand All @@ -76,21 +82,57 @@ def test_pattern_value():
assert len(third_properties) == 1
third_properties['third'] == ['4', '5', '6']

func_properties = introspected.patterns[3].properties
assert len(func_properties) == 1
func_properties['func'] == ['test']

regex_name_properties = introspected.patterns[4].properties
assert len(regex_name_properties) == 1
regex_name_properties['regex_name'] == [None]

regex_groups_properties = introspected.patterns[5].properties
assert len(regex_groups_properties) == 3
regex_groups_properties['one'] == [None]
regex_groups_properties['two'] == [None]
regex_groups_properties['three'] == [None]

func2_properties = introspected.patterns[6].properties
assert len(func2_properties) == 1
func2_properties['func2'] == [None]

append_third_properties = introspected.patterns[7].properties
assert len(append_third_properties) == 1
append_third_properties['third'] == [None]

properties = introspected.properties
assert len(properties) == 3
assert len(properties) == 9
assert properties['first'] == first_properties['first']
assert properties['second'] == second_properties['second']
assert properties['third'] == third_properties['third']
assert properties['third'] == third_properties['third'] + append_third_properties['third']
assert properties['func'] == func_properties['func']
assert properties['regex_name'] == regex_name_properties['regex_name']
assert properties['one'] == regex_groups_properties['one']
assert properties['two'] == regex_groups_properties['two']
assert properties['three'] == regex_groups_properties['three']
assert properties['func2'] == func2_properties['func2']


def test_rule_properties():
rebulk = Rebulk().rules(RuleAppend2)
rebulk = Rebulk(default_rules=False).rules(RuleAppend2, RuleAppend3)

introspected = introspector.introspect(rebulk, None)

assert len(introspected.rules) == 3
assert len(introspected.rules) == 2
assert len(introspected.patterns) == 0

rule_properties = introspected.rules[-1].properties
rule_properties = introspected.rules[0].properties
assert len(rule_properties) == 1
rule_properties['renamed'] == []
assert rule_properties['renamed'] == [None]

rule_properties = introspected.rules[1].properties
assert len(rule_properties) == 1
assert rule_properties['renamed'] == [None]

properties = introspected.properties
assert len(properties) == 1
assert properties['renamed'] == [None]
14 changes: 11 additions & 3 deletions rebulk/test/test_rebulk.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,21 +154,29 @@ def test_rebulk_tags_names():
def func(input_string):
i = input_string.find("over")
if i > -1:
return i, i + len("over")
return i, i + len("over"), {'tags': ['custom']}

rebulk.functional(func, name="fn")

def func2(input_string):
i = input_string.find("lazy")
if i > -1:
return {'start': i, 'end': i + len("lazy"), 'tags': ['custom']}

rebulk.functional(func2, name="fn")

input_string = "The quick brown fox jumps over the lazy dog"

matches = rebulk.matches(input_string)
assert len(matches) == 3
assert len(matches) == 4

assert len(matches.named("str")) == 1
assert len(matches.named("fn")) == 1
assert len(matches.named("fn")) == 2
assert len(matches.named("false")) == 0
assert len(matches.tagged("false")) == 0
assert len(matches.tagged("first")) == 1
assert len(matches.tagged("other")) == 2
assert len(matches.tagged("custom")) == 2


def test_rebulk_rules_1():
Expand Down

0 comments on commit cda3bca

Please sign in to comment.