-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_outline.py
191 lines (178 loc) · 5.51 KB
/
test_outline.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import unittest
from dataclasses import dataclass
from typing import List
from outline import OutlineItem, format_str, parse_str
class TestParse(unittest.TestCase):
def test_parse(self):
"""测试解析函数"""
@dataclass
class TestCase:
name: str
"""测试项目"""
s: str
"""输入参数"""
isErr: bool
"""是否为异常"""
want: List[OutlineItem]
"""期望的返回值"""
data = [
TestCase(
name='正常情况',
s="a a 1\n"
" b b 2\n"
" c c 3\n"
" d d 4\n"
" e e 5\n",
isErr=False,
want=[
{
'name': 'a a',
'page': 1,
'children': [
{
'name': 'b b',
'page': 2,
'children': [
{
'name': 'c c',
'page': 3,
'children': [],
},
{
'name': 'd d',
'page': 4,
'children': [],
},
],
},
{
'name': 'e e',
'page': 5,
'children': [],
},
],
},
],
),
TestCase(
name='首行目录包含缩进符',
s=" b b 2\n",
isErr=True,
want=[],
),
TestCase(
name='目录缩进字符使用空格符',
s="a 1\n"
" b 2\n",
isErr=True,
want=[],
),
TestCase(
name='负数',
s="a -1\n",
isErr=False,
want=[OutlineItem(name='a', page=-1, children=[])],
),
TestCase(
name='包含注释',
s="-- foo\na 1\n",
isErr=False,
want=[OutlineItem(name='a', page=1, children=[])],
),
]
for v in data:
if v.isErr:
with self.assertRaises(Exception):
parse_str(v.s)
else:
value = parse_str(v.s)
self.assertEqual(v.want, value, v.name)
def test_format(self):
"""测试格式化函数"""
@dataclass
class TestCase:
name: str
"""测试项目"""
items: List[OutlineItem]
"""目录项目"""
want: str
"""期望值"""
data = [
TestCase(
name='正常情况',
items=[
{
'name': 'a a',
'page': 1,
'children': [
{
'name': 'b b',
'page': 2,
'children': [
{
'name': 'c c',
'page': 3,
'children': [],
},
{
'name': 'd d',
'page': 4,
'children': [],
},
],
},
{
'name': 'e e',
'page': 5,
'children': [],
},
],
},
],
want="a a 1\n"
" b b 2\n"
" c c 3\n"
" d d 4\n"
" e e 5\n",
),
TestCase(
name='单行',
items=[
OutlineItem(
name='a',
page=1,
children=[],
),
],
want='a 1\n',
),
]
for v in data:
s = format_str(v.items)
self.assertEqual(s, v.want, v.name)
def test_cycle(self):
"""测试 parse 与 format 的结果是否可以相互转换"""
@dataclass
class TestCase:
name: str
"""测试项目"""
s: str
"""被解析项"""
data = [
TestCase(
name='基础情况',
s="a 1\n",
),
TestCase(
name="正常情况",
s="a a 1\n"
" b b 2\n"
" c c 3\n"
" d d 4\n"
" e e 5\n",
),
]
for v in data:
items = parse_str(v.s)
s = format_str(items)
self.assertEqual(s, v.s, v.name)