Skip to content

Commit

Permalink
feat(tests): improve code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelrubbioli authored and rodrigo-brito committed Oct 3, 2020
1 parent b31b417 commit 357ccf3
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
92 changes: 92 additions & 0 deletions model/node_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package model

import (
"testing"

"github.com/magiconair/properties/assert"

"github.com/rodrigo-brito/gocity/analyzer"
)

func TestNode_GenerateChildList(t *testing.T) {
n := Node{
childrenMap: map[string]*Node{
"1": {
Type: StructType,
Line: 1,
},
"2": {
Name: "name",
Type: PackageType,
},
"3": {
Type: FileType,
childrenMap: map[string]*Node{
"4": {
Type: FileType,
},
},
},
},
}

n.GenerateChildList("https://github.com/rodrigo-brito/gocity/blob/master")
assert.Equal(t, len(n.Children), 3)
assert.Equal(t, n.childrenMap["1"].URL, "https://github.com/rodrigo-brito/gocity/blob/master#L1")
assert.Equal(t, n.childrenMap["2"].URL, "https://github.com/rodrigo-brito/gocity/blob/master/name")
assert.Equal(t, n.childrenMap["3"].URL, "https://github.com/rodrigo-brito/gocity/blob/master")
assert.Equal(t, n.childrenMap["3"].childrenMap["4"].URL, "https://github.com/rodrigo-brito/gocity/blob/master")
}

func TestNode_GenerateChildrenPosition(t *testing.T) {
n := Node{}
n.GenerateChildrenPosition()
assert.Equal(t, n.Width, float64(1))
assert.Equal(t, n.Depth, float64(1))

n.Children = append(n.Children, []*Node{
{Width: 2, Depth: 2, Type: FileType, Children: []*Node{{Width: 16, Depth: 12}}},
{Width: 10, Depth: 4},
}...)
n.GenerateChildrenPosition()
assert.Equal(t, n.Children[0].Position.X, float64(0))
assert.Equal(t, n.Children[0].Position.Y, float64(-1))
assert.Equal(t, n.Children[0].Width, float64(2))
assert.Equal(t, n.Children[0].Depth, float64(2))
assert.Equal(t, n.Children[0].Children[0].Position.X, float64(0))
assert.Equal(t, n.Children[0].Children[0].Position.Y, float64(0))
assert.Equal(t, n.Children[1].Position.X, -0.5)
assert.Equal(t, n.Children[1].Position.Y, 1.5)
assert.Equal(t, n.Width, float64(3))
assert.Equal(t, n.Depth, float64(5))
}

func TestNew(t *testing.T) {
n := New(map[string]*analyzer.NodeInfo{"github.com/rodrigo-brito/gocity/blob/master/model/node.go.(Test)": {
File: "main.go",
ObjectName: "main",
Line: 1,
}}, "gocity", "master")

assert.Equal(t, n.Name, "gocity")
assert.Equal(t, n.Branch, "master")
assert.Equal(t, n.Depth, float64(9))
assert.Equal(t, n.Width, float64(9))
assert.Equal(t, n.Position.X, float64(0))
assert.Equal(t, n.Position.Y, float64(0))
assert.Equal(t, len(n.Children), 1)

n = New(map[string]*analyzer.NodeInfo{"github.com/rodrigo-brito/gocity/blob/master/model/node.go": {
File: "main.go",
ObjectName: "main",
Line: 1,
}}, "gocity", "master")

assert.Equal(t, n.Name, "gocity")
assert.Equal(t, n.Branch, "master")
assert.Equal(t, n.Depth, float64(8))
assert.Equal(t, n.Width, float64(8))
assert.Equal(t, n.Position.X, float64(0))
assert.Equal(t, n.Position.Y, float64(0))
assert.Equal(t, len(n.Children), 1)
}
20 changes: 20 additions & 0 deletions utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,23 @@ func TestTrimGoPath(t *testing.T) {
})
}
}

func TestGetIdentifier(t *testing.T) {
tests := []struct {
path string
pkg string
name string
want string
}{
{fmt.Sprintf("%s/src/gocity/main.go", os.Getenv("GOPATH")), "gocity", "/main.go", "/main.go.(/main.go)"},
{fmt.Sprintf("%s/src/gocity/foo/bar.go", os.Getenv("GOPATH")), "gocity", "/foo/bar.go", "/foo/bar.go.(/foo/bar.go)"},
{fmt.Sprintf("%s/src/gocity/vendor", os.Getenv("GOPATH")), "gocity", "/vendor", "/vendor.(/vendor)"},
{fmt.Sprintf("%s/src/gocity/vendor", os.Getenv("GOPATH")), "gocity", "", "/vendor"},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("given path %s, pkg %s and name %s", tt.path, tt.pkg, tt.name), func(t *testing.T) {
got := GetIdentifier(tt.path, tt.pkg, tt.name)
assert.Equal(t, tt.want, got)
})
}
}

0 comments on commit 357ccf3

Please sign in to comment.