tbs_app_test.go 3.0 KB
Newer Older
P
Phodal Huang 已提交
1 2 3
package tbs

import (
P
Phodal Huang 已提交
4
	"fmt"
P
Phodal Huang 已提交
5 6 7 8 9
	. "github.com/onsi/gomega"
	"github.com/phodal/coca/core/adapter"
	"github.com/phodal/coca/core/adapter/call"
	"github.com/phodal/coca/core/models"
	"github.com/phodal/coca/core/support"
P
Phodal Huang 已提交
10
	"path/filepath"
P
Phodal Huang 已提交
11 12 13
	"testing"
)

P
Phodal Huang 已提交
14
func TestTbsApp_EmptyTest(t *testing.T) {
P
Phodal Huang 已提交
15 16
	g := NewGomegaWithT(t)
	codePath := "../../../_fixtures/tbs/code/EmptyTest.java"
P
Phodal Huang 已提交
17
	codePath = filepath.FromSlash(codePath)
P
Phodal Huang 已提交
18

P
Phodal Huang 已提交
19 20
	result := buildTbsResult(codePath)

P
Phodal Huang 已提交
21
	g.Expect(result[0].Line).To(Equal(8))
P
Phodal Huang 已提交
22 23 24 25 26 27
	g.Expect(result[0].Type).To(Equal("EmptyTest"))
}

func TestTbsApp_IgnoreTest(t *testing.T) {
	g := NewGomegaWithT(t)
	codePath := "../../../_fixtures/tbs/code/IgnoreTest.java"
P
Phodal Huang 已提交
28
	codePath = filepath.FromSlash(codePath)
P
Phodal Huang 已提交
29

P
Phodal Huang 已提交
30 31
	result := buildTbsResult(codePath)

32
	g.Expect(len(result)).To(Equal(1))
P
Phodal Huang 已提交
33
	g.Expect(result[0].Line).To(Equal(0))
P
Phodal Huang 已提交
34 35 36
	g.Expect(result[0].Type).To(Equal("IgnoreTest"))
}

P
Phodal Huang 已提交
37 38 39
func TestTbsApp_RedundantPrintTest(t *testing.T) {
	g := NewGomegaWithT(t)
	codePath := "../../../_fixtures/tbs/code/RedundantPrintTest.java"
P
Phodal Huang 已提交
40
	codePath = filepath.FromSlash(codePath)
P
Phodal Huang 已提交
41

P
Phodal Huang 已提交
42 43
	result := buildTbsResult(codePath)

P
Phodal Huang 已提交
44
	g.Expect(result[0].Line).To(Equal(9))
P
Phodal Huang 已提交
45 46 47
	g.Expect(result[0].Type).To(Equal("RedundantPrintTest"))
}

P
Phodal Huang 已提交
48 49 50
func TestTbsApp_SleepyTest(t *testing.T) {
	g := NewGomegaWithT(t)
	codePath := "../../../_fixtures/tbs/code/SleepyTest.java"
P
Phodal Huang 已提交
51
	codePath = filepath.FromSlash(codePath)
P
Phodal Huang 已提交
52

P
Phodal Huang 已提交
53 54
	result := buildTbsResult(codePath)

P
Phodal Huang 已提交
55
	g.Expect(result[0].Line).To(Equal(8))
P
Phodal Huang 已提交
56 57 58
	g.Expect(result[0].Type).To(Equal("SleepyTest"))
}

P
Phodal Huang 已提交
59 60 61 62
func TestTbsApp_DuplicateAssertTest(t *testing.T) {
	g := NewGomegaWithT(t)
	codePath := "../../../_fixtures/tbs/code/DuplicateAssertTest.java"
	codePath = filepath.FromSlash(codePath)
P
Phodal Huang 已提交
63

P
Phodal Huang 已提交
64 65
	result := buildTbsResult(codePath)

P
Phodal Huang 已提交
66
	g.Expect(result[0].Line).To(Equal(23))
P
Phodal Huang 已提交
67 68 69
	g.Expect(result[0].Type).To(Equal("DuplicateAssertTest"))
}

P
Phodal Huang 已提交
70 71 72 73
func TestTbsApp_UnknownTest(t *testing.T) {
	g := NewGomegaWithT(t)
	codePath := "../../../_fixtures/tbs/code/UnknownTest.java"
	codePath = filepath.FromSlash(codePath)
P
Phodal Huang 已提交
74

P
Phodal Huang 已提交
75 76 77
	result := buildTbsResult(codePath)

	g.Expect(result[0].Type).To(Equal("EmptyTest"))
P
Phodal Huang 已提交
78
	g.Expect(result[0].Line).To(Equal(7))
P
Phodal Huang 已提交
79 80 81
	g.Expect(result[1].Type).To(Equal("UnknownTest"))
}

P
Phodal Huang 已提交
82 83 84 85 86 87 88 89 90 91 92
func TestTbsApp_CreatorNotUnknownTest(t *testing.T) {
	g := NewGomegaWithT(t)
	codePath := "../../../_fixtures/tbs/code/CreatorNotUnknownTest.java"
	codePath = filepath.FromSlash(codePath)

	result := buildTbsResult(codePath)

	// todo: fix bug
	g.Expect(len(result)).To(Equal(3))
}

P
Phodal Huang 已提交
93
func buildTbsResult(codePath string) []TestBadSmell {
P
Phodal Huang 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
	files := support.GetJavaTestFiles(codePath)
	var identifiers []models.JIdentifier

	identifiers = adapter.LoadTestIdentify(files)
	identifiersMap := adapter.BuildIdentifierMap(identifiers)

	var classes []string = nil
	for _, node := range identifiers {
		classes = append(classes, node.Package+"."+node.ClassName)
	}

	analysisApp := call.NewJavaCallApp()
	classNodes := analysisApp.AnalysisFiles(identifiers, files, classes)

	app := NewTbsApp()
	result := app.AnalysisPath(classNodes, identifiersMap)
P
Phodal Huang 已提交
110
	return result
P
Phodal Huang 已提交
111
}