Skip to content

Commit

Permalink
cl: export GetFileClassType
Browse files Browse the repository at this point in the history
  • Loading branch information
visualfc committed Apr 10, 2024
1 parent 570665d commit 890bdb4
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
68 changes: 68 additions & 0 deletions cl/builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,74 @@ 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", "AppV2", 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 *modfile.Project, ok bool) {
switch ext {
case "_yap.gox", ".yap":
return &modfile.Project{
Ext: "_yap.gox", Class: "AppV2",
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 {
classType, isTest, found := GetFileClassType(&ast.File{IsClass: test.isClass, IsNormalGox: test.isNormalGox, IsProj: test.isProj}, 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 @@ -100,6 +100,33 @@ func isGoxTestFile(ext string) bool {
return strings.HasSuffix(ext, "test.gox")
}

// GetFileClassType get ast.File classType by filename.
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 loadClass(ctx *pkgCtx, pkg *gogen.Package, file string, f *ast.File, conf *Config) *gmxProject {
tname, clsfile, ext := ClassNameAndExt(file)
gt, ok := conf.LookupClass(ext)
Expand Down

0 comments on commit 890bdb4

Please sign in to comment.