-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# Python | ||
# | ||
# This module implements tests for Header class. | ||
# | ||
# This file is part of mdutils. https://github.com/didix21/mdutils | ||
# | ||
# MIT License: (C) 2020 Dídac Coll | ||
import re | ||
|
||
|
||
class MDListHelper: | ||
def __init__(self): | ||
self.n_tabs = 0 | ||
|
||
def get_unordered_markdown_list(self, items, marker: str) -> str: | ||
md_list = "" | ||
for item in items: | ||
if isinstance(item, list): | ||
self.n_tabs += 1 | ||
md_list += self.get_unordered_markdown_list(item, marker) | ||
self.n_tabs -= 1 | ||
else: | ||
md_list += self.__add_new_item(item, marker) | ||
|
||
return md_list | ||
|
||
def get_ordered_markdown_list(self, items) -> str: | ||
md_list = "" | ||
n_marker = 1 | ||
for item in items: | ||
if isinstance(item, list): | ||
self.n_tabs += 1 | ||
md_list += self.get_ordered_markdown_list(items=item) | ||
self.n_tabs -= 1 | ||
else: | ||
md_list += self.__add_new_item(item, f"{n_marker}.") | ||
n_marker += 1 | ||
return md_list | ||
|
||
def __add_new_item(self, item: str, marker: str): | ||
item_with_hyphen = item if self.is_there_marker_in_item(item) else self.__add_hyphen(item, marker) | ||
return self.__n_spaces(4 * self.n_tabs) + item_with_hyphen + '\n' | ||
|
||
@classmethod | ||
def is_there_marker_in_item(cls, item: str) -> bool: | ||
if item.startswith('-') or item.startswith('*') or item.startswith('+') or re.search(r"^(\d\.)", item): | ||
return True | ||
return False | ||
|
||
@classmethod | ||
def __add_hyphen(cls, item: str, marker: str): | ||
return f"{marker} {item}" | ||
|
||
@classmethod | ||
def __n_spaces(cls, number_spaces: int = 1): | ||
return " " * number_spaces | ||
|
||
|
||
class MDList(MDListHelper): | ||
|
||
def __init__(self, items: [str], marked_with: str = '-'): | ||
super().__init__() | ||
self.md_list = self.get_ordered_markdown_list(items) if marked_with == '1' else \ | ||
self.get_unordered_markdown_list(items, marked_with) | ||
|
||
def get_md(self) -> str: | ||
return self.md_list | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
# Python | ||
# | ||
# This module implements tests for Header class. | ||
# | ||
# This file is part of mdutils. https://github.com/didix21/mdutils | ||
# | ||
# MIT License: (C) 2020 Dídac Coll | ||
|
||
from unittest import TestCase | ||
from mdutils.tools.MDList import MDList | ||
|
||
|
||
class TestLink(TestCase): | ||
|
||
def setUp(self): | ||
self.basic_list = "- Item 1\n" \ | ||
"- Item 2\n" \ | ||
"- Item 3\n" | ||
self.complex_list = self.basic_list + \ | ||
"- Item 4\n" \ | ||
" - Item 4.1\n" \ | ||
" - Item 4.2\n" \ | ||
" - Item 4.2.1\n" \ | ||
" - Item 4.2.2\n" \ | ||
" - Item 4.3\n" \ | ||
" - Item 4.3.1\n" \ | ||
"- Item 5\n" | ||
self.basic_items = ["Item 1", "Item 2", "Item 3"] | ||
self.complex_items = self.basic_items + ["Item 4", | ||
["Item 4.1", "Item 4.2", | ||
["Item 4.2.1", "Item 4.2.2"], | ||
"Item 4.3", ["Item 4.3.1"]], | ||
"Item 5"] | ||
|
||
def test_basic_list(self): | ||
expected_list = self.basic_list | ||
mdlist = MDList(items=self.basic_items) | ||
actual_list = mdlist.get_md() | ||
|
||
self.assertEqual(expected_list, actual_list) | ||
|
||
def test_complex_list(self): | ||
expected_list = self.complex_list | ||
mdlist = MDList(items=self.complex_items) | ||
actual_list = mdlist.get_md() | ||
|
||
self.assertEqual(expected_list, actual_list) | ||
|
||
def test_another_marker(self): | ||
marker = "*" | ||
expected_list = self.complex_list.replace("-", marker) | ||
mdlist = MDList(items=self.complex_items, marked_with=marker) | ||
actual_list = mdlist.get_md() | ||
|
||
self.assertEqual(expected_list, actual_list) | ||
|
||
def test_ordered_list(self): | ||
marker = "1" | ||
expected_list = "1. Item 1\n" \ | ||
"2. Item 2\n" \ | ||
"3. Item 3\n" \ | ||
"4. Item 4\n" \ | ||
" 1. Item 4.1\n" \ | ||
" 2. Item 4.2\n" \ | ||
" 1. Item 4.2.1\n" \ | ||
" 2. Item 4.2.2\n" \ | ||
" 3. Item 4.3\n" \ | ||
" 1. Item 4.3.1\n" \ | ||
"5. Item 5\n" | ||
mdlist = MDList(items=self.complex_items, marked_with=marker) | ||
actual_list = mdlist.get_md() | ||
|
||
self.assertEqual(expected_list, actual_list) | ||
|
||
def test_unordered_with_mixed_ordered_list(self): | ||
expected_list = self.basic_list + \ | ||
"- Item 4\n" \ | ||
" 1. Item 4.1\n" \ | ||
" 2. Item 4.2\n" \ | ||
"- Item 5\n" | ||
items = self.basic_items + ["Item 4", ["1. Item 4.1", "2. Item 4.2"], "Item 5"] | ||
mdlist = MDList(items) | ||
actual_list = mdlist.get_md() | ||
|
||
self.assertEqual(expected_list, actual_list) | ||
|
||
def test_ordered_with_mixed_unordered_list(self): | ||
expected_list = "1. Item 1\n" \ | ||
"2. Item 2\n" \ | ||
"3. Item 3\n" \ | ||
"4. Item 4\n" \ | ||
" + Item 4.1\n" \ | ||
" + Item 4.2\n" \ | ||
" + Item 4.2.1\n" \ | ||
" + Item 4.2.2\n" \ | ||
" + Item 4.3\n" \ | ||
" 1. Item 4.3.1\n" \ | ||
"5. Item 5\n" | ||
|
||
items = ["Item 1", "Item 2", "Item 3", "Item 4", | ||
["+ Item 4.1", "+ Item 4.2", | ||
["+ Item 4.2.1", "+ Item 4.2.2"], | ||
"+ Item 4.3", ["Item 4.3.1"] | ||
], | ||
"Item 5" | ||
] | ||
|
||
mdlist = MDList(items, marked_with="1") | ||
actual_list = mdlist.get_md() | ||
|
||
self.assertEqual(expected_list, actual_list) |