Skip to content

Commit

Permalink
add test for walk func
Browse files Browse the repository at this point in the history
  • Loading branch information
ddddddO committed Nov 4, 2023
1 parent 6bc3f9e commit 1ab9fb2
Show file tree
Hide file tree
Showing 2 changed files with 176 additions and 0 deletions.
80 changes: 80 additions & 0 deletions tree_handler_programmably_walk_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package gtree_test

import (
"bytes"
"fmt"
"strings"
"testing"

"github.com/ddddddO/gtree"
tu "github.com/ddddddO/gtree/testutil"
)

// TODO: 何パターンかのcallbackを用意してWalkerNode用メソッドのテストもしたい
func TestWalkProgrammably(t *testing.T) {
tests := []struct {
name string
root *gtree.Node
options []gtree.Option
out out
}{
{
name: "case(succeeded)",
root: tu.PrepareMultiNode(),
options: []gtree.Option{
gtree.WithBranchFormatIntermedialNode("+--", ": "),
gtree.WithBranchFormatLastNode("+--", " "),
},
out: out{
output: strings.TrimLeft(`
root1
+-- child 1
: +-- child 2
: +-- child 3
: +-- child 4
: +-- child 5
: +-- child 6
: +-- child 7
+-- child 8
`, "\n"),
err: nil,
},
},
{
name: "case(succeeded/massive)",
root: tu.Prepare(),
options: []gtree.Option{gtree.WithMassive(nil)},
out: out{
output: strings.TrimLeft(`
root
└── child 1
└── child 2
`, "\n"),
err: nil,
},
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

buf := &bytes.Buffer{}
callback := func(wn *gtree.WalkerNode) error {
fmt.Fprintln(buf, wn.Row())
return nil
}
gotErr := gtree.WalkProgrammably(tt.root, callback, tt.options...)
if gotErr != nil || tt.out.err != nil {
if gotErr.Error() != tt.out.err.Error() {
t.Errorf("\ngotErr: \n%s\nwantErr: \n%s", gotErr, tt.out.err.Error())
}
}
got := buf.String()
if got != tt.out.output {
t.Errorf("\ngot: \n%s\nwant: \n%s", got, tt.out.output)
}
})
}
}
96 changes: 96 additions & 0 deletions tree_handler_walk_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package gtree_test

import (
"bytes"
"fmt"
"strings"
"testing"

"github.com/ddddddO/gtree"
)

// TODO: 何パターンかのcallbackを用意してWalkerNode用メソッドのテストもしたい
func TestWalk(t *testing.T) {
tests := []struct {
name string
in in
out out
}{
{
name: "case(succeeded)",
in: in{
input: strings.NewReader(strings.TrimSpace(`
- a
- i
- u
- k
- kk
- t
- e
- o
- g`)),
},
out: out{
output: strings.TrimLeft(`
a
├── i
│ └── u
│ └── k
└── kk
└── t
e
└── o
└── g
`, "\n"),
err: nil,
},
},
{
name: "case(succeeded/change branch)",
in: in{
input: strings.NewReader(strings.TrimSpace(`
- a
- i
- u
- k
- kk
- t`)),
options: []gtree.Option{gtree.WithBranchFormatIntermedialNode("+--", ": "), gtree.WithBranchFormatLastNode("+--", " ")},
},
out: out{
output: strings.TrimLeft(`
a
+-- i
: +-- u
: +-- k
+-- kk
+-- t
`, "\n"),
err: nil,
},
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

buf := &bytes.Buffer{}
callback := func(wn *gtree.WalkerNode) error {
fmt.Fprintln(buf, wn.Row())
return nil
}
gotErr := gtree.Walk(tt.in.input, callback, tt.in.options...)
if gotErr != nil || tt.out.err != nil {
if gotErr.Error() != tt.out.err.Error() {
t.Errorf("\ngotErr: \n%s\nwantErr: \n%s", gotErr, tt.out.err)
}
}
got := buf.String()
if got != tt.out.output {
t.Errorf("\ngot: \n%s\nwant: \n%s", got, tt.out.output)
}
})
}
}

0 comments on commit 1ab9fb2

Please sign in to comment.