-
Notifications
You must be signed in to change notification settings - Fork 2
/
client_test.go
50 lines (45 loc) · 1.32 KB
/
client_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
44
45
46
47
48
49
50
package iaqualink
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestStringOrNumber(t *testing.T) {
t.Run("Encode and Decode", func(t *testing.T) {
t.Run("String", func(t *testing.T) {
var value StringOrNumber
input := []byte(`"676446"`)
err := json.Unmarshal(input, &value)
require.Nil(t, err)
assert.Equal(t, StringOrNumber{stringValue: "676446", isString: true}, value)
assert.Equal(t, "676446", value.String())
output, err := json.Marshal(value)
require.Nil(t, err)
assert.Equal(t, input, output)
})
t.Run("Number", func(t *testing.T) {
var value StringOrNumber
input := []byte(`676446`)
err := json.Unmarshal(input, &value)
require.Nil(t, err)
assert.Equal(t, StringOrNumber{stringValue: "676446", isString: false}, value)
assert.Equal(t, "676446", value.String())
output, err := json.Marshal(value)
require.Nil(t, err)
assert.Equal(t, input, output)
})
t.Run("Bad String", func(t *testing.T) {
var value StringOrNumber
input := []byte(`"676446`)
err := json.Unmarshal(input, &value)
require.NotNil(t, err)
})
t.Run("Bad Number", func(t *testing.T) {
var value StringOrNumber
input := []byte(`676 446`)
err := json.Unmarshal(input, &value)
require.NotNil(t, err)
})
})
}