1 # Copyright (c) 2012 testtools developers. See LICENSE for details.
3 """Test tag support."""
6 from testtools
import TestCase
7 from testtools
.tags
import TagContext
10 class TestTags(TestCase
):
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())
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())
23 def test_add_tag_twice(self
):
24 # Calling change_tags twice to add tags adds both tags to the current
26 tag_context
= TagContext()
27 tag_context
.change_tags(set(['foo']), set())
28 tag_context
.change_tags(set(['bar']), set())
30 set(['foo', 'bar']), tag_context
.get_current_tags())
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
)
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())
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.
49 parent
.change_tags(set(['foo']), set())
50 child
= TagContext(parent
)
52 parent
.get_current_tags(), child
.get_current_tags())
54 def test_add_to_child(self
):
55 # Adding a tag to the child context doesn't affect the parent.
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())
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.
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())
73 def test_parent(self
):
74 # The parent can be retrieved from a child context.
76 parent
.change_tags(set(['foo']), set())
77 child
= TagContext(parent
)
78 child
.change_tags(set(), set(['foo']))
79 self
.assertEqual(parent
, child
.parent
)
83 from unittest
import TestLoader
84 return TestLoader().loadTestsFromName(__name__
)