stringifier_test.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
  2. // See LICENSE.txt for license information.
  3. package plugin
  4. import (
  5. "github.com/pkg/errors"
  6. "github.com/stretchr/testify/assert"
  7. "testing"
  8. )
  9. func TestStringify(t *testing.T) {
  10. t.Run("NilShouldReturnEmpty", func(t *testing.T) {
  11. strings := stringify(nil)
  12. assert.Empty(t, strings)
  13. })
  14. t.Run("EmptyShouldReturnEmpty", func(t *testing.T) {
  15. strings := stringify(make([]interface{}, 0))
  16. assert.Empty(t, strings)
  17. })
  18. t.Run("PrimitivesAndCompositesShouldReturnCorrectValues", func(t *testing.T) {
  19. strings := stringify([]interface{}{
  20. 1234,
  21. 3.14159265358979323846264338327950288419716939937510,
  22. true,
  23. "foo",
  24. nil,
  25. []string{"foo", "bar"},
  26. map[string]int{"one": 1, "two": 2},
  27. &WithString{},
  28. &WithoutString{},
  29. &WithStringAndError{},
  30. })
  31. assert.Equal(t, []string{
  32. "1234",
  33. "3.141592653589793",
  34. "true",
  35. "foo",
  36. "<nil>",
  37. "[foo bar]",
  38. "map[one:1 two:2]",
  39. "string",
  40. "&{}",
  41. "error",
  42. }, strings)
  43. })
  44. t.Run("ErrorShouldReturnFormattedStack", func(t *testing.T) {
  45. strings := stringify([]interface{}{
  46. errors.New("error"),
  47. errors.WithStack(errors.New("error")),
  48. })
  49. stackRegexp := "error\n.*plugin.TestStringify.func\\d+\n\t.*plugin/stringifier_test.go:\\d+\ntesting.tRunner\n\t.*testing.go:\\d+.*"
  50. assert.Len(t, strings, 2)
  51. assert.Regexp(t, stackRegexp, strings[0])
  52. assert.Regexp(t, stackRegexp, strings[1])
  53. })
  54. }
  55. type WithString struct {
  56. }
  57. func (*WithString) String() string {
  58. return "string"
  59. }
  60. type WithoutString struct {
  61. }
  62. type WithStringAndError struct {
  63. }
  64. func (*WithStringAndError) String() string {
  65. return "string"
  66. }
  67. func (*WithStringAndError) Error() string {
  68. return "error"
  69. }
  70. func TestToObjects(t *testing.T) {
  71. t.Run("NilShouldReturnNil", func(t *testing.T) {
  72. objects := toObjects(nil)
  73. assert.Nil(t, objects)
  74. })
  75. t.Run("EmptyShouldReturnEmpty", func(t *testing.T) {
  76. objects := toObjects(make([]string, 0))
  77. assert.Empty(t, objects)
  78. })
  79. t.Run("ShouldReturnSliceOfObjects", func(t *testing.T) {
  80. objects := toObjects([]string{"foo", "bar"})
  81. assert.Equal(t, []interface{}{"foo", "bar"}, objects)
  82. })
  83. }