Skip to content

Commit

Permalink
Add mechanism to test contents of result.Errors()
Browse files Browse the repository at this point in the history
  • Loading branch information
timbunce committed May 31, 2020
1 parent fd8e6d1 commit 76ea6eb
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
48 changes: 45 additions & 3 deletions jsonschema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,31 @@ type jsonSchemaTest struct {
Tests []jsonSchemaTestCase `json:"tests"`
}
type jsonSchemaTestCase struct {
Description string `json:"description"`
Data interface{} `json:"data"`
Valid bool `json:"valid"`
Description string `json:"description"`
Data interface{} `json:"data"`
Valid bool `json:"valid"`
Errors []jsonSchemaTestError `json:"errors"`
}

type jsonSchemaTestError struct {
Field string `json:"field"`
Type string `json:"type"`
Context string `json:"context"` // .Context().String()
Description string `json:"description"`
Details string `json:"details"` // fmt.Sprintf %v of .Details() map[string]string
String string `json:"string"`
}

func asTestError(re ResultError) jsonSchemaTestError {
rei := jsonSchemaTestError{
Field: re.Field(),
Type: re.Type(),
Context: re.Context().String(),
Description: re.Description(),
Details: fmt.Sprintf("%v", re.Details()),
String: re.String(),
}
return rei
}

//Skip any directories not named appropiately
Expand Down Expand Up @@ -111,6 +133,26 @@ func executeTests(t *testing.T, path string) error {
*schemaString,
*testCaseString)
}

// if validation failed, and that was expected, then test the result.Errors()
if !result.Valid() && !testCase.Valid {
tcerrs := testCase.Errors
if tcerrs == nil {
t.Log("'errors' not set in test case so no coverage")
return
}
rerrs := result.Errors()
if len(rerrs) != len(tcerrs) {
t.Errorf("expected %d errors but got %d", len(tcerrs), len(rerrs))
}
for i, re := range result.Errors() {
jRE, _ := marshalToJSONString(asTestError(re))
jTC, _ := marshalToJSONString(tcerrs[i])
if *jRE != *jTC {
t.Errorf("result.Errors %d: expected\n%s got\n%s", i, *jRE, *jTC)
}
}
}
})
}
})
Expand Down
9 changes: 6 additions & 3 deletions testdata/draft4/optional/format.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
{
"description": "an invalid date-time string",
"data": "06/19/1963 08:30:06 PST",
"valid": false
"valid": false,
"errors": [ {"field":"(root)","type":"format","context":"(root)","description":"Does not match format 'date-time'","details":"map[context:(root) field:(root) format:date-time]","string":"(root): Does not match format 'date-time'"} ]
},
{
"description": "only RFC3339 not all of ISO 8601 are valid",
"data": "2013-350T01:01:01",
"valid": false
"valid": false,
"errors": [ {"field":"(root)","type":"format","context":"(root)","description":"Does not match format 'date-time'","details":"map[context:(root) field:(root) format:date-time]","string":"(root): Does not match format 'date-time'"} ]
}
]
},
Expand Down Expand Up @@ -92,7 +94,8 @@
{
"description": "an invalid protocol-relative URI Reference",
"data": "//foo.bar/?baz=qux#quux",
"valid": false
"valid": false,
"errors": [ {"field":"(root)","type":"format","context":"(root)","description":"Does not match format 'uri'","details":"map[context:(root) field:(root) format:uri]","string":"(root): Does not match format 'uri'"} ]
},
{
"description": "an invalid relative URI Reference",
Expand Down

0 comments on commit 76ea6eb

Please sign in to comment.