mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
[MM-18898] Stringify plugin.Log* parameters (#12700)
- Add stringutils with method that "stringify" object slices - "stringify" means convert each object to its string representation - Move plugin.Log* implementations to client_rpc, use stringify method before calling server method - Exclude Log* methods from generated RPC methods - No signature change for plugin.Log* API - Add test in plugin_api_test to use plugin.Log* methods with RPC
This commit is contained in:
committed by
Ben Schumacher
parent
5dbccd0f07
commit
7cc1f19453
93
plugin/stringifier_test.go
Normal file
93
plugin/stringifier_test.go
Normal file
@@ -0,0 +1,93 @@
|
||||
// Copyright (c) 2019-present Mattermost, Inc. All Rights Reserved.
|
||||
// See License.txt for license information.
|
||||
|
||||
package plugin
|
||||
|
||||
import (
|
||||
"github.com/pkg/errors"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestStringify(t *testing.T) {
|
||||
t.Run("NilShouldReturnEmpty", func(t *testing.T) {
|
||||
strings := stringify(nil)
|
||||
assert.Empty(t, strings)
|
||||
})
|
||||
t.Run("EmptyShouldReturnEmpty", func(t *testing.T) {
|
||||
strings := stringify(make([]interface{}, 0, 0))
|
||||
assert.Empty(t, strings)
|
||||
})
|
||||
t.Run("PrimitivesAndCompositesShouldReturnCorrectValues", func(t *testing.T) {
|
||||
strings := stringify([]interface{}{
|
||||
1234,
|
||||
3.14159265358979323846264338327950288419716939937510,
|
||||
true,
|
||||
"foo",
|
||||
nil,
|
||||
[]string{"foo", "bar"},
|
||||
map[string]int{"one": 1, "two": 2},
|
||||
&WithString{},
|
||||
&WithoutString{},
|
||||
&WithStringAndError{},
|
||||
})
|
||||
assert.Equal(t, []string{
|
||||
"1234",
|
||||
"3.141592653589793",
|
||||
"true",
|
||||
"foo",
|
||||
"<nil>",
|
||||
"[foo bar]",
|
||||
"map[one:1 two:2]",
|
||||
"string",
|
||||
"&{}",
|
||||
"error",
|
||||
}, strings)
|
||||
})
|
||||
t.Run("ErrorShouldReturnFormattedStack", func(t *testing.T) {
|
||||
strings := stringify([]interface{}{
|
||||
errors.New("error"),
|
||||
errors.WithStack(errors.New("error")),
|
||||
})
|
||||
stackRegexp := "error\n.*plugin.TestStringify.func\\d+\n\t.*plugin/stringifier_test.go:\\d+\ntesting.tRunner\n\t.*testing.go:\\d+.*"
|
||||
assert.Len(t, strings, 2)
|
||||
assert.Regexp(t, stackRegexp, strings[0])
|
||||
assert.Regexp(t, stackRegexp, strings[1])
|
||||
})
|
||||
}
|
||||
|
||||
type WithString struct {
|
||||
}
|
||||
|
||||
func (*WithString) String() string {
|
||||
return "string"
|
||||
}
|
||||
|
||||
type WithoutString struct {
|
||||
}
|
||||
|
||||
type WithStringAndError struct {
|
||||
}
|
||||
|
||||
func (*WithStringAndError) String() string {
|
||||
return "string"
|
||||
}
|
||||
|
||||
func (*WithStringAndError) Error() string {
|
||||
return "error"
|
||||
}
|
||||
|
||||
func TestToObjects(t *testing.T) {
|
||||
t.Run("NilShouldReturnNil", func(t *testing.T) {
|
||||
objects := toObjects(nil)
|
||||
assert.Nil(t, objects)
|
||||
})
|
||||
t.Run("EmptyShouldReturnEmpty", func(t *testing.T) {
|
||||
objects := toObjects(make([]string, 0, 0))
|
||||
assert.Empty(t, objects)
|
||||
})
|
||||
t.Run("ShouldReturnSliceOfObjects", func(t *testing.T) {
|
||||
objects := toObjects([]string{"foo", "bar"})
|
||||
assert.Equal(t, []interface{}{"foo", "bar"}, objects)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user