-
Notifications
You must be signed in to change notification settings - Fork 885
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5667 from mohamedawnallah/unitTestTemplateUtil
operator/pkg/util: unit test template
- Loading branch information
Showing
1 changed file
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |