tests: Add basic tests for vwtags

This commit is contained in:
Tomas Janousek 2020-07-29 21:00:35 +02:00 committed by Tomas Babej
parent b48123fd58
commit 91f98d54b3
3 changed files with 113 additions and 38 deletions

View file

@ -1,3 +1,3 @@
[run]
source = taskwiki
source = taskwiki, extra
data_file = /tmp/taskwiki-coverage/.coverage

View file

@ -4,52 +4,60 @@
import sys
import re
if len(sys.argv) < 3:
exit()
syntax = sys.argv[1]
filename = sys.argv[2]
rx_default_media = r"^\s*(={1,6})([^=].*[^=])\1\s*$"
rx_markdown = r"^\s*(#{1,6})([^#].*)$"
if syntax in ("default", "media"):
rx_header = re.compile(rx_default_media)
elif syntax == "markdown":
rx_header = re.compile(rx_markdown)
else:
rx_header = re.compile(rx_default_media + "|" + rx_markdown)
file_content = []
try:
with open(filename, "r") as vim_buffer:
file_content = vim_buffer.readlines()
except:
exit()
def process(file_content, filename, syntax):
if syntax in ("default", "media"):
rx_header = re.compile(rx_default_media)
elif syntax == "markdown":
rx_header = re.compile(rx_markdown)
else:
rx_header = re.compile(rx_default_media + "|" + rx_markdown)
state = [""]*6
for lnum, line in enumerate(file_content):
state = [""]*6
for lnum, line in enumerate(file_content):
match_header = rx_header.match(line)
match_header = rx_header.match(line)
if not match_header:
continue
if not match_header:
continue
match_lvl = match_header.group(1) or match_header.group(3)
match_tag = match_header.group(2) or match_header.group(4)
match_lvl = match_header.group(1) or match_header.group(3)
match_tag = match_header.group(2) or match_header.group(4)
cur_lvl = len(match_lvl)
cur_tag = match_tag.strip()
cur_searchterm = "^" + match_header.group(0).rstrip("\r\n") + "$"
cur_kind = "h"
cur_lvl = len(match_lvl)
cur_tag = match_tag.strip()
cur_searchterm = "^" + match_header.group(0).rstrip("\r\n") + "$"
cur_kind = "h"
state[cur_lvl-1] = cur_tag
for i in range(cur_lvl, 6):
state[i] = ""
state[cur_lvl-1] = cur_tag
for i in range(cur_lvl, 6):
state[i] = ""
scope = "&&&".join(
[state[i] for i in range(0, cur_lvl-1) if state[i] != ""])
if scope:
scope = "\theader:" + scope
scope = "&&&".join(
[state[i] for i in range(0, cur_lvl-1) if state[i] != ""])
if scope:
scope = "\theader:" + scope
print('{0}\t{1}\t/{2}/;"\t{3}\tline:{4}{5}'.format(
cur_tag, filename, cur_searchterm, cur_kind, str(lnum+1), scope))
yield('{0}\t{1}\t/{2}/;"\t{3}\tline:{4}{5}'.format(
cur_tag, filename, cur_searchterm, cur_kind, str(lnum+1), scope))
if __name__ == '__main__':
if len(sys.argv) < 3:
exit()
syntax = sys.argv[1]
filename = sys.argv[2]
file_content = []
try:
with open(filename, "r") as vim_buffer:
file_content = vim_buffer.readlines()
except:
exit()
for output in process(file_content, filename, syntax):
print(output)

67
tests/test_vwtags.py Normal file
View file

@ -0,0 +1,67 @@
# -*- coding: utf-8 -*-
import re
import textwrap
from itertools import zip_longest
from extra import vwtags
class TagsTest(object):
wiki_input = None
expected_output = None
markup = 'default'
def test_execute(self):
wiki_input = textwrap.dedent(self.wiki_input).splitlines()
got_output = list(vwtags.process(wiki_input, "file.wiki", self.markup))
if isinstance(self.expected_output, str):
expected_output = textwrap.dedent(self.expected_output).splitlines()
else:
expected_output = self.expected_output
explanation = {'got_output': got_output, 'expected_output': expected_output}
for line, (got, expected) in enumerate(zip_longest(got_output, expected_output)):
explanation['line'] = line
if hasattr(expected, 'search'):
assert expected.search(got) is not None, explanation
else:
assert got == expected, explanation
class MultiSyntaxTagsTest(TagsTest):
def test_execute(self, test_syntax):
markup, header_expand = test_syntax
self.markup = markup
self.wiki_input = header_expand(self.wiki_input)
super(MultiSyntaxTagsTest, self).test_execute()
class TestTagsEmpty(TagsTest):
wiki_input = ""
expected_output = ""
class TestTagsBasic(MultiSyntaxTagsTest):
wiki_input = """\
HEADER1(a)
HEADER2(b)
HEADER2(c)
HEADER3(d)
not HEADER2(e)
HEADER2(f)
HEADER1(g)
HEADER3(h)
"""
expected_output = [
re.compile(r"^a file.wiki /.*/;\" h line:1$"),
re.compile(r"^b file.wiki /.*/;\" h line:2 header:a$"),
re.compile(r"^c file.wiki /.*/;\" h line:3 header:a$"),
re.compile(r"^d file.wiki /.*/;\" h line:4 header:a&&&c$"),
re.compile(r"^f file.wiki /.*/;\" h line:6 header:a$"),
re.compile(r"^g file.wiki /.*/;\" h line:7$"),
re.compile(r"^h file.wiki /.*/;\" h line:8 header:g$"),
]