Merge pull request #3145 from my-git9/ut-template

[UT] add ut for utils/template.go
This commit is contained in:
karmada-bot 2023-02-14 10:03:55 +08:00 committed by GitHub
commit a9089325dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 68 additions and 0 deletions

View File

@ -0,0 +1,68 @@
package utils
import (
"reflect"
"testing"
)
func TestParseTemplate(t *testing.T) {
type args struct {
strTmpl string
obj interface{}
}
tests := []struct {
name string
args args
want []byte
wantErr bool
}{
{
name: "strTmpl and obj are not empty",
args: args{
strTmpl: "foo",
obj: "bar",
},
want: []byte{'f', 'o', 'o'},
wantErr: false,
},
{
name: "strTmpl is empty",
args: args{
strTmpl: "",
obj: "bar",
},
want: nil,
wantErr: false,
},
{
name: "obj is nil",
args: args{
strTmpl: "foo",
obj: nil,
},
want: []byte{'f', 'o', 'o'},
wantErr: false,
},
{
name: "obj and strTmpl are empty",
args: args{
strTmpl: "",
obj: "",
},
want: nil,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ParseTemplate(tt.args.strTmpl, tt.args.obj)
if (err != nil) != tt.wantErr {
t.Errorf("ParseTemplate() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("ParseTemplate() = %v, want %v", got, tt.want)
}
})
}
}