-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
json_test.go
79 lines (65 loc) · 1.61 KB
/
json_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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package faker
import (
"encoding/json"
"testing"
)
func TestJsonString(t *testing.T) {
faker := New()
j := faker.Json()
for i := 0; i < 10; i++ {
result := j.String()
// Attempt to unmarshal the result into a map[string]interface{}
var data map[string]interface{}
err := json.Unmarshal([]byte(result), &data)
Expect(t, err, nil)
// Ensure that the result is a valid JSON object
Expect(t, len(data) > 0, true)
// Ensure that all attribute values are valid JSON types
for _, value := range data {
switch value.(type) {
case string, float64, bool, nil:
// Valid JSON types
case []interface{}:
// Valid JSON array type
case map[string]interface{}:
// Valid JSON object type
default:
t.FailNow()
}
}
}
}
func TestJsonObject(t *testing.T) {
faker := New()
j := faker.Json()
for i := 0; i < 10; i++ {
data := j.Object()
// Ensure that the result is not nil
NotExpect(t, data, nil)
// Ensure that the result is a valid JSON object
Expect(t, len(data) > 0, true)
// Ensure that all attribute values are valid JSON types
for _, value := range data {
switch value.(type) {
case string, float64, bool, nil:
// Valid JSON types
case []interface{}:
// Valid JSON array type
case map[string]interface{}:
// Valid JSON object type
default:
t.FailNow()
}
}
}
}
func TestRandomAttributeValueFromListAsStringPanicWhenUnsupportedTypeIsPassed(t *testing.T) {
faker := New()
j := faker.Json()
defer func() {
if r := recover(); r == nil {
NotExpect(t, r, nil)
}
}()
j.randomAttributeValueFromListAsString([]string{"unsupported"})
}