Skip to content

Commit

Permalink
Merge pull request #11 from shivamMg/fix-bug
Browse files Browse the repository at this point in the history
Fix bug: last using invalid default value inside printText
  • Loading branch information
disiqueira committed Apr 10, 2018
2 parents 1c4228f + 9109629 commit fa4feb4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
6 changes: 2 additions & 4 deletions gotree.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,14 @@ func (p *printer) Print(t Tree) string {
return t.Text() + newLine + p.printItems(t.Items(), []bool{})
}

func (p *printer) printText(text string, spaces []bool) string {
func (p *printer) printText(text string, spaces []bool, last bool) string {
var result string
last := true
for _, space := range spaces {
if space {
result += emptySpace
} else {
result += continueItem
}
last = space
}

indicator := middleItem
Expand All @@ -91,7 +89,7 @@ func (p *printer) printItems(t []Tree, spaces []bool) string {
var result string
for i, f := range t {
last := i == len(t)-1
result += p.printText(f.Text(), spaces)
result += p.printText(f.Text(), spaces, last)
if len(f.Items()) > 0 {
spacesChild := append(spaces, last)
result += p.printItems(f.Items(), spacesChild)
Expand Down
26 changes: 26 additions & 0 deletions gotree_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package gotree_test

import (
"testing"

gotree "github.com/DiSiqueira/GoTree"
)

func TestLast(t *testing.T) {
want := `Pantera
├── Far Beyond Driven
│ └── 5 minutes Alone
└── Power Metal
`

artist := gotree.New("Pantera")
album := artist.Add("Far Beyond Driven")
album.Add("5 minutes Alone")
artist.Add("Power Metal")

got := artist.Print()
if got != want {
t.Errorf("Expected: \n%s\n", want)
t.Errorf("Got: \n%s\n", got)
}
}

0 comments on commit fa4feb4

Please sign in to comment.