Skip to content

Commit

Permalink
update test
Browse files Browse the repository at this point in the history
  • Loading branch information
ddddddO committed Nov 25, 2021
1 parent 4201f0d commit dd42073
Show file tree
Hide file tree
Showing 6 changed files with 568 additions and 401 deletions.
10 changes: 10 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
fmt:
go fmt

test:
go test ./... -race -v

cyclo:
gocyclo .

all: fmt test cyclo
60 changes: 59 additions & 1 deletion programmable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,69 @@ root
`, "\n"),
},
{
name: "case6(succeeded / output json format)",
name: "case6(succeeded / output json)",
root: prepareMultiNode(),
optFns: []OptFn{EncodeJSON()},
want: strings.TrimPrefix(`
{"value":"root","children":[{"value":"child 1","children":[{"value":"child 2","children":[{"value":"child 3","children":null},{"value":"child 4","children":[{"value":"child 5","children":null},{"value":"child 6","children":[{"value":"child 7","children":null}]}]}]}]},{"value":"child 8","children":null}]}
`, "\n"),
},
{
name: "case7(succeeded / output yaml)",
root: prepareMultiNode(),
optFns: []OptFn{EncodeYAML()},
want: strings.TrimPrefix(`
value: root
children:
- value: child 1
children:
- value: child 2
children:
- value: child 3
children: []
- value: child 4
children:
- value: child 5
children: []
- value: child 6
children:
- value: child 7
children: []
- value: child 8
children: []
`, "\n"),
},
{
name: "case8(succeeded / output toml)",
root: prepareMultiNode(),
optFns: []OptFn{EncodeTOML()},
want: strings.TrimPrefix(`
value = 'root'
[[children]]
value = 'child 1'
[[children.children]]
value = 'child 2'
[[children.children.children]]
value = 'child 3'
children = []
[[children.children.children]]
value = 'child 4'
[[children.children.children.children]]
value = 'child 5'
children = []
[[children.children.children.children]]
value = 'child 6'
[[children.children.children.children.children]]
value = 'child 7'
children = []
[[children]]
value = 'child 8'
children = []
`, "\n"),
},
}
Expand Down
110 changes: 110 additions & 0 deletions tree_json_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package gtree

import (
"bytes"
"strings"
"testing"
)

func TestExecute_encodeJSON(t *testing.T) {
tests := []struct {
name string
in in
out out
}{
{
name: "case 1(tab spaces & multi root & output json)",
in: in{
input: strings.NewReader(strings.TrimSpace(`
- a
- i
- u
- k
- kk
- t
- e
- o
- g
- a
- i
- u
- k
- kk
- t
- e
- o
- g`)),
optFns: []OptFn{EncodeJSON()},
},
out: out{
output: strings.TrimPrefix(`
{"value":"a","children":[{"value":"i","children":[{"value":"u","children":[{"value":"k","children":null},{"value":"kk","children":null}]},{"value":"t","children":null}]},{"value":"e","children":[{"value":"o","children":null}]},{"value":"g","children":null}]}
{"value":"a","children":[{"value":"i","children":[{"value":"u","children":[{"value":"k","children":null},{"value":"kk","children":null}]},{"value":"t","children":null}]},{"value":"e","children":[{"value":"o","children":null}]},{"value":"g","children":null}]}
`, "\n"),
err: nil,
},
},
{
name: "case 2(indent 2spaces & output json)",
in: in{
input: strings.NewReader(strings.TrimSpace(`
- a
- i
- u
- k
- kk
- t
- e
- o
- g`)),
optFns: []OptFn{IndentTwoSpaces(), EncodeJSON()},
},
out: out{
output: strings.TrimPrefix(`
{"value":"a","children":[{"value":"i","children":[{"value":"u","children":[{"value":"k","children":null},{"value":"kk","children":null}]},{"value":"t","children":null}]},{"value":"e","children":[{"value":"o","children":null}]},{"value":"g","children":null}]}
`, "\n"),
err: nil,
},
},
{
name: "case 3(indent 4spaces & output json)",
in: in{
input: strings.NewReader(strings.TrimSpace(`
- a
- i
- u
- k
- kk
- t
- e
- o
- g`)),
optFns: []OptFn{IndentFourSpaces(), EncodeJSON()},
},
out: out{
output: strings.TrimPrefix(`
{"value":"a","children":[{"value":"i","children":[{"value":"u","children":[{"value":"k","children":null},{"value":"kk","children":null}]},{"value":"t","children":null}]},{"value":"e","children":[{"value":"o","children":null}]},{"value":"g","children":null}]}
`, "\n"),
err: nil,
},
},
}

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

out := &bytes.Buffer{}
gotErr := Execute(out, tt.in.input, tt.in.optFns...)
gotOutput := out.String()

if gotOutput != tt.out.output {
t.Errorf("\ngot: \n%s\nwant: \n%s", gotOutput, tt.out.output)
}
if gotErr != tt.out.err {
t.Errorf("\ngotErr: \n%v\nwantErr: \n%v", gotErr, tt.out.err)
}
})
}
}
Loading

0 comments on commit dd42073

Please sign in to comment.