-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat_test.go
More file actions
206 lines (189 loc) · 5.96 KB
/
format_test.go
File metadata and controls
206 lines (189 loc) · 5.96 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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package cmd
import (
"os"
"reflect"
"testing"
)
func TestWrapParagraphs(t *testing.T) {
text := `
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
`
columns := 30
got := wrapParagraphs(text, columns)
want := `Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor
incididunt ut labore et dolore
magna aliqua.
Ut enim ad minim veniam, quis
nostrud exercitation ullamco
laboris nisi ut aliquip ex ea
commodo consequat. Duis aute
irure dolor in reprehenderit
in voluptate velit esse cillum
dolore eu fugiat nulla
pariatur.
Excepteur sint occaecat
cupidatat non proident, sunt
in culpa qui officia deserunt
mollit anim id est laborum.
`
if got != want {
t.Errorf("wrapParagraphs(%v) == `%v`, want `%v`", columns, got, want)
}
}
func TestWrapText(t *testing.T) {
text := `
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
`
columns := 30
got := wrapText(text, columns)
want := []string{
"Lorem ipsum dolor sit amet,",
"consectetur adipiscing elit,",
"sed do eiusmod tempor",
"incididunt ut labore et dolore",
"magna aliqua. Ut enim ad minim",
"veniam, quis nostrud",
"exercitation ullamco laboris",
"nisi ut aliquip ex ea commodo",
"consequat. Duis aute irure",
"dolor in reprehenderit in",
"voluptate velit esse cillum",
"dolore eu fugiat nulla",
"pariatur. Excepteur sint",
"occaecat cupidatat non",
"proident, sunt in culpa qui",
"officia deserunt mollit anim",
"id est laborum.",
}
if !reflect.DeepEqual(got, want) {
t.Errorf("wrapText(text, %v) == `%v`, want `%v`", columns, got, want)
}
}
func TestFormatTerms(t *testing.T) {
def := &definition{
terms: []string{"-H", "--dereference-command-line"},
}
cases := []struct {
columns int
want termLines
}{
{
columns: 30,
want: termLines{
separate: nil,
inline: "-H, --dereference-command-line",
},
},
{
columns: 26,
want: termLines{
separate: []string{"-H"},
inline: "--dereference-command-line",
},
},
{
columns: 25,
want: termLines{
separate: []string{"-H", "--dereference-command-line"},
inline: "",
},
},
}
for _, c := range cases {
got := def.formatTerms(c.columns)
if !reflect.DeepEqual(got.separate, c.want.separate) {
t.Errorf("formatTerms returned separate == %v, want %v",
got.separate, c.want.separate)
}
if got.inline != c.want.inline {
t.Errorf("formatTerms returned inline == %v, want %v",
got.inline, c.want.inline)
}
}
}
var optionsList = &definitionList{
title: "Options",
definitions: []*definition{
{
terms: []string{"-a", "--all"},
text: "do not ignore entries starting with .",
},
{
terms: []string{"-H", "--dereference-command-line"},
text: "follow symbolic links listed on the command line",
},
},
}
var commandsList = &definitionList{
title: "Commands",
definitions: []*definition{
{
terms: []string{"go"},
text: "Go go go!",
},
},
}
// func (d *definitionList) format(columns int) string {
func TestDefinitionsList(t *testing.T) {
columns := 30
got := optionsList.format(columns)
want := `Options:
-a, --all do not ignore
entries starting
with .
-H
--dereference-command-line
follow symbolic
links listed on
the command line
`
if got != want {
t.Errorf("optionsList.format(%v) == `%v`, want `%v`", columns, got, want)
}
}
func TestFormatHelp(t *testing.T) {
usage := "Usage: ls [OPTION]... [FILE]..."
summary := `
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.`
details := `
The SIZE argument is an integer and optional unit (example: 10K is 10*1024).
Units are K,M,G,T,P,E,Z,Y (powers of 1024) or KB,MB,... (powers of 1000).
The TIME_STYLE argument can be full-iso, long-iso, iso, locale, or +FORMAT.
FORMAT is interpreted like in date(1). If FORMAT is FORMAT1<newline>FORMAT2,
then FORMAT1 applies to non-recent files and FORMAT2 to recent files.
TIME_STYLE prefixed with 'posix-' takes effect only outside the POSIX locale.
Also the TIME_STYLE environment variable sets the default style to use.
`
defs := []*definitionList{
optionsList,
commandsList,
}
want := `Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default). Sort entries
alphabetically if none of -cftuvSUX nor --sort is specified.
Options:
-a, --all do not ignore entries starting with .
-H
--dereference-command-line
follow symbolic links listed on the command line
Commands:
go Go go go!
The SIZE argument is an integer and optional unit (example: 10K is 10*1024). Units are
K,M,G,T,P,E,Z,Y (powers of 1024) or KB,MB,... (powers of 1000).
The TIME_STYLE argument can be full-iso, long-iso, iso, locale, or +FORMAT. FORMAT is
interpreted like in date(1). If FORMAT is FORMAT1<newline>FORMAT2, then FORMAT1 applies to
non-recent files and FORMAT2 to recent files. TIME_STYLE prefixed with 'posix-' takes
effect only outside the POSIX locale. Also the TIME_STYLE environment variable sets the
default style to use.
`
os.Setenv("COLUMNS", "90")
got := formatHelp(usage, summary, details, defs)
if got != want {
t.Errorf("formatHelp returned `%v`, want `%v`", got, want)
}
}