def test__add__tags(self): tags1 = Tags(['xx', 'yy']) tags2 = Tags(['zz', 'ee', 'XX']) new_tags = tags1 + tags2 assert_true(isinstance(new_tags, Tags)) assert_equal(list(tags1), ['xx', 'yy']) assert_equal(list(tags2), ['ee', 'XX', 'zz']) assert_equal(list(new_tags), ['ee', 'xx', 'yy', 'zz'])
def test_getitem_with_slice(self): tags = Tags(['2', '0', '1']) self._verify_slice(tags[:], ['0', '1', '2']) self._verify_slice(tags[1:], ['1', '2']) self._verify_slice(tags[1:-1], ['1']) self._verify_slice(tags[1:-2], []) self._verify_slice(tags[::2], ['0', '2'])
def test_init_with_iterable_and_normalization_and_sorting(self): for inp in [['T 1', 't2', 't_3'], ('t2', 'T 1', 't_3'), ('t2', 'T 1', 't_3') + ('t2', 'T 1', 't_3'), ('t2', 'T 2', '__T__2__', 'T 1', 't1', 't_1', 't_3', 't3'), ('', 'T 1', '', 't2', 't_3', 'NONE', 'None')]: assert_equal(list(Tags(inp)), ['T 1', 't2', 't_3'])
def test__add__None(self): tags = Tags(['xx', 'yy']) new_tags = tags + None assert_true(isinstance(new_tags, Tags)) assert_equal(list(tags), ['xx', 'yy']) assert_equal(list(new_tags), list(tags)) assert_true(new_tags is not tags)
def test_truth(self): assert_true(not Tags()) assert_true(not Tags('NONE')) assert_true(Tags(['a']))
def test_length(self): assert_equal(len(Tags()), 0) assert_equal(len(Tags(['a', 'b'])), 2)
def test_contains(self): assert_true('a' in Tags(['a', 'b'])) assert_true('c' not in Tags(['a', 'b'])) assert_true('AA' in Tags(['a_a', 'b']))
def test_remove_using_pattern(self): tags = Tags(['t1', 't2', '1', '1more']) tags.remove('?2') assert_equal(list(tags), ['1', '1more', 't1']) tags.remove('*1*') assert_equal(list(tags), [])
def test_remove_non_existing(self): tags = Tags(['a']) tags.remove('nonex') assert_equal(list(tags), ['a'])
def test_add_iterable(self): tags = Tags(['A']) tags.add(('b b', '', 'a', 'NONE')) tags.add(Tags(['BB', 'C'])) assert_equal(list(tags), ['A', 'b b', 'C'])
def test_init_with_none(self): assert_equal(list(Tags(None)), [])
def test__eq__(self): assert_equal(Tags(['x']), Tags(['x'])) assert_equal(Tags(['X']), Tags(['x'])) assert_equal(Tags(['X']), Tags(('x', ))) assert_not_equal(Tags(['X']), ['x']) assert_not_equal(Tags(['X']), ('x', ))
def _not_excluded_by_tags(self): if self.exclude and isinstance(self.test_case_data.tags, list): tags = Tags() tags.add(self.test_case_data.tags) return not tags.match(self.exclude) return True
def test_add_and_remove_none(self): tags = Tags(['t']) tags.add(None) tags.remove(None) assert_equal(list(tags), ['t'])
def test_remove_iterable(self): tags = Tags(['a', 'B B']) tags.remove(['nonex', '', 'A']) tags.remove(Tags('__B_B__')) assert_equal(list(tags), [])
def test_remove_string(self): tags = Tags(['a', 'B B']) tags.remove('a') assert_equal(list(tags), ['B B']) tags.remove('bb') assert_equal(list(tags), [])
def test_str(self): assert_equal(str(Tags()), '[]') assert_equal(str(Tags(['y', "X'X"])), "[X'X, y]") assert_equal(str(Tags([u'\xe4', 'a'])), '[a, \xc3\xa4]')
def test__eq__normalized(self): assert_equal(Tags(['Hello world', 'Foo', 'Not_world']), Tags(['nOT WORLD', 'FOO', 'hello world']))
def test_add_string(self): tags = Tags(['Y']) tags.add('x') assert_equal(list(tags), ['x', 'Y'])
def test__add__list(self): tags = Tags(['xx', 'yy']) new_tags = tags + ['zz', 'ee', 'XX'] assert_true(isinstance(new_tags, Tags)) assert_equal(list(tags), ['xx', 'yy']) assert_equal(list(new_tags), ['ee', 'xx', 'yy', 'zz'])
def test_init_with_string(self): assert_equal(list(Tags('string')), ['string'])
def _not_excluded_by_tags(self): if self.exclude and self.test_case_data.tags: tags = Tags() tags.add(self.test_case_data.tags) return not tags.match(self.exclude) return True
def test_getitem_with_index(self): tags = Tags(['2', '0', '1']) assert_equal(tags[0], '0') assert_equal(tags[1], '1') assert_equal(tags[2], '2')
def test_contains_pattern(self): assert_true('a*' in Tags(['a', 'b'])) assert_true('a*' in Tags(['u2', 'abba'])) assert_true('a?' not in Tags(['a', 'abba']))
def _included_by_tags(self): if self.include: tags = Tags() tags.add(self.test_case_data.tags) return tags.match(self.include) return True
def test_empty_init(self): assert_equal(list(Tags()), [])
def _verify(self, tags, expected): assert_equal(list(Tags(tags)), expected)
def test_unicode(self): assert_equal(unicode(Tags()), '[]') assert_equal(unicode(Tags(['y', "X'X", 'Y'])), "[X'X, y]") assert_equal(unicode(Tags([u'\xe4', 'a'])), u'[a, \xe4]')
def test_init_with_non_strings(self): assert_equal(list(Tags([2, True, None])), ['2', 'True'])
def test_repr(self): for tags in ([], [u'y', u"X'X"], [u'\xe4', u'a']): assert_equal(repr(Tags(tags)), repr(sorted(tags)))