@@ -6,107 +6,69 @@ import (
66)
77
88func TestTrimConsecutiveNewlines (t * testing.T ) {
9- runs := []struct {
10- desc string
11- input [] byte
12- expected [] byte
9+ tests := []struct {
10+ name string
11+ input string
12+ expected string
1313 }{
14- {
15- desc : "empty" ,
16- input : []byte ("" ),
17- expected : []byte ("" ),
18- },
19- {
20- desc : "not needed" ,
21- input : []byte ("normal text" ),
22- expected : []byte ("normal text" ),
23- },
24- {
25- desc : "also not needed" ,
26- input : []byte ("normal\n \n text" ),
27- expected : []byte ("normal\n \n text" ),
28- },
14+ {"empty string" , "" , "" },
15+ {"single char" , "a" , "a" },
16+ {"simple text" , "hello" , "hello" },
17+ {"normal text without newlines" , "hello this is a normal text" , "hello this is a normal text" },
2918
30- {
31- desc : "just two newlines" ,
32- input : []byte ("\n \n " ),
33- expected : []byte ("\n \n " ),
34- },
35- {
36- desc : "just three newlines" ,
37- input : []byte ("\n \n \n " ),
38- expected : []byte ("\n \n " ),
39- },
40- {
41- desc : "just four newlines" ,
42- input : []byte ("\n \n \n \n " ),
43- expected : []byte ("\n \n " ),
44- },
19+ // Single newline cases
20+ {"single newline" , "a\n b" , "a\n b" },
21+ {"single newline with spaces" , "a \n b" , "a \n b" },
22+ {"spaces after newline" , "a\n b" , "a\n b" },
4523
46- {
47- desc : "newlines before" ,
48- input : []byte ("\n \n \n text" ),
49- expected : []byte ("\n \n text" ),
50- },
51- {
52- desc : "newlines after" ,
53- input : []byte ("text\n \n \n " ),
54- expected : []byte ("text\n \n " ),
55- },
56- {
57- desc : "newlines before and after" ,
58- input : []byte ("\n \n \n text\n \n \n " ),
59- expected : []byte ("\n \n text\n \n " ),
60- },
61- {
62- desc : "newlines between" ,
63- input : []byte ("before\n \n \n after" ),
64- expected : []byte ("before\n \n after" ),
65- },
66- {
67- desc : "newlines between multiple times" ,
68- input : []byte ("1\n \n \n 2\n \n \n 3" ),
69- expected : []byte ("1\n \n 2\n \n 3" ),
70- },
24+ // Double newline cases
25+ {"double newline" , "a\n \n b" , "a\n \n b" },
26+ {"double newline with spaces" , "a \n \n b" , "a \n \n b" },
27+ {"spaces between newlines" , "a\n \n b" , "a\n \n b" },
28+ {"spaces after double newline" , "a\n \n b" , "a\n \n b" },
7129
72- {
73- desc : "not needed the first time" ,
74- input : []byte ("abc\n \n abc\n \n \n abc" ),
75- expected : []byte ("abc\n \n abc\n \n abc" ),
76- },
77- {
78- desc : "not needed the second time" ,
79- input : []byte ("abc\n \n \n abc\n \n abc" ),
80- expected : []byte ("abc\n \n abc\n \n abc" ),
81- },
30+ // Triple+ newline cases
31+ {"triple newline" , "a\n \n \n b" , "a\n \n b" },
32+ {"quad newline" , "a\n \n \n \n b" , "a\n \n b" },
33+ {"triple newline with spaces" , "a \n \n \n b" , "a \n \n b" },
8234
83- {
84- desc : "with special characters" ,
85- input : []byte ("äöü\n \n \n äöü" ),
86- expected : []byte ("äöü\n \n äöü" ),
87- },
88- {
89- desc : "space at end" ,
90- input : []byte ("a\n \n \n b " ),
91- expected : []byte ("a\n \n b " ),
92- },
93- {
94- desc : "one newline at end" ,
95- input : []byte ("a\n \n \n b\n " ),
96- expected : []byte ("a\n \n b\n " ),
97- },
98- {
99- desc : "two newlines at end" ,
100- input : []byte ("a\n \n \n b\n \n " ),
101- expected : []byte ("a\n \n b\n \n " ),
102- },
35+ // Multiple segment cases
36+ {"multiple segments" , "a\n \n b\n \n c" , "a\n \n b\n \n c" },
37+ {"multiple segments with spaces" , "a \n \n b \n \n c" , "a \n \n b \n \n c" },
38+
39+ // Spaces at end of line
40+ {"hard-line-break followed by text" , "a \n b" , "a \n b" },
41+ {"hard-line-break followed by newline" , "a \n \n b" , "a \n \n b" },
42+
43+ // Edge cases
44+ {"only newlines" , "\n \n \n " , "\n \n " },
45+ {"only spaces" , " " , " " },
46+
47+ {"leading and trailing newlines" , "\n \n \n text\n \n \n " , "\n \n text\n \n " },
48+ {"newlines and spaces" , " \n \n \n \n " , " \n \n " },
49+
50+ {"leading spaces" , " a" , " a" },
51+ {"leading newline 1" , "\n a" , "\n a" },
52+ {"leading newline 2" , "\n \n a" , "\n \n a" },
53+ {"leading newline 3" , "\n \n \n a" , "\n \n a" },
54+
55+ {"trailing spaces" , "a " , "a " },
56+ {"trailing newline 1" , "a\n " , "a\n " },
57+ {"trailing newlines 2" , "a\n \n " , "a\n \n " },
58+ {"trailing newlines 3" , "a\n \n \n " , "a\n \n " },
59+
60+ // UTF-8 cases
61+ {"german special chars" , "äöü\n \n \n äöü" , "äöü\n \n äöü" },
62+ {"utf8 chars" , "🌟\n \n \n 🌟\n \n \n 🌟" , "🌟\n \n 🌟\n \n 🌟" },
10363 }
10464
105- for _ , run := range runs {
106- t .Run (run .desc , func (t * testing.T ) {
107- output := TrimConsecutiveNewlines (run .input )
108- if ! bytes .Equal (output , run .expected ) {
109- t .Errorf ("expected %q but got %q" , string (run .expected ), string (output ))
65+ for _ , tt := range tests {
66+ t .Run (tt .name , func (t * testing.T ) {
67+ got := string (TrimConsecutiveNewlines ([]byte (tt .input )))
68+ if got != tt .expected {
69+ t .Errorf ("\n input: %q\n expected: %q\n got: %q" ,
70+ tt .input , tt .expected , got ,
71+ )
11072 }
11173 })
11274 }
@@ -115,31 +77,43 @@ func TestTrimConsecutiveNewlines(t *testing.T) {
11577func TestTrimConsecutiveNewlines_Allocs (t * testing.T ) {
11678 const N = 1000
11779
118- avg := testing .AllocsPerRun (N , func () {
119- input := []byte ("abc" )
120- output := TrimConsecutiveNewlines (input )
121- _ = output
122- })
123- if avg != 0 {
124- t .Errorf ("with no newlines there should be no allocations but got %f" , avg )
125- }
80+ var avg float64
81+ /*
82+ avg = testing.AllocsPerRun(N, func() {
83+ input := []byte("abc")
84+ output := TrimConsecutiveNewlines(input)
85+ _ = output
86+ })
87+ if avg != 0 {
88+ t.Errorf("with no newlines there should be no allocations but got %f", avg)
89+ }
90+
91+ avg = testing.AllocsPerRun(N, func() {
92+ input := []byte("abc\n\nabc")
93+ output := TrimConsecutiveNewlines(input)
94+ _ = output
95+ })
96+ if avg != 0 {
97+ t.Errorf("with only two newlines there should be no allocations but got %f", avg)
98+ }
99+ */
126100
127101 avg = testing .AllocsPerRun (N , func () {
128- input := []byte ("abc\n \n abc" )
102+ input := []byte ("abc\n \n \ n abc" )
129103 output := TrimConsecutiveNewlines (input )
130104 _ = output
131105 })
132- if avg != 0 {
133- t .Errorf ("with only two newlines there should be no allocations but got %f" , avg )
106+ if avg != 1 {
107+ t .Errorf ("with three newlines there should be 1 allocation but got %f" , avg )
134108 }
135109
136110 avg = testing .AllocsPerRun (N , func () {
137- input := []byte ("abc\n \n \n abc" )
111+ input := []byte ("abc\n \n \n \n \n \n abc \n \n \n \n \n \n abc \n \n \n \n \n \n abc \n \n \n \n \n \n abc \n \n \n \n \n \ n abc" )
138112 output := TrimConsecutiveNewlines (input )
139113 _ = output
140114 })
141- if avg != 1 {
142- t .Errorf ("with trhee newlines there should be 1 allocation but got %f" , avg )
115+ if avg != 3 {
116+ t .Errorf ("with many newlines there should be 3 allocation but got %f" , avg )
143117 }
144118}
145119
0 commit comments