From 16a2a94538d5b7fc35fc92efc3c2dbc70616c1fc Mon Sep 17 00:00:00 2001 From: Michael Li Date: Thu, 23 Feb 2023 14:12:36 +0800 Subject: [PATCH] use github.com/onsi/ginkgo as test suite framework --- go.mod | 12 +- go.sum | 18 ++- pkg/convert/convert_suite_test.go | 17 +++ pkg/convert/convert_test.go | 169 +++++++++++------------------ pkg/util/ip_test.go | 63 ++++++----- pkg/util/iploc/iploc_suite_test.go | 17 +++ pkg/util/iploc/iploc_test.go | 49 ++++++--- pkg/util/md5_test.go | 65 ++++++----- pkg/util/util_suite_test.go | 13 +++ 9 files changed, 227 insertions(+), 196 deletions(-) create mode 100644 pkg/convert/convert_suite_test.go create mode 100644 pkg/util/iploc/iploc_suite_test.go create mode 100644 pkg/util/util_suite_test.go diff --git a/go.mod b/go.mod index d5648508..a980b021 100644 --- a/go.mod +++ b/go.mod @@ -25,10 +25,13 @@ require ( github.com/json-iterator/go v1.1.12 github.com/meilisearch/meilisearch-go v0.21.0 github.com/minio/minio-go/v7 v7.0.45 + github.com/onsi/ginkgo/v2 v2.8.3 + github.com/onsi/gomega v1.27.1 github.com/pyroscope-io/client v0.6.0 github.com/redis/go-redis/v9 v9.0.2 github.com/sirupsen/logrus v1.9.0 github.com/smartwalle/alipay/v3 v3.1.7 + github.com/sourcegraph/conc v0.2.0 github.com/spf13/viper v1.14.0 github.com/tencentyun/cos-go-sdk-v5 v0.7.35 github.com/yinheli/mahonia v0.0.0-20131226213531-0eef680515cc @@ -56,13 +59,17 @@ require ( github.com/dustin/go-humanize v1.0.0 // indirect github.com/fsnotify/fsnotify v1.6.0 // indirect github.com/gin-contrib/sse v0.1.0 // indirect + github.com/go-logr/logr v1.2.3 // indirect github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect github.com/go-playground/validator/v10 v10.11.2 // indirect github.com/go-sql-driver/mysql v1.6.0 // indirect + github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 // indirect github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect github.com/golang/protobuf v1.5.2 // indirect + github.com/google/go-cmp v0.5.9 // indirect github.com/google/go-querystring v1.1.0 // indirect + github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 // indirect github.com/google/uuid v1.3.0 // indirect github.com/hashicorp/errwrap v1.1.0 // indirect github.com/hashicorp/go-multierror v1.1.1 // indirect @@ -101,7 +108,6 @@ require ( github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect github.com/rs/xid v1.4.0 // indirect github.com/smartwalle/crypto4go v1.0.2 // indirect - github.com/sourcegraph/conc v0.2.0 github.com/spf13/afero v1.9.2 // indirect github.com/spf13/cast v1.5.0 // indirect github.com/spf13/jwalterweatherman v1.1.0 // indirect @@ -115,12 +121,12 @@ require ( golang.org/x/arch v0.0.0-20210923205945-b76863e36670 // indirect golang.org/x/crypto v0.5.0 // indirect golang.org/x/image v0.0.0-20220413100746-70e8d0d3baa9 // indirect - golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect + golang.org/x/mod v0.8.0 // indirect golang.org/x/net v0.7.0 // indirect golang.org/x/sys v0.5.0 // indirect golang.org/x/text v0.7.0 // indirect golang.org/x/time v0.0.0-20220609170525-579cf78fd858 // indirect - golang.org/x/tools v0.1.12 // indirect + golang.org/x/tools v0.6.0 // indirect google.golang.org/genproto v0.0.0-20221024183307-1bc688fe9f3e // indirect gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect diff --git a/go.sum b/go.sum index ad1b8719..0fab2ec1 100644 --- a/go.sum +++ b/go.sum @@ -557,6 +557,8 @@ github.com/go-logr/logr v0.4.0/go.mod h1:z6/tIYblkpsD+a4lm/fGIIU9mZ+XfAiaFtq7xTg github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= github.com/go-logr/logr v1.2.1/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0= +github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= github.com/go-logr/stdr v1.2.0/go.mod h1:YkVgnZu1ZjjL7xTxrfm/LLZBfkhTqSR1ydtm6jTKKwI= github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= github.com/go-ole/go-ole v1.2.1/go.mod h1:7FAglXiTm7HKlQRDeOQ6ZNUHidzCWXuZWq/1dTyBNF8= @@ -592,6 +594,7 @@ github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LB github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE= github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= +github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 h1:p104kn46Q8WdvHunIJ9dAyjPVtrBPhSr3KT2yUst43I= github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE= github.com/gobuffalo/attrs v0.0.0-20190224210810-a9411de4debd/go.mod h1:4duuawTqi2wkkpB4ePgWMaai6/Kc6WEz83bhFwpHzj0= github.com/gobuffalo/depgen v0.0.0-20190329151759-d478694a28d3/go.mod h1:3STtPUQYuzV0gBVOY3vy6CfMm/ljR4pABfrTeHNLHUY= @@ -709,6 +712,7 @@ github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/go-containerregistry v0.5.1/go.mod h1:Ct15B4yir3PLOP5jsy0GNeYVaIZs/MK/Jz5any1wFW0= github.com/google/go-github/v39 v39.2.0/go.mod h1:C1s8C5aCC9L+JXIYpJM5GYytdX52vC1bLvHEF1IhBrE= github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck= @@ -736,6 +740,7 @@ github.com/google/pprof v0.0.0-20210122040257-d980be63207e/go.mod h1:kpwsk12EmLe github.com/google/pprof v0.0.0-20210226084205-cbba55b83ad5/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= github.com/google/pprof v0.0.0-20210601050228-01bbb1931b22/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= github.com/google/pprof v0.0.0-20210609004039-a478d1d731e9/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= +github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 h1:K6RDEckDVWvDI9JAJYCmNdQXq6neHJOYx3V6jnqNEec= github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= @@ -1139,6 +1144,8 @@ github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108 github.com/onsi/ginkgo v1.13.0/go.mod h1:+REjRxOmWfHCjfv9TTWB1jD1Frx4XydAD3zm1lskyM0= github.com/onsi/ginkgo v1.14.0/go.mod h1:iSB4RoI2tjJc9BBv4NKIKWKya62Rps+oPG/Lv9klQyY= github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vvnwo0= +github.com/onsi/ginkgo/v2 v2.8.3 h1:RpbK1G8nWPNaCVFBWsOGnEQQGgASi6b8fxcWBvDYjxQ= +github.com/onsi/ginkgo/v2 v2.8.3/go.mod h1:6OaUA8BCi0aZfmzYT/q9AacwTzDpNbxILUT+TlBq6MY= github.com/onsi/gomega v0.0.0-20151007035656-2152b45fa28a/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA= github.com/onsi/gomega v0.0.0-20170829124025-dcabb60a477c/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA= github.com/onsi/gomega v1.4.1/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA= @@ -1150,6 +1157,8 @@ github.com/onsi/gomega v1.9.0/go.mod h1:Ho0h+IUsWyvy1OpqCwxlQ/21gkhVunqlU8fDGcoT github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= github.com/onsi/gomega v1.10.3/go.mod h1:V9xEwhxec5O8UDM77eCW8vLymOMltsqPVYWrpDsH8xc= github.com/onsi/gomega v1.15.0/go.mod h1:cIuvLEne0aoVhAgh/O6ac0Op8WWw9H6eYCriF+tEHG0= +github.com/onsi/gomega v1.27.1 h1:rfztXRbg6nv/5f+Raen9RcGoSecHIFgBBLQK3Wdj754= +github.com/onsi/gomega v1.27.1/go.mod h1:aHX5xOykVYzWOV4WqQy0sy8BQptgukenXpCXfadcIAw= github.com/opencontainers/go-digest v0.0.0-20170106003457-a6d0ee40d420/go.mod h1:cMLVZDEM3+U2I4VmLI6N8jQYUd2OVphdqWwCJHrFt2s= github.com/opencontainers/go-digest v0.0.0-20180430190053-c9281466c8b2/go.mod h1:cMLVZDEM3+U2I4VmLI6N8jQYUd2OVphdqWwCJHrFt2s= github.com/opencontainers/go-digest v1.0.0-rc1/go.mod h1:cMLVZDEM3+U2I4VmLI6N8jQYUd2OVphdqWwCJHrFt2s= @@ -1587,8 +1596,8 @@ golang.org/x/mod v0.4.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.5.0/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro= -golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s= -golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/mod v0.8.0 h1:LUYupSeNrTNCGzR/hVBk2NHZO4hXcVaW1k4Qx7rjPx8= +golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180719180050-a680a1efc54d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -1693,6 +1702,7 @@ golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= golang.org/x/sys v0.0.0-20180224232135-f6cff0780e54/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -1937,8 +1947,8 @@ golang.org/x/tools v0.1.2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.1.3/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.1.4/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= -golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU= -golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/tools v0.6.0 h1:BOw41kyTf3PuCW1pVQf8+Cyg8pMlkYB1oo9iJ6D/lKM= +golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= golang.org/x/xerrors v0.0.0-20190410155217-1f06c39b4373/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/pkg/convert/convert_suite_test.go b/pkg/convert/convert_suite_test.go new file mode 100644 index 00000000..572256fb --- /dev/null +++ b/pkg/convert/convert_suite_test.go @@ -0,0 +1,17 @@ +// Copyright 2023 ROC. All rights reserved. +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. + +package convert_test + +import ( + "testing" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +func TestConvert(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "Convert Suite") +} diff --git a/pkg/convert/convert_test.go b/pkg/convert/convert_test.go index a31d13e6..c8fb2e69 100644 --- a/pkg/convert/convert_test.go +++ b/pkg/convert/convert_test.go @@ -1,121 +1,78 @@ -// Copyright 2022 ROC. All rights reserved. +// Copyright 2023 ROC. All rights reserved. // Use of this source code is governed by a MIT style // license that can be found in the LICENSE file. -package convert +package convert_test import ( - "testing" -) + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" -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) - } - }) - } -} + "github.com/rocboss/paopao-ce/pkg/convert" +) -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") - } -} +var _ = Describe("Convert", func() { + It("str to string", func() { + for _, t := range []struct { + str string + want string + }{ + { + str: "test", + want: "test", + }, + } { + Expect(convert.StrTo(t.str).String()).To(Equal(t.want)) + } + }) -func TestStrTo_MustInt(t *testing.T) { - var s StrTo = "123" - i := s.MustInt() - if i != 123 { - t.Errorf("s.MustInt() should be 123") - } -} + It("str to int", func() { + var s convert.StrTo = "123" + i, err := s.Int() + Expect(err).To(BeNil()) + Expect(i).To(Equal(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") - } -} + It("str must int", func() { + var s convert.StrTo = "123" + Expect(s.MustInt()).To(Equal(int(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") - } -} + It("str to uint32", func() { + s := convert.StrTo("123") + i, err := s.UInt32() + Expect(err).To(BeNil()) + Expect(i).To(Equal(uint32(123))) + }) -func TestStrTo_MustUInt32(t *testing.T) { - var s StrTo = "123" - i := s.MustUInt32() - if i != 123 { - t.Errorf("s.MustUInt32() should be 123") - } -} + It("str to uint32 negative", func() { + s := convert.StrTo("-123") + i, err := s.UInt32() + Expect(err).To(BeNil()) + Expect(i).NotTo(Equal(uint32(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") - } -} + It("str to int64", func() { + var s convert.StrTo = "123" + i, err := s.Int64() + Expect(err).To(BeNil()) + Expect(i).To(Equal(int64(123))) + }) -func TestStrTo_MustInt64(t *testing.T) { - var s StrTo = "123" - i := s.MustInt64() - if i != 123 { - t.Errorf("s.MustInt64() should be 123") - } -} + It("str must int64", func() { + var s convert.StrTo = "123" + Expect(s.MustInt64()).To(Equal(int64(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") - } -} + It("str to float64", func() { + var s convert.StrTo = "123.456" + f, err := s.Float64() + Expect(err).To(BeNil()) + Expect(f).To(Equal(float64(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") - } -} + It("str must float64", func() { + var s convert.StrTo = "123.456" + Expect(s.Float64()).To(Equal(float64(123.456))) + }) +}) diff --git a/pkg/util/ip_test.go b/pkg/util/ip_test.go index 0740ffd5..9e173d70 100644 --- a/pkg/util/ip_test.go +++ b/pkg/util/ip_test.go @@ -1,40 +1,39 @@ -// Copyright 2022 ROC. All rights reserved. +// Copyright 2023 ROC. All rights reserved. // Use of this source code is governed by a MIT style // license that can be found in the LICENSE file. -package util +package util_test -import "testing" +import ( + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" -func TestGetIPLoc(t *testing.T) { - type args struct { - ip string + "github.com/rocboss/paopao-ce/pkg/util" +) + +var _ = Describe("Ip", Ordered, func() { + type iplocs []struct { + ip string + location string } - tests := []struct { - name string - args args - want string - }{ - { - name: "test1", - args: args{ - ip: "", + var samples iplocs + + BeforeAll(func() { + samples = iplocs{ + { + ip: "", + location: "", }, - want: "", - }, - { - name: "test2", - args: args{ - ip: "103.197.70.244", + { + ip: "103.197.70.244", + location: "香港", }, - want: "香港", - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if got := GetIPLoc(tt.args.ip); got != tt.want { - t.Errorf("GetIPLoc() = %v, want %v", got, tt.want) - } - }) - } -} + } + }) + + It("get ip location", func() { + for _, t := range samples { + Expect(util.GetIPLoc(t.ip)).To(Equal(t.location)) + } + }) +}) diff --git a/pkg/util/iploc/iploc_suite_test.go b/pkg/util/iploc/iploc_suite_test.go new file mode 100644 index 00000000..b0ea3d14 --- /dev/null +++ b/pkg/util/iploc/iploc_suite_test.go @@ -0,0 +1,17 @@ +// Copyright 2023 ROC. All rights reserved. +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. + +package iploc_test + +import ( + "testing" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +func TestIploc(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "Iploc Suite") +} diff --git a/pkg/util/iploc/iploc_test.go b/pkg/util/iploc/iploc_test.go index a38bb106..b807b101 100644 --- a/pkg/util/iploc/iploc_test.go +++ b/pkg/util/iploc/iploc_test.go @@ -1,29 +1,44 @@ -// Copyright 2022 ROC. All rights reserved. +// Copyright 2023 ROC. All rights reserved. // Use of this source code is governed by a MIT style // license that can be found in the LICENSE file. -package iploc +package iploc_test import ( - "testing" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + + "github.com/rocboss/paopao-ce/pkg/util/iploc" ) -func TestFind(t *testing.T) { - for _, data := range []struct { +var _ = Describe("Iploc", Ordered, func() { + type iplocCases []struct { ip string country string city string - }{ - {ip: "127.0.0.1", country: "本机地址", city: " CZ88.NET"}, - {ip: "180.89.94.9", country: "北京市", city: "鹏博士宽带"}, - } { - country, city := Find(data.ip) - t.Logf("ip:%v, country:%v, city:%v", data.ip, country, city) - if country != data.country { - t.Errorf("find ip:%s expect country: %s got: %s", data.ip, data.country, country) + } + var samples iplocCases + + BeforeAll(func() { + samples = iplocCases{ + { + ip: "127.0.0.1", + country: "本机地址", + city: " CZ88.NET", + }, + { + ip: "180.89.94.9", + country: "北京市", + city: "鹏博士宽带", + }, } - if city != data.city { - t.Errorf("find ip:%s expect city: %s got: %s", data.ip, data.city, city) + }) + + It("find country and city by ip", func() { + for _, t := range samples { + country, city := iploc.Find(t.ip) + Expect(country).To(Equal(t.country)) + Expect(city).To(Equal(t.city)) } - } -} + }) +}) diff --git a/pkg/util/md5_test.go b/pkg/util/md5_test.go index 12c14c86..248ab748 100644 --- a/pkg/util/md5_test.go +++ b/pkg/util/md5_test.go @@ -1,47 +1,44 @@ -// Copyright 2022 ROC. All rights reserved. +// Copyright 2023 ROC. All rights reserved. // Use of this source code is governed by a MIT style // license that can be found in the LICENSE file. -package util +package util_test -import "testing" +import ( + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" -func TestEncodeMD5(t *testing.T) { - type args struct { + "github.com/rocboss/paopao-ce/pkg/util" +) + +var _ = Describe("Md5", Ordered, func() { + type md5Cases []struct { value string + md5 string } - tests := []struct { - name string - args args - want string - }{ - { - name: "test1", - args: args{ + var samples md5Cases + + BeforeAll(func() { + samples = md5Cases{ + { value: "123456", + md5: "e10adc3949ba59abbe56e057f20f883e", }, - want: "e10adc3949ba59abbe56e057f20f883e", - }, - { - name: "test2", - args: args{ + { value: "", + md5: "d41d8cd98f00b204e9800998ecf8427e", // really odd, why? }, - want: "d41d8cd98f00b204e9800998ecf8427e", // really odd, why? - }, - { - name: "test3", - args: args{ + { value: "paopaocestr", + md5: "8a5033dda1a8919224c66e68d846a289", }, - want: "8a5033dda1a8919224c66e68d846a289", - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if got := EncodeMD5(tt.args.value); got != tt.want { - t.Errorf("EncodeMD5() = %v, want %v", got, tt.want) - } - }) - } -} + } + }) + + It("encode md5", func() { + for _, t := range samples { + Expect(util.EncodeMD5(t.value)).To(Equal(t.md5)) + } + }) + +}) diff --git a/pkg/util/util_suite_test.go b/pkg/util/util_suite_test.go new file mode 100644 index 00000000..6d6903e9 --- /dev/null +++ b/pkg/util/util_suite_test.go @@ -0,0 +1,13 @@ +package util_test + +import ( + "testing" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +func TestUtil(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "Util Suite") +}