未验证 提交 31dad88a 编写于 作者: P Phodal Huang

refactor: tbs behavior to jmethod

上级 537660bd
......@@ -13,7 +13,7 @@ func BuildCallMap(parserDeps []domain.JClassNode) map[string]int {
for _, clz := range parserDeps {
for _, method := range clz.Methods {
for _, call := range method.MethodCalls {
callMethod := call.BuilFullMethodName()
callMethod := call.BuildFullMethodName()
if _, ok := projectMethods[callMethod]; ok {
if callMap[callMethod] == 0 {
callMap[callMethod] = 1
......
......@@ -47,7 +47,7 @@ func BuildRCallMethodMap(parserDeps []domain.JClassNode, projectMaps map[string]
var caller = method.BuildFullMethodName(clz)
for _, call := range method.MethodCalls {
if call.Class != "" {
callee := call.BuilFullMethodName()
callee := call.BuildFullMethodName()
if projectMaps[callee] < 1 {
continue
}
......
......@@ -25,7 +25,7 @@ func (a TbsApp) AnalysisPath(deps []domain.JClassNode, identifiersMap map[string
callMethodMap := buildCallMethodMap(deps)
for _, clz := range deps {
for _, method := range clz.Methods {
if !isTest(method) {
if !method.IsJunitTest() {
continue
}
......@@ -44,7 +44,7 @@ func (a TbsApp) AnalysisPath(deps []domain.JClassNode, identifiersMap map[string
continue
}
methodCallMap[getMethodCallFullPath(methodCall)] = append(methodCallMap[getMethodCallFullPath(methodCall)], methodCall)
methodCallMap[methodCall.BuildFullMethodName()] = append(methodCallMap[methodCall.BuildFullMethodName()], methodCall)
checkRedundantPrintTest(clz.Path, methodCall, &results, &testType)
checkSleepyTest(clz.Path, methodCall, method, &results, &testType)
......@@ -72,7 +72,7 @@ func buildCallMethodMap(deps []domain.JClassNode) map[string]domain.JMethod {
var callMethodMap = make(map[string]domain.JMethod)
for _, clz := range deps {
for _, method := range clz.Methods {
callMethodMap[clz.Package+"."+clz.Class+"."+method.Name] = method
callMethodMap[method.BuildFullMethodName(clz)] = method
}
}
return callMethodMap
......@@ -82,7 +82,7 @@ func updateMethodCallsForSelfCall(method domain.JMethod, clz domain.JClassNode,
currentMethodCalls := method.MethodCalls
for _, methodCall := range currentMethodCalls {
if methodCall.Class == clz.Class {
jMethod := callMethodMap[getMethodCallFullPath(methodCall)]
jMethod := callMethodMap[methodCall.BuildFullMethodName()]
if jMethod.Name != "" {
currentMethodCalls = append(currentMethodCalls, jMethod.MethodCalls...)
}
......@@ -119,16 +119,6 @@ func hasAssertion(methodName string) bool {
return false
}
func isTest(method domain.JMethod) bool {
var isTest = false
for _, annotation := range method.Annotations {
if annotation.QualifiedName == "Test" || annotation.QualifiedName == "Ignore" {
isTest = true
}
}
return isTest
}
func checkUnknownTest(clz domain.JClassNode, method domain.JMethod, results *[]TestBadSmell, testType *string) {
*testType = "UnknownTest"
tbs := *&TestBadSmell{
......@@ -165,12 +155,8 @@ func checkDuplicateAssertTest(clz domain.JClassNode, results *[]TestBadSmell, me
}
}
func getMethodCallFullPath(methodCall domain.JMethodCall) string {
return methodCall.Package + "." + methodCall.Class + "." + methodCall.MethodName
}
func checkSleepyTest(path string, method domain.JMethodCall, jMethod domain.JMethod, results *[]TestBadSmell, testType *string) {
if method.MethodName == "sleep" && method.Class == "Thread" {
if method.IsThreadSleep() {
*testType = "SleepyTest"
tbs := *&TestBadSmell{
FileName: path,
......@@ -184,7 +170,7 @@ func checkSleepyTest(path string, method domain.JMethodCall, jMethod domain.JMet
}
func checkRedundantPrintTest(path string, mCall domain.JMethodCall, results *[]TestBadSmell, testType *string) {
if mCall.Class == "System.out" && (mCall.MethodName == "println" || mCall.MethodName == "printf" || mCall.MethodName == "print") {
if mCall.IsSystemOutput() {
*testType = "RedundantPrintTest"
tbs := *&TestBadSmell{
FileName: path,
......@@ -198,7 +184,7 @@ func checkRedundantPrintTest(path string, mCall domain.JMethodCall, results *[]T
}
func checkEmptyTest(path string, annotation domain.Annotation, results *[]TestBadSmell, method domain.JMethod, testType *string) {
if annotation.QualifiedName == "Test" {
if annotation.IsTest() {
if len(method.MethodCalls) <= 1 {
*testType = "EmptyTest"
tbs := *&TestBadSmell{
......@@ -214,7 +200,7 @@ func checkEmptyTest(path string, annotation domain.Annotation, results *[]TestBa
}
func checkIgnoreTest(clzPath string, annotation domain.Annotation, results *[]TestBadSmell, testType *string) {
if annotation.QualifiedName == "Ignore" {
if annotation.IsIgnoreTest() {
*testType = "IgnoreTest"
tbs := *&TestBadSmell{
FileName: clzPath,
......
......@@ -19,4 +19,16 @@ func NewAnnotation() Annotation {
func (n * Annotation) IsComponentOrRepository() bool {
return n.QualifiedName == "Component" || n.QualifiedName == "Repository"
}
func (n * Annotation) IsTest() bool {
return n.QualifiedName == "Test"
}
func (n * Annotation) IsIgnoreTest() bool {
return n.QualifiedName == "Ignore"
}
func (n * Annotation) IsIgnoreOrTest() bool {
return n.IsTest() || n.IsIgnoreTest()
}
\ No newline at end of file
......@@ -64,8 +64,18 @@ func (m *JMethod) GetAllCallString() []string {
var calls []string
for _, call := range m.MethodCalls {
if call.Class != "" {
calls = append(calls, call.BuilFullMethodName())
calls = append(calls, call.BuildFullMethodName())
}
}
return calls
}
func (m *JMethod) IsJunitTest() bool {
var isTest = false
for _, annotation := range m.Annotations {
if annotation.IsIgnoreOrTest() {
isTest = true
}
}
return isTest
}
......@@ -26,6 +26,14 @@ func NewJMethodCall() JMethodCall {
}
}
func (c *JMethodCall) BuilFullMethodName() string {
func (c *JMethodCall) BuildFullMethodName() string {
return c.Package + "." + c.Class + "." + c.MethodName
}
func (c *JMethodCall) IsSystemOutput() bool {
return c.Class == "System.out" && (c.MethodName == "println" || c.MethodName == "printf" || c.MethodName == "print")
}
func (c *JMethodCall) IsThreadSleep() bool {
return c.MethodName == "sleep" && c.Class == "Thread"
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册