1
0
mirror of https://github.com/gogf/gf.git synced 2025-04-05 11:18:50 +08:00

test/gtest: add support for string type in AssertIN and AssertNI (#3537)

This commit is contained in:
oldme 2024-04-24 21:04:54 +08:00 committed by GitHub
parent aa6d83fe1c
commit 12ff9af431
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 39 additions and 6 deletions

View File

@ -1,6 +1,7 @@
package gdebug_test
import (
"fmt"
"testing"
"github.com/gogf/gf/v2/debug/gdebug"
@ -34,13 +35,14 @@ func Test_CallerDirectory(t *testing.T) {
func Test_CallerFileLine(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
t.Assert(gstr.Contains(gdebug.CallerFileLine(), "gtest_util.go:35"), true)
fmt.Println(gdebug.CallerFileLine())
t.Assert(gstr.Contains(gdebug.CallerFileLine(), "gtest_util.go:36"), true)
})
}
func Test_CallerFileLineShort(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
t.Assert(gstr.Contains(gdebug.CallerFileLineShort(), "gtest_util.go:35"), true)
t.Assert(gstr.Contains(gdebug.CallerFileLineShort(), "gtest_util.go:36"), true)
})
}
@ -70,13 +72,13 @@ func Test_GoroutineId(t *testing.T) {
func Test_Stack(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
t.Assert(gstr.Contains(gdebug.Stack(), "gtest_util.go:35"), true)
t.Assert(gstr.Contains(gdebug.Stack(), "gtest_util.go:36"), true)
})
}
func Test_StackWithFilter(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
t.Assert(gstr.Contains(gdebug.StackWithFilter([]string{"github.com"}), "gtest_util.go:35"), true)
t.Assert(gstr.Contains(gdebug.StackWithFilter([]string{"github.com"}), "gtest_util.go:36"), true)
})
}

View File

@ -15,6 +15,7 @@ import (
"github.com/gogf/gf/v2/debug/gdebug"
"github.com/gogf/gf/v2/internal/empty"
"github.com/gogf/gf/v2/text/gstr"
"github.com/gogf/gf/v2/util/gconv"
)
@ -242,6 +243,12 @@ func AssertIN(value, expect interface{}) {
break
}
}
case reflect.String:
var (
valueStr = gconv.String(value)
expectStr = gconv.String(expect)
)
passed = gstr.Contains(expectStr, valueStr)
default:
panic(fmt.Sprintf(`[ASSERT] INVALID EXPECT VALUE TYPE: %v`, expectKind))
}
@ -274,6 +281,12 @@ func AssertNI(value, expect interface{}) {
break
}
}
case reflect.String:
var (
valueStr = gconv.String(value)
expectStr = gconv.String(expect)
)
passed = !gstr.Contains(expectStr, valueStr)
default:
panic(fmt.Sprintf(`[ASSERT] INVALID EXPECT VALUE TYPE: %v`, expectKind))
}

View File

@ -52,7 +52,6 @@ func TestCase(t *testing.T) {
}
func TestAssert(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
nilChan chan struct{}
@ -93,7 +92,6 @@ EXPECT: map[k2:v2]`)
}
func TestAssertEQ(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
nilChan chan struct{}
@ -291,6 +289,7 @@ func TestAssertIN(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
t.AssertIN("a", []string{"a", "b", "c"})
t.AssertIN(1, []int{1, 2, 3})
t.AssertIN("a", "abc")
})
gtest.C(t, func(t *gtest.T) {
@ -312,12 +311,22 @@ func TestAssertIN(t *testing.T) {
// t.AssertIN(0, []int{ 1, 2, 3})
t.AssertIN(4, []int{1, 2, 3})
})
gtest.C(t, func(t *gtest.T) {
defer func() {
if err := recover(); err != nil {
t.Assert(err, "[ASSERT] EXPECT d IN abc")
}
}()
t.AssertIN("d", "abc")
})
}
func TestAssertNI(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
t.AssertNI("d", []string{"a", "b", "c"})
t.AssertNI(4, []int{1, 2, 3})
t.AssertNI("d", "abc")
})
gtest.C(t, func(t *gtest.T) {
@ -337,6 +346,15 @@ func TestAssertNI(t *testing.T) {
}()
t.AssertNI(1, []int{1, 2, 3})
})
gtest.C(t, func(t *gtest.T) {
defer func() {
if err := recover(); err != nil {
t.Assert(err, "[ASSERT] EXPECT a NOT IN abc")
}
}()
t.AssertNI("a", "abc")
})
}
func TestAssertNil(t *testing.T) {