-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.py
executable file
·141 lines (111 loc) · 6.72 KB
/
test.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env python3
# encoding: utf-8
import os
import sys
import unittest
from md2html import md2html
ROOT = os.path.dirname(os.path.abspath(__file__))
PWD = os.getcwd()
TITLE_UNTITLED = 'Untitled'
print(f'Testing md2html: {md2html!r}')
class TestParseArgs(unittest.TestCase):
def test_parse_args_wrong_input(self):
cases = (
('-t', ),
('a.md', 'b.md'),
('-o', ),
('--body-insert', ),
('a.md', '-o'),
)
for case in cases:
self.assertRaises(SystemExit, md2html.parse_args, case)
def test_multiple_specified_args(self):
shell_args = md2html.parse_args(())
self.assertEqual(shell_args.append_css, [])
self.assertEqual(shell_args.head_insert, [])
self.assertEqual(shell_args.head_append, [])
self.assertEqual(shell_args.body_insert, [])
self.assertEqual(shell_args.body_append, [])
self.assertEqual(shell_args.style, [])
shell_args = md2html.parse_args(('--body-insert', '<dict>', '--body-insert', '<cook>'))
self.assertEqual(shell_args.body_insert, ['<dict>', '<cook>'])
shell_args = md2html.parse_args(('--append-css', '~/q/w/e.css', '--append-css', '../qwerty.css'))
self.assertEqual(shell_args.append_css, [
os.path.abspath(os.path.expanduser('~/q/w/e.css')),
os.path.abspath(os.path.join(PWD, '../qwerty.css')),
])
shell_args = md2html.parse_args(('--style', 'sidebar-toc'))
self.assertEqual(shell_args.style, ['sidebar-toc'])
self.assertRaises(SystemExit, md2html.parse_args, ('--style', 'xxx-not-exist'))
def test_parse_args_matrix(self):
path_in_1 = '~/abc/defg.md'
path_in_1_result = os.path.abspath(os.path.expanduser(path_in_1))
path_out_1 = '~/abc/defg.html'
path_out_1_result = os.path.abspath(os.path.expanduser(path_out_1))
title_result_1 = 'defg'
path_in_2 = '../abcdef/defgxx.md'
path_in_2_result = os.path.abspath(os.path.join(PWD, path_in_2))
path_out_2 = '../abcdef/defgxx.html'
path_out_2_result = os.path.abspath(os.path.join(PWD, path_out_2))
title_result_2 = 'defgxx'
title_x = 'zxcvbnm'
cases = {
(): (sys.stdin, sys.stdout, TITLE_UNTITLED),
('-o', ''): (sys.stdin, sys.stdout, TITLE_UNTITLED),
('-o', '-'): (sys.stdin, sys.stdout, TITLE_UNTITLED),
('-o', path_out_1): (sys.stdin, path_out_1_result, TITLE_UNTITLED),
('-o', path_out_2): (sys.stdin, path_out_2_result, TITLE_UNTITLED),
('', ): (sys.stdin, sys.stdout, TITLE_UNTITLED),
('', '-o', ''): (sys.stdin, sys.stdout, TITLE_UNTITLED),
('', '-o', '-'): (sys.stdin, sys.stdout, TITLE_UNTITLED),
('', '-o', path_out_1): (sys.stdin, path_out_1_result, TITLE_UNTITLED),
('', '-o', path_out_2): (sys.stdin, path_out_2_result, TITLE_UNTITLED),
('-', ): (sys.stdin, sys.stdout, TITLE_UNTITLED),
('-', '-o', ''): (sys.stdin, sys.stdout, TITLE_UNTITLED),
('-', '-o', '-'): (sys.stdin, sys.stdout, TITLE_UNTITLED),
('-', '-o', path_out_1): (sys.stdin, path_out_1_result, TITLE_UNTITLED),
('-', '-o', path_out_2): (sys.stdin, path_out_2_result, TITLE_UNTITLED),
(path_in_1, ): (path_in_1_result, path_out_1_result, title_result_1),
(path_in_1, '-o', ''): (path_in_1_result, path_out_1_result, title_result_1),
(path_in_1, '-o', '-'): (path_in_1_result, sys.stdout, title_result_1),
(path_in_1, '-o', path_out_1): (path_in_1_result, path_out_1_result, title_result_1),
(path_in_1, '-o', path_out_2): (path_in_1_result, path_out_2_result, title_result_1),
(path_in_2, ): (path_in_2_result, path_out_2_result, title_result_2),
(path_in_2, '-o', ''): (path_in_2_result, path_out_2_result, title_result_2),
(path_in_2, '-o', '-'): (path_in_2_result, sys.stdout, title_result_2),
(path_in_2, '-o', path_out_1): (path_in_2_result, path_out_1_result, title_result_2),
(path_in_2, '-o', path_out_2): (path_in_2_result, path_out_2_result, title_result_2),
('-t', title_x, ): (sys.stdin, sys.stdout, title_x),
('-t', title_x, '-o', ''): (sys.stdin, sys.stdout, title_x),
('-t', title_x, '-o', '-'): (sys.stdin, sys.stdout, title_x),
('-t', title_x, '-o', path_out_1): (sys.stdin, path_out_1_result, title_x),
('-t', title_x, '-o', path_out_2): (sys.stdin, path_out_2_result, title_x),
('-t', title_x, '', ): (sys.stdin, sys.stdout, title_x),
('-t', title_x, '', '-o', ''): (sys.stdin, sys.stdout, title_x),
('-t', title_x, '', '-o', '-'): (sys.stdin, sys.stdout, title_x),
('-t', title_x, '', '-o', path_out_1): (sys.stdin, path_out_1_result, title_x),
('-t', title_x, '', '-o', path_out_2): (sys.stdin, path_out_2_result, title_x),
('-t', title_x, '-', ): (sys.stdin, sys.stdout, title_x),
('-t', title_x, '-', '-o', ''): (sys.stdin, sys.stdout, title_x),
('-t', title_x, '-', '-o', '-'): (sys.stdin, sys.stdout, title_x),
('-t', title_x, '-', '-o', path_out_1): (sys.stdin, path_out_1_result, title_x),
('-t', title_x, '-', '-o', path_out_2): (sys.stdin, path_out_2_result, title_x),
('-t', title_x, path_in_1, ): (path_in_1_result, path_out_1_result, title_x),
('-t', title_x, path_in_1, '-o', ''): (path_in_1_result, path_out_1_result, title_x),
('-t', title_x, path_in_1, '-o', '-'): (path_in_1_result, sys.stdout, title_x),
('-t', title_x, path_in_1, '-o', path_out_1): (path_in_1_result, path_out_1_result, title_x),
('-t', title_x, path_in_1, '-o', path_out_2): (path_in_1_result, path_out_2_result, title_x),
('-t', title_x, path_in_2, ): (path_in_2_result, path_out_2_result, title_x),
('-t', title_x, path_in_2, '-o', ''): (path_in_2_result, path_out_2_result, title_x),
('-t', title_x, path_in_2, '-o', '-'): (path_in_2_result, sys.stdout, title_x),
('-t', title_x, path_in_2, '-o', path_out_1): (path_in_2_result, path_out_1_result, title_x),
('-t', title_x, path_in_2, '-o', path_out_2): (path_in_2_result, path_out_2_result, title_x),
}
for case in cases:
result_tuple = cases[case]
shell_args = md2html.parse_args(case)
self.assertEqual(shell_args.input_file_obj, result_tuple[0])
self.assertEqual(shell_args.output_file_obj, result_tuple[1])
self.assertEqual(shell_args.title, result_tuple[2])
if __name__ == '__main__':
unittest.main()