You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
113 lines
2.3 KiB
113 lines
2.3 KiB
2 years ago
|
package tree
|
||
|
|
||
|
import (
|
||
|
"reflect"
|
||
|
"testing"
|
||
|
)
|
||
|
|
||
|
func TestSplit(t *testing.T) {
|
||
|
type args struct {
|
||
|
code string
|
||
|
length int
|
||
|
}
|
||
|
tests := []struct {
|
||
|
name string
|
||
|
args args
|
||
|
wantRes []string
|
||
|
}{
|
||
|
{
|
||
|
name: "test1",
|
||
|
args: args{
|
||
|
code: "A00001B00001C00001",
|
||
|
length: 6,
|
||
|
},
|
||
|
wantRes: []string{"A00001B00001C00001", "A00001B00001", "A00001"},
|
||
|
},
|
||
|
}
|
||
|
for _, tt := range tests {
|
||
|
t.Run(tt.name, func(t *testing.T) {
|
||
|
if gotRes := Split(tt.args.code, tt.args.length); !reflect.DeepEqual(gotRes, tt.wantRes) {
|
||
|
t.Errorf("Split() = %v, want %v", gotRes, tt.wantRes)
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestTree(t *testing.T) {
|
||
|
type args[T any] struct {
|
||
|
data []T
|
||
|
parent string
|
||
|
length int
|
||
|
pathFunc func(d T) string
|
||
|
}
|
||
|
type testCase[T any] struct {
|
||
|
name string
|
||
|
args args[T]
|
||
|
wantRes []Node[T]
|
||
|
}
|
||
|
|
||
|
type D struct {
|
||
|
Code string
|
||
|
Name string
|
||
|
}
|
||
|
|
||
|
tests := []testCase[D]{
|
||
|
{
|
||
|
name: "",
|
||
|
args: args[D]{
|
||
|
data: []D{{Code: "A00001", Name: "A00001"}, {Code: "A00001B00001", Name: "A00001B00001"}, {Code: "A00001B00001C00001", Name: "A00001B00001C00001"}},
|
||
|
parent: "",
|
||
|
length: 6,
|
||
|
pathFunc: func(d D) string {
|
||
|
return d.Code
|
||
|
},
|
||
|
},
|
||
|
wantRes: []Node[D]{
|
||
|
{
|
||
|
Data: D{Code: "A00001", Name: "A00001"},
|
||
|
Children: []Node[D]{
|
||
|
{
|
||
|
Data: D{Code: "A00001B00001", Name: "A00001B00001"},
|
||
|
Children: []Node[D]{
|
||
|
{
|
||
|
Data: D{Code: "A00001B00001C00001", Name: "A00001B00001C00001"},
|
||
|
Children: nil,
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
{
|
||
|
name: "",
|
||
|
args: args[D]{
|
||
|
data: []D{{Code: "A00001B00001", Name: "A00001B00001"}, {Code: "A00001B00001C00001", Name: "A00001B00001C00001"}},
|
||
|
parent: "A00001",
|
||
|
length: 6,
|
||
|
pathFunc: func(d D) string {
|
||
|
return d.Code
|
||
|
},
|
||
|
},
|
||
|
wantRes: []Node[D]{
|
||
|
{
|
||
|
Data: D{Code: "A00001B00001", Name: "A00001B00001"},
|
||
|
Children: []Node[D]{
|
||
|
{
|
||
|
Data: D{Code: "A00001B00001C00001", Name: "A00001B00001C00001"},
|
||
|
Children: nil,
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
}
|
||
|
for _, tt := range tests {
|
||
|
t.Run(tt.name, func(t *testing.T) {
|
||
|
if gotRes := Tree(tt.args.data, tt.args.parent, tt.args.length, tt.args.pathFunc); !reflect.DeepEqual(gotRes, tt.wantRes) {
|
||
|
t.Errorf("Tree() = %v, want %v", gotRes, tt.wantRes)
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
}
|