Skip to content

Commit

Permalink
helpers: Add type checks
Browse files Browse the repository at this point in the history
  • Loading branch information
jmooring committed Sep 11, 2023
1 parent a7720c8 commit 618521d
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 23 deletions.
53 changes: 53 additions & 0 deletions helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"io"
"io/fs"
"log"
"math"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -233,3 +234,55 @@ func RemoveDirectoryContent(dir string) error {

return nil
}

// IsInt reports whether i is an integer.
func IsInt(i any) bool {
if i == nil {
return false
}
var v float64

switch s := i.(type) {
case int:
return true
case int64:
return true
case int32:
return true
case int16:
return true
case int8:
return true
case uint:
return true
case uint64:
return true
case uint32:
return true
case uint16:
return true
case uint8:
return true
case float32:
v = float64(s)
case float64:
v = s
default:
return false
}

if v == 0 || math.Mod(v, 1) == 0 {
return true
}

return false
}

// IsString reports whether i is a string.
func IsString(i any) bool {
if _, ok := i.(string); ok {
return true
}
return false

}
107 changes: 84 additions & 23 deletions helpers/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ func TestIsFile(t *testing.T) {
want bool
wantErr bool
}{
{"A", args{dPath}, false, false},
{"B", args{fPath}, true, false},
{"C", args{filepath.Join(dPath, "does-not-exist.txt")}, false, true},
{"a", args{dPath}, false, false},
{"b", args{fPath}, true, false},
{"c", args{filepath.Join(dPath, "does-not-exist.txt")}, false, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -91,9 +91,9 @@ func TestIsDir(t *testing.T) {
want bool
wantErr bool
}{
{"A", args{dPath}, true, false},
{"B", args{fPath}, false, false},
{"C", args{filepath.Join(dPath, "does-not-exist.txt")}, false, true},
{"a", args{dPath}, true, false},
{"b", args{fPath}, false, false},
{"c", args{filepath.Join(dPath, "does-not-exist.txt")}, false, true},
}

for _, tt := range tests {
Expand Down Expand Up @@ -135,9 +135,9 @@ func TestExists(t *testing.T) {
want bool
wantErr bool
}{
{"A", args{dPath}, true, false},
{"B", args{fPath}, true, false},
{"C", args{filepath.Join(dPath, "does-not-exist.txt")}, false, false},
{"a", args{dPath}, true, false},
{"b", args{fPath}, true, false},
{"c", args{filepath.Join(dPath, "does-not-exist.txt")}, false, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -186,11 +186,11 @@ func TestIsEmpty(t *testing.T) {
want bool
wantErr bool
}{
{"A", args{dPathEmpty}, true, false},
{"B", args{fPathEmpty}, true, false},
{"C", args{dPathNotEmpty}, false, false},
{"D", args{fPathNotEmpty}, false, false},
{"E", args{filepath.Join(t.TempDir(), "does-not-exist.txt")}, false, true},
{"a", args{dPathEmpty}, true, false},
{"b", args{fPathEmpty}, true, false},
{"c", args{dPathNotEmpty}, false, false},
{"d", args{fPathNotEmpty}, false, false},
{"e", args{filepath.Join(t.TempDir(), "does-not-exist.txt")}, false, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -225,9 +225,9 @@ func TestCopyFile(t *testing.T) {
args args
wantErr bool
}{
{"A", args{srcIsDir, dst}, true},
{"B", args{srcDoesNotExist, dst}, true},
{"C", args{srcIsFile, dst}, false},
{"a", args{srcIsDir, dst}, true},
{"b", args{srcDoesNotExist, dst}, true},
{"c", args{srcIsFile, dst}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -275,9 +275,9 @@ func TestCopyDirectoryContent(t *testing.T) {
args args
wantErr bool
}{
{"A", args{srcDoesNotExist, dst}, true},
{"B", args{srcIsFile, dst}, true},
{"C", args{srcIsDir, dst}, false},
{"a", args{srcDoesNotExist, dst}, true},
{"b", args{srcIsFile, dst}, true},
{"c", args{srcIsDir, dst}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -337,9 +337,9 @@ func TestRemoveDirectoryContent(t *testing.T) {
args args
wantErr bool
}{
{"A", args{doesNotExist}, true},
{"B", args{isFile}, true},
{"C", args{dir}, false},
{"a", args{doesNotExist}, true},
{"b", args{isFile}, true},
{"c", args{dir}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -358,3 +358,64 @@ func TestRemoveDirectoryContent(t *testing.T) {
t.Error("the directory was not emptied")
}
}

func TestIsString(t *testing.T) {
type args struct {
i any
}
tests := []struct {
name string
args args
want bool
}{
{"a", args{nil}, false},
{"b", args{true}, false},
{"c", args{false}, false},
{"d", args{""}, true},
{"e", args{"a"}, true},
{"f", args{"1"}, true},
{"g", args{1}, false},
{"h", args{0}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IsString(tt.args.i); got != tt.want {
t.Errorf("IsString() = %v, want %v", got, tt.want)
}
})
}
}

func TestIsInt(t *testing.T) {
type args struct {
i any
}
tests := []struct {
name string
args args
want bool
}{
{"a", args{nil}, false},
{"b", args{true}, false},
{"c", args{false}, false},
{"d", args{""}, false},
{"e", args{"a"}, false},
{"f", args{"1"}, false},
{"g", args{1}, true},
{"h", args{0}, true},
{"i", args{-1}, true},
{"j", args{1.0}, true},
{"k", args{0.0}, true},
{"l", args{-1.0}, true},
{"m", args{1.234}, false},
{"n", args{-1.234}, false},
{"o", args{30.2}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IsInt(tt.args.i); got != tt.want {
t.Errorf("IsInt() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 618521d

Please sign in to comment.