Skip to content

Commit

Permalink
Merge pull request #5667 from mohamedawnallah/unitTestTemplateUtil
Browse files Browse the repository at this point in the history
operator/pkg/util: unit test template
  • Loading branch information
karmada-bot authored Oct 11, 2024
2 parents 5dd4ab8 + 56bdcc4 commit cadb946
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions operator/pkg/util/template_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
Copyright 2024 The Karmada Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package util

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

func TestParseTemplate(t *testing.T) {
tests := []struct {
name string
template string
args interface{}
want []byte
wantErr bool
errMsg string
}{
{
name: "ParseTemplate_WithMissingVariable_ParsingFailed",
template: "Hello, {{.Name}}!",
args: struct{ Missing string }{Missing: "World"},
want: nil,
wantErr: true,
errMsg: "error when executing template",
},
{
name: "ParseTemplate_WithInvalidTemplateSyntax_ParsingFailed",
template: "Hello, {{.Name!",
args: struct{ Name string }{Name: "World"},
want: nil,
wantErr: true,
errMsg: "error when parsing template",
},
{
name: "ParseTemplate_ValidTemplateWithVariable_ParsingSucceeded",
template: "Hello, {{.Name}}!",
args: struct{ Name string }{Name: "World"},
want: []byte("Hello, World!"),
wantErr: false,
},
{
name: "ParseTemplate_EmptyTemplate_ParsingSucceeded",
template: "",
args: nil,
want: []byte(""),
wantErr: false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got, err := ParseTemplate(test.template, test.args)
if err == nil && test.wantErr {
t.Errorf("expected an error, but got none")
}
if err != nil && !test.wantErr {
t.Errorf("unexpected error, got: %v", err)
}
if err != nil && test.wantErr && !strings.Contains(err.Error(), test.errMsg) {
t.Errorf("expected error message %s to be in %s", test.errMsg, err.Error())
}
if !bytes.Equal(got, test.want) {
t.Errorf("expected parsed template bytes to be %v, but got %v", test.want, got)
}
})
}
}

0 comments on commit cadb946

Please sign in to comment.