From c7f78e0a6cc7b25ddbe7bb7555b72cdda70db7bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Mart=C3=ADnez=20Portela?= Date: Tue, 30 Jul 2024 15:33:49 +0200 Subject: [PATCH] Add reparse test line ordering of reparsing. --- tests/13-update-reparse-test.org | 22 +++++++++++++++++ tests/test_org.py | 42 +++++++++++++++++++++++++++++++- 2 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 tests/13-update-reparse-test.org diff --git a/tests/13-update-reparse-test.org b/tests/13-update-reparse-test.org new file mode 100644 index 0000000..97eee86 --- /dev/null +++ b/tests/13-update-reparse-test.org @@ -0,0 +1,22 @@ +#+TITLE: 13-Update reparse +#+DESCRIPTION: Update-Reparse org file +#+TODO: TODO(t) PAUSED(p) | DONE(d) + + +* First level + :PROPERTIES: + :ID: 13-update-reparse-first-level-id + :CREATED: [2020-01-01 Wed 01:01] + :END: + First level content + + - A list of items :: + - With a sublist + + Something after the list. + +** Second level + :PROPERTIES: + :ID: 13-update-reparse-second-level-id + :END: + Second level content diff --git a/tests/test_org.py b/tests/test_org.py index e66e338..96e0181 100644 --- a/tests/test_org.py +++ b/tests/test_org.py @@ -1,4 +1,5 @@ import os +import tempfile import unittest from datetime import datetime as DT @@ -865,7 +866,7 @@ class TestSerde(unittest.TestCase): self.assertEqual(dumps(doc), orig) - def test_update_reparse(self): + def test_update_reparse_same_structure(self): with open(os.path.join(DIR, "01-simple.org")) as f: doc = load(f) @@ -905,6 +906,45 @@ class TestSerde(unittest.TestCase): # Check after update ex.assert_matches(self, hl, accept_trailing_whitespace_changes=True) + def test_update_reparse_same_values(self): + with open(os.path.join(DIR, "13-update-reparse-test.org")) as f: + doc = load(f) + + expected_hl_contents = ''' :PROPERTIES: + :ID: 13-update-reparse-first-level-id + :CREATED: [2020-01-01 Wed 01:01] + :END: + First level content + + - A list of items :: + - With a sublist + + Something after the list. +''' + + hl = doc.getTopHeadlines()[0] + lines = list(doc.dump_headline(hl, recursive=False)) + assert lines[0].startswith('* ') # Title, skip it + content = '\n'.join(lines[1:]) + self.assertEqual(content, expected_hl_contents) + + # Check after update + hl.update_raw_contents(content) + self.assertEqual(content, expected_hl_contents) + + # Check after dump and reload + with tempfile.NamedTemporaryFile('wt') as f: + save = org_rw.dumps(doc) + f.write(save) + f.flush() + + with open(f.name, 'rt') as reader: + reloaded = org_rw.load(reader) + re_hl = reloaded.getTopHeadlines()[0] + lines = list(doc.dump_headline(hl, recursive=False)) + assert lines[0].startswith('* ') # Title, skip it + content = '\n'.join(lines[1:]) + self.assertEqual(content, expected_hl_contents) def print_tree(tree, indentation=0, headline=None): for element in tree: