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
base: master
Are you sure you want to change the base?
Changes from 7 commits
7f5f3e3
f37ae55
48a148a
f24d768
6b0dfad
594ac7d
f341282
b83d206
9e31964
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package assert | ||
|
||
import ( | ||
"bufio" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func Test_indentMessageLines(t *testing.T) { | ||
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 - over the bufio default limit", | ||
msg: strings.Repeat("hello ", bufio.MaxScanTokenSize+10), | ||
longestLabelLen: 11, | ||
expected: strings.Repeat("hello ", bufio.MaxScanTokenSize+10), | ||
}, | ||
{ | ||
name: "multi line - over the bufio default limit", | ||
msg: strings.Repeat("hello\n", bufio.MaxScanTokenSize+10), | ||
longestLabelLen: 3, | ||
expected: strings.TrimSpace( | ||
strings.TrimPrefix(strings.Repeat("\thello\n\t ", bufio.MaxScanTokenSize+10), "\t"), | ||
), | ||
}, | ||
{ | ||
name: "single line - just under the bufio default limit", | ||
msg: strings.Repeat("hello ", bufio.MaxScanTokenSize-10), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The boundary is not We probably need a utility function to build such an input string. At least There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ohh! that's right. Changed the approach a little bit. The new commit generates the input as you suggested. And instead of matching against an exact output, it asserts the pattern of the output. Like the leading white spaces etc. Because for generating the expected output, we will end up rewriting the actual function logic in the test. |
||
longestLabelLen: 11, | ||
expected: strings.Repeat("hello ", bufio.MaxScanTokenSize-10), | ||
}, | ||
{ | ||
name: "multi line - just under the bufio default limit", | ||
msg: strings.Repeat("hello\n", bufio.MaxScanTokenSize-10), | ||
longestLabelLen: 3, | ||
expected: strings.TrimSpace( | ||
strings.TrimPrefix(strings.Repeat("\thello\n\t ", bufio.MaxScanTokenSize-10), "\t"), | ||
), | ||
}, | ||
{ | ||
name: "single line - equal to the bufio default limit", | ||
msg: strings.Repeat("hello ", bufio.MaxScanTokenSize), | ||
longestLabelLen: 11, | ||
expected: strings.Repeat("hello ", bufio.MaxScanTokenSize), | ||
}, | ||
{ | ||
name: "multi line - equal to the bufio default limit", | ||
msg: strings.Repeat("hello\n", bufio.MaxScanTokenSize), | ||
longestLabelLen: 3, | ||
expected: strings.TrimSpace( | ||
strings.TrimPrefix(strings.Repeat("\thello\n\t ", bufio.MaxScanTokenSize), "\t"), | ||
), | ||
}, | ||
} | ||
|
||
for _, tc := range tt { | ||
t.Run(tc.name, func(t *testing.T) { | ||
Equal(t, tc.expected, indentMessageLines(tc.msg, tc.longestLabelLen)) | ||
}) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this safe? As we know from the assert.Len case this can contain a very large formatted slice. Having more than doubled the memory consumed by the large slice by creating a string containing the formatted version of it. Do we want to double the formatted version again? Plus all the intermediate buffers that were allocated on the heap by msgScanner before the next GC cycle?
We could at least pre-allocate the buffer for msgScanner rather than passing in
nil
. But I'd prefer that we either split or truncate lines that are unreasonably long, or we could just truncate the entire message to less thanbufio.MaxScanTokenSize
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. I've made this change.
If we decide to truncate, we could probably truncate based on the dimensions of the terminal? This makes more sense from a UX point of view.