mattermost/plugin/stringifier_test.go
Jesús Espino a63684fcb5
Consistent license message for all the go files (#13235)
* Consistent license message for all the go files

* Fixing the last set of unconsistencies with the license headers

* Addressing PR review comments

* Fixing busy.go and busy_test.go license header
2019-11-29 12:59:40 +01:00

93 lines
2.1 KiB
Go

// Copyright (c) 2015-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))
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))
assert.Empty(t, objects)
})
t.Run("ShouldReturnSliceOfObjects", func(t *testing.T) {
objects := toObjects([]string{"foo", "bar"})
assert.Equal(t, []interface{}{"foo", "bar"}, objects)
})
}