mirror of
https://github.com/kyverno/kyverno.git
synced 2025-04-18 02:06:52 +00:00
chore: add unit tests for pkg/utils/yaml (#4512)
* chore: add unit tests for pkg/utils/yaml Signed-off-by: Charles-Edouard Brétéché <charled.breteche@gmail.com> * testify Signed-off-by: Charles-Edouard Brétéché <charled.breteche@gmail.com> Signed-off-by: Charles-Edouard Brétéché <charled.breteche@gmail.com> Co-authored-by: Sambhav Kothari <sambhavs.email@gmail.com>
This commit is contained in:
parent
429fe175bf
commit
103ba4b947
1 changed files with 99 additions and 0 deletions
99
pkg/utils/yaml/utils_test.go
Normal file
99
pkg/utils/yaml/utils_test.go
Normal file
|
@ -0,0 +1,99 @@
|
|||
package yaml
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestSplitDocuments(t *testing.T) {
|
||||
type args struct {
|
||||
yamlBytes []byte
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wantDocuments []string
|
||||
wantErr bool
|
||||
}{{
|
||||
name: "nil",
|
||||
args: args{
|
||||
nil,
|
||||
},
|
||||
wantDocuments: nil,
|
||||
wantErr: false,
|
||||
}, {
|
||||
name: "empty string",
|
||||
args: args{
|
||||
[]byte(""),
|
||||
},
|
||||
wantDocuments: nil,
|
||||
wantErr: false,
|
||||
}, {
|
||||
name: "single doc",
|
||||
args: args{
|
||||
[]byte("enabled: true"),
|
||||
},
|
||||
wantDocuments: []string{
|
||||
"enabled: true\n",
|
||||
},
|
||||
wantErr: false,
|
||||
}, {
|
||||
name: "two docs",
|
||||
args: args{
|
||||
[]byte("enabled: true\n---\ndisabled: false"),
|
||||
},
|
||||
wantDocuments: []string{
|
||||
"enabled: true\n",
|
||||
"disabled: false\n",
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
// TODO those tests should fail IMHO
|
||||
{
|
||||
name: "empty doc",
|
||||
args: args{
|
||||
[]byte("enabled: true\n---\n---\ndisabled: false"),
|
||||
},
|
||||
wantDocuments: []string{
|
||||
"enabled: true\n",
|
||||
"---\ndisabled: false\n",
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "only separators",
|
||||
args: args{
|
||||
[]byte("---\n---\n"),
|
||||
},
|
||||
wantDocuments: []string{
|
||||
"---\n",
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "only separators",
|
||||
args: args{
|
||||
[]byte("---\n\n\n---\n"),
|
||||
},
|
||||
wantDocuments: []string{
|
||||
"---\n\n\n",
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
gotDocuments, err := SplitDocuments(tt.args.yamlBytes)
|
||||
if tt.wantErr {
|
||||
assert.Error(t, err)
|
||||
} else {
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, len(tt.wantDocuments), len(gotDocuments))
|
||||
for i := range gotDocuments {
|
||||
assert.Equal(t, tt.wantDocuments[i], string(gotDocuments[i]))
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue