-
Notifications
You must be signed in to change notification settings - Fork 0
/
para_test.go
152 lines (142 loc) · 2.94 KB
/
para_test.go
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
package main
import (
"bufio"
"bytes"
"strings"
"testing"
)
func wraptext(col int, in string) string {
reader := strings.NewReader(in)
scanner := bufio.NewScanner(reader)
var buffer bytes.Buffer
writer := bufio.NewWriter(&buffer)
_ = Wrapper{maxCols: col}.Wraptext(scanner, writer)
return buffer.String()
}
func sameText(in, wr string) bool {
despacer := func(x rune) rune {
if x == '\n' || x == ' ' {
return 'S'
}
return x
}
return strings.Map(despacer, wr) == strings.Map(despacer, in)
}
func TestWrapper_Compact(t *testing.T) {
for _, spec := range []struct {
input string
wrapAt int
expectedLines int
}{
{
input: "My name is Wile\nE Coyote\nGenius",
wrapAt: 40,
expectedLines: 1,
},
} {
t.Run("wrapping"+spec.input, func(t *testing.T) {
result := wraptext(spec.wrapAt, spec.input)
if !sameText(result, spec.input) {
t.Errorf("text was altered: %s", result)
}
if lines := len(strings.Split(result, "\n")); lines != spec.expectedLines {
t.Errorf("expected %d lines, got %d", spec.expectedLines, lines)
}
})
}
}
func TestWrapper_Break(t *testing.T) {
for _, spec := range []struct {
input string
wrapAt int
expectedLines int
}{
{
input: "12 12 12 2334 233",
wrapAt: 8,
expectedLines: 2,
},
{
input: "12 12 12 233 23 3 345 34",
wrapAt: 10,
expectedLines: 3,
},
{
input: "My name is Wile E. Coyote",
wrapAt: 15,
expectedLines: 2,
},
{
input: "My name is Wile E.\nCoyote.\n\nGenius",
wrapAt: 15,
expectedLines: 5,
},
{
input: "My name is Wile E.\nCoyote",
wrapAt: 15,
expectedLines: 3,
},
} {
t.Run("wrapping"+spec.input, func(t *testing.T) {
result := wraptext(spec.wrapAt, spec.input)
if !sameText(result, spec.input) {
t.Errorf("text was altered: %s", result)
}
if lines := len(strings.Split(result, "\n")); lines != spec.expectedLines {
t.Errorf("expected %d lines, got %d", spec.expectedLines, lines)
}
})
}
}
func TestWrapper_Passthrough(t *testing.T) {
for _, spec := range []struct {
name string
input string
wrapAt int
}{
{
name: "utf-8 text",
input: "Qué tal está señora?",
wrapAt: 40,
},
{
name: "blank lines",
input: "My name is E.\n\n\nCoyote",
wrapAt: 15,
},
{
name: "incompressible",
input: "12 456\n1012 is ok",
wrapAt: 10,
},
{
name: "markdown list compression",
input: `# title
This is an enumeration:
## and a title right away
* one foo bar
baz quux
* two
`,
wrapAt: 40,
},
{
name: "markdown",
input: `# title
This is an enumeration:
* one
* two
- three
- four
And then, that too`,
wrapAt: 40,
},
} {
t.Run(spec.name, func(t *testing.T) {
result := wraptext(spec.wrapAt, spec.input)
if result != spec.input {
t.Errorf("testing '%s', result altered:\n%s", spec.name, result)
}
})
}
}