Commit f1116867 authored by Denis Salem's avatar Denis Salem
Browse files

done with pattern processor unit test

parent e0420b3e
......@@ -150,7 +150,7 @@
DONE | Fix pattern (i.e .:GetEntryTitle:.) access in template's metadata.
DONE | Infinite scroll use html anchor instead of harcoded indexing.
DONE | Prevent Infinite scroll to block when ressource isn't available.
WIP | Rewrite pattern processor unit tests.
DONE | Rewrite pattern processor unit tests.
TODO | Open with navigator manual.
TODO | Client-side search engine.
TODO | Reorganize themes dependencies in setup (adding default templates, scripts, and pages to include).
......
......@@ -27,6 +27,7 @@ from venc2.datastore.theme import themes_descriptor
from venc2.datastore.theme import Theme
from venc2.helpers import notify
from venc2.helpers import rm_tree_error_handler
from venc2.helpers import keep_appart_from_markup
from venc2.l10n import messages
from venc2.patterns.code_highlight import CodeHighlight
from venc2.patterns.latex2mathml import Latex2MathML
......@@ -147,6 +148,7 @@ def export_blog(argv=list()):
# Set up of non-contextual patterns
processor = Processor()
for pattern_name in non_contextual_pattern_names_datastore.keys():
processor.set_function(pattern_name, getattr(datastore, non_contextual_pattern_names_datastore[pattern_name]))
......@@ -161,6 +163,9 @@ def export_blog(argv=list()):
for pattern_name in contextual_pattern_names.keys():
processor.blacklist.append(pattern_name)
for pattern_name in keep_appart_from_markup:
processor.keep_appart_from_markup.append(pattern_name)
processor.blacklist.append("Escape")
notify(messages.pre_process)
......
......@@ -31,6 +31,8 @@ class GenericMessage(Exception):
def __init__(self, message):
self.message = message
keep_appart_from_markup = ["CodeHighlight", "Latex2MathML", "IncludeFile", "audio", "video","EmbedContent"]
msg_format = {
"END" : '\033[0m',
"GREEN" : '\033[92m',
......
......@@ -169,7 +169,6 @@ class ProcessedString():
self.open_pattern_pos, self.close_pattern_pos, self.len_open_pattern_pos, self.len_close_pattern_pos, self.string = self.backup
self.backup = None
def keep_appart_from_markup_indexes_append(self, paragraphe, new_chunk, escape):
if escape:
new_chunk = cgi_escape(new_chunk)
......@@ -211,46 +210,31 @@ class ProcessedString():
class Processor():
def __init__(self):
self.debug = False
self.functions = dict()
self.current_input_string = str()
self.ressource = str()
self.blacklist = list()
self.functions = {}
self.current_input_string = ''
self.ressource = ''
self.blacklist = []
self.keep_appart_from_markup = []
# Run any pattern and catch exception nicely
def run_pattern(self, pattern, argv):
try:
try: # Should be refactored, Create a base PatternException
output = self.functions[pattern](argv)
except UnknownContextual as e:
output = self.handle_error(
e,
messages.unknown_contextual.format(e),
",;"+pattern+";;"+";;".join(argv)+";,",
error_origin = "{0["+str(e)[1:-1]+']}'
)
except KeyError as e:
if str(e)[1:-1] == pattern:
output = self.handle_error(
e,
messages.unknown_pattern.format(pattern),
",;"+pattern+";;"+";;".join(argv)+";,",
error_origin = [':'+pattern+':']
)
else:
output = self.handle_error(
e,
messages.unknown_contextual.format(e),
",;"+pattern+";;"+";;".join(argv)+";,",
error_origin = [':'+str(e)[1:-1]+':','{0['+str(e)[1:-1]+']}', ':'+pattern+':'] # first item might be useless ???
)
output = self.handle_error(
e,
messages.unknown_pattern.format(pattern),
",;"+pattern+";;"+";;".join(argv)+";,",
error_origin = [':'+pattern+':']
)
except AttributeError as e:
except UnknownContextual as e:
output = self.handle_error(
e,
messages.unknown_contextual.format(e),
",;"+pattern+";;"+";;".join(argv)+";,",
error_origin = [str(e).split("'")[-2], ':'+pattern+':']
error_origin = "{0["+str(e)[1:-1]+']}'
)
except PatternMissingArguments as e:
......@@ -358,7 +342,7 @@ class Processor():
current_pattern = fields[0]
if (not current_pattern in ["GetRelativeOrigin","Escape"]) and (not current_pattern in self.blacklist):
if current_pattern in ["CodeHighlight", "Latex2MathML", "IncludeFile", "audio", "video","EmbedContent"]:
if current_pattern in self.keep_appart_from_markup:
new_chunk = pre_processed.keep_appart_from_markup_indexes_append(
True,
self.run_pattern(current_pattern, fields[1:]),
......
#! /usr/bin/python3
from venc2.patterns.processor import ProcessedString # The object holding the string and its states
from venc2.patterns.processor import ProcessedString # The object holding the string and its states.
from venc2.patterns.processor import Processor # The actual string processor, holding binded methods.
from venc2.patterns.exceptions import MalformedPatterns
from venc2.patterns.exceptions import PatternMissingArguments
from venc2.patterns.exceptions import PatternInvalidArgument
from venc2.patterns.exceptions import UnknownContextual
from venc2.helpers import GenericMessage
from test_engine import run_tests
from copy import deepcopy
def add(argv):
try:
a, b = tuple(argv)
......@@ -46,11 +51,14 @@ def upper(argv):
return a.upper()
def trigger_generic_message_exception(argv):
raise GenericMessage("lol il will never work")
raise GenericMessage("lol it will never work")
def trigger_pattern_invalid_argument_exception(argv):
raise PatternInvalidArgument("some field", "some value", "some message")
def trigger_unknown_contextual_exception(argv):
raise UnknownContextual()
processor = Processor()
processor.debug = True
processor.set_function("add", add)
......@@ -59,6 +67,7 @@ processor.set_function("greater", greater)
processor.set_function("upper", upper)
processor.set_function("trigger_generic_message_exception", trigger_generic_message_exception)
processor.set_function("trigger_pattern_invalid_argument_exception", trigger_pattern_invalid_argument_exception)
processor.set_function("trigger_unknown_contextual_exception", trigger_unknown_contextual_exception)
processor.blacklist.append("blacklisted")
def test_pattern_processor(args, test_name):
......@@ -75,6 +84,17 @@ def test_pattern_processor_restore(args, test_name):
ps.restore()
return states == (ps.open_pattern_pos, ps.close_pattern_pos, ps.len_open_pattern_pos, ps.len_close_pattern_pos, ps.string)
def test_markup_language(args, test_name):
input_value, markup_language, preserved = args
ps = ProcessedString(input_value, test_name, False)
p = deepcopy(processor)
p.keep_appart_from_markup = preserved
p.process(ps)
ps.process_markup_language(markup_language)
return ps.string.replace('\n', '')
tests = [
(
"Simple pattern detection.",
......@@ -193,7 +213,7 @@ tests = [
(
"Trigger GenericMessage.",
("moo .:trigger_generic_message_exception:. foo", True),
(GenericMessage, [("message","lol il will never work")]),
(GenericMessage, [("message","lol it will never work")]),
test_pattern_processor
),
(
......@@ -202,6 +222,42 @@ tests = [
(PatternInvalidArgument, [("message","some message"), ("name", "some field"), ("value", "some value")]),
test_pattern_processor
),
(
"Trigger KeyError when unknown pattern is met.",
("moo .:UnknownPattern:. foo", True),
(KeyError, []),
test_pattern_processor
),
(
"Trigger UnknownContextual when unknown pattern is met.",
("moo .:trigger_unknown_contextual_exception:. foo", True),
(UnknownContextual, []),
test_pattern_processor
),
(
"Markdown integration.",
("# Main title\n.:add::1::1:.", "Markdown", []),
"<h1>Main title</h1><p>2</p>",
test_markup_language
),
(
"Markdown integration when pattern produce html.",
("# Main title\n.:add::1::1:.", "Markdown", ["add"]),
"<h1>Main title</h1>2",
test_markup_language
),
(
"reStructuredText integration.",
("Main title\n==========\n.:add::1::1:.", "reStructuredText", []),
"<div class=\"document\"><div class=\"section\" id=\"main-title\"><h1>Main title</h1><p>2</p></div></div>",
test_markup_language
),
(
"reStructuredText integration.",
("Main title\n==========\n.:add::1::1:.", "reStructuredText", ["add"]),
"<div class=\"document\"><div class=\"section\" id=\"main-title\"><h1>Main title</h1>2</div></div>",
test_markup_language
),
]
run_tests("Testing patterns processor...", tests)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment