Added some cursed frame inspection to get line numbers, formatted some tests.

This commit is contained in:
Maximilien Rigaut 2022-03-19 09:33:47 +01:00
parent 8e694a87da
commit 5694596f90
2 changed files with 198 additions and 96 deletions

View File

@ -164,6 +164,8 @@ def of_type(token_class):
:return: returns a function creating an instance of the given class :return: returns a function creating an instance of the given class
""" """
def _of_type(_, __, content): def _of_type(_, __, content):
if len(content) == 0: # Drop Empty token
return None
return token_class(content) return token_class(content)
return _of_type return _of_type
@ -199,12 +201,18 @@ url_characters = pp.common.url
# Composite elements # Composite elements
var = '[' + pp.delimitedList(assignation ^ value)("list_vars").set_name("list_vars") + ']' var = '[' + pp.delimitedList(assignation ^ value)("list_vars").set_name("list_vars") + ']'
# Specific elements # Specific elements
image_element = ('@{' + pp.common.identifier('image_name') + '}')("image_element") image_element = ('@{' + pp.common.identifier('image_name') + '}')("image_element")
alias_element = ('@[' + pp.common.identifier('alias_name') + ']')("alias_element") alias_element = ('@[' + pp.common.identifier('alias_name') + ']')("alias_element")
expression = pp.Word(pp.alphanums + r'=+-_\'",;:!<> ') expression = pp.Word(pp.alphanums + r'=+-_\'",;:!<> ')
html_insert = '{' + expression('html_insert') + '}' html_insert = '{' + expression('html_insert') + '}'
# Optional elements
optional = (
pp.Opt(html_insert)("html_insert") & pp.Opt(var)("var")
)("optional").add_parse_action(of_type(OptionalToken))
# Structural elements
structural_elements = ( structural_elements = (
pp.CaselessLiteral('div') | pp.CaselessLiteral('div') |
pp.CaselessLiteral('article') | pp.CaselessLiteral('article') |
@ -214,12 +222,6 @@ structural_elements = (
header_element = pp.Word('#') header_element = pp.Word('#')
display_element = pp.Word('!') display_element = pp.Word('!')
# Optional elements
optional = pp.Opt(
pp.Opt(html_insert)("html_insert") + pp.Opt(var)("var")
)("optional").add_parse_action(of_type(OptionalToken))
# Inline elements # Inline elements
il_link = pp.Regex( il_link = pp.Regex(
r"""\[(?P<text>.+)\]\(['"](?P<url>[a-zA-Z-_:\/=@#!%\?\d\(\)\.]+)['"]\)""" r"""\[(?P<text>.+)\]\(['"](?P<url>[a-zA-Z-_:\/=@#!%\?\d\(\)\.]+)['"]\)"""
@ -233,6 +235,7 @@ et_strikethrough = pp.Literal('~~')('strikethrough').add_parse_action(of_type(Et
et_custom_span = ( et_custom_span = (
pps('(#') + pp.Word(pp.nums)('span_id') + pps(')') pps('(#') + pp.Word(pp.nums)('span_id') + pps(')')
).set_name('custom_span').add_parse_action(of_type(EtCustomSpanToken)) ).set_name('custom_span').add_parse_action(of_type(EtCustomSpanToken))
# markup sums up all in-line elements # markup sums up all in-line elements
markup = il_link | et_strong | et_em | et_strikethrough | et_underline | et_custom_span markup = il_link | et_strong | et_em | et_strikethrough | et_underline | et_custom_span
enhanced_text = pp.ZeroOrMore( enhanced_text = pp.ZeroOrMore(
@ -241,17 +244,18 @@ enhanced_text = pp.ZeroOrMore(
# Multiline elements # Multiline elements
se_start = (pps('<<') + structural_elements).add_parse_action(of_type(StructuralElementStartToken)) se_start = (pps('<<') + structural_elements).add_parse_action(of_type(StructuralElementStartToken))
se_end = (structural_elements + pps('>>')).add_parse_action(of_type(StructuralElementEndToken)) # TODO: + pp.Opt(optional) se_end = (structural_elements + pps('>>')).add_parse_action(of_type(StructuralElementEndToken)) + optional
se = se_end | se_start # Structural element se = se_end | se_start # Structural element
table_row = pp.OneOrMore( table_row = pp.OneOrMore(
# pp.Regex(r'\|(\d)?')('table_colspan') + # pp.Regex(r'\|(\d)?')('table_colspan') +
(pp.Combine(pps('|') + pp.Word(pp.nums)('table_colspan')) | pps('|')) + (pp.Combine(pps('|') + pp.Word(pp.nums)('table_colspan')) | pps('|')) +
pp.SkipTo('|')('table_cell').add_parse_action(of_type(TableCellToken)) # TODO: account for enhanced text pp.SkipTo('|')('table_cell').add_parse_action(of_type(TableCellToken)) # TODO: account for enhanced text
).add_parse_action(of_type(TableRowToken)) + pps('|') # TODO: + pp.Opt(optional) ).add_parse_action(of_type(TableRowToken)) + pps('|') + pp.Opt(optional)
table_separator = pp.OneOrMore( table_separator = pp.OneOrMore(
pps('|') + pp.Word(':-') pps('|') + pp.Word(':-')
)('table_separator').add_parse_action(of_type(TableSeparatorToken)) + pps('|') )('table_separator').add_parse_action(of_type(TableSeparatorToken)) + pps('|') # TODO: Tokenize table separator ?
table = table_separator | table_row table = table_separator | table_row
# multi_line sums up all multi-line elements # multi_line sums up all multi-line elements
multi_line = se | table multi_line = se | table
@ -259,7 +263,6 @@ multi_line = se | table
one_header = ( one_header = (
header_element + pp.SkipTo(pp.match_previous_literal(header_element)) header_element + pp.SkipTo(pp.match_previous_literal(header_element))
).add_parse_action(of_type(HeaderToken)) ).add_parse_action(of_type(HeaderToken))
# TODO : Add display to tests
one_display = ( one_display = (
display_element + pp.SkipTo(pp.match_previous_literal(display_element)) display_element + pp.SkipTo(pp.match_previous_literal(display_element))
).add_parse_action(of_type(DisplayToken)) ).add_parse_action(of_type(DisplayToken))
@ -269,8 +272,9 @@ one_olist = pp.line_start + (
one_ulist = pp.line_start + ( one_ulist = pp.line_start + (
pps(pp.Literal('-')) + enhanced_text('ulist_text') pps(pp.Literal('-')) + enhanced_text('ulist_text')
).add_parse_action(of_type(EtUlistToken)) ).add_parse_action(of_type(EtUlistToken))
# one_line sums up all one-line elements # one_line sums up all one-line elements
one_line = one_header | one_display | one_olist | one_ulist # TODO: + pp.Opt(optional) one_line = (one_header | one_display | one_olist | one_ulist) + pp.Opt(optional)
# Final elements # Final elements
line = one_line | multi_line | enhanced_text line = one_line | multi_line | enhanced_text

View File

@ -2,10 +2,19 @@ from itertools import zip_longest
import pyparsing import pyparsing
import pytest import pytest
import inspect
import bootstraparse.modules.syntax as sy import bootstraparse.modules.syntax as sy
ptp = pytest.param ptp = pytest.param
__XF = pytest.mark.xfail
# Cursed frame inspection
def __GL():
return inspect.getframeinfo(inspect.currentframe().f_back).lineno
############################################################################################################## ##############################################################################################################
@ -78,159 +87,242 @@ dict_advanced_syntax_input_and_expected_output = {
# Enhanced text # Enhanced text
"et_em": [ "et_em": [
# Single em element, should only match the first "*" # Single em element, should only match the first "*"
("*", (sy.EtEmToken(["*"]),)), ("*", (sy.EtEmToken(["*"]),), __GL()),
], ],
"et_strong": [ "et_strong": [
# Single strong element, should only match the first "**" # Single strong element, should only match the first "**"
("**", (sy.EtStrongToken(["**"]),)), ("**", (sy.EtStrongToken(["**"]),), __GL()),
], ],
"et_underline": [ "et_underline": [
# Single underline element, should only match the first "__" # Single underline element, should only match the first "__"
("__", (sy.EtUnderlineToken(["__"]),)), ("__", (sy.EtUnderlineToken(["__"]),), __GL()),
], ],
"et_strikethrough": [ "et_strikethrough": [
# Single strikethrough element, should only match the first "~~" # Single strikethrough element, should only match the first "~~"
("~~", (sy.EtStrikethroughToken(["~~"]),)), ("~~", (sy.EtStrikethroughToken(["~~"]),), __GL()),
], ],
"et_custom_span": [ "et_custom_span": [
# Single custom span element, should only match the first "(#number)" # Single custom span element, should only match the first "(#number)"
("(#12345)", (sy.EtCustomSpanToken(["12345"]),)), ("(#12345)", (sy.EtCustomSpanToken(["12345"]),), __GL()),
], ],
"il_link": [ "il_link": [
# Single link element, should only match the first "[link_name]('link')" # Single link element, should only match the first "[link_name]('link')"
("[text_link]('text://www.website.com/link.html')", ("[text_link]('text://www.website.com/link.html')",
[sy.HyperlinkToken(["[text_link]('text://www.website.com/link.html')"])]) [sy.HyperlinkToken(["[text_link]('text://www.website.com/link.html')"])], __GL())
], ],
# il_link | et_strong | et_em | et_strikethrough | et_underline | et_custom_span # il_link | et_strong | et_em | et_strikethrough | et_underline | et_custom_span
# Enhanced Text # Enhanced Text
"enhanced_text": [ "enhanced_text": [
# Matches a line of text, with or without inline elements # Matches a line of text, with or without inline elements
("*test*", (sy.EtEmToken(["*"]), sy.TextToken(["test"]), sy.EtEmToken(["*"]))), ("*test*", (sy.EtEmToken(["*"]), sy.TextToken(["test"]), sy.EtEmToken(["*"])), __GL()),
("**test**", (sy.EtStrongToken(["**"]), sy.TextToken(["test"]), sy.EtStrongToken(["**"]))), ("**test**", (sy.EtStrongToken(["**"]), sy.TextToken(["test"]), sy.EtStrongToken(["**"])), __GL()),
("__test__", (sy.EtUnderlineToken(["__"]), sy.TextToken(["test"]), sy.EtUnderlineToken(["__"]))), ("__test__", (sy.EtUnderlineToken(["__"]), sy.TextToken(["test"]), sy.EtUnderlineToken(["__"])), __GL()),
("~~test~~", (sy.EtStrikethroughToken(["~~"]), sy.TextToken(["test"]), sy.EtStrikethroughToken(["~~"]))), ("~~test~~", (sy.EtStrikethroughToken(["~~"]), sy.TextToken(["test"]), sy.EtStrikethroughToken(["~~"])), __GL()),
("(#12345)", (sy.EtCustomSpanToken(["12345"]),)), ("(#12345)", (sy.EtCustomSpanToken(["12345"]),), __GL()),
("*test*test*", (sy.EtEmToken(["*"]), sy.TextToken(["test"]), sy.EtEmToken(["*"]), sy.TextToken(["test"]), ("*test*test*", (sy.EtEmToken(["*"]), sy.TextToken(["test"]), sy.EtEmToken(["*"]), sy.TextToken(["test"]),
sy.EtEmToken(["*"]))), sy.EtEmToken(["*"])), __GL()),
("**test**test**", ("**test**test**",
(sy.EtStrongToken(["**"]), sy.TextToken(["test"]), sy.EtStrongToken(["**"]), sy.TextToken(["test"]), (sy.EtStrongToken(["**"]), sy.TextToken(["test"]), sy.EtStrongToken(["**"]), sy.TextToken(["test"]),
sy.EtStrongToken(["**"]),)), sy.EtStrongToken(["**"]),), __GL()),
("__test__test__", (sy.EtUnderlineToken(["__"]), sy.TextToken(["test"]), sy.EtUnderlineToken(["__"]), ("__test__test__", (sy.EtUnderlineToken(["__"]), sy.TextToken(["test"]), sy.EtUnderlineToken(["__"]),
sy.TextToken(["test"]), sy.EtUnderlineToken(["__"]),)), sy.TextToken(["test"]), sy.EtUnderlineToken(["__"]),), __GL()),
("~~test~~test~~", (sy.EtStrikethroughToken(["~~"]), sy.TextToken(["test"]), sy.EtStrikethroughToken(["~~"]), ("~~test~~test~~", (sy.EtStrikethroughToken(["~~"]), sy.TextToken(["test"]), sy.EtStrikethroughToken(["~~"]),
sy.TextToken(["test"]), sy.EtStrikethroughToken(["~~"]),)), sy.TextToken(["test"]), sy.EtStrikethroughToken(["~~"]),), __GL()),
("(#12345)test(#12345)", (sy.EtCustomSpanToken(["12345"]), sy.TextToken(["test"]), ("(#12345)test(#12345)", (sy.EtCustomSpanToken(["12345"]), sy.TextToken(["test"]),
sy.EtCustomSpanToken(["12345"]),)), sy.EtCustomSpanToken(["12345"]),), __GL()),
("__test(#12)test**test__test~~", (sy.EtUnderlineToken(["__"]), sy.TextToken(["test"]), ("__test(#12)test**test__test~~", (sy.EtUnderlineToken(["__"]), sy.TextToken(["test"]),
sy.EtCustomSpanToken(["12"]), sy.TextToken(["test"]), sy.EtCustomSpanToken(["12"]), sy.TextToken(["test"]),
sy.EtStrongToken(["**"]), sy.EtStrongToken(["**"]),
sy.TextToken(["test"]), sy.EtUnderlineToken(["__"]), sy.TextToken(["test"]), sy.EtUnderlineToken(["__"]),
sy.TextToken(["test"]), sy.EtStrikethroughToken(["~~"]),)), sy.TextToken(["test"]), sy.EtStrikethroughToken(["~~"]),), __GL()),
("Test text with a link [link_name]('link')", ("Test text with a link [link_name]('link')", (
(sy.TextToken(["Test text with a link"]), sy.HyperlinkToken(["[link_name]('link')"]),)), sy.TextToken(["Test text with a link"]),
# noqa E501 (line too long) sy.HyperlinkToken(["[link_name]('link')"]),
("Test text with a link [link_name](\"link\") and a *bold* text", (sy.TextToken(["Test text with a link"]), ), __GL()),
sy.HyperlinkToken(['[link_name]("link")']), ("Test text with a link [link_name](\"link\") and a *bold* text", (
sy.TextToken(["and a"]), sy.EtEmToken(["*"]), sy.TextToken(["Test text with a link"]),
sy.TextToken(["bold"]), sy.HyperlinkToken(['[link_name]("link")']),
sy.EtEmToken(["*"]), sy.TextToken(["and a"]), sy.EtEmToken(["*"]),
sy.TextToken(["text"]), sy.TextToken(["bold"]),
)), sy.EtEmToken(["*"]),
("Reverse order (#123) Span __ Underline ~~ Strikethrough", sy.TextToken(["text"]),
(sy.TextToken(["Reverse order"]), ), __GL()),
sy.EtCustomSpanToken(["123"]), sy.TextToken(["Span"]), ("Reverse order (#123) Span __ Underline ~~ Strikethrough", (
sy.EtUnderlineToken(["__"]), sy.TextToken(["Underline"]), sy.TextToken(["Reverse order"]),
sy.EtStrikethroughToken(["~~"]), sy.TextToken(["Strikethrough"]),)), # noqa E501 (line too long) sy.EtCustomSpanToken(["123"]), sy.TextToken(["Span"]),
sy.EtUnderlineToken(["__"]), sy.TextToken(["Underline"]),
sy.EtStrikethroughToken(["~~"]), sy.TextToken(["Strikethrough"]),
), __GL()),
], ],
# Structural Elements # Structural Elements
"se_start": [ "se_start": [
# Matches a start of a structural element # Matches a start of a structural element
("<<div", (sy.StructuralElementStartToken(["div"]),)), ("<<div", (sy.StructuralElementStartToken(["div"]),), __GL()),
], ],
"se_end": [ "se_end": [
("div>>", (sy.StructuralElementEndToken(["div"]),)), ("div>>", (sy.StructuralElementEndToken(["div"]),), __GL()),
], ],
# "se": [ "se": [
# Matches a div element, must be at the beginning of the line, the closing div can be with arguments # Matches a div element, must be at the beginning of the line, the closing div can be with arguments
# TODO : Re-implement this ("div>> [class='blue', 123]{var='test', number=11}", (
# ("div>> [class='blue', 123#]{var='test', number=11}", [sy.StructuralElementEndToken(["div", [ sy.StructuralElementEndToken(["div"]),
# sy.OptionalToken(["[", "class='blue'", ",", "123#", "]", "{", "var='test'", ",", "number=11", "}"])]])]), sy.OptionalToken(["[", ["class", "=", "'blue'"], 123, "]", "{", "var='test'", "number=11", "}"]),
), __GL(), __XF), # Bad optional implementation
# ("div>> [class='blue', 123#]", # ("div>> [class='blue', 123#]",
# [sy.StructuralElementEndToken(["div", [sy.OptionalToken(["[", "class='blue'", ",", "123#", "]"])]])]), # [sy.StructuralElementEndToken(["div", [sy.OptionalToken(["[", "class='blue'", ",", "123#", "]"])]])]),
# ("div>> {var='test', number=11}", # ("div>> {var='test', number=11}",
# [sy.StructuralElementEndToken(["div", [sy.OptionalToken(["{", "var='test'", ",", "number=11", "}"])]])]), # [sy.StructuralElementEndToken(["div", [sy.OptionalToken(["{", "var='test'", ",", "number=11", "}"])]])]),
# ], ],
# List Elements # List Elements
# TODO: Test markup in lists # TODO: Test markup and add optionnals in lists
"one_olist": [ "one_olist": [
# Matches a one-level ordered list element, must be at the beginning of the line # Matches a one-level ordered list element, must be at the beginning of the line
# Drop the "." from the match # Drop the "." from the match
("#. Text", (sy.EtOlistToken([sy.TextToken(["Text"])]),)), ("#. Text", (sy.EtOlistToken([sy.TextToken(["Text"])]),), __GL()),
], ],
"one_ulist": [ "one_ulist": [
# Matches a one-level unordered list element, must be at the beginning of the line # Matches a one-level unordered list element, must be at the beginning of the line
# Drop the "-" from the match # Drop the "-" from the match
("- Text", (sy.EtUlistToken([sy.TextToken(["Text"])]),)), ("- Text", (sy.EtUlistToken([sy.TextToken(["Text"])]),), __GL()),
], ],
# Headers # Headers
"one_header": [ "one_header": [
# Matches a one-level header element, must be at the beginning of the line # Matches a one-level header element, must be at the beginning of the line
("# Text1 #", (sy.HeaderToken(["#", "Text1 "]),)), ("# Text1 #", (sy.HeaderToken(["#", "Text1 "]),), __GL()),
("## Text2 ##", (sy.HeaderToken(["##", "Text2 "]),)), ("## Text2 ##", (sy.HeaderToken(["##", "Text2 "]),), __GL()),
("### Text3 ###", (sy.HeaderToken(["###", "Text3 "]),)), ("### Text3 ###", (sy.HeaderToken(["###", "Text3 "]),), __GL()),
("#### Text4 ####", (sy.HeaderToken(["####", "Text4 "]),)), ("#### Text4 ####", (sy.HeaderToken(["####", "Text4 "]),), __GL()),
("##### Text5 #####", (sy.HeaderToken(["#####", "Text5 "]),)), ("##### Text5 #####", (sy.HeaderToken(["#####", "Text5 "]),), __GL()),
("###### Text6 ######", (sy.HeaderToken(["######", "Text6 "]),)), ("###### Text6 ######", (sy.HeaderToken(["######", "Text6 "]),), __GL()),
# TODO : Re-implement that
# ("## Text2 ## {var='test', number=11}", (sy.HeaderToken(["##", "Text2 "]),
# sy.OptionalToken(["{", "var='test'", ",", "number=11", "}"]))),
# ("### Text3 ### [class='blue', 123#]{var='test', number=11}", (sy.HeaderToken(["###", "Text3 "]),
# sy.OptionalToken(
# ["[", "class='blue'", ",", "123#", "]"]),
# # noqa E501 (line too long)
# sy.OptionalToken(
# ["{", "var='test'", ",", "number=11",
# "}"]))), # noqa E501 (line too long)
], ],
# One-line display elements
"one_display": [
# Matches a one-level display element, must be at the beginning of the line
("! Display1 !", (sy.DisplayToken(["!", "Display1 "]),), __GL()),
("!! Display2 !!", (sy.DisplayToken(["!!", "Display2 "]),), __GL()),
("!!! Display3 !!!", (sy.DisplayToken(["!!!", "Display3 "]),), __GL()),
],
# One-line elements
"one_line": [
# Matches a one-line element, must be at the beginning of the line
("- Text1", (sy.EtUlistToken([sy.TextToken(["Text1"])]),), __GL()),
("#. Text2", (sy.EtOlistToken([sy.TextToken(["Text2"])]),), __GL()),
("! Display3 !", (sy.DisplayToken(["!", "Display3 "]),), __GL()),
("!! Display4 !!", (sy.DisplayToken(["!!", "Display4 "]),), __GL()),
("### Text5 ###", (sy.HeaderToken(["###", "Text5 "]),), __GL()),
("#### Text6 ####", (sy.HeaderToken(["####", "Text6 "]),), __GL()),
("- Text7 [class='blue', 123]{var='test', number=11}", (
sy.EtUlistToken([
sy.TextToken(["Text7"]),
sy.OptionalToken(["[", "class='blue'", ",", "123#", "]", "{", "var='test'", ",", "number=11", "}"])
]),
), __GL(), __XF),
("#. Text8 [class='blue', 123]{var='test', number=11}", (
sy.EtOlistToken([
sy.TextToken(["Text8"]),
sy.OptionalToken(["[", "class='blue'", ",", "123", "]", "{", "var='test'", ",", "number=11", "}"])
]),
), __GL(), __XF),
("! Display9 ! [class='blue', 123]{var='test', number=11}", (
sy.DisplayToken([
"!",
"Display9",
sy.OptionalToken([
"[", "class='blue'", ",", "123", "]",
"{", "var='test'", ",", "number=11", "}"
])
]),
), __GL(), __XF), # Bad optional implementation
("## Text10 ## [class='blue', 123]{var='test', number=11}", (
sy.HeaderToken([
"##",
"Text10",
sy.OptionalToken([
"[", "class='blue'", ",", "123", "]",
"{", "var='test'", ",", "number=11", "}"])
]),
), __GL(), __XF), # Bad optional implementation
],
# Tables # Tables
"table_row": [ "table_row": [
# Matches a table element, must be at the beginning of the line # Matches a table element, must be at the beginning of the line
("| Text1 | Text2 |", (sy.TableRowToken([sy.TableCellToken(["Text1 "]), sy.TableCellToken(["Text2 "]),]),)), ("| Text1 | Text2 |", (sy.TableRowToken([sy.TableCellToken(["Text1 "]), sy.TableCellToken(["Text2 "]), ]), ), __GL()),
("| Text1 | Text2 | Text3 |", (sy.TableRowToken([sy.TableCellToken(["Text1 "]), sy.TableCellToken(["Text2 "]), sy.TableCellToken(["Text3 "])]),)), ("| Text1 | Text2 | Text3 |", (
("| 2 Text1 | Text2 |", (sy.TableRowToken([sy.TableCellToken(["2 Text1 "]), sy.TableCellToken(["Text2 "])]),)), sy.TableRowToken([
("|2 Text1 | Text2 |", (sy.TableRowToken(['2', sy.TableCellToken(["Text1 "]), sy.TableCellToken(["Text2 "])]),)), sy.TableCellToken(["Text1 "]),
("|2 Text1 |3 Text2 |", (sy.TableRowToken(['2', sy.TableCellToken(["Text1 "]), '3', sy.TableCellToken(["Text2 "])]),)), sy.TableCellToken(["Text2 "]),
# ("|2 Text1 |3 Text2 |4 Text3 |", (sy.TableRowToken(["|2", "Text1", "|3", "Text2", "|4", "Text3", "|"]),)), sy.TableCellToken(["Text3 "])
# ("|3 Text1 | Text2 | {var='test', number=11}", (sy.TableRowToken(["|3", "Text1", "|", "Text2", "|"]), ]),
# sy.UnimplementedToken( ), __GL()),
# ["{", "var='test'", ",", "number=11", "}"]))), ("| 2 Text1 | Text2 |", (sy.TableRowToken([sy.TableCellToken(["2 Text1 "]), sy.TableCellToken(["Text2 "])]),), __GL()),
("|2 Text1 | Text2 |", (
sy.TableRowToken([
'2',
sy.TableCellToken(["Text1 "]),
sy.TableCellToken(["Text2 "]),
]),
), __GL()),
("|2 Text1 |3 Text2 |", (
sy.TableRowToken(['2', sy.TableCellToken(["Text1 "]), '3', sy.TableCellToken(["Text2 "])]),
), __GL()),
("|2 Text1 |3 Text2 |4 Text3 |", (
sy.TableRowToken([
'2',
sy.TableCellToken(["Text1 "]),
'3',
sy.TableCellToken(["Text2 "]),
'4',
sy.TableCellToken(["Text3 "])]),
), __GL()),
("|3 Text1 | Text2 | {var='test', number=11}", (
sy.TableRowToken([
'3',
sy.TableCellToken(["Text1 "]),
sy.TableCellToken(["Text2 "]),
]),
sy.OptionalToken(["{", "var='test'", "number=11", "}"]),
), __GL(), __XF), # Bad optional implementation
], ],
"table_separator": [ "table_separator": [
# Matches a table element, must be at the beginning of the line # TODO: Tokens ? # Matches a table element, must be at the beginning of the line
("|---|---|", (sy.TableSeparatorToken(["---", "---"]),)), ("|---|---|", (sy.TableSeparatorToken(["---", "---"]),), __GL()),
("|---|---|---|", (sy.TableSeparatorToken(["---", "---", "---"]),)), ("|---|---|---|", (sy.TableSeparatorToken(["---", "---", "---"]),), __GL()),
("|:--|-:-|--:|--:|", (sy.TableSeparatorToken([":--", "-:-", "--:", "--:"]),)), ("|:--|-:-|--:|--:|", (sy.TableSeparatorToken([":--", "-:-", "--:", "--:"]),), __GL()),
], ],
"line": [ "line": [
# Match any line parsed by the parser (can match header, list table etc...) this is the main syntax element # Match any line parsed by the parser (can match header, list table etc...) this is the main syntax element
("# Text1 #", (sy.HeaderToken(["#", "Text1 "]),)), ("# Text1 #", (sy.HeaderToken(["#", "Text1 "]),), __GL()),
# TODO: old testing, needs freshening up ("Text *em __underline__ still em*", (
# ("Text *bold __underline__ still bold*", ( sy.TextToken(["Text "]),
# sy.TextToken(["Text ", sy.EtStrongToken(["bold", " ", sy.EtUnderlineToken(["underline"]), " still bold"])]),)), # noqa E501 (line too long) sy.EtEmToken(["*"]),
("|2 Text1 | Text2 |", (sy.TableRowToken(["2", sy.TableCellToken(["Text1 "]), sy.TableCellToken(["Text2 "])]),)), sy.TextToken(["em "]),
("|---|---|", (sy.TableSeparatorToken(["---", "---"]),)), sy.EtUnderlineToken(["__"]),
("- Text", (sy.EtUlistToken([sy.TextToken(["Text"])]),)), sy.TextToken(["underline"]),
("div>>", (sy.StructuralElementEndToken(["div"]),)), sy.EtUnderlineToken(["__"]),
sy.TextToken(["still em"]),
sy.EtEmToken(["*"]),
), __GL(), __XF), # Bad optional implementation
("|2 Text1 | Text2 |", (
sy.TableRowToken(["2", sy.TableCellToken(["Text1 "]), sy.TableCellToken(["Text2 "])]),
), __GL()),
("|---|---|", (sy.TableSeparatorToken(["---", "---"]),), __GL()),
("- Text", (sy.EtUlistToken([sy.TextToken(["Text"])]),), __GL()),
("div>>", (sy.StructuralElementEndToken(["div"]),), __GL()),
], ],
} }
# Cursed zipping oneline # Cursed zipping oneline
zipped_dict_advanced_syntax_input_and_expected_output = [ zipped_dict_advanced_syntax_input_and_expected_output = [
ptp(item[0], item[1][0], item[1][1], id=f"{item[0]}@{item[1][0][:8]} @") for sublist in [ ptp(item[0], item[1][0], item[1][1], item[1][2], marks=item[1][3:], id=f"{item[0]} [line {item[1][2]}]") for sublist in [
zip_longest([key], dict_advanced_syntax_input_and_expected_output[key], fillvalue=key) for key in zip_longest([key], dict_advanced_syntax_input_and_expected_output[key], fillvalue=key) for key in
dict_advanced_syntax_input_and_expected_output.keys() # noqa E501 (line too cursed) dict_advanced_syntax_input_and_expected_output.keys() # noqa E501 (line too cursed)
] for item in sublist ] for item in sublist
@ -318,7 +410,7 @@ def test_of_type_creator(token_class):
:type token_class: sy.SemanticType :type token_class: sy.SemanticType
""" """
fcr = sy.of_type(token_class) fcr = sy.of_type(token_class)
tnk = fcr(None, None, None) tnk = fcr(None, None, ['Text'])
# noinspection PyTypeChecker # noinspection PyTypeChecker
assert isinstance(tnk, token_class) # pylint: disable=unidiomatic-typecheck assert isinstance(tnk, token_class) # pylint: disable=unidiomatic-typecheck
assert tnk.label == token_class.label assert tnk.label == token_class.label
@ -401,22 +493,28 @@ def test_expression_matching(expression, to_parse):
assert expr.parse_string(string) is not None assert expr.parse_string(string) is not None
@pytest.mark.parametrize("markup_element, to_parse, expected", zipped_dict_advanced_syntax_input_and_expected_output) @pytest.mark.parametrize("markup_element, to_parse, expected, line_test", zipped_dict_advanced_syntax_input_and_expected_output) # noqa: E501 (line too long)
def test_advanced_expression_and_token_creation(markup_element, to_parse, expected): def test_advanced_expression_and_token_creation(markup_element, to_parse, expected, line_test):
""" """
Test that the advanced syntax is correctly parsed and returns the correct tokens. Test that the advanced syntax is correctly parsed and returns the correct tokens.
:param markup_element: The expression to test. :param markup_element: The expression to test.
:param to_parse: The string to test. :param to_parse: The string to test.
:param expected: The expected tokens. :param expected: The expected tokens.
:param line_test: The line number of the test.
:type markup_element: str :type markup_element: str
:type to_parse: str :type to_parse: str
:type expected: list :type expected: list
:type line_test: int
""" """
expr = find_expression_from_str(markup_element) expr = find_expression_from_str(markup_element)
assert isinstance(expr, pyparsing.ParserElement) assert isinstance(expr, pyparsing.ParserElement)
result = expr.parse_string(to_parse) result = expr.parse_string(to_parse)
print(f"Found: {result} (len:{len(result)}). Expected {expected} (len:{len(expected)})") print()
print(f"Parsing expression: {to_parse} with {markup_element}")
print("Defined at %(filename)s:%(lineno)d" % {'filename': __file__, 'lineno': line_test})
print(f"Found: {result} (len:{len(result)}).")
print(f"Expected: {expected} (len:{len(expected)})")
assert result is not None assert result is not None
if len(result) != len(expected): if len(result) != len(expected):
raise ReturnArgumentSizeError(len(expected), len(result)) raise ReturnArgumentSizeError(len(expected), len(result))