org-rw/tests/utils/assertions.py

232 lines
6.5 KiB
Python
Raw Normal View History

2020-06-21 19:27:40 +00:00
import collections
import unittest
from datetime import datetime
2020-12-20 11:39:47 +00:00
from org_rw import (Bold, Code, Italic, Line, Strike, Text, 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)
2020-10-25 19:23:08 +00:00
def get_raw(doc):
if isinstance(doc, str):
return doc
elif isinstance(doc, list):
return "".join([get_raw(e) for e in doc])
2020-10-25 19:23:08 +00:00
else:
return doc.get_raw()
2020-06-21 19:27:40 +00:00
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")
2020-06-21 19:27:40 +00:00
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-21 19:27:40 +00:00
2020-10-25 19:23:08 +00:00
test_case.assertEqual(get_raw_contents(doc), self.get_raw())
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
test_case.assertEqual(len(doc_headlines), len(self.children), self.title)
2020-06-21 19:27:40 +00:00
for i, children in enumerate(self.children):
children.assert_matches(test_case, doc_headlines[i])
2020-06-27 17:20:34 +00:00
2020-10-25 19:23:08 +00:00
def get_raw(self):
return "".join(map(get_raw, self.content))
2020-10-25 19:23:08 +00:00
2020-06-27 17:20:34 +00:00
class SPAN:
def __init__(self, *kwargs):
self.contents = kwargs
2020-10-25 19:23:08 +00:00
def get_raw(self):
2020-06-27 17:20:34 +00:00
chunks = []
for section in self.contents:
if isinstance(section, str):
chunks.append(section)
elif isinstance(section, list):
for subsection in section:
if isinstance(subsection, str):
chunks.append(subsection)
else:
chunks.append(subsection.get_raw())
2020-06-27 17:20:34 +00:00
else:
2020-10-25 19:23:08 +00:00
chunks.append(section.get_raw())
2020-06-27 17:20:34 +00:00
return "".join(chunks)
2020-06-27 17:20:34 +00:00
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
2020-10-25 19:23:08 +00:00
def get_raw(self):
return "*{}*".format(get_raw(self.text))
2020-06-27 17:20:34 +00:00
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
2020-10-25 19:23:08 +00:00
def get_raw(self):
return "~{}~".format(get_raw(self.text))
2020-06-27 17:20:34 +00:00
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
2020-06-27 17:20:34 +00:00
class ITALIC:
def __init__(self, text):
self.text = text
2020-10-25 19:23:08 +00:00
def get_raw(self):
return "/{}/".format(get_raw(self.text))
2020-06-27 17:20:34 +00:00
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
2020-06-27 17:20:34 +00:00
class STRIKE:
def __init__(self, text):
self.text = text
2020-10-25 19:23:08 +00:00
def get_raw(self):
return "+{}+".format(get_raw(self.text))
2020-06-27 17:20:34 +00:00
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
2020-10-25 19:23:08 +00:00
def get_raw(self):
return "_{}_".format(get_raw(self.text))
2020-06-27 17:20:34 +00:00
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
2020-06-27 17:20:34 +00:00
class VERBATIM:
def __init__(self, text):
self.text = text
2020-10-25 19:23:08 +00:00
def get_raw(self):
return "={}=".format(get_raw(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)
2020-10-25 19:23:08 +00:00
2020-10-25 19:23:08 +00:00
class WEB_LINK:
def __init__(self, text, link):
self.text = text
self.link = link
def get_raw(self):
if self.text:
return "[[{}][{}]]".format(self.link, self.text)
else:
return "[[{}]]".format(self.link)
2020-10-25 19:23:08 +00:00
def assertEqual(self, test_case, other):
test_case.assertTrue(isinstance(other, WebLink))
test_case.assertEqual(self.text, other.contents)
test_case.assertEqual(self.link, other.link)
class Tokens:
BOLD_END = "*"
BOLD_START = "*"
VERBATIM_START = "="
VERBATIM_END = "="
ITALIC_START = "/"
ITALIC_END = "/"
STRIKE_START = "+"
STRIKE_END = "+"
UNDERLINED_START = "_"
UNDERLINED_END = "_"
CODE_START = "~"
CODE_END = "~"