Skip to content

Commit

Permalink
调整测试文件
Browse files Browse the repository at this point in the history
  • Loading branch information
yangyile committed Dec 3, 2024
1 parent 4597f46 commit 10a10cb
Show file tree
Hide file tree
Showing 25 changed files with 308 additions and 93 deletions.
2 changes: 1 addition & 1 deletion cls_stub_gen/stub_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type StubGenConfig struct {
AllowFileCreation bool //当目标文件不存在时,能否能新建文件,假如设置为不能,就必须得找到文件才能写内容
}

func GenerateStubsFile(config *StubGenConfig, stubParams ...*StubParam) {
func GenerateStubs(config *StubGenConfig, stubParams ...*StubParam) {
utils.PrintObject(config)

must.Nice(config.SourceRootPath)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package example0node
package example0

var NODE = &A{}
var STUB1 = &A{}
var STUB2 = &A{}

type A struct{}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package example0x1gen

import "github.com/yyle88/sure/internal/examples/example_cls_stub_gen/example0"

func Get() *example0.A {
return example0.STUB1.Get()
}
func Set(arg string) {
example0.STUB1.Set(arg)
}
func Add(x int, y int) int {
return example0.STUB1.Add(x, y)
}
func Sub(x int, y int) (int, error) {
return example0.STUB1.Sub(x, y)
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
package example0
package example0x1gen

import (
"testing"

"github.com/yyle88/runpath"
"github.com/yyle88/runpath/runtestpath"
"github.com/yyle88/sure/cls_stub_gen"
"github.com/yyle88/sure/internal/examples/example_cls_stub_gen/example0/example0node"
"github.com/yyle88/sure/internal/examples/example_cls_stub_gen/example0"
"github.com/yyle88/syntaxgo"
"github.com/yyle88/syntaxgo/syntaxgo_ast"
)

func TestGen(t *testing.T) {
param := cls_stub_gen.NewStubParam(&example0node.A{}, "example0node.NODE")
param := cls_stub_gen.NewStubParam(&example0.A{}, "example0.STUB1")

cfg := &cls_stub_gen.StubGenConfig{
SourceRootPath: runpath.PARENT.Join("example0node"),
SourceRootPath: runpath.PARENT.Join("../../example0"),
TargetPackageName: syntaxgo.CurrentPackageName(),
ImportOptions: syntaxgo_ast.NewPackageImportOptions(),
OutputPath: runtestpath.SrcPath(t),
AllowFileCreation: false,
}
cls_stub_gen.GenerateStubsFile(cfg, param)
cls_stub_gen.GenerateStubs(cfg, param)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package example0x2gen

import "github.com/yyle88/sure/internal/examples/example_cls_stub_gen/example0"

func Get() *example0.A {
return example0.STUB2.Get()
}
func Set(arg string) {
example0.STUB2.Set(arg)
}
func Add(x int, y int) int {
return example0.STUB2.Add(x, y)
}
func Sub(x int, y int) (int, error) {
return example0.STUB2.Sub(x, y)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package example0x2gen

import (
"testing"

"github.com/yyle88/runpath"
"github.com/yyle88/runpath/runtestpath"
"github.com/yyle88/sure/cls_stub_gen"
"github.com/yyle88/sure/internal/examples/example_cls_stub_gen/example0"
"github.com/yyle88/syntaxgo"
"github.com/yyle88/syntaxgo/syntaxgo_ast"
)

func TestGen(t *testing.T) {
param := cls_stub_gen.NewStubParam(&example0.A{}, "example0.STUB2")

cfg := &cls_stub_gen.StubGenConfig{
SourceRootPath: runpath.PARENT.Join("../../example0"),
TargetPackageName: syntaxgo.CurrentPackageName(),
ImportOptions: syntaxgo_ast.NewPackageImportOptions(),
OutputPath: runtestpath.SrcPath(t),
AllowFileCreation: false,
}
cls_stub_gen.GenerateStubs(cfg, param)
}
16 changes: 0 additions & 16 deletions internal/examples/example_cls_stub_gen/example0/gen.go

This file was deleted.

13 changes: 13 additions & 0 deletions internal/examples/example_cls_stub_gen/example6/example6.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package example6

type A struct {
name string
}

func NewA(name string) *A {
return &A{name: name}
}

func (a *A) Name() string {
return a.name
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package example6x1gen

func Name() string {
return A100.Name()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package example6x1gen

import (
"testing"

"github.com/yyle88/runpath"
"github.com/yyle88/runpath/runtestpath"
"github.com/yyle88/sure/cls_stub_gen"
"github.com/yyle88/sure/internal/examples/example_cls_stub_gen/example6"
"github.com/yyle88/syntaxgo"
"github.com/yyle88/syntaxgo/syntaxgo_ast"
)

func TestGen(t *testing.T) {
param := cls_stub_gen.NewStubParam(&example6.A{}, "A100")

cfg := &cls_stub_gen.StubGenConfig{
SourceRootPath: runpath.PARENT.Join("../../example6"),
TargetPackageName: syntaxgo.CurrentPackageName(),
ImportOptions: syntaxgo_ast.NewPackageImportOptions(),
OutputPath: runtestpath.SrcPath(t),
AllowFileCreation: false,
}
cls_stub_gen.GenerateStubs(cfg, param)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package example6x1gen

import "github.com/yyle88/sure/internal/examples/example_cls_stub_gen/example6"

var A100 = example6.NewA("100")
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package example6x2gen

func Name() string {
return A200.Name()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package example6x2gen

import (
"testing"

"github.com/yyle88/runpath"
"github.com/yyle88/runpath/runtestpath"
"github.com/yyle88/sure/cls_stub_gen"
"github.com/yyle88/sure/internal/examples/example_cls_stub_gen/example6"
"github.com/yyle88/syntaxgo"
"github.com/yyle88/syntaxgo/syntaxgo_ast"
)

func TestGen(t *testing.T) {
param := cls_stub_gen.NewStubParam(&example6.A{}, "A200")

cfg := &cls_stub_gen.StubGenConfig{
SourceRootPath: runpath.PARENT.Join("../../example6"),
TargetPackageName: syntaxgo.CurrentPackageName(),
ImportOptions: syntaxgo_ast.NewPackageImportOptions(),
OutputPath: runtestpath.SrcPath(t),
AllowFileCreation: false,
}
cls_stub_gen.GenerateStubs(cfg, param)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package example6x2gen

import "github.com/yyle88/sure/internal/examples/example_cls_stub_gen/example6"

var A200 = example6.NewA("200")
26 changes: 26 additions & 0 deletions internal/examples/example_cls_stub_gen/examples.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package example_cls_stub_gen

import (
"math/rand/v2"

"github.com/yyle88/sure/internal/examples/example_cls_stub_gen/example0/example0x1gen"
"github.com/yyle88/sure/internal/examples/example_cls_stub_gen/example0/example0x2gen"
"github.com/yyle88/sure/internal/examples/example_cls_stub_gen/example6/example6x1gen"
"github.com/yyle88/sure/internal/examples/example_cls_stub_gen/example6/example6x2gen"
)

func Example0x1() int {
return example0x1gen.Add(rand.IntN(100), rand.IntN(100))
}

func Example0x2() int {
return example0x2gen.Add(rand.IntN(100), rand.IntN(100))
}

func Example6x1() string {
return example6x1gen.Name()
}

func Example6x2() string {
return example6x2gen.Name()
}
23 changes: 23 additions & 0 deletions internal/examples/example_cls_stub_gen/examples_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package example_cls_stub_gen

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestExample0x1(t *testing.T) {
t.Log(Example0x1())
}

func TestExample0x2(t *testing.T) {
t.Log(Example0x2())
}

func TestExample6x1(t *testing.T) {
require.Equal(t, "100", Example6x1())
}

func TestExample6x2(t *testing.T) {
require.Equal(t, "200", Example6x2())
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
package example5surenode
package example5sure_custom

import (
"github.com/yyle88/zaplog"
"go.uber.org/zap"
)

var NODE = &Node{}
var NODE = &SureClass{}

type Node struct{}
type SureClass struct{}

// Must 硬硬的,当有err时直接panic崩溃掉,流程中止
func (node *Node) Must(err error) {
func (node *SureClass) Must(err error) {
if err != nil {
zaplog.LOGS.P1.Panic("must", zap.Error(err))
}
}

// Soft 软软的,当有err时只打印个告警日志,流程继续
func (node *Node) Soft(err error) {
func (node *SureClass) Soft(err error) {
if err != nil {
zaplog.LOGS.P1.Warn("soft", zap.Error(err))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package example5sure_custom

import (
"testing"

"github.com/pkg/errors"
)

func TestNode_Must(t *testing.T) {
run := func() (string, error) {
return "OK", nil
}
res, err := run()
NODE.Must(err)
t.Log(res)
}

func TestNode_Soft(t *testing.T) {
run := func() (string, error) {
return "", errors.New("wrong")
}
res, err := run()
NODE.Soft(err)
t.Log(res)
}
18 changes: 9 additions & 9 deletions internal/examples/example_sure_cls_gen/example5/gen.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package example5

import "github.com/yyle88/sure/internal/examples/example_sure_cls_gen/example5/example5surenode"
import "github.com/yyle88/sure/internal/examples/example_sure_cls_gen/example5/example5sure_custom"

type ExampleMust struct{ a *Example }

Expand All @@ -9,12 +9,12 @@ func (a *Example) Must() *ExampleMust {
}
func (T *ExampleMust) GetN() (res int) {
res, err1 := T.a.GetN()
example5surenode.NODE.Must(err1)
example5sure_custom.NODE.Must(err1)
return res
}
func (T *ExampleMust) GetS() (res string) {
res, err1 := T.a.GetS()
example5surenode.NODE.Must(err1)
example5sure_custom.NODE.Must(err1)
return res
}

Expand All @@ -25,12 +25,12 @@ func (a *Example) Soft() *ExampleSoft {
}
func (T *ExampleSoft) GetN() (res int) {
res, err1 := T.a.GetN()
example5surenode.NODE.Soft(err1)
example5sure_custom.NODE.Soft(err1)
return res
}
func (T *ExampleSoft) GetS() (res string) {
res, err1 := T.a.GetS()
example5surenode.NODE.Soft(err1)
example5sure_custom.NODE.Soft(err1)
return res
}

Expand All @@ -41,12 +41,12 @@ func (a *Demo) Must() *DemoMust {
}
func (T *DemoMust) GetN() (res int) {
res, err1 := T.a.GetN()
example5surenode.NODE.Must(err1)
example5sure_custom.NODE.Must(err1)
return res
}
func (T *DemoMust) GetS() (res string) {
res, err1 := T.a.GetS()
example5surenode.NODE.Must(err1)
example5sure_custom.NODE.Must(err1)
return res
}

Expand All @@ -57,11 +57,11 @@ func (a *Demo) Soft() *DemoSoft {
}
func (T *DemoSoft) GetN() (res int) {
res, err1 := T.a.GetN()
example5surenode.NODE.Soft(err1)
example5sure_custom.NODE.Soft(err1)
return res
}
func (T *DemoSoft) GetS() (res string) {
res, err1 := T.a.GetS()
example5surenode.NODE.Soft(err1)
example5sure_custom.NODE.Soft(err1)
return res
}
Loading

0 comments on commit 10a10cb

Please sign in to comment.