Skip to content

Commit

Permalink
feat: add IsZero convenience checker
Browse files Browse the repository at this point in the history
  • Loading branch information
iamwavecut committed Nov 17, 2024
1 parent b8c77ad commit c30c6e7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
16 changes: 16 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
// Используйте IntelliSense, чтобы узнать о возможных атрибутах.
// Наведите указатель мыши, чтобы просмотреть описания существующих атрибутов.
// Для получения дополнительной информации посетите: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch Test",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/tool_test.go"
}

]
}
6 changes: 6 additions & 0 deletions tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,12 @@ func NonZero[T comparable](ts ...T) T {
return zeroValue
}

// IsZero Checks if value is zero
func IsZero[T comparable](v T) bool {
var zero T
return v == zero
}

// identifyPanic Helper function to get user-friendly call stack message.
func identifyPanic() string {
var name, file string
Expand Down
17 changes: 13 additions & 4 deletions tool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package tool
import (
"errors"
"fmt"
"github.com/stretchr/testify/suite"
"reflect"
"strconv"
"testing"

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

type (
Expand Down Expand Up @@ -63,17 +64,17 @@ func (s *ToolTestSuite) TestIn() {
func (s *ToolTestSuite) TestConsole() {
s.Run("1", func() {
Console("123", "456", "789")
s.Equal("[github.com/iamwavecut/tool:65]> 123 456 789\n", testLog.buf)
s.Equal("[github.com/iamwavecut/tool:66]> 123 456 789\n", testLog.buf)
})
s.Run("2", func() {
testLog.buf = ""
Console(struct{ int }{123})
s.Equal("[github.com/iamwavecut/tool:70]> {int:123}\n", testLog.buf)
s.Equal("[github.com/iamwavecut/tool:71]> {int:123}\n", testLog.buf)
})
s.Run("3", func() {
testLog.buf = ""
Console(nil)
s.Equal("[github.com/iamwavecut/tool:75]> <nil>\n", testLog.buf)
s.Equal("[github.com/iamwavecut/tool:76]> <nil>\n", testLog.buf)
})
}

Expand Down Expand Up @@ -506,3 +507,11 @@ func (s *ToolTestSuite) TestConvertSlice() {
s.Equal(result, expectedOutput, "slice conversion not as expected")
})
}

func (s *ToolTestSuite) TestIsZero() {
s.True(IsZero(0))
s.True(IsZero(""))
s.True(IsZero(false))
var v *int
s.True(IsZero(v))
}

0 comments on commit c30c6e7

Please sign in to comment.