refactor: move method call to json node

上级 3fae76a7
......@@ -72,6 +72,34 @@ func TestCocagoParser_ProcessFile(t *testing.T) {
}
}
func Test_Method_Call(t *testing.T) {
tests := []struct {
name string
fileName string
}{
{
"local_var_method_call",
"local_var_method_call",
},
{
"param_method_call",
"param_method_call",
},
{
"var_inside_method_with_call",
"var_inside_method_with_call",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
filePath := "testdata/method_call/" + tt.fileName
if got := testParser.ProcessFile(filePath + ".code"); !cocatest.JSONFileBytesEqual(got, filePath+".json") {
t.Errorf("ProcessFile() = %v, want %v", got, tt.fileName)
}
})
}
}
func getFilePath(name string) string {
return "testdata/node_infos/" + name
}
......@@ -116,84 +144,6 @@ func Test_NestedMethod(t *testing.T) {
g.Expect(cocatest.JSONFileBytesEqual(results, filePath+".json")).To(Equal(true))
}
// var call
func Test_VarMethodCall(t *testing.T) {
t.Parallel()
g := NewGomegaWithT(t)
results := testParser.ProcessString(`
package main
import (
"fmt"
"sync"
)
var l *sync.Mutex
func main() {
l = new(sync.Mutex)
l.Lock()
defer l.Unlock()
fmt.Println("1")
}
`, "call", nil)
calls := results.Members[0].FunctionNodes[0].FunctionCalls
fmt.Println(calls)
g.Expect(len(results.Fields)).To(Equal(1))
g.Expect(calls[0].Package).To(Equal("sync"))
g.Expect(calls[0].Type).To(Equal("sync.Mutex"))
g.Expect(len(calls)).To(Equal(3))
}
// should call local
func Test_LocalMethodCall(t *testing.T) {
t.Parallel()
g := NewGomegaWithT(t)
results := testParser.ProcessString(`
package main
import (
"fmt"
"sync"
)
func main() {
l := new(sync.Mutex)
l.Lock()
defer l.Unlock()
fmt.Println("1")
}
`, "call", nil)
calls := results.Members[0].FunctionNodes[0].FunctionCalls
g.Expect(calls[0].Package).To(Equal("sync"))
g.Expect(calls[0].Type).To(Equal("sync.Mutex"))
g.Expect(calls[2].Package).To(Equal("fmt"))
g.Expect(len(calls)).To(Equal(3))
}
func Test_ShouldSetParameterInterfaceToCallNodes(t *testing.T) {
t.Parallel()
g := NewGomegaWithT(t)
results := testParser.ProcessString(`
package api_domain
import "sort"
func SortAPIs(callAPIs []CallAPI) {
sort.Slice(callAPIs, func(i, j int) bool {
return callAPIs[i].Size < callAPIs[j].Size
})
}
`, "call", nil)
g.Expect(results.PackageName).To(Equal("api_domain"))
g.Expect(len(results.Members[0].FunctionNodes[0].FunctionCalls)).To(Equal(3))
}
func Test_RelatedImport(t *testing.T) {
t.Parallel()
g := NewGomegaWithT(t)
......
package main
import (
"fmt"
"sync"
)
var l *sync.Mutex
func main() {
l = new(sync.Mutex)
l.Lock()
defer l.Unlock()
fmt.Println("1")
}
{
"DataStructures": null,
"Fields": [
{
"Modifiers": null,
"TypeType": "sync.Mutex",
"TypeValue": "l"
}
],
"FullName": "testdata.method_call",
"Imports": [
{
"AsName": "",
"ImportName": "",
"Scope": "",
"Source": "fmt",
"UsageName": null
},
{
"AsName": "",
"ImportName": "",
"Scope": "",
"Source": "sync",
"UsageName": null
}
],
"Members": [
{
"AliasPackage": "main",
"DataStructID": "default",
"FileID": "testdata.method_call",
"FunctionNodes": [
{
"Annotations": null,
"Extension": null,
"FunctionCalls": [
{
"MethodName": "",
"NodeName": "new",
"Package": "testdata.method_call",
"Parameters": null,
"Position": {
"StartLine": 0,
"StartLinePosition": 0,
"StopLine": 0,
"StopLinePosition": 0
},
"Type": ""
},
{
"MethodName": "Lock",
"NodeName": "l",
"Package": "sync",
"Parameters": null,
"Position": {
"StartLine": 0,
"StartLinePosition": 0,
"StopLine": 0,
"StopLinePosition": 0
},
"Type": "sync.Mutex"
},
{
"MethodName": "Unlock",
"NodeName": "l",
"Package": "sync",
"Parameters": null,
"Position": {
"StartLine": 0,
"StartLinePosition": 0,
"StopLine": 0,
"StopLinePosition": 0
},
"Type": "sync.Mutex"
},
{
"MethodName": "Println",
"NodeName": "fmt",
"Package": "fmt",
"Parameters": [
{
"Modifiers": null,
"ParamName": "",
"Parameters": null,
"ReturnTypes": null,
"TypeType": "STRING",
"TypeValue": "\"1\""
}
],
"Position": {
"StartLine": 0,
"StartLinePosition": 0,
"StopLine": 0,
"StopLinePosition": 0
},
"Type": "fmt"
}
],
"InnerFunctions": null,
"InnerStructures": null,
"IsConstructor": false,
"IsReturnNull": false,
"Modifiers": null,
"MultipleReturns": null,
"Name": "main",
"Override": false,
"Parameters": null,
"Position": {
"StartLine": 0,
"StartLinePosition": 0,
"StopLine": 0,
"StopLinePosition": 0
},
"ReturnType": ""
}
],
"ID": "main:main",
"Name": "",
"Namespace": null,
"Position": {
"StartLine": 0,
"StartLinePosition": 0,
"StopLine": 0,
"StopLinePosition": 0
},
"Structures": null,
"Type": "method"
}
],
"PackageName": "main"
}
\ No newline at end of file
package api_domain
import "sort"
func SortAPIs(callAPIs []CallAPI) {
sort.Slice(callAPIs, func(i, j int) bool {
return callAPIs[i].Size < callAPIs[j].Size
})
}
{
"DataStructures": null,
"Fields": null,
"FullName": "testdata.method_call",
"Imports": [
{
"AsName": "",
"ImportName": "",
"Scope": "",
"Source": "sort",
"UsageName": null
}
],
"Members": [
{
"AliasPackage": "api_domain",
"DataStructID": "default",
"FileID": "testdata.method_call",
"FunctionNodes": [
{
"Annotations": null,
"Extension": null,
"FunctionCalls": [
{
"MethodName": "Size",
"NodeName": "CallAPI",
"Package": "testdata.method_call",
"Parameters": null,
"Position": {
"StartLine": 0,
"StartLinePosition": 0,
"StopLine": 0,
"StopLinePosition": 0
},
"Type": ""
},
{
"MethodName": "Size",
"NodeName": "CallAPI",
"Package": "testdata.method_call",
"Parameters": null,
"Position": {
"StartLine": 0,
"StartLinePosition": 0,
"StopLine": 0,
"StopLinePosition": 0
},
"Type": ""
},
{
"MethodName": "Slice",
"NodeName": "sort",
"Package": "sort",
"Parameters": [
{
"Modifiers": null,
"ParamName": "",
"Parameters": null,
"ReturnTypes": null,
"TypeType": "var",
"TypeValue": "callAPIs"
},
{
"Modifiers": null,
"ParamName": "",
"Parameters": null,
"ReturnTypes": null,
"TypeType": "",
"TypeValue": ""
}
],
"Position": {
"StartLine": 0,
"StartLinePosition": 0,
"StopLine": 0,
"StopLinePosition": 0
},
"Type": "sort"
}
],
"InnerFunctions": null,
"InnerStructures": null,
"IsConstructor": false,
"IsReturnNull": false,
"Modifiers": null,
"MultipleReturns": null,
"Name": "SortAPIs",
"Override": false,
"Parameters": [
{
"Modifiers": null,
"ParamName": "callAPIs",
"Parameters": null,
"ReturnTypes": null,
"TypeType": "ArrayType",
"TypeValue": "CallAPI"
}
],
"Position": {
"StartLine": 0,
"StartLinePosition": 0,
"StopLine": 0,
"StopLinePosition": 0
},
"ReturnType": ""
}
],
"ID": "api_domain:SortAPIs",
"Name": "",
"Namespace": null,
"Position": {
"StartLine": 0,
"StartLinePosition": 0,
"StopLine": 0,
"StopLinePosition": 0
},
"Structures": null,
"Type": "method"
}
],
"PackageName": "api_domain"
}
\ No newline at end of file
package main
import (
"fmt"
"sync"
)
func main() {
l := new(sync.Mutex)
l.Lock()
defer l.Unlock()
fmt.Println("1")
}
{
"DataStructures": null,
"Fields": null,
"FullName": "testdata.method_call",
"Imports": [
{
"AsName": "",
"ImportName": "",
"Scope": "",
"Source": "fmt",
"UsageName": null
},
{
"AsName": "",
"ImportName": "",
"Scope": "",
"Source": "sync",
"UsageName": null
}
],
"Members": [
{
"AliasPackage": "main",
"DataStructID": "default",
"FileID": "testdata.method_call",
"FunctionNodes": [
{
"Annotations": null,
"Extension": null,
"FunctionCalls": [
{
"MethodName": "",
"NodeName": "new",
"Package": "testdata.method_call",
"Parameters": null,
"Position": {
"StartLine": 0,
"StartLinePosition": 0,
"StopLine": 0,
"StopLinePosition": 0
},
"Type": ""
},
{
"MethodName": "Lock",
"NodeName": "l",
"Package": "sync",
"Parameters": null,
"Position": {
"StartLine": 0,
"StartLinePosition": 0,
"StopLine": 0,
"StopLinePosition": 0
},
"Type": "sync.Mutex"
},
{
"MethodName": "Unlock",
"NodeName": "l",
"Package": "sync",
"Parameters": null,
"Position": {
"StartLine": 0,
"StartLinePosition": 0,
"StopLine": 0,
"StopLinePosition": 0
},
"Type": "sync.Mutex"
},
{
"MethodName": "Println",
"NodeName": "fmt",
"Package": "fmt",
"Parameters": [
{
"Modifiers": null,
"ParamName": "",
"Parameters": null,
"ReturnTypes": null,
"TypeType": "STRING",
"TypeValue": "\"1\""
}
],
"Position": {
"StartLine": 0,
"StartLinePosition": 0,
"StopLine": 0,
"StopLinePosition": 0
},
"Type": "fmt"
}
],
"InnerFunctions": null,
"InnerStructures": null,
"IsConstructor": false,
"IsReturnNull": false,
"Modifiers": null,
"MultipleReturns": null,
"Name": "main",
"Override": false,
"Parameters": null,
"Position": {
"StartLine": 0,
"StartLinePosition": 0,
"StopLine": 0,
"StopLinePosition": 0
},
"ReturnType": ""
}
],
"ID": "main:main",
"Name": "",
"Namespace": null,
"Position": {
"StartLine": 0,
"StartLinePosition": 0,
"StopLine": 0,
"StopLinePosition": 0
},
"Structures": null,
"Type": "method"
}
],
"PackageName": "main"
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册