source: rtems-central/rtemsqual/tests/test_content.py @ 520ba1dd

Last change on this file since 520ba1dd was 520ba1dd, checked in by Sebastian Huber <sebastian.huber@…>, on 04/28/20 at 11:27:38

content: Rework API

Use context managers for indent and comment blocks.

  • Property mode set to 100644
File size: 2.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 os
28
29from rtemsqual.content import Content
30
31
32def test_append():
33    content = Content("BSD-2-Clause")
34    content.append("")
35    assert str(content) == """
36"""
37    content.append(["a", "b"])
38    assert str(content) == """
39a
40b
41"""
42    with content.indent():
43        content.append(["c", "d"])
44        assert str(content) == """
45a
46b
47  c
48  d
49"""
50
51
52def test_prepend():
53    content = Content("BSD-2-Clause")
54    content.prepend("")
55    assert str(content) == """
56"""
57    content.prepend(["a", "b"])
58    assert str(content) == """a
59b
60
61"""
62    with content.indent():
63        content.prepend(["c", "d"])
64        assert str(content) == """  c
65  d
66a
67b
68
69"""
70
71
72def test_add():
73    content = Content("BSD-2-Clause")
74    content.add("")
75    assert str(content) == ""
76    content.add("a")
77    assert str(content) == """a
78"""
79    content.add(["b", "c"])
80    assert str(content) == """a
81
82b
83c
84"""
85
86
87def test_add_blank_line():
88    content = Content("BSD-2-Clause")
89    content.add_blank_line()
90    assert str(content) == """
91"""
92
93
94def test_indent():
95    content = Content("BSD-2-Clause")
96    content.add_blank_line()
97    content.append("x")
98    content.indent_lines(3)
99    assert str(content) == """
100      x
101"""
102
103
104def test_write(tmpdir):
105    content = Content("BSD-2-Clause")
106    content.append("x")
107    path = os.path.join(tmpdir, "x", "y")
108    content.write(path)
109    with open(path, "r") as src:
110        assert src.read() == """x
111"""
112    tmpdir.chdir()
113    path = "z"
114    content.write(path)
115    with open(path, "r") as src:
116        assert src.read() == """x
117"""
Note: See TracBrowser for help on using the repository browser.