def test_exclude(self): for excl, tags in self._incl_excl_data: builder = TagStatisticsBuilder(Criticality(), [], excl) builder.add_test(TestCase(status='PASS', tags=tags)) matcher = MultiMatcher(excl) expected = [tag for tag in tags if not matcher.match(tag)] assert_equals([s.name for s in builder.stats], sorted(expected))
def test_include(self): for incl, tags in self._incl_excl_data: builder = TagStatisticsBuilder(Criticality(), incl, []) builder.add_test(TestCase(status='PASS', tags=tags)) matcher = MultiMatcher(incl, match_if_no_patterns=True) expected = [tag for tag in tags if matcher.match(tag)] assert_equal([s.name for s in builder.stats], sorted(expected))
def test_exclude(self): for excl, tags in self._incl_excl_data: builder = TagStatisticsBuilder(excluded=excl) builder.add_test(TestCase(status='PASS', tags=tags)) matcher = MultiMatcher(excl) expected = [tag for tag in tags if not matcher.match(tag)] assert_equal([s.name for s in builder.stats], sorted(expected))
def test_include(self): for incl, tags in self._incl_excl_data: builder = TagStatisticsBuilder(Criticality(), incl, []) builder.add_test(TestCase(status='PASS', tags=tags)) matcher = MultiMatcher(incl, match_if_no_patterns=True) expected = [tag for tag in tags if matcher.match(tag)] assert_equals([s.name for s in builder.stats], sorted(expected))
def __init__(self, include=None, exclude=None, combine=None, docs=None, links=None): self.stats = NormalizedDict(ignore=['_']) self._include = MultiMatcher(include, ignore=['_']) self._exclude = MultiMatcher(exclude, ignore=['_']) self._combine = combine or [] info = TagStatInfo(docs or [], links or []) self._get_doc = info.get_doc self._get_links = info.get_links
def __init__(self, expand_keywords): self.matched_ids = [] if not expand_keywords: expand_keywords = [] elif not is_list_like(expand_keywords): expand_keywords = [expand_keywords] names = [n[5:] for n in expand_keywords if n[:5].lower() == 'name:'] tags = [p[4:] for p in expand_keywords if p[:4].lower() == 'tag:'] self._match_name = MultiMatcher(names).match self._match_tags = MultiMatcher(tags).match_any
class TagStatistics: def __init__(self, include=None, exclude=None, combine=None, docs=None, links=None): self.stats = NormalizedDict(ignore=['_']) self._include = MultiMatcher(include, ignore=['_']) self._exclude = MultiMatcher(exclude, ignore=['_']) self._combine = combine or [] info = TagStatInfo(docs or [], links or []) self._get_doc = info.get_doc self._get_links = info.get_links def add_test(self, test, critical): self._add_tags_statistics(test, critical) self._add_combined_statistics(test) def _add_tags_statistics(self, test, critical): for tag in test.tags: if not self._is_included(tag): continue if tag not in self.stats: self.stats[tag] = TagStat(tag, self._get_doc(tag), self._get_links(tag), critical.is_critical(tag), critical.is_non_critical(tag)) self.stats[tag].add_test(test) def _is_included(self, tag): if self._include and not self._include.match(tag): return False return not self._exclude.match(tag) def _add_combined_statistics(self, test): for pattern, name in self._combine: name = name or pattern if name not in self.stats: self.stats[name] = TagStat(name, self._get_doc(name), self._get_links(name), combined=pattern) if TagPatterns(pattern).match(test.tags): self.stats[name].add_test(test) def serialize(self, serializer): serializer.start_tag_stats(self) for stat in sorted(self.stats.values()): stat.serialize(serializer) serializer.end_tag_stats(self) def sort(self): for stat in self.stats.values(): stat.tests.sort()
def show(self, *names): if MultiMatcher(names, match_if_no_patterns=True).match('intro'): self._show_intro(self._libdoc) if self._libdoc.inits: self._show_inits(self._libdoc) for kw in self._keywords.search(names): self._show_keyword(kw)
class FlattenKeywordMatcher(object): def __init__(self, flattened): self._types = [] names = self._yield_names_and_set_types(flattened, self._types) self._name_matcher = MultiMatcher(names) def _yield_names_and_set_types(self, flattened, types): if isinstance(flattened, basestring): flattened = [flattened] for flat in flattened: upper = flat.upper() if upper in ('FOR', 'FORITEM'): types.append(flat.lower()) elif upper.startswith('NAME:'): yield flat[5:] else: raise DataError("Expected 'FOR', 'FORITEM', or " "'NAME:<pattern>' but got '%s'." % flat) def match_name(self, kwname, libname=None): name = '%s.%s' % (libname, kwname) if libname else kwname return self._name_matcher.match(name) def match_type(self, kwtype): return kwtype in self._types
def test_len(self): assert_equal(len(MultiMatcher()), 0) assert_equal(len(MultiMatcher([])), 0) assert_equal(len(MultiMatcher(['one', 'two'])), 2) assert_equal(len(MultiMatcher(regexp=True)), 0) assert_equal(len(MultiMatcher([], regexp=True)), 0) assert_equal(len(MultiMatcher(['one', 'two'], regexp=True)), 2)
class FlattenByNameMatcher(object): def __init__(self, flatten): if not is_list_like(flatten): flatten = [flatten] names = [n[5:] for n in flatten if n[:5].lower() == 'name:'] self._matcher = MultiMatcher(names) def match(self, kwname, libname=None): name = '%s.%s' % (libname, kwname) if libname else kwname return self._matcher.match(name) def __nonzero__(self): return bool(self._matcher)
class _NamePatterns(object): def __init__(self, patterns=None): self._matcher = MultiMatcher(patterns, ignore=['_']) def match(self, name, longname=None): return self._match(name) or longname and self._match_longname(longname) def _match(self, name): return self._matcher.match(name) def _match_longname(self, name): raise NotImplementedError def __nonzero__(self): return bool(self._matcher)
class NamePatterns: def __init__(self, patterns=None): self._matcher = MultiMatcher(patterns, ignore='_') def match(self, name, longname=None): return self._match(name) or longname and self._match_longname(longname) def _match(self, name): return self._matcher.match(name) def _match_longname(self, name): raise NotImplementedError def __bool__(self): return bool(self._matcher) def __iter__(self): return iter(self._matcher)
class FlattenKeywordMatcher(object): def __init__(self, flattened): self._types = [] names = self._yield_names_and_set_types(flattened, self._types) self._name_matcher = MultiMatcher(names) def _yield_names_and_set_types(self, flattened, types): if isinstance(flattened, string_types): flattened = [flattened] for flat in flattened: upper = flat.upper() if upper in ("FOR", "FORITEM"): types.append(flat.lower()) elif upper.startswith("NAME:"): yield flat[5:] else: raise DataError("Expected 'FOR', 'FORITEM', or " "'NAME:<pattern>' but got '%s'." % flat) def match(self, name, type): return self._name_matcher.match(name) or type in self._types
class FlattenKeywordMatcher(object): def __init__(self, flattened): self._types = [] names = self._yield_names_and_set_types(flattened, self._types) self._name_matcher = MultiMatcher(names) def _yield_names_and_set_types(self, flattened, types): if isinstance(flattened, basestring): flattened = [flattened] for flat in flattened: upper = flat.upper() if upper in ('FOR', 'FORITEM'): types.append(flat.lower()) elif upper.startswith('NAME:'): yield flat[5:] else: raise DataError("Expected 'FOR', 'FORITEM', or " "'NAME:<pattern>' but got '%s'." % flat) def match(self, name, type): return self._name_matcher.match(name) or type in self._types
def test_match_any(self): matcher = MultiMatcher(['H?llo', 'w*']) assert matcher.match_any(('Hi', 'world')) assert matcher.match_any(['jam', 'is', 'hillo']) assert not matcher.match_any(('no', 'match', 'here')) assert not matcher.match_any(())
def test_regexp_single_string_is_converted_to_list(self): matcher = MultiMatcher('one string', regexp=True) assert matcher.match('one string') assert not matcher.match('o') assert_equal(len(matcher), 1)
def __init__(self, flatten): if not is_list_like(flatten): flatten = [flatten] names = [n[5:] for n in flatten if n[:5].lower() == 'name:'] self._matcher = MultiMatcher(names)
def __init__(self, flattened): self.match = MultiMatcher(self._yield_patterns(flattened)).match
def __init__(self, patterns=None): self._matcher = MultiMatcher(patterns, ignore=['_'])
def __init__(self, flattened): self._types = [] names = self._yield_names_and_set_types(flattened, self._types) self._name_matcher = MultiMatcher(names)
def test_match_pattern(self): matcher = MultiMatcher(['xxx', 'f*'], ignore='.:') assert matcher.match('xxx') assert matcher.match('foo') assert matcher.match('..::FOO::..') assert not matcher.match('bar')
def search(self, patterns): matcher = MultiMatcher(patterns, match_if_no_patterns=True) for kw in self._keywords: if matcher.match(kw.name): yield kw
def __init__(self, flatten): if isinstance(flatten, basestring): flatten = [flatten] names = [n[5:] for n in flatten if n[:5].lower() == 'name:'] self._matcher = MultiMatcher(names)
def test_regexp_match_any(self): matcher = MultiMatcher(['H.llo', 'w.*'], regexp=True) assert matcher.match_any(('Hi', 'world')) assert matcher.match_any(['jam', 'is', 'hillo']) assert not matcher.match_any(('no', 'match', 'here')) assert not matcher.match_any(())
def test_match_regexp_pattern(self): matcher = MultiMatcher(['xxx', 'f.*'], ignore='_:', regexp=True) assert matcher.match('xxx') assert matcher.match('foo') assert matcher.match('__::FOO::__') assert not matcher.match('bar')
def test_do_not_match_when_no_patterns_by_default(self): assert not MultiMatcher().match('xxx')
def test_configure_to_match_when_no_patterns(self): assert MultiMatcher(match_if_no_patterns=True).match('xxx') assert MultiMatcher(match_if_no_patterns=True, regexp=True).match('xxx')
def test_iter(self): assert_equal(tuple(MultiMatcher()), ()) assert_equal(list(MultiMatcher(['1', 'xxx', '3'])), ['1', 'xxx', '3']) assert_equal(tuple(MultiMatcher(regexp=True)), ()) assert_equal(list(MultiMatcher(['1', 'xxx', '3'], regexp=True)), ['1', 'xxx', '3'])