1 | # SPDX-License-Identifier: BSD-2-Clause |
---|
2 | """ Unit tests for the rtemsqual.content module. """ |
---|
3 | |
---|
4 | # Copyright (C) 2020 embedded brains GmbH (http://www.embedded-brains.de) |
---|
5 | # |
---|
6 | # Redistribution and use in source and binary forms, with or without |
---|
7 | # modification, are permitted provided that the following conditions |
---|
8 | # are met: |
---|
9 | # 1. Redistributions of source code must retain the above copyright |
---|
10 | # notice, this list of conditions and the following disclaimer. |
---|
11 | # 2. Redistributions in binary form must reproduce the above copyright |
---|
12 | # notice, this list of conditions and the following disclaimer in the |
---|
13 | # documentation and/or other materials provided with the distribution. |
---|
14 | # |
---|
15 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
16 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
17 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
18 | # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
19 | # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
20 | # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
21 | # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
22 | # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
23 | # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
24 | # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
25 | # POSSIBILITY OF SUCH DAMAGE. |
---|
26 | |
---|
27 | import pytest |
---|
28 | |
---|
29 | from rtemsqual.content import SphinxContent |
---|
30 | from rtemsqual.content import MacroToSphinx |
---|
31 | from rtemsqual.items import Item, ItemCache |
---|
32 | |
---|
33 | |
---|
34 | class EmptyCache(ItemCache): |
---|
35 | def __init__(self): |
---|
36 | return |
---|
37 | |
---|
38 | |
---|
39 | def test_add_label(): |
---|
40 | sc = SphinxContent() |
---|
41 | sc.add_label("x") |
---|
42 | assert str(sc) == """.. _x: |
---|
43 | """ |
---|
44 | |
---|
45 | |
---|
46 | def test_add_header(): |
---|
47 | sc = SphinxContent() |
---|
48 | sc.add_header("x") |
---|
49 | assert str(sc) == """x |
---|
50 | = |
---|
51 | """ |
---|
52 | |
---|
53 | |
---|
54 | def test_append(): |
---|
55 | sc = SphinxContent() |
---|
56 | sc.append("x") |
---|
57 | assert str(sc) == """x |
---|
58 | """ |
---|
59 | with sc.indent(): |
---|
60 | sc.append("y") |
---|
61 | assert str(sc) == """x |
---|
62 | y |
---|
63 | """ |
---|
64 | sc.append("") |
---|
65 | assert str(sc) == """x |
---|
66 | y |
---|
67 | |
---|
68 | """ |
---|
69 | |
---|
70 | |
---|
71 | def test_add_index_entries(): |
---|
72 | sc = SphinxContent() |
---|
73 | sc.add_index_entries(["x", "y"]) |
---|
74 | assert str(sc) == """.. index:: x |
---|
75 | .. index:: y |
---|
76 | """ |
---|
77 | sc.add_index_entries("z") |
---|
78 | assert str(sc) == """.. index:: x |
---|
79 | .. index:: y |
---|
80 | |
---|
81 | .. index:: z |
---|
82 | """ |
---|
83 | |
---|
84 | |
---|
85 | def test_add_definition_item(): |
---|
86 | sc = SphinxContent() |
---|
87 | sc.add_definition_item("x", ["y", "z"]) |
---|
88 | assert str(sc) == """x |
---|
89 | y |
---|
90 | z |
---|
91 | """ |
---|
92 | sc = SphinxContent() |
---|
93 | sc.add_definition_item("a", "\n b\n") |
---|
94 | assert str(sc) == """a |
---|
95 | b |
---|
96 | """ |
---|
97 | |
---|
98 | |
---|
99 | def test_license(): |
---|
100 | sc = SphinxContent() |
---|
101 | with pytest.raises(ValueError): |
---|
102 | sc.register_license("x") |
---|
103 | sc.register_license("CC-BY-SA-4.0") |
---|
104 | assert str(sc) == "" |
---|
105 | sc.add_licence_and_copyrights() |
---|
106 | assert str(sc) == """.. SPDX-License-Identifier: CC-BY-SA-4.0 |
---|
107 | |
---|
108 | """ |
---|
109 | |
---|
110 | |
---|
111 | def test_license_and_copyrights(): |
---|
112 | sc = SphinxContent() |
---|
113 | with pytest.raises(ValueError): |
---|
114 | sc.register_license("x") |
---|
115 | sc.register_copyright("Copyright (C) A") |
---|
116 | assert str(sc) == "" |
---|
117 | sc.add_licence_and_copyrights() |
---|
118 | assert str(sc) == """.. SPDX-License-Identifier: CC-BY-SA-4.0 |
---|
119 | |
---|
120 | .. Copyright (C) A |
---|
121 | |
---|
122 | """ |
---|
123 | |
---|
124 | |
---|
125 | def test_substitute(): |
---|
126 | macro_to_sphinx = MacroToSphinx() |
---|
127 | data = {} |
---|
128 | data["glossary-term"] = "y" |
---|
129 | terms = {} |
---|
130 | terms["x"] = Item(EmptyCache(), "x", data) |
---|
131 | macro_to_sphinx.set_terms(terms) |
---|
132 | assert "@" == macro_to_sphinx.substitute("@@") |
---|
133 | assert "@x" == macro_to_sphinx.substitute("@x") |
---|
134 | with pytest.raises(KeyError): |
---|
135 | macro_to_sphinx.substitute("@x{y}") |
---|
136 | assert ":term:`y`" == macro_to_sphinx.substitute("@term{x}") |
---|