Skip to content

Commit

Permalink
add gop.GetFileClassType
Browse files Browse the repository at this point in the history
  • Loading branch information
visualfc committed Apr 12, 2024
1 parent 5192883 commit 38adad7
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
74 changes: 74 additions & 0 deletions cl/builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,80 @@ func TestClassNameAndExt(t *testing.T) {
}
}

func TestFileClassType(t *testing.T) {
type testData struct {
isClass bool
isNormalGox bool
isProj bool
fileName string
classType string
isTest bool
found bool
}
tests := []*testData{
{false, false, false, "abc.gop", "", false, false},
{false, false, false, "abc_test.gop", "", true, false},

{true, true, false, "abc.gox", "abc", false, true},
{true, true, false, "Abc.gox", "Abc", false, true},
{true, true, false, "abc_demo.gox", "abc", false, true},
{true, true, false, "Abc_demo.gox", "Abc", false, true},

{true, true, false, "main.gox", "_main", false, true},
{true, true, false, "main_demo.gox", "_main", false, true},
{true, true, false, "abc_xtest.gox", "abc", false, true},
{true, true, false, "main_xtest.gox", "_main", false, true},

{true, true, false, "abc_test.gox", "case_abc", true, true},
{true, true, false, "Abc_test.gox", "caseAbc", true, true},
{true, true, false, "main_test.gox", "case_main", true, true},

{true, false, false, "get.yap", "get", false, true},
{true, false, false, "get_p_#id.yap", "get_p_id", false, true},
{true, false, true, "main.yap", "AppV2", false, true},

{true, false, false, "abc_yap.gox", "abc", false, true},
{true, false, false, "Abc_yap.gox", "Abc", false, true},
{true, false, true, "main_yap.gox", "App", false, true},

{true, false, false, "abc_ytest.gox", "case_abc", true, true},
{true, false, false, "Abc_ytest.gox", "caseAbc", true, true},
{true, false, true, "main_ytest.gox", "App", true, true},
}
lookupClass := func(ext string) (c *Project, ok bool) {
switch ext {
case ".yap":
return &modfile.Project{
Ext: ".yap", Class: "AppV2",
Works: []*modfile.Class{{Ext: ".yap", Class: "Handler"}},
PkgPaths: []string{"github.com/goplus/yap"}}, true
case "_yap.gox":
return &modfile.Project{
Ext: "_yap.gox", Class: "App",
PkgPaths: []string{"github.com/goplus/yap"}}, true
case "_ytest.gox":
return &modfile.Project{
Ext: "_ytest.gox", Class: "App",
Works: []*modfile.Class{{Ext: "_ytest.gox", Class: "Case"}},
PkgPaths: []string{"github.com/goplus/yap/ytest", "testing"}}, true
}
return
}
for _, test := range tests {
f := &ast.File{IsClass: test.isClass, IsNormalGox: test.isNormalGox, IsProj: test.isProj}
classType, isTest, found := GetFileClassType(f, test.fileName, lookupClass)
if found != test.found {
t.Fatalf("%v found classType want %v, got %v.", test.fileName, test.found, found)
}
if isTest != test.isTest {
t.Fatalf("%v check classType isTest want %v, got %v.", test.fileName, test.isTest, isTest)
}
if classType != test.classType {
t.Fatalf("%v getClassType want %v, got %v.", test.fileName, test.classType, classType)
}
}
}

func TestErrMultiStarRecv(t *testing.T) {
defer func() {
if e := recover(); e == nil {
Expand Down
27 changes: 27 additions & 0 deletions cl/classfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,33 @@ func ClassNameAndExt(file string) (name, clsfile, ext string) {
return
}

// GetFileClassType get ast.File classType
func GetFileClassType(file *ast.File, filename string, lookupClass func(ext string) (c *Project, ok bool)) (classType string, isTest bool, ok bool) {
if file.IsClass {
var ext string
classType, _, ext = ClassNameAndExt(filename)
ok = true
if file.IsNormalGox {
isTest = strings.HasSuffix(ext, "_test.gox")
if !isTest && classType == "main" {
classType = "_main"
}
} else {
isTest = strings.HasSuffix(ext, "test.gox")
}
if file.IsProj {
if gt, ok := lookupClass(ext); ok {
classType = gt.Class
}
} else if isTest {
classType = casePrefix + testNameSuffix(classType)
}
} else if strings.HasSuffix(filename, "_test.gop") {
isTest = true
}
return
}

func isGoxTestFile(ext string) bool {
return strings.HasSuffix(ext, "test.gox")
}
Expand Down
7 changes: 7 additions & 0 deletions load.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,3 +403,10 @@ var (
)

// -----------------------------------------------------------------------------

// GetFileClassType get gop module file classType.
func GetFileClassType(mod *gopmod.Module, file *ast.File, filename string) (classType string, isTest bool, ok bool) {
return cl.GetFileClassType(file, filename, mod.LookupClass)
}

// -----------------------------------------------------------------------------

0 comments on commit 38adad7

Please sign in to comment.