org-rw/tests/utils/dom_assertions.py

182 lines
5.6 KiB
Python
Raw Normal View History

2020-06-21 19:27:40 +00:00
import collections
import unittest
from datetime import datetime
2020-10-09 22:39:32 +00:00
from org_dom import Line, Text, Bold, Code, Italic, Strike, Underlined, Verbatim, get_raw_contents
2020-06-27 17:20:34 +00:00
2020-06-21 19:27:40 +00:00
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].key, prop[0])
2020-06-21 19:27:40 +00:00
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):
2020-06-27 17:20:34 +00:00
test_case.assertEqual(self.title, doc.title)
2020-06-21 19:27:40 +00:00
# Check properties
if self.props is None:
2020-06-27 17:20:34 +00:00
test_case.assertEqual(len(doc.properties), 0)
2020-06-21 19:27:40 +00:00
else:
2020-06-27 17:20:34 +00:00
doc_props = doc.properties
2020-06-21 19:27:40 +00:00
test_case.assertEqual(len(doc_props), len(self.props))
for i, prop in enumerate(self.props):
test_case.assertEqual(doc_props[i].key, prop[0])
2020-06-21 19:27:40 +00:00
if isinstance(prop[1], datetime):
test_case.assertEqual(
timestamp_to_datetime(doc_props[i].value), prop[1])
2020-06-27 17:20:34 +00:00
if isinstance(self.content, str):
test_case.assertEqual(get_raw_contents(doc), self.content)
else:
2020-10-09 22:39:32 +00:00
if len(doc.contents) != len(self.content):
print("Contents:", doc.contents)
print("Expected:", self.content)
2020-06-27 17:20:34 +00:00
test_case.assertEqual(len(doc.contents), len(self.content))
for i, content in enumerate(self.content):
2020-10-09 22:39:32 +00:00
content.assert_matches(test_case, doc.contents[i])
2020-06-21 19:27:40 +00:00
# Check children
if self.children is None:
2020-06-27 17:20:34 +00:00
test_case.assertEqual(len(doc.children), 0)
2020-06-21 19:27:40 +00:00
else:
2020-06-27 17:20:34 +00:00
doc_headlines = doc.children
2020-06-21 19:27:40 +00:00
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])
2020-06-27 17:20:34 +00:00
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)
2020-10-09 22:39:32 +00:00
def assert_matches(self, test_case, doc):
if not isinstance(doc, Line):
return False
for i, section in enumerate(self.contents):
if isinstance(section, str):
test_case.assertTrue(isinstance(doc.contents[i], Text))
test_case.assertEqual(section, doc.contents[i].get_raw())
else:
section.assertEqual(test_case, doc.contents[i])
2020-06-27 17:20:34 +00:00
class BOLD:
def __init__(self, text):
self.text = text
def to_raw(self):
return '*{}*'.format(self.text)
2020-10-09 22:39:32 +00:00
def assertEqual(self, test_case, other):
test_case.assertTrue(isinstance(other, Bold))
test_case.assertEqual(self.text, other.contents)
2020-06-27 17:20:34 +00:00
class CODE:
def __init__(self, text):
self.text = text
def to_raw(self):
return '~{}~'.format(self.text)
2020-10-09 22:39:32 +00:00
def assertEqual(self, test_case, other):
test_case.assertTrue(isinstance(other, Code))
test_case.assertEqual(self.text, other.contents)
2020-06-27 17:20:34 +00:00
class ITALIC:
def __init__(self, text):
self.text = text
def to_raw(self):
return '/{}/'.format(self.text)
2020-10-09 22:39:32 +00:00
def assertEqual(self, test_case, other):
test_case.assertTrue(isinstance(other, Italic))
test_case.assertEqual(self.text, other.contents)
2020-06-27 17:20:34 +00:00
class STRIKE:
def __init__(self, text):
self.text = text
def to_raw(self):
return '+{}+'.format(self.text)
2020-10-09 22:39:32 +00:00
def assertEqual(self, test_case, other):
test_case.assertTrue(isinstance(other, Strike))
test_case.assertEqual(self.text, other.contents)
2020-06-27 17:20:34 +00:00
class UNDERLINED:
def __init__(self, text):
self.text = text
def to_raw(self):
return '_{}_'.format(self.text)
2020-10-09 22:39:32 +00:00
def assertEqual(self, test_case, other):
test_case.assertTrue(isinstance(other, Underlined))
test_case.assertEqual(self.text, other.contents)
2020-06-27 17:20:34 +00:00
class VERBATIM:
def __init__(self, text):
self.text = text
def to_raw(self):
return '={}='.format(self.text)
2020-10-09 22:39:32 +00:00
def assertEqual(self, test_case, other):
test_case.assertTrue(isinstance(other, Verbatim))
test_case.assertEqual(self.text, other.contents)