From 650818fc92c612944d1d1efc56d4ed88010a7b6a Mon Sep 17 00:00:00 2001 From: injet-zhou Date: Wed, 1 Jun 2022 23:26:36 +0800 Subject: [PATCH 1/2] test: add convert unit test --- pkg/convert/convert_test.go | 117 ++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 pkg/convert/convert_test.go diff --git a/pkg/convert/convert_test.go b/pkg/convert/convert_test.go new file mode 100644 index 00000000..a746e3e7 --- /dev/null +++ b/pkg/convert/convert_test.go @@ -0,0 +1,117 @@ +package convert + +import ( + "testing" +) + +func TestStrTo_String(t *testing.T) { + type fields struct { + Str string + } + tests := []struct { + name string + fields fields + want string + }{ + { + name: "test", + fields: fields{Str: "test"}, + want: "test", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + s := StrTo(tt.fields.Str) + if got := s.String(); got != tt.want { + t.Errorf("StrTo.String() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestStrTo_Int(t *testing.T) { + var s StrTo = "123" + i, err := s.Int() + if err != nil { + t.Errorf("s.Int() error: %v", err) + } + if i != 123 { + t.Errorf("s.Int() should be 123") + } +} + +func TestStrTo_MustInt(t *testing.T) { + var s StrTo = "123" + i := s.MustInt() + if i != 123 { + t.Errorf("s.MustInt() should be 123") + } +} + +func TestStrTo_UInt32(t *testing.T) { + var s StrTo = "123" + i, err := s.UInt32() + if err != nil { + t.Errorf("s.UInt32() error: %v", err) + } + if i != 123 { + t.Errorf("s.UInt32() should be 123") + } +} + +func TestStrTo_UInt32_2(t *testing.T) { + var s StrTo = "-123" + i, err := s.UInt32() + if err != nil { + t.Errorf("s.UInt32() error: %v", err) + } + if i == 123 { + t.Errorf("s.UInt32() should not be 123") + } +} + +func TestStrTo_MustUInt32(t *testing.T) { + var s StrTo = "123" + i := s.MustUInt32() + if i != 123 { + t.Errorf("s.MustUInt32() should be 123") + } +} + +func TestStrTo_Int64(t *testing.T) { + var s StrTo = "123" + i, err := s.Int64() + if err != nil { + t.Errorf("s.Int64() error: %v", err) + } + if i != 123 { + t.Errorf("s.Int64() should be 123") + } +} + +func TestStrTo_MustInt64(t *testing.T) { + var s StrTo = "123" + i := s.MustInt64() + if i != 123 { + t.Errorf("s.MustInt64() should be 123") + } +} + +func TestStrTo_Float64(t *testing.T) { + var s StrTo = "123.456" + f, err := s.Float64() + if err != nil { + t.Errorf("s.Float64() error: %v", err) + } + if f != 123.456 { + t.Errorf("s.Float64() should be 123.456") + } +} + +func TestStrTo_MustFloat64(t *testing.T) { + var s StrTo = "123.456" + f := s.MustFloat64() + if f != 123.456 { + t.Errorf("s.MustFloat64() should be 123.456") + } +} From c8eb939a63e0b6e2b9f065bc93ecac4520743b49 Mon Sep 17 00:00:00 2001 From: alimy Date: Sat, 4 Jun 2022 05:04:10 +0800 Subject: [PATCH 2/2] add test rule in Makefile --- Makefile | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 4a23ba98..ba019737 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -.PHONY: all build run clean fmt help +.PHONY: all build run test clean fmt help BUILD_VERSION := $(shell cat version) BUILD_DATE := $(shell date +'%Y-%m-%d %H:%M:%S') SHA_SHORT := $(shell git rev-parse --short HEAD) @@ -22,6 +22,8 @@ fmt: @go vet -composites=false ./global/... @go vet -composites=false ./internal/... @go vet -composites=false ./pkg/... +test: + @go test ./... help: @echo "make: make" @echo "make run: start api server"