cocago_parser_test.go 1.5 KB
Newer Older
1 2 3 4
package cocago

import (
	. "github.com/onsi/gomega"
P
Phodal Huang 已提交
5
	"github.com/phodal/coca/cocatest"
P
Phodal Huang 已提交
6
	"os"
7 8 9
	"testing"
)

P
Phodal Huang 已提交
10 11 12 13 14 15 16 17 18 19 20
func TestMain(m *testing.M) {
	setup()
	code := m.Run()
	shutdown()
	os.Exit(code)
}

var testParser *CocagoParser

func setup() {
	testParser = NewCocagoParser()
P
Phodal Huang 已提交
21
	testParser.SetOutput(true)
P
Phodal Huang 已提交
22 23 24 25 26 27
}

func shutdown() {
	testParser = nil
}

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
func TestCocagoParser_ProcessFile(t *testing.T) {
	tests := []struct {
		name     string
		fileName string
	}{
		{
			"data_struct_property",
			"data_struct_property",
		},
		{
			"struct_with_func",
			"struct_with_func",
		},
		{
			"struct_with_func_decl",
			"struct_with_func_decl",
		},
		{
			"struct_type_zero",
			"struct_type_zero",
		},
		{
			"normal_method",
			"normal_method",
		},
		{
			"hello_world",
			"hello_world",
		},
		{
			"basic_interface",
			"basic_interface",
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
64
			filePath := getFilePath(tt.fileName)
P
Phodal Huang 已提交
65
			if got := testParser.ProcessFile(filePath + ".code"); !cocatest.JSONFileBytesEqual(got, filePath+".json") {
66 67 68 69
				t.Errorf("ProcessFile() = %v, want %v", got, tt.fileName)
			}
		})
	}
70
}
71

72 73 74 75
func getFilePath(name string) string {
	return "testdata/node_infos/" + name
}

P
Phodal Huang 已提交
76
// todo: support it
77 78 79 80
func Test_NestedMethod(t *testing.T) {
	t.Parallel()
	g := NewGomegaWithT(t)

81 82 83
	filePath := getFilePath("nested_method")
	results := testParser.ProcessFile(filePath + ".code")
	g.Expect(cocatest.JSONFileBytesEqual(results, filePath+".json")).To(Equal(true))
P
Phodal Huang 已提交
84
}