-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: added tostr method * chore: refactor + unit tests
- Loading branch information
Showing
3 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
}) | ||
|
||
} |