Debian Codegraph @

tests/test_content.py

raw
1 # SPDX-FileCopyrightText: © 2023 Christian BUHTZ <[email protected]>
2 # SPDX-FileCopyrightText: © 2025 Michael Olie <[email protected]>
3 #
4 # SPDX-License-Identifier: GPL-3.0-only
5 #
6 # This file is part of the program "Hyperorg" which is released under GNU
7 # General Public License v3 (GPLv3).
8 # See folder LICENSES or go to <https://www.gnu.org/licenses/#GPL>.
9 """Tests related to content.py.
10
11 About Node class in most cases.
12 """
13 import unittest
14 import datetime
15 from hyperorg import content
16 from hyperorg import elements
17 from hyperorg import textruns
18 from . import helper
19
20
21 def _reset_node_class():
22 content.Node.nodes = {}
23 content.Node.heading_ids = {}
24
25
26 class TestNode(unittest.TestCase):
27 """Test related to Node class"""
28 # pylint: disable=protected-access
29
30 def setUp(self):
31 _reset_node_class()
32
33 def test_class_attributes(self):
34 """Default class attributes"""
35 self.assertEqual(content.Node.nodes, {})
36 self.assertEqual(content.Node.heading_ids, {})
37
38 def test_empty(self):
39 """empty by default"""
40 node = content.Node([], '')
41 self.assertEqual(len(node), 0)
42
43 def test_string(self):
44 """Strings allowed"""
45 randstring = helper.rand_string()
46 note = content.Node([randstring], '')
47 self.assertEqual(len(note), 1)
48
49 def test_number(self):
50 """Numbers not allowed"""
51 with self.assertRaises(TypeError):
52 content.Node(1) # pylint: disable=no-value-for-parameter
53
54 def test_inherit_meta_fields(self):
55 """Meta fields are inherited by child nodes/subheadings."""
56
57 node_a = content.Node([
58 '#+filetags: One "Two zwei" Three',
59 '#+title: Titel',
60 '#+date: [1982-08-06 18:23]',
61 'foobar'],
62 '',
63 ['aliasA', 'B C'])
64
65 node_b = content.Node([], '', ['alias Y'])
66
67 # parent node
68 self.assertEqual(
69 node_a._meta,
70 {
71 'filetags': ['One', 'Two zwei', 'Three'],
72 'aliases_org': ['aliasA', 'B C'],
73 'aliases_runs': [['aliasA'], ['B C']],
74 'date': datetime.datetime(1982, 8, 6, 18, 23),
75 })
76
77 # child node (before inheritance)
78 self.assertEqual(
79 node_b._meta,
80 {
81 'aliases_org': ['alias Y'],
82 'aliases_runs': [['alias Y']]
83 }
84 )
85
86 # Action!
87 node_b.inherit_meta_fields_from(node_a)
88
89 # child node (After inheritance)
90 self.assertEqual(
91 node_b._meta,
92 {
93 'aliases_org': ['alias Y'],
94 'aliases_runs': [['alias Y']],
95 'filetags': ['One', 'Two zwei', 'Three'],
96 'date': datetime.datetime(1982, 8, 6, 18, 23),
97 }
98 )
99
100 def test_org_properties(self):
101 """ Buffer wide org properties."""
102
103 node = content.Node(['#+title: Foobar', '#+foo: Bar'], '')
104
105 self.assertEqual(len(node), 0)
106 self.assertEqual(node._meta, {'foo': 'Bar'})
107 self.assertEqual(node.title_runs, ['Foobar'])
108 self.assertEqual(node.title_org, 'Foobar')
109
110
111 class Titles(unittest.TestCase):
112 """Titles of the node"""
113
114 # pylint: disable=protected-access
115
116 def test_title(self):
117 """Title set via parse lines."""
118
119 node = content.Node([
120 '#+title: Foobar', '#+foo: Bar', 'one'], '')
121
122 self.assertEqual(node.title_runs, ['Foobar'])
123 self.assertEqual(node.title_org, 'Foobar')
124
125 def test_title_with_markup(self):
126 """Title with markup tokens"""
127
128 node = content.Node(['#+title: Foo =bar='], '')
129
130 self.assertEqual(node.title_org, 'Foo =bar=')
131 self.assertEqual(
132 node.title_runs,
133 [
134 'Foo ',
135 textruns.Markup('bar', textruns.Markup.Type.VERBATIM)
136 ]
137 )
138
139 def test_alias_titles(self):
140 """Alias titles"""
141 node = content.Node([], 'filename', ['alias a', 'boo'])
142
143 self.assertEqual(node._meta['aliases_org'], ['alias a', 'boo'])
144 self.assertEqual(node._meta['aliases_runs'], [['alias a'], ['boo']])
145
146 def test_no_alias_titles(self):
147 """No alias titles"""
148 node = content.Node([], 'filename')
149
150 self.assertNotIn('aliases', node._meta)
151
152
153 class Linking(unittest.TestCase):
154 """Tests related to links and linking nodes and subheadings"""
155
156 def setUp(self):
157 _reset_node_class()
158
159 def test_get_orgid_links(self):
160 """Orgids contained in one Node"""
161 lines = [
162 '#+title: Titel',
163 'Paragraph without link',
164 '',
165 'Link [[id:123][with label]]',
166 '',
167 'Link without [[id:456]]',
168 ]
169 sut = content.Node(lines, '')
170
171 result = sut.get_orgid_links()
172
173 self.assertEqual(result, ['123', '456'])
174
175 def test_get_orgid_links_but_without_subheadings(self):
176 """A node knows its orgids but not the orgids of its
177 child's (subheadings).
178 """
179 lines = [
180 '#+title: Titel',
181 'Paragraph without link',
182 '',
183 'Link [[id:123][with label]]',
184 ]
185 node = content.Node(lines, '')
186
187 lines = [
188 '* Subheading',
189 ':PROPERTIES:',
190 ':ID:b0572191-4c8d-4834-a240-5853b3d6a2e5',
191 ':END:',
192 'This [[id:456][link]]',
193 ]
194 subn = content.Node(lines, '')
195 node.add_subheading_content(subn)
196
197 result = node.get_orgid_links()
198
199 self.assertEqual(result, ['123'])
200
201
202 class Backlinking(unittest.TestCase):
203 """Tests related to backlinking"""
204
205 def setUp(self):
206 _reset_node_class()
207
208 def test_add_backlink(self):
209 """Implicit creation of Backlink elements"""
210
211 # create two nodes
212 content.Node.nodes['A'] = content.Node(['#+title: A-title'], '')
213 content.Node.nodes['X'] = content.Node(['#+title: X-title'], '')
214
215 # they are empty
216 self.assertEqual(len(content.Node.nodes['X']), 0)
217 self.assertEqual(len(content.Node.nodes['A']), 0)
218
219 # assuming A links to X
220 content.Node.nodes['X'].add_backlinks({'A': []})
221
222 # then X should contain a backlink element to A
223 self.assertEqual(len(content.Node.nodes['X']), 1)
224
225 # but A shouldn't
226 self.assertEqual(len(content.Node.nodes['A']), 0)
227
228 # There is a Backlink element in X
229 backlink = content.Node.nodes['X'][0]
230 self.assertIsInstance(backlink, elements.Backlinks)
231
232 # its one and only item has one Link run
233 link = backlink[0][0]
234 self.assertEqual(link.target, 'A')
235 self.assertEqual(link.link_type, textruns.Link.Type.ORGID)
236 self.assertEqual(link[0], 'A-title')
237
238 def test_add_two_backlinks(self):
239 """Backlink element with two links"""
240
241 # create two nodes
242 content.Node.nodes['A'] = content.Node(['#+title: A-title'], '')
243 content.Node.nodes['B'] = content.Node(['#+title: B-title'], '')
244 content.Node.nodes['X'] = content.Node(['#+title: X-title'], '')
245
246 # they are empty
247 self.assertEqual(len(content.Node.nodes['X']), 0)
248 self.assertEqual(len(content.Node.nodes['A']), 0)
249 self.assertEqual(len(content.Node.nodes['B']), 0)
250
251 # assuming A links to X
252 content.Node.nodes['X'].add_backlinks({'A': [], 'B': []})
253
254 # There are two Backlink element
255 backlink = content.Node.nodes['X'][0]
256 self.assertIsInstance(backlink, elements.Backlinks)
257
258 # its one and only item has two Link runs
259 self.assertEqual(len(backlink), 2)