Debian Codegraph @

cloud_sptheme/tests/test_ext_relbar_toc.py

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 """
2 cloud_sptheme.tests -- test relbar_toc extension
3 """
4 #=============================================================================
5 # imports
6 #=============================================================================
7 # core
8 from __future__ import absolute_import, division, print_function
9 import logging
10 log = logging.getLogger(__name__)
11 import os
12 # site
•13 from unittest import mock
14 # pkg
15 from .utils import TestCase, unittest
16 # subject
17 from cloud_sptheme.ext import relbar_links
18 # local
19 __all__ = [
20 ]
21
22 #=============================================================================
23 # extension test
24 #=============================================================================
25
26 # root_dir = os.path.abspath(os.path.join(__file__, *[".."]*3))
27 # docs_dir = os.path.join(root_dir, "docs")
28
29 # TODO: would like to test extensions *actual* behavior with sphinx,
30 # and/or test our assumptions about format of ctx & ctx['rellinks'].
31 # Have idea of running integration test by running sphinx,
32 # and building either our docs, or some "test" docs;
33 # and then examining the output. not sure best way to approach
34 # that though.
35 # from sphinx import main
36 # target_dir = self.makeTempDir()
37 # self.assertFalse(main(["sphinx", "-b", "html", "-E", docs_dir, target_dir]))
38
39 #=============================================================================
40 # utils tests
41 #=============================================================================
42 class UtilsTestCase(TestCase):
43
44 def setUp(self):
45 super(UtilsTestCase, self).setUp()
46
47 # patch sphinx's _() to be noop
48 patch = mock.patch.object(relbar_links, "_", side_effect=lambda s: s)
•49 patch.start()
50 self.addCleanup(patch.stop)
51
52 def test_insert_no_links(self):
53 """
54 test insert hooks -- with no links present (e.g. json builder)
55 """
56 from cloud_sptheme.ext.relbar_links import insert_relbar_links
57 app = mock.Mock()
58 insert_relbar_links(app, None, None, {}, None)
59
60 def test_insert_default(self):
61 """
62 test insert hooks -- with default links
63 """
64 from cloud_sptheme.ext.relbar_links import insert_relbar_links
65 app = mock.Mock()
66 app.config.master_doc = "test-contents"
67 app.config.relbar_links = ["toc"]
68 app.config.relbar_links_after = ["next", "previous"]
69 links = [
70 ("genindex", "General Index", "I", "genidx"),
71 ("next-test-page", "Next Page", "N", "next"),
72 ("previous-test-page", "Previous Page", "P", "previous"),
73 ]
74 insert_relbar_links(app, None, None, dict(rellinks=links), None)
75 self.assertEqual(links, [
76 ("genindex", "General Index", "I", "genidx"),
77 ("test-contents", "Table Of Contents", "C", "toc"),
78 ("next-test-page", "Next Page", "N", "next"),
79 ("previous-test-page", "Previous Page", "P", "previous"),
80 ])
81
82 def test_insert_custom(self):
83 """
84 test insert hooks -- with custom links
85 """
86 from cloud_sptheme.ext.relbar_links import insert_relbar_links
87 app = mock.Mock()
88 app.config.relbar_links = [dict(page="my-page", label="my page")]
89 app.config.relbar_links_after = ["previous"]
90 links = [
91 ("genindex", "General Index", "I", "genidx"),
92 ("next-test-page", "Next Page", "N", "next"),
93 ("previous-test-page", "Previous Page", "P", "previous"),
94 ]
95 insert_relbar_links(app, None, None, dict(rellinks=links), None)
96 self.assertEqual(links, [
97 ("genindex", "General Index", "I", "genidx"),
98 ("next-test-page", "Next Page", "N", "next"),
99 ("my-page", "my page", None, "my page"),
100 ("previous-test-page", "Previous Page", "P", "previous"),
101 ])
102
103 def test_parse_entry(self):
104 """
105 test parse_entry() helper
106 """
107 from cloud_sptheme.ext.relbar_links import parse_entry
108 app = mock.Mock()
109 app.config.master_doc = "test-master-doc"
110
111 #
112 # dicts
113 #
114 self.assertRaises(ValueError, parse_entry, app, dict())
115
116 self.assertRaises(ValueError, parse_entry, app, dict(page="page"))
117
118 self.assertRaises(ValueError, parse_entry, app, dict(label="label"))
119
120 self.assertEqual(parse_entry(app, dict(page="page", label="label")),
121 ("page", "label", None, "label"))
122
123 self.assertEqual(parse_entry(app, dict(page="page", title="title", key="key", label="label")),
124 ("page", "title", "key", "label"))
125
126 #
127 # tuples
128 #
129 self.assertRaises(ValueError, parse_entry, app, ())
130
131 self.assertRaises(ValueError, parse_entry, app, ("a",))
132
133 self.assertEqual(parse_entry(app, ("page", "label")),
134 ("page", "label", None, "label"))
135
136 self.assertRaises(ValueError, parse_entry, app, ("a", "b", "c"))
137
138 self.assertEqual(parse_entry(app, ("page", "title", "key", "label")),
139 ("page", "title", "key", "label"))
140
141 #
142 # other
143 #
144 self.assertEqual(parse_entry(app, "toc"),
145 ("test-master-doc", "Table Of Contents", "C", "toc"))
146
147 #=============================================================================
148 # eof
149 #=============================================================================
150