2021-07-16 17:28:13 +03:00
|
|
|
package severity
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"testing"
|
2021-07-19 21:04:08 +03:00
|
|
|
|
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2021-07-16 17:28:13 +03:00
|
|
|
)
|
|
|
|
|
|
2021-07-20 13:06:18 +03:00
|
|
|
func TestYamlUnmarshal(t *testing.T) {
|
|
|
|
|
testUnmarshal(t, yaml.Unmarshal, func(value string) string { return value })
|
2021-07-16 17:28:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestYamlUnmarshalFail(t *testing.T) {
|
2021-07-19 21:04:08 +03:00
|
|
|
testUnmarshalFail(t, yaml.Unmarshal, createYAML)
|
2021-07-16 17:28:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func testUnmarshal(t *testing.T, unmarshaller func(data []byte, v interface{}) error, payloadCreator func(value string) string) {
|
|
|
|
|
payloads := [...]string{
|
|
|
|
|
payloadCreator("Info"),
|
|
|
|
|
payloadCreator("info"),
|
|
|
|
|
payloadCreator("inFo "),
|
|
|
|
|
payloadCreator("infO "),
|
|
|
|
|
payloadCreator(" INFO "),
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-19 21:04:08 +03:00
|
|
|
for _, payload := range payloads { // nolint:scopelint // false-positive
|
2021-07-16 17:28:13 +03:00
|
|
|
t.Run(payload, func(t *testing.T) {
|
|
|
|
|
result := unmarshal(payload, unmarshaller)
|
|
|
|
|
assert.Equal(t, result.Severity, Info)
|
|
|
|
|
assert.Equal(t, result.Severity.String(), "info")
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-19 21:04:08 +03:00
|
|
|
func testUnmarshalFail(t *testing.T, unmarshaller func(data []byte, v interface{}) error, payloadCreator func(value string) string) {
|
|
|
|
|
assert.Panics(t, func() { unmarshal(payloadCreator("invalid"), unmarshaller) })
|
2021-07-16 17:28:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func unmarshal(value string, unmarshaller func(data []byte, v interface{}) error) SeverityHolder {
|
|
|
|
|
severityStruct := SeverityHolder{}
|
|
|
|
|
var err = unmarshaller([]byte(value), &severityStruct)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
return severityStruct
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-19 21:04:08 +03:00
|
|
|
func createYAML(value string) string {
|
2021-07-16 17:28:13 +03:00
|
|
|
return "severity: " + value + "\n"
|
|
|
|
|
}
|