| 1 |
# SPDX-FileCopyrightText: © 2023 Christian BUHTZ <[email protected]> |
| 2 |
# |
| 3 |
# SPDX-License-Identifier: GPL-3.0-only |
| 4 |
# |
| 5 |
# This file is part of the program "Hyperorg" which is released under GNU |
| 6 |
# General Public License v3 (GPLv3). |
| 7 |
# See folder LICENSES or go to <https://www.gnu.org/licenses/#GPL>. |
| 8 |
"""Tests related to parser.py especially about properties, meta data and |
| 9 |
comments in org files.""" |
| 10 |
import unittest |
| 11 |
import datetime |
| 12 |
from hyperorg import elements |
| 13 |
from hyperorg import parser |
| 14 |
|
| 15 |
|
| 16 |
class Properties(unittest.TestCase): |
| 17 |
"""Test org node with properties""" |
| 18 |
|
| 19 |
def test_simple(self): |
| 20 |
"""Simple properties""" |
| 21 |
content = [ |
| 22 |
'#+foo', |
| 23 |
'#+bar and 42', |
| 24 |
] |
| 25 |
sut = parser.OrgContentParser(content) |
| 26 |
|
| 27 |
self.assertEqual(len(sut.result()[1]), 0) |
| 28 |
# pylint: disable-next=protected-access |
| 29 |
self.assertEqual(sut._property_lines, content) |
| 30 |
|
| 31 |
|
| 32 |
class Comments(unittest.TestCase): |
| 33 |
"""Ignore comments while parsing""" |
| 34 |
|
| 35 |
def test_inline(self): |
| 36 |
"""Inline comment""" |
| 37 |
content = [ |
| 38 |
'# Inline comment', |
| 39 |
'foo', |
| 40 |
'# Inline comment', |
| 41 |
'', |
| 42 |
'bar', |
| 43 |
] |
| 44 |
_, sut = parser.OrgContentParser(content).result() |
| 45 |
|
| 46 |
self.assertEqual(len(sut), 2) |
| 47 |
self.assertIsInstance(sut[0], elements.Paragraph) |
| 48 |
self.assertIsInstance(sut[1], elements.Paragraph) |
| 49 |
self.assertEqual(sut[0][0], 'foo') |
| 50 |
self.assertEqual(sut[1][0], 'bar') |
| 51 |
|
| 52 |
def test_block(self): |
| 53 |
"""Simple figure.""" |
| 54 |
content = [ |
| 55 |
'#+BEGIN_COMMENT', |
| 56 |
'block comment', |
| 57 |
'#+END_COMMENT', |
| 58 |
'banana', |
| 59 |
'', |
| 60 |
'#+BEGIN_COMMENT', |
| 61 |
'block comment', |
| 62 |
'#+END_COMMENT', |
| 63 |
'strawberry', |
| 64 |
] |
| 65 |
_, sut = parser.OrgContentParser(content).result() |
| 66 |
|
| 67 |
self.assertEqual(len(sut), 2) |
| 68 |
self.assertIsInstance(sut[0], elements.Paragraph) |
| 69 |
self.assertIsInstance(sut[1], elements.Paragraph) |
| 70 |
self.assertEqual(sut[0][0], 'banana') |
| 71 |
self.assertEqual(sut[1][0], 'strawberry') |
| 72 |
|
| 73 |
def test_block_not_case_sensitive(self): |
| 74 |
"""Block comment mixed upper/lower case""" |
| 75 |
content = [ |
| 76 |
'#+BEGIN_comment', |
| 77 |
'block comment', |
| 78 |
'#+end_COMMENT', |
| 79 |
'banana', |
| 80 |
] |
| 81 |
_, sut = parser.OrgContentParser(content).result() |
| 82 |
|
| 83 |
self.assertEqual(len(sut), 1) |
| 84 |
self.assertIsInstance(sut[0], elements.Paragraph) |
| 85 |
self.assertEqual(sut[0][0], 'banana') |
| 86 |
|
| 87 |
def test_invalid_inline(self): |
| 88 |
"""This is not a comment""" |
| 89 |
content = [ |
| 90 |
'#invalid inline comment', |
| 91 |
'', |
| 92 |
'foo', |
| 93 |
] |
| 94 |
_, sut = parser.OrgContentParser(content).result() |
| 95 |
|
| 96 |
self.assertEqual(len(sut), 2) |
| 97 |
self.assertIsInstance(sut[0], elements.Paragraph) |
| 98 |
self.assertIsInstance(sut[1], elements.Paragraph) |
| 99 |
self.assertEqual(sut[0][0], '#invalid inline comment') |
| 100 |
self.assertEqual(sut[1][0], 'foo') |
| 101 |
|
| 102 |
|
| 103 |
class ParsingMeta(unittest.TestCase): |
| 104 |
"""Parsing meta fields.""" |
| 105 |
|
| 106 |
# pylint: disable=protected-access |
| 107 |
|
| 108 |
def test_title(self): |
| 109 |
"""Title set via parse lines.""" |
| 110 |
|
| 111 |
content = [ |
| 112 |
'#+title: Foobar', |
| 113 |
'#+foo: Bar', |
| 114 |
'one' |
| 115 |
] |
| 116 |
sut, elem = parser.OrgContentParser(content).result() |
| 117 |
|
| 118 |
# The title element (Heading) is not created by the parser but |
| 119 |
# by the Node calling the parser. |
| 120 |
self.assertEqual(sut, {'title': 'Foobar', 'foo': 'Bar'}) |
| 121 |
|
| 122 |
# Just the paragraph |
| 123 |
self.assertEqual(len(elem), 1) |
| 124 |
|
| 125 |
def test_meta_at_file_beginning(self): |
| 126 |
"""Parse meta (property) lines at beginning of a file.""" |
| 127 |
|
| 128 |
content = [ |
| 129 |
'#+title: Foobar', |
| 130 |
'#+date: 3022-08-06 18:23', |
| 131 |
'#+foo: bar', |
| 132 |
'one', |
| 133 |
'two', |
| 134 |
'three' |
| 135 |
] |
| 136 |
|
| 137 |
meta, _ = parser.OrgContentParser(content).result() |
| 138 |
|
| 139 |
self.assertEqual( |
| 140 |
meta, |
| 141 |
{ |
| 142 |
'title': 'Foobar', |
| 143 |
'date': datetime.datetime(3022, 8, 6, 18, 23), |
| 144 |
'foo': 'bar' |
| 145 |
} |
| 146 |
) |
| 147 |
|
| 148 |
def test_meta_in_file_body(self): |
| 149 |
"""Possible meta (property) lines in the body of a file. |
| 150 |
|
| 151 |
The key point here is that "meta" lines in the file body are not |
| 152 |
parsed as meta data. |
| 153 |
""" |
| 154 |
|
| 155 |
content = [ |
| 156 |
'#+title: Foobar', |
| 157 |
'#+date: 3022-08-06 18:23', |
| 158 |
'#+foo: bar', |
| 159 |
'one', |
| 160 |
'two', |
| 161 |
'#+DOWNLOADED: url', |
| 162 |
'three', |
| 163 |
'#+filetags: foo' |
| 164 |
] |
| 165 |
|
| 166 |
meta, elem = parser.OrgContentParser(content).result() |
| 167 |
|
| 168 |
self.assertEqual( |
| 169 |
meta, |
| 170 |
{ |
| 171 |
'title': 'Foobar', |
| 172 |
'date': datetime.datetime(3022, 8, 6, 18, 23), |
| 173 |
'foo': 'bar', |
| 174 |
'filetags': ['foo'], |
| 175 |
} |
| 176 |
) |
| 177 |
|
| 178 |
self.assertEqual(len(elem[0]), 1) |
| 179 |
self.assertEqual(elem[0][0], 'one two three') |
| 180 |
|
| 181 |
def test_ignore_meta_missing_hash(self): |
| 182 |
"""The '#' is missing.""" |
| 183 |
content = ['+foo: bar', 'three'] |
| 184 |
meta, elem = parser.OrgContentParser(content).result() |
| 185 |
self.assertEqual(meta, {}) |
| 186 |
self.assertEqual(len(elem), 1) |
| 187 |
|
| 188 |
def test_ignore_meta_missing_blank(self): |
| 189 |
"""Missing blank after ':'.""" |
| 190 |
|
| 191 |
content = ['#+foo:bar', 'three'] |
| 192 |
meta, elem = parser.OrgContentParser(content).result() |
| 193 |
|
| 194 |
self.assertEqual(len(elem), 1) |
| 195 |
self.assertEqual(meta, {}) |
| 196 |
|
| 197 |
def test_filetags_complex(self): |
| 198 |
"""Multiple filetags with multiple words""" |
| 199 |
|
| 200 |
content = ['#+filetags: One "Two zwei" Three'] |
| 201 |
sut, _ = parser.OrgContentParser(content).result() |
| 202 |
|
| 203 |
self.assertEqual(sut, {'filetags': ['One', 'Two zwei', 'Three']}) |
| 204 |
|
| 205 |
def test_filetags_simple(self): |
| 206 |
"""One word filetag""" |
| 207 |
content = ['#+filetags: One'] |
| 208 |
sut, _ = parser.OrgContentParser(content).result() |
| 209 |
|
| 210 |
self.assertEqual(sut, {'filetags': ['One']}) |
| 211 |
|
| 212 |
|
| 213 |
class ParsingMetaDate(unittest.TestCase): |
| 214 |
"""Parsing date fields in meta data.""" |
| 215 |
|
| 216 |
def test_date_without_brackets(self): |
| 217 |
"""Date without brackets.""" |
| 218 |
|
| 219 |
content = ['#+date: 2021-11-28 16:20'] |
| 220 |
meta, _ = parser.OrgContentParser(content).result() |
| 221 |
|
| 222 |
self.assertEqual(meta, |
| 223 |
{'date': datetime.datetime(2021, 11, 28, 16, 20)}) |
| 224 |
|
| 225 |
def test_date_active(self): |
| 226 |
"""Active dates with <>.""" |
| 227 |
content = ['#+date: <2021-11-28 16:20>'] |
| 228 |
meta, _ = parser.OrgContentParser(content).result() |
| 229 |
|
| 230 |
self.assertEqual(meta, |
| 231 |
{'date': datetime.datetime(2021, 11, 28, 16, 20)}) |
| 232 |
|
| 233 |
def test_date_inactive(self): |
| 234 |
"""Inactive dates with [].""" |
| 235 |
content = ['#+date: [2021-11-28 16:20]'] |
| 236 |
meta, _ = parser.OrgContentParser(content).result() |
| 237 |
|
| 238 |
self.assertEqual(meta, |
| 239 |
{'date': datetime.datetime(2021, 11, 28, 16, 20)}) |