-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils_test.go
52 lines (42 loc) · 1.62 KB
/
utils_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
51
52
package cloudns
import (
"encoding/json"
"github.com/stretchr/testify/assert"
"testing"
)
func TestContainsString(t *testing.T) {
result1 := containsString("yes", []string{"no", "yes", "maybe"})
assert.True(t, result1, "`yes` should be found inside string slice")
result2 := containsString("what", []string{"no", "yes", "maybe"})
assert.False(t, result2, "`what` should not be found inside string slice")
}
func TestAPIBool_MarshalJSON(t *testing.T) {
trueResult, err := json.Marshal(APIBool(true))
assert.NoError(t, err, "JSON marshalling of APIBool with `true` value should not fail")
assert.Equal(t, []byte("1"), trueResult, "JSON for APIBool with `true` should return `1`")
falseResult, err := json.Marshal(APIBool(false))
assert.NoError(t, err, "JSON marshalling of APIBool with `false` value should not fail")
assert.Equal(t, []byte("0"), falseResult, "JSON for APIBool with `false` should return `0`")
}
func TestAPIBool_UnmarshalJSON(t *testing.T) {
test := func(value string, expected APIBool) {
var actual APIBool
err := json.Unmarshal([]byte(value), &actual)
assert.NoError(t, err, "JSON unmarshalling of APIBool(%s) should not fail", value)
assert.Equal(t, expected, actual, "Unmarshalled APIBool(%s) should return %t", expected)
}
test(`true`, true)
test(`1`, true)
test(`"1"`, true)
test(`"true"`, true)
test(`false`, false)
test(`0`, false)
test(`"0"`, false)
test(`"false"`, false)
test(`""`, false)
}
func TestAPIBool_UnmarshalJSON_Invalid(t *testing.T) {
var actual APIBool
err := json.Unmarshal([]byte(`["wat"]`), &actual)
assert.Error(t, err, "JSON unmarshalling of invalid APIBool should fail")
}