-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringtest.go
More file actions
244 lines (208 loc) · 6.2 KB
/
Copy pathstringtest.go
File metadata and controls
244 lines (208 loc) · 6.2 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package stringtest
import "strings"
// JoinLF joins strings with LF (\n) line endings.
//
// Use this to construct expected test output with explicit line endings.
// See also [JoinCRLF] for Windows-style line endings.
// See also [LinesLF] for newline-terminated output.
//
// Example:
//
// want := stringtest.JoinLF(
// "line1",
// "line2",
// "line3",
// ) // -> "line1\nline2\nline3"
func JoinLF(ss ...string) string {
var sb strings.Builder
for i, s := range ss {
if i > 0 {
sb.WriteByte('\n')
}
sb.WriteString(s)
}
return sb.String()
}
// JoinCRLF joins strings with CRLF (\r\n) line endings.
//
// Use this to construct expected test output with explicit Windows-style line
// endings. See also [JoinLF] for Unix-style line endings.
//
// Example:
//
// want := stringtest.JoinCRLF(
// "line1",
// "line2",
// "line3",
// ) // -> "line1\r\nline2\r\nline3"
func JoinCRLF(ss ...string) string {
var sb strings.Builder
for i, s := range ss {
if i > 0 {
sb.WriteByte('\r')
sb.WriteByte('\n')
}
sb.WriteString(s)
}
return sb.String()
}
// TrimLineEnds trims trailing spaces and tabs from every line of s,
// preserving line structure and any final newline. CRLF line endings are
// preserved: whitespace before a trailing \r is trimmed and the \r kept.
//
// Use this to normalize rendered output before comparing it against
// expected strings built with [JoinLF]; terminal renderers such as
// lipgloss pad lines with trailing spaces to the render width.
func TrimLineEnds(s string) string {
if s == "" {
return ""
}
lines := strings.Split(s, "\n")
for i, line := range lines {
if strings.HasSuffix(line, "\r") {
lines[i] = strings.TrimRight(line[:len(line)-1], " \t") + "\r"
} else {
lines[i] = strings.TrimRight(line, " \t")
}
}
return strings.Join(lines, "\n")
}
// LinesLF joins strings as newline-terminated lines using LF (\n) line endings.
//
// Each argument is treated as one complete line; every line, including the
// last, is terminated with "\n". LinesLF("a", "b") == "a\nb\n". LinesLF()
// == "".
//
// This differs from [JoinLF], which uses "\n" as a separator and does not
// terminate the final element. See also [JoinLF] when the output does not
// end in a newline. See also [LinesCRLF] for Windows-style line endings.
//
// Example:
//
// want := stringtest.LinesLF(
// "line1",
// "line2",
// "line3",
// ) // -> "line1\nline2\nline3\n"
func LinesLF(ss ...string) string {
if len(ss) == 0 {
return ""
}
var sb strings.Builder
for _, s := range ss {
sb.WriteString(s)
sb.WriteByte('\n')
}
return sb.String()
}
// LinesCRLF joins strings as newline-terminated lines using CRLF (\r\n) line endings.
//
// Each argument is treated as one complete line; every line, including the
// last, is terminated with "\r\n". LinesCRLF("a", "b") == "a\r\nb\r\n".
// LinesCRLF() == "".
//
// This differs from [JoinCRLF], which uses "\r\n" as a separator and does
// not terminate the final element. See also [JoinCRLF] when the output does
// not end in a newline. See also [LinesLF] for Unix-style line endings.
//
// Example:
//
// want := stringtest.LinesCRLF(
// "line1",
// "line2",
// "line3",
// ) // -> "line1\r\nline2\r\nline3\r\n"
func LinesCRLF(ss ...string) string {
if len(ss) == 0 {
return ""
}
var sb strings.Builder
for _, s := range ss {
sb.WriteString(s)
sb.WriteByte('\r')
sb.WriteByte('\n')
}
return sb.String()
}
// Margin normalizes a test string using margin markers, preserving
// significant leading whitespace. Each line is written with a '|' marker;
// everything before and including the first '|' (which must be preceded
// only by spaces and tabs) is removed, and everything after it is kept
// verbatim. Lines without a marker are left unchanged.
//
// At most one leading newline is stripped (so the block can start on the
// line after the opening backtick), and a final whitespace-only line
// without a marker is dropped along with the newline that precedes it (so
// the closing backtick can be indented and the result carries no trailing
// newline). Unlike [Input], trailing whitespace on marked lines is
// preserved. Pair with [LinesLF] semantics by appending "\n" when a
// newline-terminated want is needed.
//
// Example:
//
// want := stringtest.Margin(`
// | 1 | first line with leading spaces
// | 2 | second line
// | 3 |
// `)
// // -> " 1 | first line with leading spaces\n 2 | second line\n 3 |"
func Margin(s string) string {
s = strings.TrimPrefix(s, "\n")
lines := strings.Split(s, "\n")
// Drop the final line if it is whitespace-only and contains no '|'.
if n := len(lines); n > 0 {
last := lines[n-1]
if strings.TrimSpace(last) == "" && !strings.Contains(last, "|") {
lines = lines[:n-1]
}
}
for i, line := range lines {
// Find the first non-space/non-tab character.
trimmed := strings.TrimLeft(line, " \t")
if strings.HasPrefix(trimmed, "|") {
lines[i] = trimmed[1:]
}
// Otherwise leave the line unchanged.
}
return strings.Join(lines, "\n")
}
// Input is a helper to normalize test input strings.
//
// It dedents the string by removing the common leading whitespace from all
// lines, allowing test inputs to be indented for readability while producing
// clean output.
//
// At most one leading newline and one trailing newline are stripped.
func Input(s string) string {
// Strip at most one leading newline (allows backtick strings to start on next line).
s = strings.TrimPrefix(s, "\n")
// Strip trailing spaces/tabs (allows closing backtick to be indented).
s = strings.TrimRight(s, " \t")
// Strip at most one trailing newline.
s = strings.TrimSuffix(s, "\n")
lines := strings.Split(s, "\n")
if len(lines) == 0 {
return ""
}
// Find minimum indentation across non-empty lines.
minIndent := -1
for _, line := range lines {
if strings.TrimSpace(line) == "" {
continue // Skip empty/whitespace-only lines.
}
indent := len(line) - len(strings.TrimLeft(line, " \t"))
if minIndent == -1 || indent < minIndent {
minIndent = indent
}
}
if minIndent <= 0 {
return strings.Join(lines, "\n")
}
// Remove common indentation from all lines.
for i, line := range lines {
if len(line) >= minIndent {
lines[i] = line[minIndent:]
}
}
return strings.Join(lines, "\n")
}