testtools: Update to latest version.
[nivanova/samba-autobuild/.git] / lib / testtools / testtools / tests / test_tags.py
1 # Copyright (c) 2012 testtools developers. See LICENSE for details.
2
3 """Test tag support."""
4
5
6 from testtools import TestCase
7 from testtools.tags import TagContext
8
9
10 class TestTags(TestCase):
11
12     def test_no_tags(self):
13         # A tag context has no tags initially.
14         tag_context = TagContext()
15         self.assertEqual(set(), tag_context.get_current_tags())
16
17     def test_add_tag(self):
18         # A tag added with change_tags appears in get_current_tags.
19         tag_context = TagContext()
20         tag_context.change_tags(set(['foo']), set())
21         self.assertEqual(set(['foo']), tag_context.get_current_tags())
22
23     def test_add_tag_twice(self):
24         # Calling change_tags twice to add tags adds both tags to the current
25         # tags.
26         tag_context = TagContext()
27         tag_context.change_tags(set(['foo']), set())
28         tag_context.change_tags(set(['bar']), set())
29         self.assertEqual(
30             set(['foo', 'bar']), tag_context.get_current_tags())
31
32     def test_change_tags_returns_tags(self):
33         # change_tags returns the current tags.  This is a convenience.
34         tag_context = TagContext()
35         tags = tag_context.change_tags(set(['foo']), set())
36         self.assertEqual(set(['foo']), tags)
37
38     def test_remove_tag(self):
39         # change_tags can remove tags from the context.
40         tag_context = TagContext()
41         tag_context.change_tags(set(['foo']), set())
42         tag_context.change_tags(set(), set(['foo']))
43         self.assertEqual(set(), tag_context.get_current_tags())
44
45     def test_child_context(self):
46         # A TagContext can have a parent.  If so, its tags are the tags of the
47         # parent at the moment of construction.
48         parent = TagContext()
49         parent.change_tags(set(['foo']), set())
50         child = TagContext(parent)
51         self.assertEqual(
52             parent.get_current_tags(), child.get_current_tags())
53
54     def test_add_to_child(self):
55         # Adding a tag to the child context doesn't affect the parent.
56         parent = TagContext()
57         parent.change_tags(set(['foo']), set())
58         child = TagContext(parent)
59         child.change_tags(set(['bar']), set())
60         self.assertEqual(set(['foo', 'bar']), child.get_current_tags())
61         self.assertEqual(set(['foo']), parent.get_current_tags())
62
63     def test_remove_in_child(self):
64         # A tag that was in the parent context can be removed from the child
65         # context without affect the parent.
66         parent = TagContext()
67         parent.change_tags(set(['foo']), set())
68         child = TagContext(parent)
69         child.change_tags(set(), set(['foo']))
70         self.assertEqual(set(), child.get_current_tags())
71         self.assertEqual(set(['foo']), parent.get_current_tags())
72
73     def test_parent(self):
74         # The parent can be retrieved from a child context.
75         parent = TagContext()
76         parent.change_tags(set(['foo']), set())
77         child = TagContext(parent)
78         child.change_tags(set(), set(['foo']))
79         self.assertEqual(parent, child.parent)
80
81
82 def test_suite():
83     from unittest import TestLoader
84     return TestLoader().loadTestsFromName(__name__)