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 os |
---|
28 | import pytest |
---|
29 | |
---|
30 | from rtemsqual.content import SphinxContent |
---|
31 | from rtemsqual.content import MacroToSphinx |
---|
32 | from rtemsqual.items import Item, ItemCache |
---|
33 | |
---|
34 | |
---|
35 | class EmptyCache(ItemCache): |
---|
36 | def __init__(self): |
---|
37 | return |
---|
38 | |
---|
39 | |
---|
40 | def test_add_label(): |
---|
41 | sc = SphinxContent() |
---|
42 | sc.add_label("x") |
---|
43 | assert ".. _x:\n" == sc.content |
---|
44 | |
---|
45 | |
---|
46 | def test_add_header(): |
---|
47 | sc = SphinxContent() |
---|
48 | sc.add_header("x") |
---|
49 | assert "x\n=\n" == sc.content |
---|
50 | |
---|
51 | |
---|
52 | def test_add_blank_line(): |
---|
53 | sc = SphinxContent() |
---|
54 | sc.add_blank_line() |
---|
55 | assert "\n" == sc.content |
---|
56 | |
---|
57 | |
---|
58 | def test_add_line(): |
---|
59 | sc = SphinxContent() |
---|
60 | sc.add_line("x") |
---|
61 | assert "x\n" == sc.content |
---|
62 | sc.add_line("y", 1) |
---|
63 | assert "x\n y\n" == sc.content |
---|
64 | sc.add_line("") |
---|
65 | assert "x\n y\n\n" == sc.content |
---|
66 | |
---|
67 | |
---|
68 | def test_add_lines(): |
---|
69 | sc = SphinxContent() |
---|
70 | sc.add_lines("x") |
---|
71 | assert sc.content == "x\n" |
---|
72 | sc.add_lines("y", 1) |
---|
73 | assert sc.content == "x\n y\n" |
---|
74 | sc.add_lines(["a", "b"]) |
---|
75 | assert sc.content == "x\n y\na\nb\n" |
---|
76 | |
---|
77 | |
---|
78 | def test_add_index_entries(): |
---|
79 | sc = SphinxContent() |
---|
80 | sc.add_index_entries(["x", "y"]) |
---|
81 | assert "\n.. index:: x\n.. index:: y\n" == sc.content |
---|
82 | sc.add_index_entries("z") |
---|
83 | assert "\n.. index:: x\n.. index:: y\n\n.. index:: z\n" == sc.content |
---|
84 | |
---|
85 | |
---|
86 | def test_add_definition_item(): |
---|
87 | sc = SphinxContent() |
---|
88 | sc.add_definition_item("x", ["y", "z"]) |
---|
89 | assert sc.content == "\nx\n y\n z\n" |
---|
90 | sc = SphinxContent() |
---|
91 | sc.add_definition_item("a", "\n b\n") |
---|
92 | assert sc.content == "\na\n b\n" |
---|
93 | |
---|
94 | |
---|
95 | def test_license(): |
---|
96 | sc = SphinxContent() |
---|
97 | with pytest.raises(ValueError): |
---|
98 | sc.register_license("x") |
---|
99 | sc.register_license("CC-BY-SA-4.0") |
---|
100 | assert "" == sc.content |
---|
101 | sc.add_licence_and_copyrights() |
---|
102 | assert ".. SPDX-License-Identifier: CC-BY-SA-4.0\n\n" == sc.content |
---|
103 | |
---|
104 | |
---|
105 | def test_license_and_copyrights(): |
---|
106 | sc = SphinxContent() |
---|
107 | with pytest.raises(ValueError): |
---|
108 | sc.register_license("x") |
---|
109 | sc.register_copyright("Copyright (C) A") |
---|
110 | assert "" == sc.content |
---|
111 | sc.add_licence_and_copyrights() |
---|
112 | assert ".. SPDX-License-Identifier: CC-BY-SA-4.0\n\n.. Copyright (C) A\n\n" == sc.content |
---|
113 | |
---|
114 | |
---|
115 | def test_write(tmpdir): |
---|
116 | sc = SphinxContent() |
---|
117 | sc.add_line("x") |
---|
118 | path = os.path.join(tmpdir, "x", "y") |
---|
119 | sc.write(path) |
---|
120 | with open(path, "r") as src: |
---|
121 | assert "x\n" == src.read() |
---|
122 | tmpdir.chdir() |
---|
123 | path = "z" |
---|
124 | sc.write(path) |
---|
125 | with open(path, "r") as src: |
---|
126 | assert "x\n" == src.read() |
---|
127 | |
---|
128 | |
---|
129 | def test_substitute(): |
---|
130 | macro_to_sphinx = MacroToSphinx() |
---|
131 | data = {} |
---|
132 | data["glossary-term"] = "y" |
---|
133 | terms = {} |
---|
134 | terms["x"] = Item(EmptyCache(), "x", data) |
---|
135 | macro_to_sphinx.set_terms(terms) |
---|
136 | assert "@" == macro_to_sphinx.substitute("@@") |
---|
137 | assert "@x" == macro_to_sphinx.substitute("@x") |
---|
138 | with pytest.raises(KeyError): |
---|
139 | macro_to_sphinx.substitute("@x{y}") |
---|
140 | assert ":term:`y`" == macro_to_sphinx.substitute("@term{x}") |
---|