1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
|
package gotree
import (
"fmt"
"reflect"
"testing"
)
func ExampleTree() {
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")
album.Add("I’m Broken")
album.Add("Good Friends and a Bottle of Pills")
artist.Add("Power Metal\n(1988)")
artist.Add("Cowboys from Hell\n(1990)")
fmt.Println(artist.Print())
// Output:
// Pantera
// ├── Far Beyond Driven
// │ see https://en.wikipedia.org/wiki/Pantera
// │ (1994)
// │ ├── 5 minutes Alone
// │ │ └── song by American
// │ │ groove metal
// │ ├── I’m Broken
// │ └── Good Friends and a Bottle of Pills
// ├── Power Metal
// │ (1988)
// └── 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)
}
})
}
}
|