-
Notifications
You must be signed in to change notification settings - Fork 0
/
types_test.go
43 lines (37 loc) · 1020 Bytes
/
types_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package proxmox
import (
"encoding/json"
"github.com/stretchr/testify/require"
"testing"
)
type testIntOrStringExample struct {
ID IntOrString `json:"id"`
}
// TestUnmarshalIntOrString_Positive tests the unmarshalling of valid IntOrString values.
func TestUnmarshalIntOrString_Positive(t *testing.T) {
tests := []struct {
input string
expected string
}{
{`{"id": 12345}`, "12345"},
{`{"id": "abc123"}`, "abc123"},
}
for _, test := range tests {
var example testIntOrStringExample
err := json.Unmarshal([]byte(test.input), &example)
require.NoError(t, err)
require.Equal(t, IntOrString(test.expected), example.ID)
}
}
// TestUnmarshalIntOrString_Negative tests the unmarshalling of invalid IntOrString values.
func TestUnmarshalIntOrString_Negative(t *testing.T) {
tests := []string{
`{"id": {"nested": "object"}}`,
`{"id": [12345]}`,
}
for _, test := range tests {
var example testIntOrStringExample
err := json.Unmarshal([]byte(test), &example)
require.Error(t, err)
}
}