-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_codeblocks.py
More file actions
75 lines (59 loc) · 2.75 KB
/
Copy pathtest_codeblocks.py
File metadata and controls
75 lines (59 loc) · 2.75 KB
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
66
67
68
69
70
71
72
73
74
75
import contextlib
import os.path
import shutil
import sys
import tempfile
import unittest
import codeblocks
class CodeblocksEditTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
test_dir = os.path.dirname(os.path.abspath(__file__))
cls._files_dir = files_dir = os.path.join(test_dir, "files")
cls._temp_dir = temp_dir = tempfile.mkdtemp()
files = [os.path.join(files_dir, file) for file in os.listdir(files_dir) if not file.endswith("_result.md")]
files_to_edit = [shutil.copy(file, temp_dir) for file in files]
codeblocks.add_language(files_to_edit, edit_files=True)
def check_file(self, filename):
self.maxDiff = None
actual = os.path.join(self._temp_dir, filename)
expected = os.path.join(self._files_dir, filename[:-3] + "_result.md")
with open(actual, "r", encoding="utf-8") as actual, open(expected, "r", encoding="utf-8") as expected:
self.assertEqual(actual.read(), expected.read())
def test_single_block_at_start(self):
self.check_file("single_block_at_start.md")
def test_single_block_at_end(self):
self.check_file("single_block_at_end.md")
def test_single_block_in_middle(self):
self.check_file("single_block_in_middle.md")
def test_multiple_blocks(self):
self.check_file("multiple_blocks.md")
def test_multiple_blocks_indent(self):
self.check_file("multiple_blocks_indent.md")
def test_inline_code(self):
self.check_file("inline_code.md")
def test_more_backticks(self):
self.check_file("more_backticks.md")
def test_less_backticks(self):
self.check_file("less_backticks.md")
def test_block_termination(self):
self.check_file("block_termination.md")
@classmethod
def tearDownClass(cls):
shutil.rmtree(cls._temp_dir)
class CodeblocksPrintTest(unittest.TestCase):
def test_codeblocks_terminal_output(self):
self.maxDiff = None
test_dir = os.path.join("test", "files")
files = [
os.path.join(test_dir, "multiple_blocks_indent.md"),
os.path.join(test_dir, "single_block_for_terminal.md")
]
with tempfile.NamedTemporaryFile("w+", encoding="utf-8", delete=False) as actual:
with contextlib.redirect_stdout(actual):
codeblocks.add_language(files, edit_files=False)
actual.flush()
actual.seek(0)
result = "terminal_output_win_result.md" if sys.platform == "win32" else "terminal_output_result.md"
with open(os.path.join(test_dir, result), "r", encoding="utf-8") as expected:
self.assertEqual(actual.read(), expected.read())