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 724f070
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 0 deletions.
9 changes: 9 additions & 0 deletions cl/builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,15 @@ func TestClassNameAndExt(t *testing.T) {
}
}

func TestClassTypeForTest(t *testing.T) {
if name := ClassTypeForTest("foo"); name != "case_foo" {
t.Fatal("ClassTypeForTest error:", name)
}
if name := ClassTypeForTest("Foo"); name != "caseFoo" {
t.Fatal("ClassTypeForTest error:", name)
}
}

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

func ClassTypeForTest(classType string) string {
return casePrefix + testNameSuffix(classType)
}

func isGoxTestFile(ext string) bool {
return strings.HasSuffix(ext, "test.gox")
}
Expand Down
52 changes: 52 additions & 0 deletions mod.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2022 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package gop

import (
"strings"

"github.com/goplus/gop/ast"
"github.com/goplus/gop/cl"
"github.com/goplus/mod/gopmod"
)

// GetFileClassType get gop module file classType.
func GetFileClassType(mod *gopmod.Module, filename string, file *ast.File) (classType string, isTest bool, ok bool) {
if file.IsClass {
var ext string
classType, _, ext = cl.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 := mod.LookupClass(ext); ok {
classType = gt.Class
}
} else if isTest {
classType = cl.ClassTypeForTest(classType)
}
} else if strings.HasSuffix(filename, "_test.gop") {
isTest = true
}
return
}
86 changes: 86 additions & 0 deletions mod_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package gop_test

import (
"testing"

"github.com/goplus/gop"
"github.com/goplus/gop/ast"
"github.com/goplus/mod/gopmod"
"github.com/goplus/mod/modfile"
"github.com/goplus/mod/modload"
)

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},
}
m := modload.Default
m.Opt.Projects = append(m.Opt.Projects,
&modfile.Project{
Ext: ".yap", Class: "AppV2",
Works: []*modfile.Class{{Ext: ".yap", Class: "Handler"}},
PkgPaths: []string{"github.com/goplus/yap"}},
&modfile.Project{
Ext: "_yap.gox", Class: "App",
PkgPaths: []string{"github.com/goplus/yap"}},
&modfile.Project{
Ext: "_ytest.gox", Class: "App",
Works: []*modfile.Class{{Ext: "_ytest.gox", Class: "Case"}},
PkgPaths: []string{"github.com/goplus/yap/ytest", "testing"}},
)
mod := gopmod.New(m)
err := mod.ImportClasses()
if err != nil {
t.Fatal(err)
}

for _, test := range tests {
f := &ast.File{IsClass: test.isClass, IsNormalGox: test.isNormalGox, IsProj: test.isProj}
classType, isTest, found := gop.GetFileClassType(mod, test.fileName, f)
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)
}
}
}

0 comments on commit 724f070

Please sign in to comment.