improve sliceValidateError.Error performance using switch and strings.Builder (#2765)

fix missing nil pointer check

use simpler switch case

add missing tests

use for-loop instead of range

add benchmark test codes
This commit is contained in:
ziheng
2021-06-28 22:05:29 +08:00
committed by GitHub
parent 1d0f938f28
commit e3ee01d185
3 changed files with 58 additions and 6 deletions
+20
View File
@@ -16,6 +16,26 @@ func TestSliceValidateError(t *testing.T) {
want string
}{
{"has nil elements", sliceValidateError{errors.New("test error"), nil}, "[0]: test error"},
{"has zero elements", sliceValidateError{}, ""},
{"has one element", sliceValidateError{errors.New("test one error")}, "[0]: test one error"},
{"has two elements",
sliceValidateError{
errors.New("first error"),
errors.New("second error"),
},
"[0]: first error\n[1]: second error",
},
{"has many elements",
sliceValidateError{
errors.New("first error"),
errors.New("second error"),
nil,
nil,
nil,
errors.New("last error"),
},
"[0]: first error\n[1]: second error\n[5]: last error",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {