Skip to content
This repository has been archived by the owner on Mar 8, 2020. It is now read-only.

Commit

Permalink
Merge pull request #200 from juanjux/feature/remove_dups
Browse files Browse the repository at this point in the history
Remove duplicated roles. Remove old file.
  • Loading branch information
juanjux authored Nov 3, 2017
2 parents 5494751 + d5425ca commit 91edda5
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 113 deletions.
13 changes: 12 additions & 1 deletion uast/ann/ann.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,13 +366,24 @@ func AddRoles(roles ...uast.Role) Action {
if len(n.Roles) > 0 && n.Roles[0] == uast.Unannotated {
n.Roles = n.Roles[:0]
}
n.Roles = append(n.Roles, roles...)
appendUniqueRoles(n, roles...)
return nil
},
desc: strings.Join(descs, ", "),
}
}

func appendUniqueRoles(n *uast.Node, roles ...uast.Role) {
addedRoles := make(map[string]bool)

for _, role := range roles {
if _, ok := addedRoles[role.String()]; !ok {
n.Roles = append(n.Roles, role)
addedRoles[role.String()] = true
}
}
}

// ReturnError creates an action that always returns a RuleError
// wrapping the given error with the offending node information.
func ReturnError(err error) Action {
Expand Down
13 changes: 13 additions & 0 deletions uast/ann/ann_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,19 @@ func TestAddRoles(t *testing.T) {
require.Equal(expected, input)
}

func TestAddDuplicatedRoles(t *testing.T) {
require := require.New(t)

a := AddRoles(uast.Statement, uast.Expression, uast.Statement, uast.Expression,
uast.Call, uast.Call)
input := uast.NewNode()
expected := uast.NewNode()
expected.Roles = []uast.Role{uast.Statement, uast.Expression, uast.Call}
err := a.Do(input)
require.NoError(err)
require.Equal(expected, input)
}

func TestRuleOnApply(t *testing.T) {
require := require.New(t)

Expand Down
61 changes: 27 additions & 34 deletions uast/uast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@ import (
"github.com/stretchr/testify/require"
)

func tokens(n *Node) []string {
var tokens []string
iter := NewOrderPathIter(NewPath(n))
for {
p := iter.Next()
if p.IsEmpty() {
break
}

n := p.Node()
if n.Token != "" {
tokens = append(tokens, n.Token)
}
}
return tokens
}

func TestPrefixTokens(t *testing.T) {
require := require.New(t)

Expand All @@ -17,7 +34,7 @@ func TestPrefixTokens(t *testing.T) {
{InternalType: "left", Token: "tok_pre_left"},
{InternalType: "right", Token: "tok_pre_right"},
}}}}
result := Tokens(n)
result := tokens(n)
expected := []string{"id3", "Prefix+", "tok_pre_left", "tok_pre_right"}
require.Equal(expected, result)
}
Expand All @@ -43,7 +60,7 @@ func TestPrefixTokensSubtree(t *testing.T) {
{InternalType: "right", Token: "tok_pre_right"},
},
}}}
result := Tokens(n)
result := tokens(n)
expected := []string{"id3", "Prefix+", "tok_pre_left", "subleft_1a", "subleft_1a_2a",
"subleft_1a_2b", "subleft_1b", "subleft_b_2a", "subleft_b_2b",
"tok_pre_right"}
Expand All @@ -61,7 +78,7 @@ func TestPrefixTokensPlain(t *testing.T) {
{InternalType: "left", Token: "tok_pre_left"},
{InternalType: "right", Token: "tok_pre_right"},
}}
result := Tokens(n)
result := tokens(n)
expected := []string{"id3", "Prefix+", "tok_pre_left", "tok_pre_right"}
require.Equal(expected, result)
}
Expand All @@ -75,7 +92,7 @@ func TestInfixTokens(t *testing.T) {
{InternalType: "left", Token: "tok_in_left"},
{InternalType: "right", Token: "tok_in_right"},
}}}}
result := Tokens(n)
result := tokens(n)
expected := []string{"id1", "tok_in_left", "Infix+", "tok_in_right"}
require.Equal(expected, result)
}
Expand All @@ -101,7 +118,7 @@ func TestInfixTokensSubtree(t *testing.T) {
{InternalType: "right", Token: "right"},
},
}}}
result := Tokens(n)
result := tokens(n)
expected := []string{"id3", "subleft_1a_2a", "subleft_1a", "subleft_1a_2b", "left",
"subleft_1b_2a", "subleft_1b", "subleft_1b_2b", "op_infix", "right"}

