-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmarshal_test.go
65 lines (62 loc) · 1.5 KB
/
marshal_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
package schema_test
import (
"encoding/json"
"strings"
"testing"
"github.com/lestrrat-go/jsschema"
"github.com/lestrrat-go/jsschema/validator"
"github.com/stretchr/testify/assert"
)
func TestMarshal(t *testing.T) {
// roundTripSchemas are schemas that can be read and written back to the same
// content. They also include an object that should match the schema to make
// sure the schema is correctly written.
var roundTripSchemas = []struct {
Name string
Schema string
ValidValue interface{}
}{{
Name: "Integer",
Schema: `{
"type": "integer"
}`,
ValidValue: int(0),
}, {
Name: "String",
Schema: `{
"type": "string"
}`,
ValidValue: "value",
}, {
Name: "Object",
Schema: `{
"additionalProperties": false,
"properties": {
"attr": {
"type": "integer"
}
},
"type": "object"
}`,
ValidValue: struct{ attr int }{10},
}}
for _, definition := range roundTripSchemas {
t.Logf("Testing schema %s", definition.Name)
s, err := schema.Read(strings.NewReader(definition.Schema))
if !assert.NoError(t, err, "schema.Read should succeed") {
return
}
v := validator.New(s)
err = v.Validate(definition.ValidValue)
if !assert.NoError(t, err, "ValidValue should Validate successfully") {
return
}
output, err := json.MarshalIndent(s, "", " ")
if !assert.NoError(t, err, "json.Marshal should succeed") {
return
}
if !assert.Equal(t, definition.Schema, string(output), "json.Marshal should result in the same value as the input.") {
return
}
}
}