Skip to content

Commit

Permalink
feat: added tostr method (#4785)
Browse files Browse the repository at this point in the history
* feat: added tostr method

* chore: refactor + unit tests
  • Loading branch information
exu committed Dec 19, 2023
1 parent 1c1112d commit 22bc40e
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/event/kind/webhook/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
thttp "github.com/kubeshop/testkube/pkg/http"
"github.com/kubeshop/testkube/pkg/log"
"github.com/kubeshop/testkube/pkg/utils"
"github.com/kubeshop/testkube/pkg/utils/text"
)

var _ common.Listener = (*WebhookListener)(nil)
Expand Down Expand Up @@ -170,6 +171,7 @@ func (l *WebhookListener) processTemplate(field, body string, event testkube.Eve

var tmpl *template.Template
tmpl, err := utils.NewTemplate(field).Funcs(template.FuncMap{
"tostr": text.ToStr,
"executionstatustostring": testkube.ExecutionStatusString,
"testsuiteexecutionstatustostring": testkube.TestSuiteExecutionStatusString,
}).Parse(body)
Expand Down
7 changes: 7 additions & 0 deletions pkg/utils/text/tostr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package text

import "fmt"

func ToStr(in any) string {
return fmt.Sprintf("%v", in)
}
44 changes: 44 additions & 0 deletions pkg/utils/text/tostr_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package text

import (
"testing"

"github.com/stretchr/testify/assert"
)

type stringerTestImpl struct {
name string
}

func (tt stringerTestImpl) String() string {
return tt.name
}

func TestToStr(t *testing.T) {

t.Run("converts stringer to string", func(t *testing.T) {
test := stringerTestImpl{name: "test"}
assert.Equal(t, "test", ToStr(test))
})

t.Run("converts int to str", func(t *testing.T) {
test := 1
assert.Equal(t, "1", ToStr(test))
})

t.Run("converts float to str", func(t *testing.T) {
test := 1.1
assert.Equal(t, "1.1", ToStr(test))
})

t.Run("converts bool to str", func(t *testing.T) {
test := true
assert.Equal(t, "true", ToStr(test))
})

t.Run("converts nil to str", func(t *testing.T) {
var test *string
assert.Equal(t, "<nil>", ToStr(test))
})

}

0 comments on commit 22bc40e

Please sign in to comment.