improve unit testing cases (#2468)

This commit is contained in:
HaiLaz
2023-02-23 10:07:40 +08:00
committed by GitHub
parent b4f76b8448
commit adf90c876f
14 changed files with 633 additions and 64 deletions

View File

@ -221,6 +221,7 @@ func AssertLE(value, expect interface{}) {
// The `expect` should be a slice,
// but the `value` can be a slice or a basic type variable.
// TODO map support.
// TODO: gconv.Strings(0) is not [0]
func AssertIN(value, expect interface{}) {
var (
passed = true

View File

@ -7,18 +7,38 @@
package gtest_test
import (
"errors"
"path/filepath"
"strconv"
"testing"
"github.com/gogf/gf/v2/test/gtest"
)
var (
map1 = map[string]string{"k1": "v1"}
map1Expect = map[string]string{"k1": "v1"}
map2 = map[string]string{"k2": "v2"}
mapLong1 = map[string]string{"k1": "v1", "k2": "v2"}
mapLong1Expect = map[string]string{"k2": "v2", "k1": "v1"}
)
func TestC(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
t.Assert(1, 1)
t.AssertNE(1, 0)
t.AssertEQ(float32(123.456), float32(123.456))
t.AssertEQ(float32(123.456), float32(123.456))
t.Assert(map[string]string{"1": "1"}, map[string]string{"1": "1"})
})
gtest.C(t, func(t *gtest.T) {
defer func() {
if err := recover(); err != nil {
t.Assert(err, "[ASSERT] EXPECT 1 == 0")
}
}()
t.Assert(1, 0)
})
}
@ -32,19 +52,48 @@ func TestCase(t *testing.T) {
}
func TestAssert(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
nilChan chan struct{}
)
t.Assert(1, 1)
t.Assert(nilChan, nil)
m1 := map[string]string{"k1": "v1", "k2": "v2"}
m2 := map[string]string{"k2": "v2", "k1": "v1"}
t.Assert(m1, m2)
t.Assert(map1, map1Expect)
})
gtest.C(t, func(t *gtest.T) {
defer func() {
if err := recover(); err != nil {
t.Assert(err, `[ASSERT] EXPECT VALUE map["k2"]: == map["k2"]:v2
GIVEN : map[k1:v1]
EXPECT: map[k2:v2]`)
}
}()
t.Assert(map1, map2)
})
gtest.C(t, func(t *gtest.T) {
defer func() {
if err := recover(); err != nil {
t.Assert(err, `[ASSERT] EXPECT MAP LENGTH 2 == 1`)
}
}()
t.Assert(mapLong1, map2)
})
gtest.C(t, func(t *gtest.T) {
defer func() {
if err := recover(); err != nil {
t.Assert(err, `[ASSERT] EXPECT VALUE TO BE A MAP, BUT GIVEN "int"`)
}
}()
t.Assert(0, map1)
})
}
func TestAssertEQ(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
nilChan chan struct{}
@ -52,9 +101,54 @@ func TestAssertEQ(t *testing.T) {
t.AssertEQ(nilChan, nil)
t.AssertEQ("0", "0")
t.AssertEQ(float32(123.456), float32(123.456))
m1 := map[string]string{"k1": "v1", "k2": "v2"}
m2 := map[string]string{"k2": "v2", "k1": "v1"}
t.AssertEQ(m1, m2)
t.AssertEQ(mapLong1, mapLong1Expect)
})
gtest.C(t, func(t *gtest.T) {
defer func() {
if err := recover(); err != nil {
t.Assert(err, "[ASSERT] EXPECT 1 == 0")
}
}()
t.AssertEQ(1, 0)
})
gtest.C(t, func(t *gtest.T) {
defer func() {
if err := recover(); err != nil {
t.Assert(err, "[ASSERT] EXPECT TYPE 1[int] == 1[string]")
}
}()
t.AssertEQ(1, "1")
})
gtest.C(t, func(t *gtest.T) {
defer func() {
if err := recover(); err != nil {
t.Assert(err, `[ASSERT] EXPECT VALUE map["k2"]: == map["k2"]:v2
GIVEN : map[k1:v1]
EXPECT: map[k2:v2]`)
}
}()
t.AssertEQ(map1, map2)
})
gtest.C(t, func(t *gtest.T) {
defer func() {
if err := recover(); err != nil {
t.Assert(err, `[ASSERT] EXPECT MAP LENGTH 2 == 1`)
}
}()
t.AssertEQ(mapLong1, map2)
})
gtest.C(t, func(t *gtest.T) {
defer func() {
if err := recover(); err != nil {
t.Assert(err, `[ASSERT] EXPECT VALUE TO BE A MAP, BUT GIVEN "int"`)
}
}()
t.AssertEQ(0, map1)
})
}
@ -66,9 +160,25 @@ func TestAssertNE(t *testing.T) {
t.AssertNE(nil, c)
t.AssertNE("0", "1")
t.AssertNE(float32(123.456), float32(123.4567))
m1 := map[string]string{"k1": "v1", "k2": "v2"}
m2 := map[string]string{"k2": "v1", "k1": "v2"}
t.AssertNE(m1, m2)
t.AssertNE(map1, map2)
})
gtest.C(t, func(t *gtest.T) {
defer func() {
if err := recover(); err != nil {
t.Assert(err, "[ASSERT] EXPECT 1 != 1")
}
}()
t.AssertNE(1, 1)
})
gtest.C(t, func(t *gtest.T) {
defer func() {
if err := recover(); err != nil {
t.Assert(err, `[ASSERT] EXPECT map[k1:v1] != map[k1:v1]`)
}
}()
t.AssertNE(map1, map1Expect)
})
}
@ -77,6 +187,24 @@ func TestAssertNQ(t *testing.T) {
t.AssertNQ(1, "0")
t.AssertNQ(float32(123.456), float64(123.4567))
})
gtest.C(t, func(t *gtest.T) {
defer func() {
if err := recover(); err != nil {
t.Assert(err, "[ASSERT] EXPECT 1 != 1")
}
}()
t.AssertNQ(1, "1")
})
gtest.C(t, func(t *gtest.T) {
defer func() {
if err := recover(); err != nil {
t.Assert(err, "[ASSERT] EXPECT TYPE 1[int] != 1[int]")
}
}()
t.AssertNQ(1, 1)
})
}
func TestAssertGT(t *testing.T) {
@ -86,6 +214,15 @@ func TestAssertGT(t *testing.T) {
t.AssertGT(uint(1), uint(0))
t.AssertGT(float32(123.45678), float32(123.4567))
})
gtest.C(t, func(t *gtest.T) {
defer func() {
if err := recover(); err != nil {
t.Assert(err, "[ASSERT] EXPECT -1 > 1")
}
}()
t.AssertGT(-1, 1)
})
}
func TestAssertGE(t *testing.T) {
@ -99,6 +236,15 @@ func TestAssertGE(t *testing.T) {
t.AssertGE(float32(123.45678), float32(123.4567))
t.AssertGE(float32(123.456), float32(123.456))
})
gtest.C(t, func(t *gtest.T) {
defer func() {
if err := recover(); err != nil {
t.Assert(err, "[ASSERT] EXPECT -1(int) >= 1(int)")
}
}()
t.AssertGE(-1, 1)
})
}
func TestAssertLT(t *testing.T) {
@ -108,6 +254,15 @@ func TestAssertLT(t *testing.T) {
t.AssertLT(uint(0), uint(1))
t.AssertLT(float32(123.456), float32(123.4567))
})
gtest.C(t, func(t *gtest.T) {
defer func() {
if err := recover(); err != nil {
t.Assert(err, "[ASSERT] EXPECT 1 < -1")
}
}()
t.AssertLT(1, -1)
})
}
func TestAssertLE(t *testing.T) {
@ -121,6 +276,15 @@ func TestAssertLE(t *testing.T) {
t.AssertLE(float32(123.456), float32(123.4567))
t.AssertLE(float32(123.456), float32(123.456))
})
gtest.C(t, func(t *gtest.T) {
defer func() {
if err := recover(); err != nil {
t.Assert(err, "[ASSERT] EXPECT 1 <= -1")
}
}()
t.AssertLE(1, -1)
})
}
func TestAssertIN(t *testing.T) {
@ -128,6 +292,26 @@ func TestAssertIN(t *testing.T) {
t.AssertIN("a", []string{"a", "b", "c"})
t.AssertIN(1, []int{1, 2, 3})
})
gtest.C(t, func(t *gtest.T) {
defer func() {
if err := recover(); err != nil {
t.Assert(err, "[ASSERT] INVALID EXPECT VALUE TYPE: int")
}
}()
t.AssertIN(0, 0)
})
gtest.C(t, func(t *gtest.T) {
defer func() {
if err := recover(); err != nil {
t.Assert(err, "[ASSERT] EXPECT 4 IN [1 2 3]")
}
}()
// t.AssertIN(0, []int{0, 1, 2, 3})
// t.AssertIN(0, []int{ 1, 2, 3})
t.AssertIN(4, []int{1, 2, 3})
})
}
func TestAssertNI(t *testing.T) {
@ -135,6 +319,24 @@ func TestAssertNI(t *testing.T) {
t.AssertNI("d", []string{"a", "b", "c"})
t.AssertNI(4, []int{1, 2, 3})
})
gtest.C(t, func(t *gtest.T) {
defer func() {
if err := recover(); err != nil {
t.Assert(err, "[ASSERT] INVALID EXPECT VALUE TYPE: int")
}
}()
t.AssertNI(0, 0)
})
gtest.C(t, func(t *gtest.T) {
defer func() {
if err := recover(); err != nil {
t.Assert(err, "[ASSERT] EXPECT 1 NOT IN [1 2 3]")
}
}()
t.AssertNI(1, []int{1, 2, 3})
})
}
func TestAssertNil(t *testing.T) {
@ -146,4 +348,46 @@ func TestAssertNil(t *testing.T) {
_, err := strconv.ParseInt("123", 10, 64)
t.AssertNil(err)
})
gtest.C(t, func(t *gtest.T) {
defer func() {
if err := recover(); err != nil {
t.Assert(err, "error")
}
}()
t.AssertNil(errors.New("error"))
})
gtest.C(t, func(t *gtest.T) {
defer func() {
if err := recover(); err != nil {
t.AssertNE(err, nil)
}
}()
t.AssertNil(1)
})
}
func TestAssertError(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
defer func() {
if err := recover(); err != nil {
t.Assert(err, "[ERROR] this is an error")
}
}()
t.Error("this is an error")
})
}
func TestDataPath(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
t.Assert(filepath.ToSlash(gtest.DataPath("testdata.txt")), `./testdata/testdata.txt`)
})
}
func TestDataContent(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
t.Assert(gtest.DataContent("testdata.txt"), `hello`)
t.Assert(gtest.DataContent(""), "")
})
}

1
test/gtest/testdata/testdata.txt vendored Normal file
View File

@ -0,0 +1 @@
hello