Expand All @@ -117,7 +134,7 @@ func TestInfixTokensPlain(t *testing.T) {
{InternalType: "op_infix", Roles: []Role{Infix}, Token: "Infix+"},
{InternalType: "right", Token: "tok_in_right"},
}}
result := Tokens(n)
result := tokens(n)
expected := []string{"id1", "tok_in_left", "Infix+", "tok_in_right"}
require.Equal(expected, result)
}
Expand All @@ -131,7 +148,7 @@ func TestPostfixTokens(t *testing.T) {
{InternalType: "left", Token: "tok_post_left"},
{InternalType: "right", Token: "tok_post_right"},
}}}}
result := Tokens(n)
result := tokens(n)
expected := []string{"id2", "tok_post_left", "tok_post_right", "Postfix+"}
require.Equal(expected, result)
}
Expand All @@ -157,7 +174,7 @@ func TestPostfixTokensSubtree(t *testing.T) {
{InternalType: "right", Token: "right"},
},
}}}
result := Tokens(n)
result := tokens(n)
expected := []string{"id2", "subleft_1a_2a", "subleft_1a_2b", "subleft_1a", "subleft_1b_2a",
"subleft_1b_2b", "subleft_1b", "left", "right", "op_postfix"}
require.Equal(expected, result)
Expand All @@ -172,7 +189,7 @@ func TestPostfixTokensPlain(t *testing.T) {
{InternalType: "right", Token: "tok_post_right"},
{InternalType: "op_postfix", Roles: []Role{Postfix}, Token: "Postfix+"},
}}
result := Tokens(n)
result := tokens(n)
expected := []string{"id2", "tok_post_left", "tok_post_right", "Postfix+"}
require.Equal(expected, result)
}
Expand Down Expand Up @@ -211,33 +228,9 @@ func TestOrderTokens(t *testing.T) {
}},
}}}}

result := Tokens(n)
result := tokens(n)
expected := []string{"id1", "tok_in_left", "Infix+", "subright1", "subright2", "tok_in_right",
"id2", "tok_post_left", "tok_post_right", "subright_pre1", "subright_pre2", "Postfix+",
"id3", "Prefix+", "tok_pre_left", "subright_in1", "tok_pre_right", "subright_in2"}
require.Equal(expected, result)
}

func TestCyclomaticComplexity(t *testing.T) {
require := require.New(t)
n := &Node{InternalType: "module",
Children: []*Node{
{InternalType: "root"}, // 1 (initial)
// Prefix is the default so it doesnt need any role
{InternalType: "if1", Roles: []Role{Statement, If}, Children: []*Node{ // 2 (If)
{InternalType: "if1else1", Roles: []Role{Statement, Then}, Children: []*Node{ // 0
{InternalType: "if1else1foreach", Roles: []Role{Statement, For, Iterator}, Children: []*Node{ // 3 (For)
{InternalType: "foreach_child1"}, // 0
{InternalType: "foreach_child2_continue", Roles: []Role{Statement, Continue}}, // 4 (Continue)
}},
{InternalType: "if1else1if", Roles: []Role{Statement, If}, Children: []*Node{ // 5 (If)
{InternalType: "elseif_child1"}, // 0
{InternalType: "opAnd", Roles: []Role{Operator, Binary, Boolean, And}}, // 6 (And)
{InternalType: "elseif_child2"}, // 0
}},
}},
{InternalType: "break", Roles: []Role{Break}},
},
}}}
require.Equal(CyclomaticComplexity(n), 6)
}
78 changes: 0 additions & 78 deletions uast/utils.go

This file was deleted.

0 comments on commit 91edda5

Please sign in to comment.