Repository: goplus/gop
Pull request #1852 (2024-04-10 05:50:10)

export GetFileClassType

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/goplus/gop/pull/1852?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goplus) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 93.85%. Comparing base [(`570665d`)](https://app.codecov.io/gh/goplus/gop/commit/570665d69c60d04027c1e55a5833abc30d2dc8da?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goplus) to head [(`05fb87c`)](https://app.codecov.io/gh/goplus/gop/pull/1852?dropdown=coverage&src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goplus).


<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##             main    #1852      +/-   ##
==========================================
+ Coverage   93.23%   93.85%   +0.62%     
==========================================
  Files          40       39       -1     
  Lines       11533     9510    -2023     
==========================================
- Hits        10753     8926    -1827     
+ Misses        623      427     -196     
  Partials      157      157              
```



</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/goplus/gop/pull/1852?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goplus).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goplus).


Patch:
diff --git a/cl/classfile.go b/cl/classfile.go
index 2944b03d..06572f0c 100644
--- a/cl/classfile.go
+++ b/cl/classfile.go
@@ -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")
 }
diff --git a/load.go b/load.go
index 05e8337e..127d3397 100644
--- a/load.go
+++ b/load.go
@@ -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)
+}
+
+// -----------------------------------------------------------------------------


Test patch:
diff --git a/cl/builtin_test.go b/cl/builtin_test.go
index bdae13aa..ce7133df 100644
--- a/cl/builtin_test.go
+++ b/cl/builtin_test.go
@@ -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 {