From c30c6e7a043b36bac3484394ad47697ab2aecec1 Mon Sep 17 00:00:00 2001 From: Valeriy Selitskiy <239034+iamwavecut@users.noreply.github.com> Date: Mon, 18 Nov 2024 00:31:07 +0100 Subject: [PATCH] feat: add IsZero convenience checker --- .vscode/launch.json | 16 ++++++++++++++++ tool.go | 6 ++++++ tool_test.go | 17 +++++++++++++---- 3 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..2505e36 --- /dev/null +++ b/.vscode/launch.json @@ -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" + } + + ] +} diff --git a/tool.go b/tool.go index 9b8f505..67d30fa 100644 --- a/tool.go +++ b/tool.go @@ -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 diff --git a/tool_test.go b/tool_test.go index 34a4894..20c11eb 100644 --- a/tool_test.go +++ b/tool_test.go @@ -3,10 +3,11 @@ package tool import ( "errors" "fmt" - "github.com/stretchr/testify/suite" "reflect" "strconv" "testing" + + "github.com/stretchr/testify/suite" ) type ( @@ -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]> \n", testLog.buf) + s.Equal("[github.com/iamwavecut/tool:76]> \n", testLog.buf) }) } @@ -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)) +}