From 357ccf3d81d12194a47555f64e322c1993c9f3f4 Mon Sep 17 00:00:00 2001 From: Rafael Rubbioli Date: Sat, 3 Oct 2020 10:48:38 -0300 Subject: [PATCH] feat(tests): improve code coverage --- model/node_test.go | 92 +++++++++++++++++++++++++++++++++++++++++++++ utils/utils_test.go | 20 ++++++++++ 2 files changed, 112 insertions(+) create mode 100644 model/node_test.go diff --git a/model/node_test.go b/model/node_test.go new file mode 100644 index 0000000..78cb0fa --- /dev/null +++ b/model/node_test.go @@ -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) +} diff --git a/utils/utils_test.go b/utils/utils_test.go index a5301ad..21a8b8f 100644 --- a/utils/utils_test.go +++ b/utils/utils_test.go @@ -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) + }) + } +}