Skip to content

Commit

Permalink
change PrefixEachLine to add newline at the end
Browse files Browse the repository at this point in the history
  • Loading branch information
letFunny committed Dec 15, 2023
1 parent 58e7b6f commit 9d741b0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
9 changes: 3 additions & 6 deletions internal/testutil/reindent.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,9 @@ func Reindent(in string) []byte {
func PrefixEachLine(text string, prefix string) string {
var result strings.Builder
lines := strings.Split(text, "\n")
for i, line := range lines {
newLine := prefix + line
if i < len(lines)-1 {
newLine += "\n"
}
_, err := result.WriteString(newLine)
for _, line := range lines {
prefixed := prefix + line + "\n"
_, err := result.WriteString(prefixed)
if err != nil {
panic(err)
}
Expand Down
15 changes: 8 additions & 7 deletions internal/testutil/reindent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package testutil_test
import (
"strings"

"github.com/canonical/chisel/internal/testutil"
. "gopkg.in/check.v1"

"github.com/canonical/chisel/internal/testutil"
)

type reindentTest struct {
Expand Down Expand Up @@ -78,26 +79,26 @@ type prefixEachLineTest struct {
var prefixEachLineTests = []prefixEachLineTest{{
raw: "a\n\tb\n \t\tc\td\n\t ",
prefix: "foo",
result: "fooa\nfoo\tb\nfoo \t\tc\td\nfoo\t ",
result: "fooa\nfoo\tb\nfoo \t\tc\td\nfoo\t \n",
}, {
raw: "foo",
prefix: "pref",
result: "preffoo",
result: "preffoo\n",
}, {
raw: "",
prefix: "",
result: "",
result: "\n",
}, {
raw: "\n",
prefix: "\t",
result: "\t\n\t",
result: "\t\n\t\n",
}}

func (s *S) TestPrefixEachLine(c *C) {
for _, test := range prefixEachLineTests {
c.Logf("Test: %#v", test)

indented := testutil.PrefixEachLine(test.raw, test.prefix)
c.Assert(string(indented), Equals, test.result)
prefixed := testutil.PrefixEachLine(test.raw, test.prefix)
c.Assert(prefixed, Equals, test.result)
}
}

0 comments on commit 9d741b0

Please sign in to comment.