Skip to content

Commit

Permalink
feat: [tbs] add RedundantAssertionTest
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Dec 30, 2019
1 parent a046289 commit b4893c4
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 23 deletions.
18 changes: 18 additions & 0 deletions core/adapter/call/java_call_app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,21 @@ func Test_ShouldNotGetCreators(t *testing.T) {

g.Expect(len(methodMap["testTrue"].Creators)).To(Equal(0))
}

func Test_ShouldGetMethodCallParameters(t *testing.T) {
g := NewGomegaWithT(t)

codePath := "../../../_fixtures/tbs/code/RedundantAssertionTest.java"
codePath = filepath.FromSlash(codePath)

callNodes := getCallNodes(codePath)

methodCallMap := make(map[string]models.JMethodCall)
for _, method := range callNodes[0].Methods {
for _, call := range method.MethodCalls {
methodCallMap[call.MethodName] = call
}
}

g.Expect(methodCallMap["assertEquals"].Parameters).To(Equal([]string{"true", "true"}))
}
35 changes: 24 additions & 11 deletions core/adapter/call/java_call_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,24 +318,29 @@ func buildMethodParameters(parameters parser.IFormalParametersContext, method *m
return true
}

var methodParams []models.JParameter = nil
parameterList := parameters.GetChild(1).(*parser.FormalParameterListContext)
formalParameter := parameterList.AllFormalParameter()
for _, param := range formalParameter {
paramContext := param.(*parser.FormalParameterContext)
paramType := paramContext.TypeType().GetText()
paramValue := paramContext.VariableDeclaratorId().(*parser.VariableDeclaratorIdContext).IDENTIFIER().GetText()

localVars[paramValue] = paramType
methodParams = append(methodParams, *&models.JParameter{Name: paramValue, Type: paramType})
}
methodParams := getMethodParameters(parameters)

method.Parameters = methodParams
updateMethod(method)
}
return false
}

func getMethodParameters(parameters parser.IFormalParametersContext) []models.JParameter {
var methodParams []models.JParameter = nil
parameterList := parameters.GetChild(1).(*parser.FormalParameterListContext)
formalParameter := parameterList.AllFormalParameter()
for _, param := range formalParameter {
paramContext := param.(*parser.FormalParameterContext)
paramType := paramContext.TypeType().GetText()
paramValue := paramContext.VariableDeclaratorId().(*parser.VariableDeclaratorIdContext).IDENTIFIER().GetText()

localVars[paramValue] = paramType
methodParams = append(methodParams, *&models.JParameter{Name: paramValue, Type: paramType})
}
return methodParams
}

func updateMethod(method *models.JMethod) {
if currentType == "CreatorClass" {
creatorMethodMap[getMethodMapName(*method)] = *method
Expand Down Expand Up @@ -535,6 +540,14 @@ func (s *JavaCallListener) EnterMethodCall(ctx *parser.MethodCallContext) {
}
jMethodCall.Class = targetType

if ctx.ExpressionList() != nil {
var parameters []string
for _, expression := range ctx.ExpressionList().(*parser.ExpressionListContext).AllExpression() {
parameters = append(parameters, expression.(*parser.ExpressionContext).GetText())
}
jMethodCall.Parameters = parameters
}

addMethodCall(jMethodCall)
}

Expand Down
8 changes: 0 additions & 8 deletions core/adapter/sql/sql_identifier_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,6 @@ func NewSqlIdentifierListener() *SqlIdentifierListener {
return &SqlIdentifierListener{}
}

func (s *SqlIdentifierListener) EnterSelect_stmt(ctx *parser.Select_stmtContext) {

}

func (s *SqlIdentifierListener) EnterSelect_or_values(ctx *parser.Select_or_valuesContext) {

}

func (s *SqlIdentifierListener) EnterSelect_core(ctx *parser.Select_coreContext) {
columns := ctx.AllResult_column()
for _, col := range columns {
Expand Down
15 changes: 14 additions & 1 deletion core/domain/tbs/tbs_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,21 @@ func (a TbsApp) AnalysisPath(deps []models.JClassNode, identifiersMap map[string
return results
}

func checkRedundantAssertionTest(path string, call models.JMethodCall, method models.JMethod, result *[]TestBadSmell, testType *string) {
func checkRedundantAssertionTest(path string, call models.JMethodCall, method models.JMethod, results *[]TestBadSmell, testType *string) {
TWO_PARAMETERS := 2
if len(call.Parameters) == TWO_PARAMETERS {
if call.Parameters[0] == call.Parameters[1] {
*testType = "RedundantAssertionTest"
tbs := *&TestBadSmell{
FileName: path,
Type: *testType,
Description: "",
Line: method.StartLine,
}

*results = append(*results, tbs)
}
}
}

func hasAssertion(methodName string) bool {
Expand Down
4 changes: 2 additions & 2 deletions core/domain/tbs/tbs_app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ func TestTbsApp_RedundantAssertionTest(t *testing.T) {

result := buildTbsResult(codePath)

g.Expect(len(result)).To(Equal(0))
//g.Expect(result[0].Type).To(Equal("EmptyTest"))
g.Expect(len(result)).To(Equal(1))
g.Expect(result[0].Type).To(Equal("RedundantAssertionTest"))
}

func TestTbsApp_CreatorNotUnknownTest(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion core/models/jmethod_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ type JMethodCall struct {
Type string
Class string
MethodName string
Parameters []JParameter
Parameters []string
StartLine int
StartLinePosition int
StopLine int
Expand Down

0 comments on commit b4893c4

Please sign in to comment.