1
2
3
4 package cli
5
6 import (
7 "errors"
8 "testing"
9 )
10
11 func TestToMarkdownFull(t *testing.T) {
12
13 app := testApp()
14
15
16 res, err := app.ToMarkdown()
17
18
19 expect(t, err, nil)
20 expectFileContent(t, "testdata/expected-doc-full.md", res)
21 }
22
23 func TestToMarkdownNoFlags(t *testing.T) {
24
25 app := testApp()
26 app.Flags = nil
27
28
29 res, err := app.ToMarkdown()
30
31
32 expect(t, err, nil)
33 expectFileContent(t, "testdata/expected-doc-no-flags.md", res)
34 }
35
36 func TestToMarkdownNoCommands(t *testing.T) {
37
38 app := testApp()
39 app.Commands = nil
40
41
42 res, err := app.ToMarkdown()
43
44
45 expect(t, err, nil)
46 expectFileContent(t, "testdata/expected-doc-no-commands.md", res)
47 }
48
49 func TestToMarkdownNoAuthors(t *testing.T) {
50
51 app := testApp()
52 app.Authors = []*Author{}
53
54
55 res, err := app.ToMarkdown()
56
57
58 expect(t, err, nil)
59 expectFileContent(t, "testdata/expected-doc-no-authors.md", res)
60 }
61
62 func TestToMarkdownNoUsageText(t *testing.T) {
63
64 app := testApp()
65 app.UsageText = ""
66
67
68 res, err := app.ToMarkdown()
69
70
71 expect(t, err, nil)
72 expectFileContent(t, "testdata/expected-doc-no-usagetext.md", res)
73 }
74
75 func TestToMan(t *testing.T) {
76
77 app := testApp()
78
79
80 res, err := app.ToMan()
81
82
83 expect(t, err, nil)
84 expectFileContent(t, "testdata/expected-doc-full.man", res)
85 }
86
87 func TestToManParseError(t *testing.T) {
88
89 app := testApp()
90
91
92
93 tmp := MarkdownDocTemplate
94 MarkdownDocTemplate = `{{ .App.Name`
95 _, err := app.ToMan()
96 MarkdownDocTemplate = tmp
97
98
99 expect(t, err, errors.New(`template: cli:1: unclosed action`))
100 }
101
102 func TestToManWithSection(t *testing.T) {
103
104 app := testApp()
105
106
107 res, err := app.ToManWithSection(8)
108
109
110 expect(t, err, nil)
111 expectFileContent(t, "testdata/expected-doc-full.man", res)
112 }
113
114 func Test_prepareUsageText(t *testing.T) {
115 t.Run("no UsageText provided", func(t *testing.T) {
116
117 cmd := Command{}
118
119
120 res := prepareUsageText(&cmd)
121
122
123 expect(t, res, "")
124 })
125
126 t.Run("single line UsageText", func(t *testing.T) {
127
128 cmd := Command{UsageText: "Single line usage text"}
129
130
131 res := prepareUsageText(&cmd)
132
133
134 expect(t, res, ">Single line usage text\n")
135 })
136
137 t.Run("multiline UsageText", func(t *testing.T) {
138
139 cmd := Command{
140 UsageText: `
141 Usage for the usage text
142 - Should be a part of the same code block
143 `,
144 }
145
146
147 res := prepareUsageText(&cmd)
148
149
150 test := ` Usage for the usage text
151 - Should be a part of the same code block
152 `
153 expect(t, res, test)
154 })
155
156 t.Run("multiline UsageText has formatted embedded markdown", func(t *testing.T) {
157
158 cmd := Command{
159 UsageText: `
160 Usage for the usage text
161
162 ` + "```" + `
163 func() { ... }
164 ` + "```" + `
165
166 Should be a part of the same code block
167 `,
168 }
169
170
171 res := prepareUsageText(&cmd)
172
173
174 test := ` Usage for the usage text
175
176 ` + "```" + `
177 func() { ... }
178 ` + "```" + `
179
180 Should be a part of the same code block
181 `
182 expect(t, res, test)
183 })
184 }
185
186 func Test_prepareUsage(t *testing.T) {
187 t.Run("no Usage provided", func(t *testing.T) {
188
189 cmd := Command{}
190
191
192 res := prepareUsage(&cmd, "")
193
194
195 expect(t, res, "")
196 })
197
198 t.Run("simple Usage", func(t *testing.T) {
199
200 cmd := Command{Usage: "simple usage text"}
201
202
203 res := prepareUsage(&cmd, "")
204
205
206 expect(t, res, cmd.Usage+"\n")
207 })
208
209 t.Run("simple Usage with UsageText", func(t *testing.T) {
210
211 cmd := Command{Usage: "simple usage text"}
212
213
214 res := prepareUsage(&cmd, "a non-empty string")
215
216
217 expect(t, res, cmd.Usage+"\n\n")
218 })
219 }
220
View as plain text