Skip to content

Commit

Permalink
improve unit testing cases (#2468)
Browse files Browse the repository at this point in the history
  • Loading branch information
hailaz authored Feb 23, 2023
1 parent b4f76b8 commit adf90c8
Show file tree
Hide file tree
Showing 14 changed files with 633 additions and 64 deletions.
12 changes: 10 additions & 2 deletions frame/g/g_z_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package g_test

import (
"context"
"os"
"testing"

"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/test/gtest"
"github.com/gogf/gf/v2/util/gutil"
"os"
"testing"
)

var (
Expand Down Expand Up @@ -60,6 +61,13 @@ func Test_TryCatch(t *testing.T) {
g.Dump(exception)
})
})
gtest.C(t, func(t *gtest.T) {
g.TryCatch(ctx, func(ctx context.Context) {
g.Throw("GoFrame")
}, func(ctx context.Context, exception error) {
t.Assert(exception.Error(), "GoFrame")
})
})
}

func Test_IsNil(t *testing.T) {
Expand Down
74 changes: 45 additions & 29 deletions frame/gins/gins_z_unit_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,48 @@
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.

package gins

//func Test_Server(t *testing.T) {
// gtest.C(t, func(t *gtest.T) {
// var (
// path = gcfg.DefaultConfigFileName
// serverConfigContent = gtest.DataContent("server", "config.yaml")
// err = gfile.PutContents(path, serverConfigContent)
// )
// t.AssertNil(err)
// defer gfile.Remove(path)
//
// time.Sleep(time.Second)
//
// instance.Clear()
// defer instance.Clear()
//
// s := Server("tempByInstanceName")
// s.BindHandler("/", func(r *ghttp.Request) {
// r.Response.Write("hello")
// })
// s.SetDumpRouterMap(false)
// t.AssertNil(s.Start())
// defer t.AssertNil(s.Shutdown())
//
// content := HttpClient().GetContent(gctx.New(), `http://127.0.0.1:8003/`)
// t.Assert(content, `hello`)
// })
//}
package gins_test

import (
"fmt"
"testing"
"time"

"github.com/gogf/gf/v2/frame/gins"
"github.com/gogf/gf/v2/internal/instance"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/os/gcfg"
"github.com/gogf/gf/v2/os/gctx"
"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/test/gtest"
)

func Test_Server(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
path = gcfg.DefaultConfigFileName
serverConfigContent = gtest.DataContent("server", "config.yaml")
err = gfile.PutContents(path, serverConfigContent)
)
t.AssertNil(err)
defer gfile.Remove(path)

instance.Clear()
defer instance.Clear()

s := gins.Server("tempByInstanceName")
s.BindHandler("/", func(r *ghttp.Request) {
r.Response.Write("hello")
})
s.SetDumpRouterMap(false)
s.Start()
defer s.Shutdown()

time.Sleep(100 * time.Millisecond)

prefix := fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())
client := gins.HttpClient()
client.SetPrefix(prefix)
t.Assert(client.GetContent(gctx.New(), "/"), "hello")
})
}
2 changes: 1 addition & 1 deletion frame/gins/testdata/server/config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
server:
address: ":8000"
tempByInstanceName:
address: ":8003"
accessLogEnabled: false
24 changes: 24 additions & 0 deletions i18n/gi18n/gi18n_z_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package gi18n_test

import (
"github.com/gogf/gf/v2/os/gctx"
_ "github.com/gogf/gf/v2/os/gres/testdata/data"

"context"
Expand Down Expand Up @@ -155,3 +156,26 @@ func Test_Resource(t *testing.T) {
t.Assert(m.T(context.Background(), "{#hello}{#world}"), "你好世界")
})
}

func Test_SetCtxLanguage(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
ctx := gctx.New()
t.Assert(gi18n.LanguageFromCtx(ctx), "")
})

gtest.C(t, func(t *gtest.T) {
t.Assert(gi18n.LanguageFromCtx(nil), "")
})

gtest.C(t, func(t *gtest.T) {
ctx := gctx.New()
ctx = gi18n.WithLanguage(ctx, "zh-CN")
t.Assert(gi18n.LanguageFromCtx(ctx), "zh-CN")
})

gtest.C(t, func(t *gtest.T) {
ctx := gi18n.WithLanguage(nil, "zh-CN")
t.Assert(gi18n.LanguageFromCtx(ctx), "zh-CN")
})

}
47 changes: 47 additions & 0 deletions os/gcfg/gcfg_z_unit_adapter_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,35 @@ import (
"github.com/gogf/gf/v2/test/gtest"
)

func TestAdapterFile_Dump(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
c, err := gcfg.NewAdapterFile("config.yml")
t.AssertNil(err)

t.Assert(c.GetFileName(), "config.yml")

c.Dump()
c.Data(ctx)
})

gtest.C(t, func(t *gtest.T) {
c, err := gcfg.NewAdapterFile("testdata/default/config.toml")
t.AssertNil(err)

c.Dump()
c.Data(ctx)
c.GetPaths()
})

}
func TestAdapterFile_Available(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
c, err := gcfg.NewAdapterFile("testdata/default/config.toml")
t.AssertNil(err)
c.Available(ctx)
})
}

