Skip to content

Commit

Permalink
#2 Refactor to use mappings
Browse files Browse the repository at this point in the history
Signed-off-by: Davide Petilli <davide@petilli.me>
  • Loading branch information
k3rn31 committed Jun 25, 2022
1 parent 1a40e71 commit 1195b78
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 37 deletions.
20 changes: 19 additions & 1 deletion mutator/types.go → mutator/mappings.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,26 @@

package mutator

import "go/token"

type MutantType int

const (
CONDITIONAL_BOUNDARY MutantType = iota
ConditionalBoundary MutantType = iota
)

func (mt MutantType) String() string {
switch mt {
case ConditionalBoundary:
return "Conditional Boundary"
default:
return ""
}
}

var tokenMutantType = map[token.Token]MutantType{
token.GTR: ConditionalBoundary,
token.LSS: ConditionalBoundary,
token.GEQ: ConditionalBoundary,
token.LEQ: ConditionalBoundary,
}
36 changes: 9 additions & 27 deletions mutator/mutator.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (

type Mutant struct {
MutantType MutantType
TokenType token.Token
Token token.Token
Position token.Position
Covered bool
}
Expand Down Expand Up @@ -58,31 +58,13 @@ func (m Mutator) RunWithFileName(fileName string) []Mutant {
}

func inspectBinaryExpr(set *token.FileSet, be *ast.BinaryExpr) (Mutant, bool) {
switch be.Op {
case token.GTR:
return Mutant{
MutantType: CONDITIONAL_BOUNDARY,
TokenType: token.GTR,
Position: set.Position(be.OpPos),
}, true
case token.LSS:
return Mutant{
MutantType: CONDITIONAL_BOUNDARY,
TokenType: token.LSS,
Position: set.Position(be.OpPos),
}, true
case token.LEQ:
return Mutant{
MutantType: CONDITIONAL_BOUNDARY,
TokenType: token.LEQ,
Position: set.Position(be.OpPos),
}, true
case token.GEQ:
return Mutant{
MutantType: CONDITIONAL_BOUNDARY,
TokenType: token.GEQ,
Position: set.Position(be.OpPos),
}, true
mt, ok := tokenMutantType[be.Op]
if !ok {
return Mutant{}, false
}
return Mutant{}, false
return Mutant{
MutantType: mt,
Token: be.Op,
Position: set.Position(be.OpPos),
}, true
}
18 changes: 9 additions & 9 deletions mutator/mutator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,39 +44,39 @@ func TestMutations(t *testing.T) {
{
name: "it recognizes CONDITIONAL_BOUNDARY with GTR",
fileName: "testdata/fixtures/conditional_boundary_gtr_go",
mutantType: mutator.CONDITIONAL_BOUNDARY,
mutantType: mutator.ConditionalBoundary,
tokenType: token.GTR,
covProfile: coveredPosition("testdata/fixtures/conditional_boundary_gtr_go"),
covered: true,
},
{
name: "it recognizes CONDITIONAL_BOUNDARY with LSS",
fileName: "testdata/fixtures/conditional_boundary_lss_go",
mutantType: mutator.CONDITIONAL_BOUNDARY,
mutantType: mutator.ConditionalBoundary,
tokenType: token.LSS,
covProfile: notCoveredPosition("testdata/fixtures/conditional_boundary_lss_go"),
covered: false,
},
{
name: "it recognizes CONDITIONAL_BOUNDARY with LEQ",
fileName: "testdata/fixtures/conditional_boundary_leq_go",
mutantType: mutator.CONDITIONAL_BOUNDARY,
mutantType: mutator.ConditionalBoundary,
tokenType: token.LEQ,
covProfile: notCoveredPosition("testdata/fixtures/conditional_boundary_leq_go"),
covered: false,
},
{
name: "it recognizes CONDITIONAL_BOUNDARY with GEQ",
fileName: "testdata/fixtures/conditional_boundary_geq_go",
mutantType: mutator.CONDITIONAL_BOUNDARY,
mutantType: mutator.ConditionalBoundary,
tokenType: token.GEQ,
covProfile: notCoveredPosition("testdata/fixtures/conditional_boundary_geq_go"),
covered: false,
},
{
name: "it skips illegal CONDITIONAL_BOUNDARY",
fileName: "testdata/fixtures/conditional_boundary_illegal_go",
mutantType: mutator.CONDITIONAL_BOUNDARY,
mutantType: mutator.ConditionalBoundary,
tokenType: token.ILLEGAL,
covProfile: notCoveredPosition("testdata/fixtures/conditional_boundary_illegal_go"),
covered: false,
Expand All @@ -95,11 +95,11 @@ func TestMutations(t *testing.T) {
}
return
}
if !cmp.Equal(got[0].MutantType, mutator.CONDITIONAL_BOUNDARY) {
t.Errorf(cmp.Diff(got[0].MutantType, mutator.CONDITIONAL_BOUNDARY))
if !cmp.Equal(got[0].MutantType, mutator.ConditionalBoundary) {
t.Errorf(cmp.Diff(got[0].MutantType, mutator.ConditionalBoundary))
}
if !cmp.Equal(got[0].TokenType, tc.tokenType) {
t.Errorf(cmp.Diff(got[0].TokenType, tc.tokenType))
if !cmp.Equal(got[0].Token, tc.tokenType) {
t.Errorf(cmp.Diff(got[0].Token, tc.tokenType))
}
if !cmp.Equal(got[0].Covered, tc.covered) {
t.Errorf(cmp.Diff(got[0].Covered, tc.covered))
Expand Down

0 comments on commit 1195b78

Please # to comment.