Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assert: handle TokenTooLong error scenario #1559

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
14 changes: 9 additions & 5 deletions assert/assertions.go
Expand Up @@ -301,13 +301,17 @@ func messageFromMsgAndArgs(msgAndArgs ...interface{}) string {
func indentMessageLines(message string, longestLabelLen int) string {
outBuf := new(bytes.Buffer)

for i, scanner := 0, bufio.NewScanner(strings.NewReader(message)); scanner.Scan(); i++ {
// no need to align first line because it starts at the correct location (after the label)
if i != 0 {
// append alignLen+1 spaces to align with "{{longestLabel}}:" before adding tab
msgScanner := bufio.NewScanner(strings.NewReader(message))
msgScanner.Buffer([]byte{}, len(message)+1)
arjunmahishi marked this conversation as resolved.
Show resolved Hide resolved

first := true
for msgScanner.Scan() {
if !first {
outBuf.WriteString("\n\t" + strings.Repeat(" ", longestLabelLen+1) + "\t")
arjunmahishi marked this conversation as resolved.
Show resolved Hide resolved
}
outBuf.WriteString(scanner.Text())

outBuf.WriteString(msgScanner.Text())
arjunmahishi marked this conversation as resolved.
Show resolved Hide resolved
first = false
}

return outBuf.String()
Expand Down
42 changes: 42 additions & 0 deletions assert/assertions_test.go
Expand Up @@ -3192,3 +3192,45 @@ func TestErrorAs(t *testing.T) {
})
}
}

func Test_indentMessageLines(t *testing.T) {
arjunmahishi marked this conversation as resolved.
Show resolved Hide resolved
tt := []struct {
name string
msg string
longestLabelLen int
expected string
}{
{
name: "single line",
msg: "Hello World\n",
longestLabelLen: 11,
expected: "Hello World",
},
{
name: "multi line",
msg: "Hello\nWorld\n",
longestLabelLen: 11,
expected: "Hello\n\t \tWorld",
},
{
name: "single line - extreamly long",
msg: strings.Repeat("hello ", 20000),
longestLabelLen: 11,
expected: strings.Repeat("hello ", 20000),
},
{
name: "multi line - extreamly long",
arjunmahishi marked this conversation as resolved.
Show resolved Hide resolved
msg: strings.Repeat("hello\n", 20000),
longestLabelLen: 3,
expected: strings.TrimSpace(
strings.TrimPrefix(strings.Repeat("\thello\n\t ", 20000), "\t"),
),
},
}

for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
Equal(t, tc.expected, indentMessageLines(tc.msg, tc.longestLabelLen))
})
}
}