func TestAdapterFile_SetPath(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
c, err := gcfg.NewAdapterFile("config.yml")
Expand All @@ -24,6 +53,15 @@ func TestAdapterFile_SetPath(t *testing.T) {
err = c.SetPath("/tmp")
t.AssertNil(err)

err = c.SetPath("notexist")
t.AssertNE(err, nil)

err = c.SetPath("testdata/c1.toml")
t.AssertNE(err, nil)

err = c.SetPath("")
t.AssertNil(err)

err = c.SetPath("gcfg.go")
t.AssertNE(err, nil)

Expand All @@ -41,6 +79,15 @@ func TestAdapterFile_AddPath(t *testing.T) {
err = c.AddPath("/tmp")
t.AssertNil(err)

err = c.AddPath("notexist")
t.AssertNE(err, nil)

err = c.SetPath("testdata/c1.toml")
t.AssertNE(err, nil)

err = c.SetPath("")
t.AssertNil(err)

err = c.AddPath("gcfg.go")
t.AssertNE(err, nil)

Expand Down
7 changes: 6 additions & 1 deletion os/gview/gview_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,12 @@ func (view *View) SetConfigWithMap(m map[string]interface{}) error {
_, v1 := gutil.MapPossibleItemByKey(m, "paths")
_, v2 := gutil.MapPossibleItemByKey(m, "path")
if v1 == nil && v2 != nil {
m["paths"] = []interface{}{v2}
switch v2.(type) {
case string:
m["paths"] = []string{v2.(string)}
case []string:
m["paths"] = v2
}
}
err := gconv.Struct(m, &view.config)
if err != nil {
Expand Down
91 changes: 91 additions & 0 deletions os/gview/gview_z_unit_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@ import (
"testing"

"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/i18n/gi18n"
"github.com/gogf/gf/v2/internal/command"
"github.com/gogf/gf/v2/os/gview"
"github.com/gogf/gf/v2/test/gtest"
)

func Test_Config(t *testing.T) {
// show error print
command.Init("-gf.gview.errorprint=true")
gtest.C(t, func(t *gtest.T) {
config := gview.Config{
Paths: []string{gtest.DataPath("config")},
Expand All @@ -25,10 +29,13 @@ func Test_Config(t *testing.T) {
DefaultFile: "test.html",
Delimiters: []string{"${", "}"},
}

view := gview.New()
err := view.SetConfig(config)
t.AssertNil(err)

view.SetI18n(gi18n.New())

str := `hello ${.name},version:${.version}`
view.Assigns(g.Map{"version": "1.7.0"})
result, err := view.ParseContent(context.TODO(), str, nil)
Expand All @@ -38,6 +45,38 @@ func Test_Config(t *testing.T) {
result, err = view.ParseDefault(context.TODO())
t.AssertNil(err)
t.Assert(result, "name:gf")

t.Assert(view.GetDefaultFile(), "test.html")
})
// SetConfig path fail: notexist
gtest.C(t, func(t *gtest.T) {
config := gview.Config{
Paths: []string{"notexist", gtest.DataPath("config/test.html")},
Data: g.Map{
"name": "gf",
},
DefaultFile: "test.html",
Delimiters: []string{"${", "}"},
}

view := gview.New()
err := view.SetConfig(config)
t.AssertNE(err, nil)
})
// SetConfig path fail: set file path
gtest.C(t, func(t *gtest.T) {
config := gview.Config{
Paths: []string{gtest.DataPath("config/test.html")},
Data: g.Map{
"name": "gf",
},
DefaultFile: "test.html",
Delimiters: []string{"${", "}"},
}

view := gview.New()
err := view.SetConfig(config)
t.AssertNE(err, nil)
})
}

Expand All @@ -64,4 +103,56 @@ func Test_ConfigWithMap(t *testing.T) {
t.AssertNil(err)
t.Assert(result, "name:gf")
})
// path as paths
gtest.C(t, func(t *gtest.T) {
view := gview.New()
err := view.SetConfigWithMap(g.Map{
"Path": gtest.DataPath("config"),
"DefaultFile": "test.html",
"Delimiters": []string{"${", "}"},
"Data": g.Map{
"name": "gf",
},
})
t.AssertNil(err)

str := `hello ${.name},version:${.version}`
view.Assigns(g.Map{"version": "1.7.0"})
result, err := view.ParseContent(context.TODO(), str, nil)
t.AssertNil(err)
t.Assert(result, "hello gf,version:1.7.0")

result, err = view.ParseDefault(context.TODO())
t.AssertNil(err)
t.Assert(result, "name:gf")
})
// path as paths
gtest.C(t, func(t *gtest.T) {
view := gview.New()
err := view.SetConfigWithMap(g.Map{
"Path": []string{gtest.DataPath("config")},
"DefaultFile": "test.html",
"Delimiters": []string{"${", "}"},
"Data": g.Map{
"name": "gf",
},
})
t.AssertNil(err)

str := `hello ${.name},version:${.version}`
view.Assigns(g.Map{"version": "1.7.0"})
result, err := view.ParseContent(context.TODO(), str, nil)
t.AssertNil(err)
t.Assert(result, "hello gf,version:1.7.0")

result, err = view.ParseDefault(context.TODO())
t.AssertNil(err)
t.Assert(result, "name:gf")
})
// map is nil
gtest.C(t, func(t *gtest.T) {
view := gview.New()
err := view.SetConfigWithMap(nil)
t.AssertNE(err, nil)
})
}
Loading

0 comments on commit adf90c8

Please sign in to comment.