github.com/haalcala/mattermost-server-change-repo@v0.0.0-20210713015153-16753fbeee5f/plugin/stringifier_test.go (about)

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