11"""
22This file contains the pytest tests for the markdown_helper.py file.
33"""
4+ import pytest
45try :
56 from src import markdown_helper as markdown
67except ModuleNotFoundError :
78 try :
89 import markdown_helper as markdown
910 except ModuleNotFoundError :
10- from ..src import markdown_helper as markdown # pylint: disable=import-error, relative-beyond-top-level
11+ from ..src import ( # pylint: disable=relative-beyond-top-level
12+ markdown_helper as markdown , # pylint: disable=import-error
13+ ) # pylint: disable=relative-beyond-top-level
14+
15+
1116def test_ordered_list ():
1217 """
1318 This function tests the markdown.List class.
@@ -18,6 +23,16 @@ def test_ordered_list():
1823 ), "String representation of ordered list is incorrect."
1924 assert list_1 .items == ["item 1" , "item 2" , "item 3" ], "List items are incorrect."
2025
26+ def test_empty_list ():
27+ """
28+ This function tests the markdown.List class.
29+ """
30+ list_1 : markdown .List = markdown .List ([], ordered = True )
31+ assert (
32+ str (list_1 ) == ""
33+ ), "String representation of ordered list is incorrect."
34+ assert list_1 .items == [], "List items are incorrect."
35+ assert repr (list_1 ) == "List(title=False, items=[], ordered=True)" , "List items are incorrect."
2136
2237def test_modify_list ():
2338 """
@@ -69,6 +84,9 @@ def test_link():
6984 ), "String representation of link is incorrect."
7085 assert link_1 .url == "http://www.google.com" , "Link URL is incorrect."
7186 assert link_1 .text == "Google" , "Link text is incorrect."
87+ assert (
88+ repr (link_1 ) == "Link(url=http://www.google.com, text=Google, title=False, new_tab=False)"
89+ )
7290
7391
7492def test_link_no_trailing ():
@@ -119,6 +137,22 @@ def test_image():
119137 assert image_1 .alt == "Google" , "Image text is incorrect."
120138
121139
140+ def test_image_texts ():
141+ """
142+ This function tests the markdown.Image class.
143+ """
144+ image_1 = markdown .Image (
145+ "http://www.google.com" , alt = "Google" , title = "Title" , caption = "Caption"
146+ )
147+ assert (
148+ image_1 .markdown () == "### Title\n \n _Caption_\n "
149+ ), "Markdown string representation of image is incorrect."
150+ assert (
151+ repr (image_1 )
152+ == "Image(url=http://www.google.com, title=Title, alt=Google, width=False, height=False, align=False,caption=Caption)" # pylint: disable=line-too-long
153+ ), "String representation of image is incorrect."
154+
155+
122156def test_image_size ():
123157 """
124158 This function tests the markdown.Image class.
@@ -182,6 +216,7 @@ def test_table_add_row():
182216 {"col 1" : "item 1" , "col 2" : "item 2" , "col 3" : "item 3" }
183217 ], "Table rows are incorrect."
184218
219+
185220def test_table_add_row_no_headers ():
186221 """
187222 This function tests the table.add_row function with a list input.
@@ -202,6 +237,23 @@ def test_table_add_row_no_headers():
202237 ], "Table rows are incorrect."
203238
204239
240+ def test_table_title ():
241+ """
242+ This function tests the markdown.Table class.
243+ """
244+ table_1 = markdown .Table (["col 1" , "col 2" , "col 3" ], title = "My Table" )
245+ table_1 .add_row ({"col 1" : "value 1" , "col 2" : "value 2" , "col 3" : "value 3" })
246+ assert (
247+ str (table_1 )
248+ == "### My Table\n | col 1 | col 2 | col 3 |\n | --- | --- | --- |\n | value 1 | value 2 | value 3 |\n " # pylint: disable=line-too-long
249+ ), "String representation of table is incorrect."
250+ assert table_1 .headers == [
251+ "col 1" ,
252+ "col 2" ,
253+ "col 3" ,
254+ ], "Table columns are incorrect."
255+ assert table_1 .title == "My Table" , "Table title is incorrect."
256+
205257
206258def test_table_sort ():
207259 """
@@ -214,21 +266,63 @@ def test_table_sort():
214266 table_1 .add_row ({"Name" : "Third" , "Value" : 3 })
215267 assert (
216268 str (table_1 )
217- == "| Name | Value |\n | --- | --- |\n | First | 1 |\n | Second | 2 |\n | Third | 3 |\n | Fourth | 4 |\n " # pylint: disable=line-too-long
269+ == "| Name | Value |\n | --- | --- |\n | First | 1 |\n | Second | 2 |\n | Third | 3 |\n | Fourth | 4 |\n " # pylint: disable=line-too-long
218270 ), "Sorted Table is incorrect."
219271 assert table_1 .headers == ["Name" , "Value" ], "Table columns are incorrect."
220272 table_1 .sort_reverse = True
221273 assert (
222274 str (table_1 )
223- == "| Name | Value |\n | --- | --- |\n | Fourth | 4 |\n | Third | 3 |\n | Second | 2 |\n | First | 1 |\n " # pylint: disable=line-too-long
275+ == "| Name | Value |\n | --- | --- |\n | Fourth | 4 |\n | Third | 3 |\n | Second | 2 |\n | First | 1 |\n " # pylint: disable=line-too-long
224276 ), "Reverse sorted Table is incorrect."
225277 table_1 .sort_reverse = False
226278 table_1 .sort_key = "Name"
227279 assert (
228280 str (table_1 )
229- == "| Name | Value |\n | --- | --- |\n | First | 1 |\n | Fourth | 4 |\n | Second | 2 |\n | Third | 3 |\n " # pylint: disable=line-too-long
281+ == "| Name | Value |\n | --- | --- |\n | First | 1 |\n | Fourth | 4 |\n | Second | 2 |\n | Third | 3 |\n " # pylint: disable=line-too-long
230282 ), "Second sorted Table is incorrect."
231283
284+ def test_table_sort_error ():
285+ """
286+ This function tests the markdown.Table class.
287+ """
288+ table_1 = markdown .Table (["Name" , "Value" ], sort_key = "Value" )
289+ table_1 .add_row ({"Name" : "First" , "Value" : 1 })
290+ table_1 .add_row ({"Name" : "Second" , "Value" : False })
291+ table_1 .add_row ({"Name" : "Fourth" , "Value" : 4 })
292+ table_1 .add_row ({"Name" : "Third" , "Value" : "String" })
293+ with pytest .raises (ValueError ):
294+ table_1 .sort_key = "invalid"
295+ table_1 .sort_table ()
296+
297+ def test_table_disable_convert ():
298+ """
299+ This function tests the markdown.Table class.
300+ """
301+ table_1 = markdown .Table (["Name" , "Value" ], sort_key = "Value" )
302+ table_1 .add_row ({"Name" : "First" , "Value" : 1 })
303+ table_1 .add_row ({"Name" : "Second" , "Value" : 0 })
304+ table_1 .add_row ({"Name" : "Fourth" , "Value" : 1 })
305+ table_1 .add_row ({"Name" : "Third" , "Value" : 0 })
306+ table_1 .sort_table (True )
307+ assert (
308+ table_1 .rows == [{'Name' : 'Second' , 'Value' : 0 }, {'Name' : 'Third' , 'Value' : 0 },
309+ {'Name' : 'First' , 'Value' : 1 }, {'Name' : 'Fourth' , 'Value' : 1 }]
310+ ), "Sorted Table is incorrect."
311+
312+ def test_table_sort_convert ():
313+ """
314+ This function tests the markdown.Table class.
315+ """
316+ table_1 = markdown .Table (["Name" , "Value" ], sort_key = "Value" )
317+ table_1 .add_row ({"Name" : "First" , "Value" : 1 })
318+ table_1 .add_row ({"Name" : "Second" , "Value" : 0 })
319+ table_1 .add_row ({"Name" : "Fourth" , "Value" : 1 })
320+ table_1 .add_row ({"Name" : "Third" , "Value" : 0 })
321+ table_1 .sort_table ()
322+ assert (
323+ table_1 .rows == [{'Name' : 'Second' , 'Value' : False }, {'Name' : 'Third' , 'Value' : False },
324+ {'Name' : 'First' , 'Value' : True }, {'Name' : 'Fourth' , 'Value' : True }]
325+ ), "Sorted Table is incorrect."
232326
233327def test_table_flexible ():
234328 """
@@ -241,11 +335,55 @@ def test_table_flexible():
241335 table_1 .add_row ({"Name" : "Fourth" , "Value" : 4 })
242336 assert (
243337 str (table_1 )
244- == "| Name | Value | Extra |\n | --- | --- | --- |\n | First | 1 | |\n | Second | 2 | |\n | Third | 3 | Extra Value |\n | Fourth | 4 | |\n " # pylint: disable=line-too-long
338+ == "| Name | Value | Extra |\n | --- | --- | --- |\n | First | 1 | |\n | Second | 2 | |\n | Third | 3 | Extra Value |\n | Fourth | 4 | |\n " # pylint: disable=line-too-long
245339 ), "Flexible Table is incorrect."
246340 assert table_1 .headers == ["Name" , "Value" , "Extra" ], "Table columns are incorrect."
247341
342+ def test_table_add_rows ():
343+ """
344+ This function tests the markdown.Table class.
345+ """
346+ table_1 = markdown .Table (["Name" , "Value" ])
347+ table_1 .add_rows ([{"Name" : "First" , "Value" : 1 }, {"Name" : "Second" , "Value" : 2 }])
348+ assert (
349+ str (table_1 )
350+ == "| Name | Value |\n | --- | --- |\n | First | 1 |\n | Second | 2 |\n "
351+ ), "Table is incorrect."
352+ assert table_1 .headers == ["Name" , "Value" ], "Table columns are incorrect."
248353
354+ def test_table_remap ():
355+ """
356+ This function tests the markdown.Table class.
357+ """
358+ table_1 = markdown .Table (["Name" , "Value" ], custom_map = {"Name" :{"First" :"1st" ,"Second" :"2nd" }})
359+ table_1 .add_row ({"Name" : "First" , "Value" : 1 })
360+ table_1 .add_row ({"Name" : "Second" , "Value" : 2 })
361+ assert (
362+ str (table_1 .get_table ())
363+ == "| Name | Value |\n | --- | --- |\n | 1st | 1 |\n | 2nd | 2 |\n " # pylint: disable=line-too-long
364+ ), "Remapped Table is incorrect."
365+ assert table_1 .headers == ["Name" , "Value" ], "Table columns are incorrect."
366+ table_2 = markdown .Table (["Name" , "Value" ], custom_map = {"Name" :{"First" :"1st" ,"Second" :"2nd" }})
367+ table_2 .add_row ({"Name" : "First" , "Value" : 1 })
368+ table_2 .remap ()
369+ assert (
370+ str (table_2 )
371+ == "| Name | Value |\n | --- | --- |\n | 1st | 1 |\n " # pylint: disable=line-too-long
372+ ), "Remapped Table is incorrect."
373+
374+
375+ def test_header ():
376+ """
377+ This function tests the markdown.Header class.
378+ """
379+ header_1 = markdown .Header ("Header 1" , 2 )
380+ assert (
381+ str (header_1 ) == "## Header 1"
382+ ), "String representation of header is incorrect."
383+ assert header_1 .text == "Header 1" , "Header text is incorrect."
384+ assert (
385+ repr (header_1 ) == "## Header 1"
386+ )
249387def test_section ():
250388 """
251389 This function tests the markdown.Section class.
@@ -266,6 +404,23 @@ def test_section_add():
266404 assert (
267405 str (section_1 ) == "# Section 1\n This is a paragraph. \n "
268406 ), "String representation of section is incorrect."
407+ assert section_1 .title .text == "Section 1" , "Section title is incorrect."
408+ assert (
409+ repr (section_1 ) == "Section(title=# Section 1, content=This is a paragraph. )"
410+ ), "Section representation is incorrect."
411+
412+
413+ def test_section_add_nonempty ():
414+ """
415+ This function tests the markdown.Section class.
416+ """
417+ section_1 = markdown .Section ("Section 1" )
418+ section_1 .add ("This is a paragraph." )
419+ section_1 .add ("This is another paragraph." )
420+ assert (
421+ str (section_1 ) == "# Section 1\n This is a paragraph. \n This is another paragraph. \n "
422+ ), "String representation of section is incorrect."
423+ assert section_1 .title .text == "Section 1" , "Section title is incorrect."
269424
270425
271426def test_document ():
@@ -283,6 +438,26 @@ def test_document():
283438 str (document_1 ) == "# Document 1\n ## Section 1\n This is a paragraph. \n "
284439 ), "String representation of document is incorrect."
285440
441+ def test_add_section ():
442+ """
443+ This function tests the markdown.Document class.
444+ """
445+ document_1 = markdown .Document ("Document 1" , filename = "document_1.md" )
446+ document_1 .add_section ("RAW SECTION" )
447+ assert (
448+ str (document_1 ) == "# Document 1\n # RAW SECTION\n \n "
449+ ), "String representation of document is incorrect."
450+
451+ def test_document_toc ():
452+ """
453+ This function tests the markdown.Document class.
454+ """
455+ document_1 = markdown .Document ("Document 1" , filename = "document_1.md" , table_of_contents = True ) # pylint: disable=line-too-long
456+ document_1 .add_section (markdown .Section (markdown .Header ("Section 1" , 2 )))
457+ assert (
458+ document_1 .get_document () == "# Document 1\n ## Table of Contents\n * [Section 1](#section-1)\n ## Section 1\n \n " # pylint: disable=line-too-long
459+ ), "String representation of document is incorrect."
460+
286461
287462def test_document_save (tmp_path ):
288463 """
0 commit comments