From 99c152aeb74def9c722bc394f4c405618a8d1503 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Charles-Edouard=20Br=C3=A9t=C3=A9ch=C3=A9?= Date: Tue, 3 Oct 2023 17:37:49 +0200 Subject: [PATCH] chore: add unit tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Charles-Edouard Brétéché --- pkg/utils/reflect/kind_test.go | 49 ++++++++++++++++++++++++ pkg/utils/reflect/number_test.go | 66 ++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 pkg/utils/reflect/kind_test.go create mode 100644 pkg/utils/reflect/number_test.go diff --git a/pkg/utils/reflect/kind_test.go b/pkg/utils/reflect/kind_test.go new file mode 100644 index 00000000..08201e6a --- /dev/null +++ b/pkg/utils/reflect/kind_test.go @@ -0,0 +1,49 @@ +package reflect + +import ( + "reflect" + "testing" +) + +func TestGetKind(t *testing.T) { + tests := []struct { + name string + value interface{} + want reflect.Kind + }{{ + name: "nil", + value: nil, + want: reflect.Invalid, + }, { + name: "int", + value: int(42), + want: reflect.Int, + }, { + name: "int32", + value: int32(42), + want: reflect.Int32, + }, { + name: "int64", + value: int64(42), + want: reflect.Int64, + }, { + name: "string", + value: "foo", + want: reflect.String, + }, { + name: "map", + value: map[interface{}]interface{}{}, + want: reflect.Map, + }, { + name: "slice", + value: []interface{}{}, + want: reflect.Slice, + }} + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := GetKind(tt.value); got != tt.want { + t.Errorf("GetKind() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/pkg/utils/reflect/number_test.go b/pkg/utils/reflect/number_test.go new file mode 100644 index 00000000..70d44ca7 --- /dev/null +++ b/pkg/utils/reflect/number_test.go @@ -0,0 +1,66 @@ +package reflect + +import ( + "reflect" + "testing" +) + +func TestToNumber(t *testing.T) { + tests := []struct { + name string + value interface{} + want float64 + wantOk bool + }{{ + name: "nil", + value: nil, + want: 0, + wantOk: false, + }, { + name: "float32", + value: float32(42), + want: 42, + wantOk: true, + }, { + name: "float64", + value: float64(42), + want: 42, + wantOk: true, + }, { + name: "int32", + value: int32(42), + want: 42, + wantOk: true, + }, { + name: "int64", + value: int64(42), + want: 42, + wantOk: true, + }, { + name: "uint32", + value: uint32(42), + want: 42, + wantOk: true, + }, { + name: "uint64", + value: uint64(42), + want: 42, + wantOk: true, + }, { + name: "string", + value: "foo", + want: 0, + wantOk: false, + }} + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, got1 := ToNumber(reflect.ValueOf(tt.value)) + if got != tt.want { + t.Errorf("ToNumber() got = %v, want %v", got, tt.want) + } + if got1 != tt.wantOk { + t.Errorf("ToNumber() got1 = %v, want %v", got1, tt.wantOk) + } + }) + } +}