Skip to content

Commit

Permalink
Reuse go logic and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Timothée Peignier committed Feb 15, 2018
1 parent 35aee55 commit 91f3abe
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
13 changes: 3 additions & 10 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,6 @@ func asComment(c string) string {
removeNewlines := func(s string) string {
return strings.Replace(s, "\n", "\n// ", -1)
}
findLastIndex := func(rs []rune, r rune) int {
l := -1
for i, v := range rs {
if v == r {
l = i
}
}
return l
}
r := []rune(c)
for len(r) > 0 {
line := r
Expand All @@ -139,7 +130,9 @@ func asComment(c string) string {
break
}
line = line[:maxLen]
si := findLastIndex(line, []rune(" ")[0])
si := strings.LastIndexFunc(string(line), func(r rune) bool {
return unicode.IsSpace(r)
})
if si != -1 {
line = line[:si]
}
Expand Down
35 changes: 35 additions & 0 deletions helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,38 @@ func TestInitialCap(t *testing.T) {
}
}
}

var asCommentTests = []struct {
Comment string
Commented string
}{
{
Comment: `This is a multi-line
comment, it contains multiple
lines.`,
Commented: `// This is a multi-line
// comment, it contains multiple
// lines.
`,
},
{
Comment: `This is a fairly long comment line, it should be over seventy characters.`,
Commented: `// This is a fairly long comment line, it should be over seventy
// characters.
`,
},
{
Comment: `散りぬべき時知りてこそ世の中の花も花なれ人も人なれ`,
Commented: `// 散りぬべき時知りてこそ世の中の花も花なれ人も人なれ
`,
},
}

func TestAsComment(t *testing.T) {
for i, act := range asCommentTests {
c := asComment(act.Comment)
if c != act.Commented {
t.Errorf("%d: wants %v+, got %v+", i, act.Commented, c)
}
}
}

0 comments on commit 91f3abe

Please sign in to comment.