-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
dedent_test.go
178 lines (161 loc) · 4.18 KB
/
dedent_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
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
package dedent
import (
"fmt"
"testing"
)
const errorMsg = "\nexpected %q\ngot %q"
type dedentTest struct {
text, expect string
}
func TestDedentNoMargin(t *testing.T) {
texts := []string{
// No lines indented
"Hello there.\nHow are you?\nOh good, I'm glad.",
// Similar with a blank line
"Hello there.\n\nBoo!",
// Some lines indented, but overall margin is still zero
"Hello there.\n This is indented.",
// Again, add a blank line.
"Hello there.\n\n Boo!\n",
}
for _, text := range texts {
if text != Dedent(text) {
t.Errorf(errorMsg, text, Dedent(text))
}
}
}
func TestDedentEven(t *testing.T) {
texts := []dedentTest{
{
// All lines indented by two spaces
text: " Hello there.\n How are ya?\n Oh good.",
expect: "Hello there.\nHow are ya?\nOh good.",
},
{
// Same, with blank lines
text: " Hello there.\n\n How are ya?\n Oh good.\n",
expect: "Hello there.\n\nHow are ya?\nOh good.\n",
},
{
// Now indent one of the blank lines
text: " Hello there.\n \n How are ya?\n Oh good.\n",
expect: "Hello there.\n\nHow are ya?\nOh good.\n",
},
}
for _, text := range texts {
if text.expect != Dedent(text.text) {
t.Errorf(errorMsg, text.expect, Dedent(text.text))
}
}
}
func TestDedentUneven(t *testing.T) {
texts := []dedentTest{
{
// Lines indented unevenly
text: `
def foo():
while 1:
return foo
`,
expect: `
def foo():
while 1:
return foo
`,
},
{
// Uneven indentation with a blank line
text: " Foo\n Bar\n\n Baz\n",
expect: "Foo\n Bar\n\n Baz\n",
},
{
// Uneven indentation with a whitespace-only line
text: " Foo\n Bar\n \n Baz\n",
expect: "Foo\n Bar\n\n Baz\n",
},
}
for _, text := range texts {
if text.expect != Dedent(text.text) {
t.Errorf(errorMsg, text.expect, Dedent(text.text))
}
}
}
// Dedent() should not mangle internal tabs.
func TestDedentPreserveInternalTabs(t *testing.T) {
text := " hello\tthere\n how are\tyou?"
expect := "hello\tthere\nhow are\tyou?"
if expect != Dedent(text) {
t.Errorf(errorMsg, expect, Dedent(text))
}
// Make sure that it preserves tabs when it's not making any changes at all
if expect != Dedent(expect) {
t.Errorf(errorMsg, expect, Dedent(expect))
}
}
// Dedent() should not mangle tabs in the margin (i.e. tabs and spaces both
// count as margin, but are *not* considered equivalent).
func TestDedentPreserveMarginTabs(t *testing.T) {
texts := []string{
" hello there\n\thow are you?",
// Same effect even if we have 8 spaces
" hello there\n\thow are you?",
}
for _, text := range texts {
d := Dedent(text)
if text != d {
t.Errorf(errorMsg, text, d)
}
}
texts2 := []dedentTest{
{
// Dedent() only removes whitespace that can be uniformly removed!
text: "\thello there\n\thow are you?",
expect: "hello there\nhow are you?",
},
{
text: " \thello there\n \thow are you?",
expect: "hello there\nhow are you?",
},
{
text: " \t hello there\n \t how are you?",
expect: "hello there\nhow are you?",
},
{
text: " \thello there\n \t how are you?",
expect: "hello there\n how are you?",
},
}
for _, text := range texts2 {
if text.expect != Dedent(text.text) {
t.Errorf(errorMsg, text.expect, Dedent(text.text))
}
}
}
func ExampleDedent() {
s := `
Lorem ipsum dolor sit amet,
consectetur adipiscing elit.
Curabitur justo tellus, facilisis nec efficitur dictum,
fermentum vitae ligula. Sed eu convallis sapien.`
fmt.Println(Dedent(s))
fmt.Println("-------------")
fmt.Println(s)
// Output:
// Lorem ipsum dolor sit amet,
// consectetur adipiscing elit.
// Curabitur justo tellus, facilisis nec efficitur dictum,
// fermentum vitae ligula. Sed eu convallis sapien.
// -------------
//
// Lorem ipsum dolor sit amet,
// consectetur adipiscing elit.
// Curabitur justo tellus, facilisis nec efficitur dictum,
// fermentum vitae ligula. Sed eu convallis sapien.
}
func BenchmarkDedent(b *testing.B) {
for i := 0; i < b.N; i++ {
Dedent(`Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Curabitur justo tellus, facilisis nec efficitur dictum,
fermentum vitae ligula. Sed eu convallis sapien.`)
}
}