Skip to content

Commit 516741b

Browse files
committed
analyzer: add AST analyzer tests
1 parent 8481373 commit 516741b

File tree

4 files changed

+177
-1
lines changed

4 files changed

+177
-1
lines changed

pkg/analyzer/completion_item.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,8 @@ type MarkdownString struct {
5454
// Value is string contents
5555
Value string `json:"value"`
5656
}
57+
58+
// NewMarkdownString returns markdown string
59+
func NewMarkdownString(val string) MarkdownString {
60+
return MarkdownString{Value: val}
61+
}

pkg/analyzer/package_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package analyzer
22

33
import (
44
"github.com/stretchr/testify/require"
5+
"github.com/x1unix/go-playground/pkg/testutil"
56
"testing"
67
)
78

@@ -83,5 +84,10 @@ func TestPackage_HasChildren(t *testing.T) {
8384
}
8485

8586
func TestPackage_AllSymbols(t *testing.T) {
86-
87+
SetRoot("testdata")
88+
SetLogger(testutil.GetLogger(t).Desugar())
89+
want := examplePackageSummary
90+
pkg := Package{Path: "example"}
91+
require.NoError(t, pkg.Analyze())
92+
require.Equal(t, want, pkg.PackageSummary)
8793
}

pkg/analyzer/scanner_test.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package analyzer
2+
3+
import (
4+
"path/filepath"
5+
"testing"
6+
7+
"github.com/stretchr/testify/require"
8+
"github.com/x1unix/go-playground/pkg/testutil"
9+
)
10+
11+
// should be in sync with "testdata/src" !!!
12+
var examplePackageSummary = PackageSummary{
13+
Functions: newSymbolIndex([]*CompletionItem{
14+
{
15+
Label: "SomeFunc",
16+
Kind: Function,
17+
Detail: "func(val string) string",
18+
InsertText: "SomeFunc()",
19+
Documentation: NewMarkdownString(
20+
"SomeFunc is test function sample\nwith doc that contains code sample:" +
21+
"\n\n```\n\ta := \"foo\"\n\tfmt.PrintLn(a)\n\n```\nend\n\n",
22+
),
23+
},
24+
{
25+
Label: "ChanArrFunc",
26+
Kind: Function,
27+
Detail: "func(items ...string) chan string",
28+
InsertText: "ChanArrFunc()",
29+
Documentation: NewMarkdownString("ChanArrFunc is stub\n\n"),
30+
},
31+
{
32+
Label: "SomeFunc2",
33+
Kind: Function,
34+
Detail: "func(m map[string]interface{}, v *int) []interface{}",
35+
InsertText: "SomeFunc2()",
36+
Documentation: NewMarkdownString("SomeFunc2 is func stub\n\n"),
37+
},
38+
{
39+
Label: "IfaceFunc",
40+
Kind: Function,
41+
Detail: "func() Action",
42+
InsertText: "IfaceFunc()",
43+
Documentation: NewMarkdownString("IfaceFunc is stub with unterminated code block\n```\n\t2 + 2\n\n```\n"),
44+
},
45+
{
46+
Label: "FuncReturnFuncAndIface",
47+
Kind: Function,
48+
Detail: "func() (func() (string, error), interface{\nf func()\n})",
49+
InsertText: "FuncReturnFuncAndIface()",
50+
Documentation: NewMarkdownString("FuncReturnFuncAndIface is stub\n\n"),
51+
},
52+
}),
53+
Values: newSymbolIndex([]*CompletionItem{
54+
{
55+
Label: "SomeConst",
56+
Kind: Constant,
57+
Detail: "SomeConst",
58+
Documentation: "",
59+
InsertText: "SomeConst",
60+
},
61+
{
62+
Label: "SomeVar",
63+
Kind: Variable,
64+
Detail: "SomeVar",
65+
Documentation: "SomeVar is public var example\n",
66+
InsertText: "SomeVar",
67+
},
68+
{
69+
Label: "AnonIfaceVar",
70+
Kind: Variable,
71+
Detail: "AnonIfaceVar",
72+
Documentation: "AnonIfaceVar is var with anonymous interface sample\n",
73+
InsertText: "AnonIfaceVar",
74+
},
75+
}),
76+
}
77+
78+
func TestPackageScanner_Scan(t *testing.T) {
79+
want := examplePackageSummary
80+
tempRoot := testutil.TestdataPath("src")
81+
SetLogger(testutil.GetLogger(t).Desugar())
82+
s := NewPackageScanner("example", filepath.Join(tempRoot, "example"), false)
83+
got, err := s.Scan()
84+
require.NoError(t, err)
85+
require.Equal(t, want, got)
86+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Package example is Go package sample for scanner tests
2+
//
3+
// See: pkg/analyzer/scanner_test.go
4+
package example
5+
6+
// SomeConst is const example
7+
const SomeConst = 32
8+
9+
var (
10+
// SomeVar is public var example
11+
SomeVar = "someVar"
12+
13+
// AnonIfaceVar is var with anonymous interface sample
14+
AnonIfaceVar interface {
15+
// Foo does something
16+
Foo()
17+
}
18+
)
19+
20+
var privateVar = "privateVar"
21+
22+
// SomeType is public struct example
23+
type SomeType struct{}
24+
25+
// Any is type sample
26+
type Any interface{}
27+
28+
// Action is interface sample
29+
type Action interface {
30+
// Do is interface method sample
31+
Do() error
32+
}
33+
34+
// Method is struct method sample
35+
func (st SomeType) Method() {}
36+
37+
// SomeInterface is interface example
38+
type SomeInterface interface{}
39+
40+
// privateType is private type example
41+
type privateType struct {
42+
foo int
43+
}
44+
45+
// SomeFunc is test function sample
46+
// with doc that contains code sample:
47+
//
48+
// a := "foo"
49+
// fmt.PrintLn(a)
50+
//
51+
// end
52+
func SomeFunc(val string) string {
53+
return "foo" + val
54+
}
55+
56+
// ChanArrFunc is stub
57+
func ChanArrFunc(items ...string) chan string {
58+
ch := make(chan string, len(items))
59+
for _, i := range items {
60+
ch <- i
61+
}
62+
return ch
63+
}
64+
65+
// SomeFunc2 is func stub
66+
func SomeFunc2(m map[string]interface{}, v *int) []interface{} {
67+
return []interface{}{m, v}
68+
}
69+
70+
// IfaceFunc is stub with unterminated code block
71+
// 2 + 2
72+
func IfaceFunc() Action {
73+
return nil
74+
}
75+
76+
// FuncReturnFuncAndIface is stub
77+
func FuncReturnFuncAndIface() (func() (string, error), interface{ f() }) {
78+
return nil, nil
79+
}

0 commit comments

Comments
 (0)