Skip to content

Commit

Permalink
Fix index out of range error (#37)
Browse files Browse the repository at this point in the history
* Fix index out of range error

* Add unit test for MakeNodeID; add fix
  • Loading branch information
saheienko authored and awalterschulze committed Jan 31, 2018
1 parent bf3a0bd commit c45f112
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
6 changes: 3 additions & 3 deletions ast/ast.go
Expand Up @@ -596,9 +596,9 @@ func MakeNodeID(id string, port string) *NodeID {
p := Port{"", ""}
if len(port) > 0 {
ps := strings.Split(port, ":")
p.ID1 = ID(ps[1])
if len(ps) > 2 {
p.ID2 = ID(ps[2])
p.ID1 = ID(ps[0])
if len(ps) > 1 {
p.ID2 = ID(ps[1])
}
}
return &NodeID{ID(id), p}
Expand Down
46 changes: 46 additions & 0 deletions ast/ast_test.go
@@ -0,0 +1,46 @@
package ast

import (
"testing"
)

func TestMakeNodeID(t *testing.T) {
tcs := []struct {
id string
port string
want NodeID
}{
{ // TC#1
id: "",
port: "",
want: NodeID{}},
{ // TC#2
id: "id",
port: "p1",
want: NodeID{"id", Port{"p1", ""}},
},
{ // TC#3
id: "_id",
port: "p1:p2",
want: NodeID{"_id", Port{"p1", "p2"}},
},
{ // TC#4
id: "1id",
port: "p1:p2:p3",
want: NodeID{"1id", Port{"p1", "p2"}},
},
{ // TC#5
id: "?id",
port: ":p2:p3",
want: NodeID{"?id", Port{"", "p2"}},
},
}

for i, tc := range tcs {
n := MakeNodeID(tc.id, tc.port)

if *n != tc.want {
t.Fatalf("TC#%d: MakeNodeId(%q, %q)=%#v, want %#v", i+1, tc.id, tc.port, *n, tc.want)
}
}
}

0 comments on commit c45f112

Please sign in to comment.