1
0
mirror of https://github.com/beego/bee.git synced 2024-12-18 03:20:50 +00:00

add more test case

This commit is contained in:
John 2021-02-07 10:47:42 +08:00
parent 637637efb2
commit 824fa639f0

View File

@ -26,57 +26,103 @@ import (
//package model //package model
// //
//import ( //import (
// "github.com/shopspring/decimal" //"sync"
//
//"example.com/pkgnotexist"
//"github.com/shopspring/decimal"
//) //)
// //
//type Object struct{ //type Object struct {
// Total decimal.Decimal // Field1 decimal.Decimal
// Field2 pkgnotexist.TestType
// Field3 sync.Map
//} //}
func TestCheckAndLoadPackageOnGoMod(t *testing.T) { func TestCheckAndLoadPackageOnGoMod(t *testing.T) {
var (
pkgName = "decimal"
pkgImportPath = "github.com/shopspring/decimal"
)
defer os.Setenv("GO111MODULE", os.Getenv("GO111MODULE")) defer os.Setenv("GO111MODULE", os.Getenv("GO111MODULE"))
os.Setenv("GO111MODULE", "on") os.Setenv("GO111MODULE", "on")
imports := []*ast.ImportSpec{ testCases := []struct {
pkgName string
pkgImportPath string
imports []*ast.ImportSpec
realType string
curPkgName string
expected bool
}{
{
pkgName: "decimal",
pkgImportPath: "github.com/shopspring/decimal",
imports: []*ast.ImportSpec{
{ {
Path: &ast.BasicLit{ Path: &ast.BasicLit{
Value: pkgImportPath, Value: "github.com/shopspring/decimal",
}, },
}, },
},
realType: "decimal.Decimal",
curPkgName: "model",
expected: true,
},
{
pkgName: "pkgnotexist",
pkgImportPath: "example.com/pkgnotexist",
imports: []*ast.ImportSpec{
{
Path: &ast.BasicLit{
Value: "example.com/pkgnotexist",
},
},
},
realType: "pkgnotexist.TestType",
curPkgName: "model",
expected: false,
},
{
pkgName: "sync",
pkgImportPath: "sync",
imports: []*ast.ImportSpec{
{
Path: &ast.BasicLit{
Value: "sync",
},
},
},
realType: "sync.Map",
curPkgName: "model",
expected: false,
},
} }
checkAndLoadPackage(imports, "decimal.Decimal", "model")
if len(astPkgs) == 0 { for _, test := range testCases {
t.Fatalf("failed to load module: %s", pkgImportPath) checkAndLoadPackage(test.imports, test.realType, test.curPkgName)
} result := false
notLoadFlag := true
for _, v := range astPkgs { for _, v := range astPkgs {
if v.Name == pkgName { if v.Name == test.pkgName {
notLoadFlag = false result = true
} }
} }
if notLoadFlag { if test.expected != result {
t.Fatalf("failed to load module: %s", pkgImportPath) t.Fatalf("load module error, expected: %v, result: %v", test.expected, result)
}
} }
} }
//package model //package model
// //
//import ( //import (
//"sync"
//
//"example.com/comm" //"example.com/comm"
//"example.com/pkgnotexist"
//) //)
// //
//type Object struct { //type Object struct {
// Total comm.Common // Field1 comm.Common
// Field2 pkgnotexist.TestType
// Field3 sync.Map
//} //}
func TestCheckAndLoadPackageOnGoPath(t *testing.T) { func TestCheckAndLoadPackageOnGoPath(t *testing.T) {
var ( var (
pkgName = "comm"
pkgImportPath = "example.com/comm"
testCommPkg = ` testCommPkg = `
package comm package comm
@ -108,24 +154,68 @@ type Common struct {
os.Setenv("GOPATH", gopath) os.Setenv("GOPATH", gopath)
build.Default.GOPATH = gopath build.Default.GOPATH = gopath
imports := []*ast.ImportSpec{ testCases := []struct {
pkgName string
pkgImportPath string
imports []*ast.ImportSpec
realType string
curPkgName string
expected bool
}{
{
pkgName: "comm",
pkgImportPath: "example.com/comm",
imports: []*ast.ImportSpec{
{ {
Path: &ast.BasicLit{ Path: &ast.BasicLit{
Value: pkgImportPath, Value: "example.com/comm",
}, },
}, },
},
realType: "comm.Common",
curPkgName: "model",
expected: true,
},
{
pkgName: "pkgnotexist",
pkgImportPath: "example.com/pkgnotexist",
imports: []*ast.ImportSpec{
{
Path: &ast.BasicLit{
Value: "example.com/pkgnotexist",
},
},
},
realType: "pkgnotexist.TestType",
curPkgName: "model",
expected: false,
},
{
pkgName: "sync",
pkgImportPath: "sync",
imports: []*ast.ImportSpec{
{
Path: &ast.BasicLit{
Value: "sync",
},
},
},
realType: "sync.Map",
curPkgName: "model",
expected: false,
},
} }
checkAndLoadPackage(imports, "comm.Common", "model")
if len(astPkgs) == 0 { for _, test := range testCases {
t.Fatalf("failed to load module: %s", pkgImportPath) checkAndLoadPackage(test.imports, test.realType, test.curPkgName)
} result := false
notLoadFlag := true
for _, v := range astPkgs { for _, v := range astPkgs {
if v.Name == pkgName { if v.Name == test.pkgName {
notLoadFlag = false result = true
} }
} }
if notLoadFlag { if test.expected != result {
t.Fatalf("failed to load module: %s", pkgImportPath) t.Fatalf("load module error, expected: %v, result: %v", test.expected, result)
}
} }
} }