-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_markdown_parser.py
65 lines (48 loc) · 2.12 KB
/
test_markdown_parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import unittest
from markdown_parser import parse_code_blocks, CodeBlock
class TestParseCodeBlocks(unittest.TestCase):
def test_no_code_blocks(self):
# Given
markdown_text = "This is regular markdown text."
# When
code_blocks = parse_code_blocks(markdown_text)
# Then
self.assertEqual(len(code_blocks), 0)
def test_single_code_block(self):
# Given
markdown_text = "Here is a code block: \n```python\nprint('hello world')\n```"
# When
code_blocks = parse_code_blocks(markdown_text)
# Then
self.assertEqual(len(code_blocks), 1)
self.assertEqual(code_blocks[0].language, "python")
self.assertEqual(code_blocks[0].code, "print('hello world')")
def test_multiple_code_blocks(self):
# Given
markdown_text = "This is a markdown text. \n" \
"```python\nprint('hello world')\n```\n" \
" This is another markdown text. ```javascript\nconsole.log('hello world')\n```"
# When
code_blocks = parse_code_blocks(markdown_text)
# Then
self.assertEqual(len(code_blocks), 2)
self.assertEqual(code_blocks[0].language, "python")
self.assertEqual(code_blocks[0].code, "print('hello world')")
self.assertEqual(code_blocks[1].language, "javascript")
self.assertEqual(code_blocks[1].code, "console.log('hello world')")
def test_no_language_specified(self):
# Given
markdown_text = "Here is a code block without a language specified: \n```\nprint('hello world')\n```"
# When
code_blocks = parse_code_blocks(markdown_text)
# Then
self.assertEqual(len(code_blocks), 1)
self.assertEqual(code_blocks[0].language, None)
self.assertEqual(code_blocks[0].code, "print('hello world')")
def test_no_triple_backticks(self):
# Given
markdown_text = "Here is a code block without triple backticks: \n`print('hello world')`"
# When
code_blocks = parse_code_blocks(markdown_text)
# Then
self.assertEqual(len(code_blocks), 0)