Skip to content

Commit 7f8a235

Browse files
committed
update:GoxTestClassType
1 parent 459c98a commit 7f8a235

File tree

3 files changed

+97
-36
lines changed

3 files changed

+97
-36
lines changed

gopls/internal/lsp/source/code_lens_gox.go

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"regexp"
1313
"strings"
1414

15-
"github.com/goplus/gop"
1615
"github.com/goplus/gop/ast"
1716
"github.com/goplus/gop/parser"
1817
"golang.org/x/tools/gopls/internal/goxls"
@@ -186,15 +185,11 @@ func gopCommandCodeLens(ctx context.Context, snapshot Snapshot, fh FileHandle) (
186185
if strings.HasSuffix(filename, "_test.go") || strings.HasSuffix(filename, "_test.gop") {
187186
return nil, nil
188187
}
189-
mod, err := snapshot.GopModForFile(ctx, fh.URI())
190-
if err != nil {
191-
return nil, err
192-
}
193188
pgf, err := snapshot.ParseGop(ctx, fh, parser.PackageClauseOnly)
194189
if err != nil {
195190
return nil, err
196191
}
197-
if classType, isTest := gop.GetFileClassType(mod, pgf.File, filename); isTest {
192+
if classType, isGoxTest := GoxTestClassType(pgf.File, filename); isGoxTest {
198193
return goxTestCodeLens(pgf, classType)
199194
}
200195
if pgf.File.Name.Name == "main" {
@@ -216,35 +211,28 @@ func gopCommandCodeLens(ctx context.Context, snapshot Snapshot, fh FileHandle) (
216211
}
217212

218213
func goxTestCodeLens(pgf *ParsedGopFile, classType string) ([]protocol.CodeLens, error) {
219-
if pgf.File.Name.Name == "main" {
220-
rng, err := pgf.PosRange(pgf.File.Pos(), pgf.File.Pos())
221-
if err != nil {
222-
return nil, err
223-
}
224-
pattern := regexp.MustCompile(`^case(?:_)?`) //goxls: remove case or case_
225-
if pattern.MatchString(classType) {
226-
classType = pattern.ReplaceAllString(classType, "")
227-
}
228-
args, err := command.MarshalArgs(
229-
map[string]string{
230-
"functionName": "Test_" + classType,
231-
},
232-
)
233-
if err != nil {
234-
return nil, err
235-
}
236-
codelens := []protocol.CodeLens{
237-
{Range: rng, Command: &protocol.Command{
238-
Title: "run test package",
239-
Command: "gop.test.package",
240-
}},
241-
{Range: rng, Command: &protocol.Command{ // goxls: add test cursor as test file
242-
Title: "run file tests",
243-
Command: "gop.test.cursor",
244-
Arguments: args,
245-
}},
246-
}
247-
return codelens, nil
214+
rng, err := pgf.PosRange(pgf.File.Pos(), pgf.File.Pos())
215+
if err != nil {
216+
return nil, err
248217
}
249-
return nil, nil
218+
args, err := command.MarshalArgs(
219+
map[string]string{
220+
"functionName": "Test_" + classType,
221+
},
222+
)
223+
if err != nil {
224+
return nil, err
225+
}
226+
codelens := []protocol.CodeLens{
227+
{Range: rng, Command: &protocol.Command{
228+
Title: "run test package",
229+
Command: "gop.test.package",
230+
}},
231+
{Range: rng, Command: &protocol.Command{ // goxls: add test cursor as test file
232+
Title: "run file tests",
233+
Command: "gop.test.cursor",
234+
Arguments: args,
235+
}},
236+
}
237+
return codelens, nil
250238
}

gopls/internal/lsp/source/util_gox.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"strings"
1212

1313
"github.com/goplus/gop/ast"
14+
"github.com/goplus/gop/cl"
1415
"github.com/goplus/gop/printer"
1516
"github.com/goplus/gop/token"
1617
"github.com/goplus/gop/x/typesutil"
@@ -287,3 +288,16 @@ func gopEmbeddedIdent(x ast.Expr) *ast.Ident {
287288
}
288289
return nil
289290
}
291+
292+
func GoxTestClassType(file *ast.File, filename string) (classType string, isGoxTest bool) {
293+
if file.IsClass {
294+
var ext string
295+
classType, _, ext = cl.ClassNameAndExt(filename)
296+
if file.IsNormalGox {
297+
isGoxTest = strings.HasSuffix(ext, "_test.gox")
298+
} else {
299+
isGoxTest = strings.HasSuffix(ext, "test.gox")
300+
}
301+
}
302+
return
303+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package source
2+
3+
import (
4+
"github.com/goplus/gop/ast"
5+
6+
"testing"
7+
)
8+
9+
func TestGoxTestClassType(t *testing.T) {
10+
type testData struct {
11+
isClass bool
12+
isNormalGox bool
13+
isProj bool
14+
fileName string
15+
classType string
16+
isGoxTest bool
17+
}
18+
tests :=
19+
[]*testData{
20+
{false, false, false, "abc.gop", "", false},
21+
{false, false, false, "abc_test.gop", "", false},
22+
23+
{true, true, false, "abc.gox", "abc", false},
24+
{true, true, false, "Abc.gox", "Abc", false},
25+
{true, true, false, "abc_demo.gox", "abc", false},
26+
{true, true, false, "Abc_demo.gox", "Abc", false},
27+
28+
{true, false, false, "get.yap", "get", false},
29+
{true, false, false, "get_p_#id.yap", "get_p_id", false},
30+
{true, false, true, "main.yap", "main", false},
31+
32+
{true, true, false, "main.gox", "main", false},
33+
{true, true, false, "main_demo.gox", "main", false},
34+
{true, true, false, "abc_xtest.gox", "abc", false},
35+
{true, true, false, "main_xtest.gox", "main", false},
36+
37+
{true, true, false, "abc_test.gox", "abc", true},
38+
{true, true, false, "Abc_test.gox", "Abc", true},
39+
{true, true, false, "main_test.gox", "main", true},
40+
41+
{true, false, false, "abc_yap.gox", "abc", false},
42+
{true, false, false, "Abc_yap.gox", "Abc", false},
43+
{true, false, true, "main_yap.gox", "main", false},
44+
45+
{true, false, false, "abc_ytest.gox", "abc", true},
46+
{true, false, false, "Abc_ytest.gox", "Abc", true},
47+
{true, false, true, "main_ytest.gox", "main", true},
48+
}
49+
for _, test := range tests {
50+
f := &ast.File{IsClass: test.isClass, IsNormalGox: test.isNormalGox, IsProj: test.isProj}
51+
classType, isGoxTest := GoxTestClassType(f, test.fileName)
52+
if isGoxTest != test.isGoxTest {
53+
t.Fatalf("%v check classType isTest want %v, got %v.", test.fileName, test.isGoxTest, isGoxTest)
54+
}
55+
if classType != test.classType {
56+
t.Fatalf("%v getClassType want %v, got %v.", test.fileName, test.classType, classType)
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)