forked from kenkeiras/org-rw
Add base support for markup tests.
This commit is contained in:
parent
d23ee1adba
commit
0dab7e4703
6 changed files with 209 additions and 22 deletions
21
tests/02-markup.org
Normal file
21
tests/02-markup.org
Normal file
|
@ -0,0 +1,21 @@
|
|||
#+TITLE: 02-Markup
|
||||
#+DESCRIPTION: Simple org file to test markup
|
||||
#+TODO: TODO(t) PAUSED(p) | DONE(d)
|
||||
|
||||
|
||||
* First level
|
||||
:PROPERTIES:
|
||||
:ID: 02-markup-first-level-id
|
||||
:CREATED: [2020-01-01 Wed 01:01]
|
||||
:END:
|
||||
This is a *bold phrase*.
|
||||
|
||||
This is a =verbatim phrase=.
|
||||
|
||||
This is a /italic phrase/.
|
||||
|
||||
This is a +strike-through phrase+.
|
||||
|
||||
This is a _underlined phrase_.
|
||||
|
||||
This is a ~code phrase~.
|
|
@ -4,7 +4,8 @@ import unittest
|
|||
from datetime import datetime as DT
|
||||
|
||||
from org_dom import dumps, load, loads
|
||||
from utils.dom_assertions import HL, Dom
|
||||
from utils.dom_assertions import (BOLD, CODE, HL, ITALIC, SPAN, STRIKE,
|
||||
UNDERLINED, VERBATIM, Dom)
|
||||
|
||||
DIR = os.path.dirname(os.path.abspath(__file__))
|
||||
|
||||
|
@ -23,15 +24,15 @@ class TestSerde(unittest.TestCase):
|
|||
('ID', '01-simple-first-level-id'),
|
||||
('CREATED', DT(2020, 1, 1, 1, 1)),
|
||||
],
|
||||
content='First level content',
|
||||
content=' First level content\n',
|
||||
children=[
|
||||
HL('Second level',
|
||||
props=[('ID', '01-simple-second-level-id')],
|
||||
content='Second level content',
|
||||
content='\n Second level content\n',
|
||||
children=[
|
||||
HL('Third level',
|
||||
props=[('ID', '01-simple-third-level-id')],
|
||||
content='Third level content')
|
||||
content='\n Third level content\n')
|
||||
])
|
||||
])))
|
||||
|
||||
|
@ -44,3 +45,38 @@ class TestSerde(unittest.TestCase):
|
|||
doc = loads(orig)
|
||||
|
||||
self.assertEqual(dumps(doc), orig)
|
||||
|
||||
def test_markup_file_02(self):
|
||||
with open(os.path.join(DIR, '02-markup.org')) as f:
|
||||
doc = load(f)
|
||||
|
||||
ex = Dom(props=[('TITLE', '02-Markup'),
|
||||
('DESCRIPTION', 'Simple org file to test markup'),
|
||||
('TODO', 'TODO(t) PAUSED(p) | DONE(d)')],
|
||||
children=(HL('First level',
|
||||
props=[
|
||||
('ID', '02-markup-first-level-id'),
|
||||
('CREATED', DT(2020, 1, 1, 1, 1)),
|
||||
],
|
||||
content=[
|
||||
SPAN(" This is a ", BOLD("bold phrase"),
|
||||
"."),
|
||||
SPAN(""),
|
||||
SPAN(" This is a ",
|
||||
VERBATIM("verbatim phrase"), "."),
|
||||
SPAN(""),
|
||||
SPAN(" This is a ", ITALIC("italic phrase"),
|
||||
"."),
|
||||
SPAN(""),
|
||||
SPAN(" This is a ",
|
||||
STRIKE("strike-through phrase"), "."),
|
||||
SPAN(""),
|
||||
SPAN(" This is a ",
|
||||
UNDERLINED("underlined phrase"), "."),
|
||||
SPAN(""),
|
||||
SPAN(" This is a ", CODE("code phrase"),
|
||||
"."),
|
||||
SPAN(""),
|
||||
])))
|
||||
|
||||
ex.assert_matches(self, doc)
|
||||
|
|
|
@ -2,6 +2,8 @@ import collections
|
|||
import unittest
|
||||
from datetime import datetime
|
||||
|
||||
from org_dom import get_raw_contents
|
||||
|
||||
|
||||
def timestamp_to_datetime(ts):
|
||||
return datetime(ts.year, ts.month, ts.day, ts.hour, ts.minute)
|
||||
|
@ -48,13 +50,13 @@ class HL:
|
|||
self.children = children
|
||||
|
||||
def assert_matches(self, test_case: unittest.TestCase, doc):
|
||||
test_case.assertEqual(self.title, doc['title'])
|
||||
test_case.assertEqual(self.title, doc.title)
|
||||
|
||||
# Check properties
|
||||
if self.props is None:
|
||||
test_case.assertEqual(len(doc['properties']), 0)
|
||||
test_case.assertEqual(len(doc.properties), 0)
|
||||
else:
|
||||
doc_props = doc['properties']
|
||||
doc_props = doc.properties
|
||||
test_case.assertEqual(len(doc_props), len(self.props))
|
||||
|
||||
for i, prop in enumerate(self.props):
|
||||
|
@ -63,15 +65,84 @@ class HL:
|
|||
test_case.assertEqual(
|
||||
timestamp_to_datetime(doc_props[i].value), prop[1])
|
||||
|
||||
# @TODO: Check properties
|
||||
if isinstance(self.content, str):
|
||||
test_case.assertEqual(get_raw_contents(doc), self.content)
|
||||
else:
|
||||
test_case.assertEqual(len(doc.contents), len(self.content))
|
||||
for i, content in enumerate(self.content):
|
||||
test_case.assertEqual(get_raw_contents(doc.contents[i]),
|
||||
content.to_raw())
|
||||
|
||||
# Check children
|
||||
if self.children is None:
|
||||
test_case.assertEqual(len(doc['children']), 0)
|
||||
test_case.assertEqual(len(doc.children), 0)
|
||||
else:
|
||||
doc_headlines = doc['children']
|
||||
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])
|
||||
|
||||
|
||||
class SPAN:
|
||||
def __init__(self, *kwargs):
|
||||
self.contents = kwargs
|
||||
|
||||
def to_raw(self):
|
||||
chunks = []
|
||||
for section in self.contents:
|
||||
if isinstance(section, str):
|
||||
chunks.append(section)
|
||||
else:
|
||||
chunks.append(section.to_raw())
|
||||
|
||||
return ''.join(chunks)
|
||||
|
||||
|
||||
class BOLD:
|
||||
def __init__(self, text):
|
||||
self.text = text
|
||||
|
||||
def to_raw(self):
|
||||
return '*{}*'.format(self.text)
|
||||
|
||||
|
||||
class CODE:
|
||||
def __init__(self, text):
|
||||
self.text = text
|
||||
|
||||
def to_raw(self):
|
||||
return '~{}~'.format(self.text)
|
||||
|
||||
|
||||
class ITALIC:
|
||||
def __init__(self, text):
|
||||
self.text = text
|
||||
|
||||
def to_raw(self):
|
||||
return '/{}/'.format(self.text)
|
||||
|
||||
|
||||
class STRIKE:
|
||||
def __init__(self, text):
|
||||
self.text = text
|
||||
|
||||
def to_raw(self):
|
||||
return '+{}+'.format(self.text)
|
||||
|
||||
|
||||
class UNDERLINED:
|
||||
def __init__(self, text):
|
||||
self.text = text
|
||||
|
||||
def to_raw(self):
|
||||
return '_{}_'.format(self.text)
|
||||
|
||||
|
||||
class VERBATIM:
|
||||
def __init__(self, text):
|
||||
self.text = text
|
||||
|
||||
def to_raw(self):
|
||||
return '={}='.format(self.text)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue