event-gateway/internal/source/types/types_test.go

63 lines
1.2 KiB
Go
Raw Permalink Normal View History

2024-04-20 20:31:06 +00:00
// Copyright © 2023 Roberto Hidalgo <event-gateway@un.rob.mx>
// SPDX-License-Identifier: Apache-2.0
package types_test
import (
"encoding/json"
"fmt"
"testing"
"git.rob.mx/nidito/event-gateway/internal/source/types"
)
func TestUmarshalJSON(t *testing.T) {
cases := []struct {
Name string
JSON string
Error error
Expected types.Kind
}{
{
Name: "empty",
JSON: `""`,
Error: types.ErrInvalid,
},
{
Name: "number",
JSON: `42`,
Error: fmt.Errorf("json: cannot unmarshal number into Go value of type string"),
},
{
Name: "invalid",
JSON: `"42"`,
Error: types.ErrInvalid,
},
{
Name: "valid",
JSON: `"consul"`,
Error: nil,
Expected: types.Consul,
},
}
for _, c := range cases {
t.Run(c.Name, func(t *testing.T) {
var res types.Kind
if err := json.Unmarshal([]byte(c.JSON), &res); err != nil {
if c.Error == nil {
t.Fatalf("unexpected error for %s: %s", c.Name, err)
}
if err.Error() != c.Error.Error() {
t.Fatalf("unexpected error for %s:\n\twanted: %s\n\n\tgot : %s", c.Name, c.Error, err)
}
return
}
if res != c.Expected {
t.Fatalf("did not parse into correct value, wanted: %s got: %s", c.Expected, res)
}
})
}
}