Initial commit, simplistic parsing.

This commit is contained in:
Sergio Martínez Portela 2020-06-21 21:27:40 +02:00
commit d29058cb5e
9 changed files with 525 additions and 0 deletions

25
tests/01-simple.org Normal file
View file

@ -0,0 +1,25 @@
#+TITLE: 01-Simple
#+DESCRIPTION: Simple org file
#+TODO: TODO(t) PAUSED(p) | DONE(d)
* First level
:PROPERTIES:
:ID: 01-simple-first-level-id
:CREATED: [2020-01-01 Wed 01:01]
:END:
First level content
** Second level
:PROPERTIES:
:ID: 01-simple-second-level-id
:END:
Second level content
*** Third level
:PROPERTIES:
:ID: 01-simple-third-level-id
:END:
Third level content

39
tests/test_dom.py Normal file
View file

@ -0,0 +1,39 @@
import logging
import os
import sys
import unittest
from datetime import datetime as DT
from org_dom import load, loads
from utils.dom_assertions import HL, Dom
DIR = os.path.dirname(os.path.abspath(__file__))
class TestSerde(unittest.TestCase):
def test_simple_file_01(self):
with open(os.path.join(DIR, '01-simple.org')) as f:
doc = load(f)
ex = Dom(props=[('TITLE', '01-Simple'),
('DESCRIPTION', 'Simple org file'),
('TODO', 'TODO(t) PAUSED(p) | DONE(d)')],
children=(HL(
'First level',
props=[
('ID', '01-simple-first-level-id'),
('CREATED', DT(2020, 1, 1, 1, 1)),
],
content='First level content',
children=[
HL('Second level',
props=[('ID', '01-simple-second-level-id')],
content='Second level content',
children=[
HL('Third level',
props=[('ID', '01-simple-third-level-id')],
content='Third level content')
])
])))
ex.assert_matches(self, doc)

View file

@ -0,0 +1,77 @@
import collections
import unittest
from datetime import datetime
def timestamp_to_datetime(ts):
return datetime(ts.year, ts.month, ts.day, ts.hour, ts.minute)
class Dom:
def __init__(self, *, props=None, children=None):
self.props = props
self.children = children
if isinstance(self.children, HL):
self.children = [self.children]
def assert_matches(self, test_case: unittest.TestCase, doc):
# Check properties
if self.props is None:
test_case.assertEqual(len(doc.getProperties()), 0)
else:
doc_props = doc.getProperties()
test_case.assertEqual(len(doc_props), len(self.props))
for i, prop in enumerate(self.props):
test_case.assertEqual(doc_props[i].name, prop[0])
test_case.assertEqual(doc_props[i].value, prop[1])
# @TODO: Check properties
# Check children
if self.children is None:
test_case.assertEqual(len(doc.getTopHeadlines()), 0, "Top")
else:
doc_headlines = doc.getTopHeadlines()
test_case.assertEqual(len(doc_headlines), len(self.children),
"Top")
for i, children in enumerate(self.children):
children.assert_matches(test_case, doc_headlines[i])
class HL:
def __init__(self, title, *, props=None, content=None, children=None):
self.title = title
self.props = props
self.content = content
self.children = children
def assert_matches(self, test_case: unittest.TestCase, doc):
test_case.assertEqual(self.title, doc['title'])
# Check properties
if self.props is None:
test_case.assertEqual(len(doc['properties']), 0)
else:
doc_props = doc['properties']
test_case.assertEqual(len(doc_props), len(self.props))
for i, prop in enumerate(self.props):
test_case.assertEqual(doc_props[i].name, prop[0])
if isinstance(prop[1], datetime):
test_case.assertEqual(
timestamp_to_datetime(doc_props[i].value), prop[1])
# @TODO: Check properties
# Check children
if self.children is None:
test_case.assertEqual(len(doc['children']), 0)
else:
doc_headlines = doc['children']
test_case.assertEqual(len(doc_headlines), len(self.children),
self.title)
for i, children in enumerate(self.children):
children.assert_matches(test_case, doc_headlines[i])