Skip to content

Commit 91f3abe

Browse files
author
Timothée Peignier
committed
Reuse go logic and add tests
1 parent 35aee55 commit 91f3abe

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-10
lines changed

helpers.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,6 @@ func asComment(c string) string {
122122
removeNewlines := func(s string) string {
123123
return strings.Replace(s, "\n", "\n// ", -1)
124124
}
125-
findLastIndex := func(rs []rune, r rune) int {
126-
l := -1
127-
for i, v := range rs {
128-
if v == r {
129-
l = i
130-
}
131-
}
132-
return l
133-
}
134125
r := []rune(c)
135126
for len(r) > 0 {
136127
line := r
@@ -139,7 +130,9 @@ func asComment(c string) string {
139130
break
140131
}
141132
line = line[:maxLen]
142-
si := findLastIndex(line, []rune(" ")[0])
133+
si := strings.LastIndexFunc(string(line), func(r rune) bool {
134+
return unicode.IsSpace(r)
135+
})
143136
if si != -1 {
144137
line = line[:si]
145138
}

helpers_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,38 @@ func TestInitialCap(t *testing.T) {
3636
}
3737
}
3838
}
39+
40+
var asCommentTests = []struct {
41+
Comment string
42+
Commented string
43+
}{
44+
{
45+
Comment: `This is a multi-line
46+
comment, it contains multiple
47+
lines.`,
48+
Commented: `// This is a multi-line
49+
// comment, it contains multiple
50+
// lines.
51+
`,
52+
},
53+
{
54+
Comment: `This is a fairly long comment line, it should be over seventy characters.`,
55+
Commented: `// This is a fairly long comment line, it should be over seventy
56+
// characters.
57+
`,
58+
},
59+
{
60+
Comment: `散りぬべき時知りてこそ世の中の花も花なれ人も人なれ`,
61+
Commented: `// 散りぬべき時知りてこそ世の中の花も花なれ人も人なれ
62+
`,
63+
},
64+
}
65+
66+
func TestAsComment(t *testing.T) {
67+
for i, act := range asCommentTests {
68+
c := asComment(act.Comment)
69+
if c != act.Commented {
70+
t.Errorf("%d: wants %v+, got %v+", i, act.Commented, c)
71+
}
72+
}
73+
}

0 commit comments

Comments
 (0)