Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions cmd/gf/internal/cmd/cmd_z_unit_fix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"testing"

"github.com/gogf/gf/v2/test/gtest"
"github.com/gogf/gf/v2/text/gstr"
)

func Test_Fix_doFixV25Content(t *testing.T) {
Expand All @@ -22,3 +23,82 @@ func Test_Fix_doFixV25Content(t *testing.T) {
t.AssertNil(err)
})
}

func Test_Fix_doFixV25Content_WithReplacement(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
f = cFix{}
content = `s.BindHookHandlerByMap("/path", map[string]ghttp.HandlerFunc{
ghttp.HookBeforeServe: func(r *ghttp.Request) {},
})`
)
newContent, err := f.doFixV25Content(content)
t.AssertNil(err)
// Verify the replacement was made
t.Assert(gstr.Contains(newContent, "map[ghttp.HookName]ghttp.HandlerFunc"), true)
t.Assert(gstr.Contains(newContent, "map[string]ghttp.HandlerFunc"), false)
})
}

func Test_Fix_doFixV25Content_NoMatch(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
f = cFix{}
content = `package main

func main() {
fmt.Println("Hello World")
}
`
)
newContent, err := f.doFixV25Content(content)
t.AssertNil(err)
// Content should remain unchanged
t.Assert(newContent, content)
})
}

func Test_Fix_doFixV25Content_MultipleMatches(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
f = cFix{}
content = `
s.BindHookHandlerByMap("/path1", map[string]ghttp.HandlerFunc{})
s.BindHookHandlerByMap("/path2", map[string]ghttp.HandlerFunc{})
`
)
newContent, err := f.doFixV25Content(content)
t.AssertNil(err)
// Both should be replaced
count := gstr.Count(newContent, "map[ghttp.HookName]ghttp.HandlerFunc")
t.Assert(count, 2)
})
}

func Test_Fix_doFixV25Content_EmptyContent(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
f = cFix{}
content = ""
)
newContent, err := f.doFixV25Content(content)
t.AssertNil(err)
t.Assert(newContent, "")
})
}

func Test_Fix_doFixV25Content_ComplexPath(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
f = cFix{}
content = `s.BindHookHandlerByMap("/api/v1/user/{id}/profile", map[string]ghttp.HandlerFunc{
ghttp.HookBeforeServe: func(r *ghttp.Request) {
r.Response.Write("before")
},
})`
)
newContent, err := f.doFixV25Content(content)
t.AssertNil(err)
t.Assert(gstr.Contains(newContent, "map[ghttp.HookName]ghttp.HandlerFunc"), true)
})
}
Loading