source: rtems-central/tests/test_content.py @ 28b8f50

Last change on this file since 28b8f50 was 28b8f50, checked in by Sebastian Huber <sebastian.huber@…>, on 03/17/20 at 14:45:40

content: Add helper to accept a string or a list

  • Property mode set to 100644
File size: 5.9 KB
Line 
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
27import pytest
28
29from rtemsqual.content import Copyright
30from rtemsqual.content import Copyrights
31from rtemsqual.content import SphinxContent
32from rtemsqual.content import MacroToSphinx
33from rtemsqual.items import Item
34
35
36class TestCopyright(object):
37    def test_get_statement(self):
38        c = Copyright("John Doe")
39        assert "Copyright (C) John Doe" == c.get_statement()
40        c.add_year("3")
41        assert "Copyright (C) 3 John Doe" == c.get_statement()
42        c.add_year("3")
43        assert "Copyright (C) 3 John Doe" == c.get_statement()
44        c.add_year("5")
45        assert "Copyright (C) 3, 5 John Doe" == c.get_statement()
46        c.add_year("4")
47        assert "Copyright (C) 3, 5 John Doe" == c.get_statement()
48        c.add_year("2")
49        assert "Copyright (C) 2, 5 John Doe" == c.get_statement()
50
51    def test_lt(self):
52        a = Copyright("A")
53        b = Copyright("B")
54        c = Copyright("C")
55        assert b < a
56        assert c < a
57        assert c < b
58        b.add_year("1")
59        assert b < c
60        a.add_year("2")
61        assert a < b
62
63
64class TestCopyrights(object):
65    def test_register(self):
66        c = Copyrights()
67        with pytest.raises(ValueError):
68            c.register("abc")
69        c.register("Copyright (C) A")
70        c.register("Copyright (C) 2 A")
71        c.register("Copyright (C) 2, 3 A")
72        c.register("Copyright (C) D")
73        c.register("Copyright (C) 1 D")
74        c.register("Copyright (C) 1, 4 D")
75        c.register("Copyright (C) C")
76        c.register("Copyright (C) 1 B")
77        s = c.get_statements()
78        assert 4 == len(s)
79        assert "Copyright (C) C" == s[0]
80        assert "Copyright (C) 2, 3 A" == s[1]
81        assert "Copyright (C) 1, 4 D" == s[2]
82        assert "Copyright (C) 1 B" == s[3]
83
84
85class TestSphinxContent(object):
86    def test_add_label(self):
87        sc = SphinxContent()
88        sc.add_label("x")
89        assert ".. _x:\n" == sc.content
90
91    def test_add_header(self):
92        sc = SphinxContent()
93        sc.add_header("x")
94        assert "x\n=\n" == sc.content
95
96    def test_add_blank_line(self):
97        sc = SphinxContent()
98        sc.add_blank_line()
99        assert "\n" == sc.content
100
101    def test_add_line(self):
102        sc = SphinxContent()
103        sc.add_line("x")
104        assert "x\n" == sc.content
105        sc.add_line("y", 1)
106        assert "x\n    y\n" == sc.content
107        sc.add_line("")
108        assert "x\n    y\n\n" == sc.content
109
110    def test_add_lines(self):
111        sc = SphinxContent()
112        sc.add_lines("x")
113        assert sc.content == "x\n"
114        sc.add_lines("y", 1)
115        assert sc.content == "x\n    y\n"
116        sc.add_lines(["a", "b"])
117        assert sc.content == "x\n    y\na\nb\n"
118
119    def test_add_index_entries(self):
120        sc = SphinxContent()
121        sc.add_index_entries(["x", "y"])
122        assert "\n.. index:: x\n.. index:: y\n" == sc.content
123        sc.add_index_entries("z")
124        assert "\n.. index:: x\n.. index:: y\n\n.. index:: z\n" == sc.content
125
126    def test_add_definition_item(self):
127        sc = SphinxContent()
128        sc.add_definition_item("x", ["y", "z"])
129        assert "\nx\n    y\n    z\n" == sc.content
130
131    def test_license(self):
132        sc = SphinxContent()
133        with pytest.raises(ValueError):
134            sc.register_license("x")
135        sc.register_license("CC-BY-SA-4.0")
136        assert "" == sc.content
137        sc.add_licence_and_copyrights()
138        assert ".. SPDX-License-Identifier: CC-BY-SA-4.0\n\n" == sc.content
139
140    def test_license_and_copyrights(self):
141        sc = SphinxContent()
142        with pytest.raises(ValueError):
143            sc.register_license("x")
144        sc.register_copyright("Copyright (C) A")
145        assert "" == sc.content
146        sc.add_licence_and_copyrights()
147        assert ".. SPDX-License-Identifier: CC-BY-SA-4.0\n\n.. Copyright (C) A\n\n" == sc.content
148
149    def test_write(self, tmpdir):
150        sc = SphinxContent()
151        sc.add_line("x")
152        path = tmpdir + "/y/z"
153        sc.write(path)
154        with open(path, "r") as src:
155            assert "x\n" == src.read()
156
157
158class TestMacroToSphinx(object):
159    def test_substitute(self):
160        macro_to_sphinx = MacroToSphinx()
161        data = {}
162        data["glossary-term"] = "y"
163        terms = {}
164        terms["x"] = Item("x", data)
165        macro_to_sphinx.set_terms(terms)
166        assert "@" == macro_to_sphinx.substitute("@@")
167        assert "@x" == macro_to_sphinx.substitute("@x")
168        with pytest.raises(KeyError):
169            macro_to_sphinx.substitute("@x{y}")
170        assert ":term:`y`" == macro_to_sphinx.substitute("@term{x}")
Note: See TracBrowser for help on using the repository browser.