Skip to content

Commit

Permalink
Add test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
d6o committed Oct 12, 2019
1 parent 7de6a05 commit 730eb3e
Showing 1 changed file with 301 additions and 5 deletions.
306 changes: 301 additions & 5 deletions gotree_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package gotree_test
package gotree

import (
"fmt"

gotree "github.com/DiSiqueira/GoTree"
"reflect"
"testing"
)

func ExampleTree() {
artist := gotree.New("Pantera")
artist := New("Pantera")
album := artist.Add("Far Beyond Driven\nsee https://en.wikipedia.org/wiki/Pantera\n(1994)")
five := album.Add("5 minutes Alone")
five.Add("song by American\ngroove metal")
Expand All @@ -33,3 +32,300 @@ func ExampleTree() {
// └── Cowboys from Hell
// (1990)
}

func TestNew(t *testing.T) {
type args struct {
text string
}
tests := []struct {
name string
args args
want Tree
}{
{
name: "Create new Tree",
args: args{
text: "new tree",
},
want: &tree{
text: "new tree",
items: []Tree{},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := New(tt.args.text); !reflect.DeepEqual(got, tt.want) {
t.Errorf("New() = %v, want %v", got, tt.want)
}
})
}
}

func Test_tree_Add(t *testing.T) {
type fields struct {
text string
items []Tree
}
type args struct {
text string
}
tests := []struct {
name string
fields fields
args args
want Tree
parentCount int
}{
{
name: "Adding a new item into an empty tree",
args: args{
text: "child item",
},
fields: fields{
items: []Tree{},
},
want: &tree{
text: "child item",
items: []Tree{},
},
parentCount: 1,
},
{
name: "Adding a new item into a full tree",
args: args{
text: "fourth item",
},
fields: fields{
items: []Tree{
New("test"),
New("test2"),
New("test3"),
},
},
want: &tree{
text: "fourth item",
items: []Tree{},
},
parentCount: 4,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tree := &tree{
text: tt.fields.text,
items: tt.fields.items,
}
got := tree.Add(tt.args.text)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("tree.Add() = %v, want %v", got, tt.want)
}
if tt.parentCount != len(tree.Items()) {
t.Errorf("tree total items = %v, want %v", got, tt.want)
}
})
}
}

func Test_tree_AddTree(t *testing.T) {
type fields struct {
text string
items []Tree
}
type args struct {
tree Tree
}
tests := []struct {
name string
fields fields
args args
itemCount int
}{
{
name: "Adding a new item into an empty tree",
args: args{
tree: New("child item"),
},
fields: fields{
items: []Tree{},
},
itemCount: 1,
},
{
name: "Adding a new item into a full tree",
args: args{
tree: New("fourth item"),
},
fields: fields{
items: []Tree{
New("test"),
New("test2"),
New("test3"),
},
},
itemCount: 4,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tree := &tree{
text: tt.fields.text,
items: tt.fields.items,
}
tree.AddTree(tt.args.tree)
})
}
}

func Test_tree_Text(t *testing.T) {
type fields struct {
text string
items []Tree
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "Return the correct value",
fields: fields{
text: "item",
},
want: "item",
},
{
name: "Return the correct value while empty",
fields: fields{
text: "",
},
want: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tree := &tree{
text: tt.fields.text,
items: tt.fields.items,
}
if got := tree.Text(); got != tt.want {
t.Errorf("tree.Text() = %v, want %v", got, tt.want)
}
})
}
}

func Test_tree_Items(t *testing.T) {
type fields struct {
text string
items []Tree
}
tests := []struct {
name string
fields fields
want []Tree
}{
{
name: "Return empty if there is no items under the tree",
fields: fields{
text: "top level item",
items: []Tree{},
},
want: []Tree{},
},
{
name: "Return all items under the tree",
fields: fields{
text: "top level item",
items: []Tree{
New("first child"),
New("second child"),
},
},
want: []Tree{
New("first child"),
New("second child"),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tree := &tree{
text: tt.fields.text,
items: tt.fields.items,
}
if got := tree.Items(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("tree.Items() = %v, want %v", got, tt.want)
}
})
}
}

func Test_tree_Print(t *testing.T) {
threeLevelTree := New("First Level")
threeLevelTree.Add("Second level").Add("Third Level")

complexTree := New("Daft Punk")
ram := complexTree.Add("Random Access Memories")
complexTree.Add("Humam After All")
alive := complexTree.Add("Alive 2007")

ram.Add("Give Life Back to Music")
ram.Add("Giorgio by Moroder")
ram.Add("Within")

alive.Add("Touch It/Technologic")
alive.Add("Face to Face/Too Long")

type fields struct {
tree Tree
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "Print a single item tree",
fields: fields{
tree: New("single item"),
},
want: `single item
`,
},
{
name: "Print a three level tree",
fields: fields{
tree: threeLevelTree,
},
want: `First Level
└── Second level
└── Third Level
`,
},
{
name: "Print a three level tree",
fields: fields{
tree: complexTree,
},
want: `Daft Punk
├── Random Access Memories
│ ├── Give Life Back to Music
│ ├── Giorgio by Moroder
│ └── Within
├── Humam After All
└── Alive 2007
├── Touch It/Technologic
└── Face to Face/Too Long
`,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.fields.tree.Print(); got != tt.want {
t.Errorf("tree.Print() = %#v, want %#v", got, tt.want)
}
})
}
}

0 comments on commit 730eb3e

Please sign in to comment.