v / vlib / flag / flag_to_doc_test.v
208 lines · 169 sloc · 7.24 KB · 22e8b0fe27c2b6f1fb9b76cf4cd094980ae5e77f
Raw
1import flag
2
3const doc1 = 'an app 1.2.3
4--------------------------------------------------------
5Some config struct
6
7Options:
8 -v, --show-version Show version and exit
9
10 -d, --debug <int> Debug level
11
12 -l <f32> This doc text is overwritten
13Use ESC to exit'
14
15const doc2 = 'an app 1.2.3
16-----------------------------------------------------------------------------
17Some config struct
18
19Options:
20 -v, --show-version Show version and exit
21
22 -d, --debug <int> Debug level
23
24 -l <f32> This is a doc string of the field `level` on
25 struct `Config`
26
27 -e, --extra Extra flag that does not exist on the struct, but
28 we want documented (in same format as the others)
29Use ESC to exit'
30
31const doc3 = 'my app 1.0
32--------------------------------------------------------
33My application
34
35Options:
36 -v, --show-version Show version and exit
37
38 -d, --debug <int> Debug level
39
40 -l <f32> This doc text is overwritten
41Use ESC to exit'
42
43@[xdoc: 'Some config struct']
44@[footer: 'Use ESC to exit']
45@[name: 'an app']
46@[version: '1.2.3']
47struct Config {
48 show_version bool @[short: v; xdoc: 'Show version and exit']
49 debug_level int @[long: debug; short: d; xdoc: 'Debug level']
50 level f32 @[only: l; xdoc: 'This doc text is overwritten']
51}
52
53fn test_direct_to_doc() {
54 assert flag.to_doc[Config]()! == doc1
55}
56
57fn test_attrs() {
58 assert flag.to_doc[Config](
59 fields: {
60 'level': 'This is a doc string of the field `level` on struct `Config`'
61 '-e, --extra': 'Extra flag that does not exist on the struct, but we want documented (in same format as the others)'
62 }
63 )! == doc2
64}
65
66fn test_attrs_override() {
67 assert flag.to_doc[Config](
68 name: 'my app'
69 version: '1.0'
70 description: 'My application'
71 )! == doc3
72}
73
74const doc4 = 'flag_to_doc_test 1.0
75--------------------------------------------------------------------------------
76Flag to doc test.
77Content here
78
79Options:
80 -v, --show-version Show version and exit
81
82 -d, --debug <int> Debug level
83
84 -l <f32> Level of lorem ipsum
85 and more
86 many many many more.
87 Notice how user newlines/format is kept since
88 input lines are all less or within
89 the default layout.description_padding
90 and max width
91
92 --example <string> Looong example text without newlines or anything
93 else and lorem ipsum and more and many many many
94 more. Should be auto fitted
95
96 --square
97
98 -m, -mmm... (can repeat) This flag can be repeated
99
100 -w, --wroom <int> (allowed multiple times)
101
102 --the-limit <string> Looongbobbytextwithoutnewlinesoranythingelseandlorem
103 ipsumandmoreandmanymanymanymore
104 ffffffffffffffffffffffffffffffff f
105
106 -e, --extra Secret flag that does not exist on the struct,
107 but we want documented (in same format as the
108 others)
109
110 -q, --quiet-and-quite-long-flag <string>
111 Mega long description and secret flag that does
112 not exist on the struct, but we want documented.
113 Also the flag has custom newlines and the flag
114 line itself is super long
115
116Footer content'
117
118const doc5 = 'flag_to_doc_test 1.0
119--------------------------------------------------------------------------------
120Flag to doc test.
121Content here
122
123Options:
124 -v, --show-version Show version and exit
125 -d, --debug <int> Debug level
126 -l <f32> Level of lorem ipsum
127 and more
128 many many many more.
129 Notice how user newlines/format is kept since
130 input lines are all less or within
131 the default layout.description_padding
132 and max width
133 --example <string> Looong example text without newlines or anything
134 else and lorem ipsum and more and many many many
135 more. Should be auto fitted
136 --square
137 -m, -mmm... (can repeat) This flag can be repeated
138 -w, --wroom <int> (allowed multiple times)
139 --the-limit <string> Looongbobbytextwithoutnewlinesoranythingelseandlorem
140 ipsumandmoreandmanymanymanymore
141 ffffffffffffffffffffffffffffffff f
142 -e, --extra Secret flag that does not exist on the struct,
143 but we want documented (in same format as the
144 others)
145 -q, --quiet-and-quite-long-flag <string>
146 Mega long description and secret flag that does
147 not exist on the struct, but we want documented.
148 Also the flag has custom newlines and the flag
149 line itself is super long
150
151Footer content'
152
153@[name: 'flag_to_doc_test']
154@[version: '1.0']
155struct DocTest {
156 show_version bool @[short: v; xdoc: 'Show version and exit']
157 debug_level int @[long: debug; short: d; xdoc: 'Debug level']
158 level f32 @[only: l; xdoc: 'Override this doc string']
159 example string
160 square bool
161 multi int @[only: m; repeats]
162 wroom []int @[short: w]
163 the_limit string
164}
165
166const field_docs = {
167 'level': 'Level of lorem ipsum\nand more\nmany many many more.\nNotice how user newlines/format is kept since\ninput lines are all less or within\nthe default layout.description_padding\nand max width'
168 'example': 'Looong example text without newlines or anything else and lorem ipsum and more and many many many more. Should be auto fitted'
169 'the_limit': 'Looongbobbytextwithoutnewlinesoranythingelseandlorem ipsumandmoreandmanymanymanymore ffffffffffffffffffffffffffffffff f'
170 'multi': 'This flag can be repeated'
171 '-e, --extra': 'Secret flag that does not exist on the struct, but we want documented (in same format as the others)'
172 '-q, --quiet-and-quite-long-flag <string>': 'Mega long description and secret flag that does not exist on the struct, but we want documented. Also the flag has custom newlines\nand the flag line itself is super long'
173}
174
175fn test_flag_to_doc_spacing_and_new_lines() {
176 assert flag.to_doc[DocTest](
177 description: 'Flag to doc test.
178Content here'
179 footer: '
180Footer content'
181 fields: unsafe { field_docs }
182 )! == doc4
183
184 // Test in compact mode also
185 assert flag.to_doc[DocTest](
186 options: flag.DocOptions{
187 compact: true
188 }
189 description: 'Flag to doc test.
190Content here'
191 footer: '
192Footer content'
193 fields: unsafe { field_docs }
194 )! == doc5
195}
196
197struct DocTestXDoc {
198 dim int @[xdoc: 'dimensions, one of: 1, 2, 3.']
199}
200
201fn test_to_doc_with_xdoc_containing_colon() {
202 expected := '
203Options:
204 --dim <int> dimensions, one of: 1, 2, 3.'
205
206 help := flag.to_doc[DocTestXDoc]()!
207 assert help == expected
208}
209