Skip to content

Commit 730eb3e

Browse files
committed
Add test coverage
1 parent 7de6a05 commit 730eb3e

File tree

1 file changed

+301
-5
lines changed

1 file changed

+301
-5
lines changed

gotree_test.go

Lines changed: 301 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
package gotree_test
1+
package gotree
22

33
import (
44
"fmt"
5-
6-
gotree "github.com/DiSiqueira/GoTree"
5+
"reflect"
6+
"testing"
77
)
8-
98
func ExampleTree() {
10-
artist := gotree.New("Pantera")
9+
artist := New("Pantera")
1110
album := artist.Add("Far Beyond Driven\nsee https://en.wikipedia.org/wiki/Pantera\n(1994)")
1211
five := album.Add("5 minutes Alone")
1312
five.Add("song by American\ngroove metal")
@@ -33,3 +32,300 @@ func ExampleTree() {
3332
// └── Cowboys from Hell
3433
// (1990)
3534
}
35+
36+
func TestNew(t *testing.T) {
37+
type args struct {
38+
text string
39+
}
40+
tests := []struct {
41+
name string
42+
args args
43+
want Tree
44+
}{
45+
{
46+
name: "Create new Tree",
47+
args: args{
48+
text: "new tree",
49+
},
50+
want: &tree{
51+
text: "new tree",
52+
items: []Tree{},
53+
},
54+
},
55+
}
56+
for _, tt := range tests {
57+
t.Run(tt.name, func(t *testing.T) {
58+
if got := New(tt.args.text); !reflect.DeepEqual(got, tt.want) {
59+
t.Errorf("New() = %v, want %v", got, tt.want)
60+
}
61+
})
62+
}
63+
}
64+
65+
func Test_tree_Add(t *testing.T) {
66+
type fields struct {
67+
text string
68+
items []Tree
69+
}
70+
type args struct {
71+
text string
72+
}
73+
tests := []struct {
74+
name string
75+
fields fields
76+
args args
77+
want Tree
78+
parentCount int
79+
}{
80+
{
81+
name: "Adding a new item into an empty tree",
82+
args: args{
83+
text: "child item",
84+
},
85+
fields: fields{
86+
items: []Tree{},
87+
},
88+
want: &tree{
89+
text: "child item",
90+
items: []Tree{},
91+
},
92+
parentCount: 1,
93+
},
94+
{
95+
name: "Adding a new item into a full tree",
96+
args: args{
97+
text: "fourth item",
98+
},
99+
fields: fields{
100+
items: []Tree{
101+
New("test"),
102+
New("test2"),
103+
New("test3"),
104+
},
105+
},
106+
want: &tree{
107+
text: "fourth item",
108+
items: []Tree{},
109+
},
110+
parentCount: 4,
111+
},
112+
}
113+
for _, tt := range tests {
114+
t.Run(tt.name, func(t *testing.T) {
115+
tree := &tree{
116+
text: tt.fields.text,
117+
items: tt.fields.items,
118+
}
119+
got := tree.Add(tt.args.text)
120+
if !reflect.DeepEqual(got, tt.want) {
121+
t.Errorf("tree.Add() = %v, want %v", got, tt.want)
122+
}
123+
if tt.parentCount != len(tree.Items()) {
124+
t.Errorf("tree total items = %v, want %v", got, tt.want)
125+
}
126+
})
127+
}
128+
}
129+
130+
func Test_tree_AddTree(t *testing.T) {
131+
type fields struct {
132+
text string
133+
items []Tree
134+
}
135+
type args struct {
136+
tree Tree
137+
}
138+
tests := []struct {
139+
name string
140+
fields fields
141+
args args
142+
itemCount int
143+
}{
144+
{
145+
name: "Adding a new item into an empty tree",
146+
args: args{
147+
tree: New("child item"),
148+
},
149+
fields: fields{
150+
items: []Tree{},
151+
},
152+
itemCount: 1,
153+
},
154+
{
155+
name: "Adding a new item into a full tree",
156+
args: args{
157+
tree: New("fourth item"),
158+
},
159+
fields: fields{
160+
items: []Tree{
161+
New("test"),
162+
New("test2"),
163+
New("test3"),
164+
},
165+
},
166+
itemCount: 4,
167+
},
168+
}
169+
for _, tt := range tests {
170+
t.Run(tt.name, func(t *testing.T) {
171+
tree := &tree{
172+
text: tt.fields.text,
173+
items: tt.fields.items,
174+
}
175+
tree.AddTree(tt.args.tree)
176+
})
177+
}
178+
}
179+
180+
func Test_tree_Text(t *testing.T) {
181+
type fields struct {
182+
text string
183+
items []Tree
184+
}
185+
tests := []struct {
186+
name string
187+
fields fields
188+
want string
189+
}{
190+
{
191+
name: "Return the correct value",
192+
fields: fields{
193+
text: "item",
194+
},
195+
want: "item",
196+
},
197+
{
198+
name: "Return the correct value while empty",
199+
fields: fields{
200+
text: "",
201+
},
202+
want: "",
203+
},
204+
}
205+
for _, tt := range tests {
206+
t.Run(tt.name, func(t *testing.T) {
207+
tree := &tree{
208+
text: tt.fields.text,
209+
items: tt.fields.items,
210+
}
211+
if got := tree.Text(); got != tt.want {
212+
t.Errorf("tree.Text() = %v, want %v", got, tt.want)
213+
}
214+
})
215+
}
216+
}
217+
218+
func Test_tree_Items(t *testing.T) {
219+
type fields struct {
220+
text string
221+
items []Tree
222+
}
223+
tests := []struct {
224+
name string
225+
fields fields
226+
want []Tree
227+
}{
228+
{
229+
name: "Return empty if there is no items under the tree",
230+
fields: fields{
231+
text: "top level item",
232+
items: []Tree{},
233+
},
234+
want: []Tree{},
235+
},
236+
{
237+
name: "Return all items under the tree",
238+
fields: fields{
239+
text: "top level item",
240+
items: []Tree{
241+
New("first child"),
242+
New("second child"),
243+
},
244+
},
245+
want: []Tree{
246+
New("first child"),
247+
New("second child"),
248+
},
249+
},
250+
}
251+
for _, tt := range tests {
252+
t.Run(tt.name, func(t *testing.T) {
253+
tree := &tree{
254+
text: tt.fields.text,
255+
items: tt.fields.items,
256+
}
257+
if got := tree.Items(); !reflect.DeepEqual(got, tt.want) {
258+
t.Errorf("tree.Items() = %v, want %v", got, tt.want)
259+
}
260+
})
261+
}
262+
}
263+
264+
func Test_tree_Print(t *testing.T) {
265+
threeLevelTree := New("First Level")
266+
threeLevelTree.Add("Second level").Add("Third Level")
267+
268+
complexTree := New("Daft Punk")
269+
ram := complexTree.Add("Random Access Memories")
270+
complexTree.Add("Humam After All")
271+
alive := complexTree.Add("Alive 2007")
272+
273+
ram.Add("Give Life Back to Music")
274+
ram.Add("Giorgio by Moroder")
275+
ram.Add("Within")
276+
277+
alive.Add("Touch It/Technologic")
278+
alive.Add("Face to Face/Too Long")
279+
280+
type fields struct {
281+
tree Tree
282+
}
283+
tests := []struct {
284+
name string
285+
fields fields
286+
want string
287+
}{
288+
{
289+
name: "Print a single item tree",
290+
fields: fields{
291+
tree: New("single item"),
292+
},
293+
want: `single item
294+
`,
295+
},
296+
{
297+
name: "Print a three level tree",
298+
fields: fields{
299+
tree: threeLevelTree,
300+
},
301+
want: `First Level
302+
└── Second level
303+
└── Third Level
304+
`,
305+
},
306+
{
307+
name: "Print a three level tree",
308+
fields: fields{
309+
tree: complexTree,
310+
},
311+
want: `Daft Punk
312+
├── Random Access Memories
313+
│ ├── Give Life Back to Music
314+
│ ├── Giorgio by Moroder
315+
│ └── Within
316+
├── Humam After All
317+
└── Alive 2007
318+
├── Touch It/Technologic
319+
└── Face to Face/Too Long
320+
`,
321+
},
322+
}
323+
324+
for _, tt := range tests {
325+
t.Run(tt.name, func(t *testing.T) {
326+
if got := tt.fields.tree.Print(); got != tt.want {
327+
t.Errorf("tree.Print() = %#v, want %#v", got, tt.want)
328+
}
329+
})
330+
}
331+
}

0 commit comments

Comments
 (0)