mirror of
				https://github.com/beego/bee.git
				synced 2025-11-04 04:13:26 +00:00 
			
		
		
		
	add more test case
This commit is contained in:
		@@ -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
 | 
				
			||||||
 | 
						}{
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			Path: &ast.BasicLit{
 | 
								pkgName:       "decimal",
 | 
				
			||||||
				Value: pkgImportPath,
 | 
								pkgImportPath: "github.com/shopspring/decimal",
 | 
				
			||||||
 | 
								imports: []*ast.ImportSpec{
 | 
				
			||||||
 | 
									{
 | 
				
			||||||
 | 
										Path: &ast.BasicLit{
 | 
				
			||||||
 | 
											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 == test.pkgName {
 | 
				
			||||||
		if v.Name == pkgName {
 | 
									result = true
 | 
				
			||||||
			notLoadFlag = false
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							if test.expected != result {
 | 
				
			||||||
 | 
								t.Fatalf("load module error, expected: %v, result: %v", test.expected, result)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	if notLoadFlag {
 | 
					 | 
				
			||||||
		t.Fatalf("failed to load module: %s", pkgImportPath)
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//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
 | 
				
			||||||
 | 
						}{
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			Path: &ast.BasicLit{
 | 
								pkgName:       "comm",
 | 
				
			||||||
				Value: pkgImportPath,
 | 
								pkgImportPath: "example.com/comm",
 | 
				
			||||||
 | 
								imports: []*ast.ImportSpec{
 | 
				
			||||||
 | 
									{
 | 
				
			||||||
 | 
										Path: &ast.BasicLit{
 | 
				
			||||||
 | 
											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 == test.pkgName {
 | 
				
			||||||
		if v.Name == pkgName {
 | 
									result = true
 | 
				
			||||||
			notLoadFlag = false
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							if test.expected != result {
 | 
				
			||||||
 | 
								t.Fatalf("load module error, expected: %v, result: %v", test.expected, result)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if notLoadFlag {
 | 
					 | 
				
			||||||
		t.Fatalf("failed to load module: %s", pkgImportPath)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user