def test_no_matches(): """ A test of the parser where there are no matches. """ input_text = [ r"hello world", r"goodbye world", "", ] def colgen_findme(input): return Collector(input, r"%%\\findme{.*?}") def colgen_lookfor(input): return Collector(input, r"%%\\lookfor{.*?}") def colgen_thethird(input): return Collector(input, r"%%\\thethird{.*?}") generators = { re.compile(r"%%\\findme{.*?}"): colgen_findme, re.compile(r"%%\\lookfor{.*?}"): colgen_lookfor, re.compile(r"%%\\thethird{.*?}"): colgen_thethird, } parser = Parser(input_text, generators) expected_output = [r"hello world", r"goodbye world", ""] assert expected_output == parser.text
def test_section(): """ Unit test for the parser using the collector. Not yet implemented: line numbers. """ input_text = [ r"hello world", r"%%\findme{Section}", r"goodbye world", "", ] def secgen(input): return Section(input, r"%%\\findme{(.*?)}", capture=1) generators = { re.compile(r"%%\\findme{(.*?)}"): secgen, } parser = Parser(input_text, generators) expected_output = [ r"hello world", "# Section".format(parser.matches[1].uid), r"goodbye world", "" ] assert expected_output == parser.text
def test_collector(): """ Unit test for the parser using the collector. Not yet implemented: line numbers. """ input_text = [ r"hello world", r"%%\findme{collector}", r"goodbye world", "", ] def colgen(input): return Collector(input, r"%%\\findme{.*?}") generators = { re.compile(r"%%\\findme{.*?}"): colgen, } parser = Parser(input_text, generators) expected_output = [ r"hello world", "<!-- Collector {} -->".format(parser.matches[1].uid), r"goodbye world", "" ] assert expected_output == parser.text
def test_assign_section_line_numbers(): """ Unit test for the function that assigns line numbers to the sections. """ input_text = [ r"hello world", r"%%\findme{Section}", r"%%\findme{Section 2}", r"goodbye world", r"%%\findme{Section 3}", "", ] def secgen(input): return Section(input, r"%%\\findme{(.*?)}", capture=1) generators = { re.compile(r"%%\\findme{(.*?)}"): secgen, } parser = Parser(input_text, generators) line_numbered_sections = assign_section_line_numbers(parser) # from list line_numbers = [[m.startline, m.endline] for m in line_numbered_sections] # from object only_sections = [m for m in parser.matches.values() if isinstance(m, Section)] line_numbers_object = [[m.startline, m.endline] for m in only_sections] expected_output = [[1,2], [2,4], [4, 6]] assert expected_output == line_numbers assert expected_output == line_numbers_object
def test_removal(): """ Unit test for the removal postprocessing functions. """ input_text = [ r"hello world", r"%%\beginpdfonly", r"THIS IS ONLY FOR THE PDF!", r"%%\endpdfonly", r"goodbye world", "", ] def remgens(input): return Removal(input, r"%%\\beginpdfonly", se="s", id="pdfonly") def remgene(input): return Removal(input, r"%%\\endpdfonly", se="e", id="pdfonly") generators = { re.compile(r"%%\\beginpdfonly"): remgens, re.compile(r"%%\\endpdfonly"): remgene } parser = Parser(input_text, generators) assign_removal_line_numbers(parser, "pdfonly") assign_removal_text(parser, "pdfonly") expected_text = "<!--\nTHIS IS ONLY FOR THE PDF!\n-->" assert parser.matches[1].text == expected_text
def test_assign_section_text_multiple(): """ Unit test for the function that assigns line numbers to the sections. """ input_text = [ r"hello world", r"%%\findme{Section}", r"%%\findyou{Section 2}", r"goodbye world", r"%%\findme{Section 3}", "", ] def secgen(input): return Section(input, r"%%\\findme{(.*?)}", capture=1, id=0) def secgenii(input): return Section(input, r"%%\\findyou{(.*?)}", capture=1, id=1) generators = { re.compile(r"%%\\findme{(.*?)}"): secgen, re.compile(r"%%\\findyou{(.*?)}"): secgenii, } parser = Parser(input_text, generators) line_numbered_sections_0 = assign_section_line_numbers(parser, id=0) line_numbered_sections_1 = assign_section_line_numbers(parser, id=1) expected_output_0 = [ "# Section\n# Section 2\ngoodbye world", "# Section 3\n" ] expected_output_1 = [ "# Section 2\ngoodbye world\n# Section 3\n" ] assigned_sections_0 = assign_section_text(parser, id=0) assigned_sections_1 = assign_section_text(parser, id=1) output_0 = [m.text for m in assigned_sections_0] output_1 = [m.text for m in assigned_sections_1] assert output_0 == expected_output_0 assert output_1 == expected_output_1
def test_assign_section_text(): """ Unit test for the function that assigns line numbers to the sections. """ input_text = [ r"hello world", r"%%\findme{Section}", r"%%\findme{Section 2}", r"goodbye world", r"%%\findme{Section 3}", "", ] def secgen(input): return Section(input, r"%%\\findme{(.*?)}", capture=1) generators = { re.compile(r"%%\\findme{(.*?)}"): secgen, } parser = Parser(input_text, generators) line_numbered_sections = assign_section_line_numbers(parser) expected_output = [ "# Section", "# Section 2\ngoodbye world", "# Section 3\n", ] assigned_sections = assign_section_text(parser) output = [m.text for m in assigned_sections] output_parser = [m.text for m in parser.matches.values() if isinstance(m, Section)] assert output == expected_output assert output_parser == expected_output
def test_removal(): """ Unit test for the removal class. """ input_text = [ r"hello world", r"%%\beginpdfonly", r"THIS IS ONLY FOR THE PDF!", r"%%\endpdfonly", r"goodbye world", "", ] def remgens(input): return Removal(input, r"%%\\beginpdfonly", se="s", id="pdfonly") def remgene(input): return Removal(input, r"%%\\endpdfonly", se="e", id="pdfonly") generators = { re.compile(r"%%\\beginpdfonly"): remgens, re.compile(r"%%\\endpdfonly"): remgene } parser = Parser(input_text, generators) expected_output = [ r"hello world", r"<!--", r"THIS IS ONLY FOR THE PDF!", r"-->", r"goodbye world", "", ] assert parser.text == expected_output
def test_section_and_collector(): """ Unit test for the parser using the collector and the section at the same time! Not yet implemented: line numbers. """ input_text = [ r"hello world", r"%%\findme{Section}", r"%%\findcollector{Collector}", r"goodbye world", "", ] def secgen(input): return Section(input, r"%%\\findme{(.*?)}", capture=1) def colgen(input): return Collector(input, r"%%\\findcollector{.*?}") generators = { re.compile(r"%%\\findme{(.*?)}"): secgen, re.compile(r"%%\\findcollector{.*?}"): colgen, } parser = Parser(input_text, generators) expected_output = [ r"hello world", "# {}".format(parser.matches[1].text), "<!-- Collector {} -->".format(parser.matches[2].uid), r"goodbye world", "" ] assert expected_output == parser.text