Skip to content

Commit

Permalink
Use t.Run() to execute TestFormats tests as subtests
Browse files Browse the repository at this point in the history
Now -run is more useful and the logs look like this:
--- PASS: TestFormats (0.06s)
    --- PASS: TestFormats/draft4 (0.01s)
        --- PASS: TestFormats/draft4/validation_of_date-time_strings (0.00s)
            --- PASS: TestFormats/draft4/validation_of_date-time_strings/a_valid_date-time_string (0.00s)
            --- PASS: TestFormats/draft4/validation_of_date-time_strings/an_invalid_date-time_string (0.00s)
            --- PASS: TestFormats/draft4/validation_of_date-time_strings/only_RFC3339_not_all_of_ISO_8601_are_valid (0.00s)
        --- PASS: TestFormats/draft4/validation_of_URIs (0.00s)
            --- PASS: TestFormats/draft4/validation_of_URIs/a_valid_URL_with_anchor_tag (0.00s)
            --- PASS: TestFormats/draft4/validation_of_URIs/a_valid_URL_with_anchor_tag_and_parantheses (0.00s)
  • Loading branch information
timbunce committed May 31, 2020
1 parent 4e8b446 commit fd8e6d1
Showing 1 changed file with 53 additions and 52 deletions.
105 changes: 53 additions & 52 deletions jsonschema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ func executeTests(t *testing.T, path string) error {
d := json.NewDecoder(file)
d.UseNumber()
err = d.Decode(&tests)

if err != nil {
t.Errorf("Error (%s)\n", err.Error())
}
Expand All @@ -71,49 +70,50 @@ func executeTests(t *testing.T, path string) error {
}

for _, test := range tests {
fmt.Println(" " + test.Description)

if test.Disabled {
continue
}

testSchemaLoader := NewRawLoader(test.Schema)
sl := NewSchemaLoader()
sl.Draft = draft
sl.Validate = true
testSchema, err := sl.Compile(testSchemaLoader)

if err != nil {
t.Errorf("Error (%s)\n", err.Error())
}

for _, testCase := range test.Tests {
testDataLoader := NewRawLoader(testCase.Data)
result, err := testSchema.Validate(testDataLoader)
t.Run(test.Description, func(t *testing.T) {

sl := NewSchemaLoader()
sl.Draft = draft
sl.Validate = true
testSchema, err := sl.Compile(NewRawLoader(test.Schema))
if err != nil {
t.Errorf("Error (%s)\n", err.Error())
}

if result.Valid() != testCase.Valid {
schemaString, _ := marshalToJSONString(test.Schema)
testCaseString, _ := marshalToJSONString(testCase.Data)

t.Errorf("Test failed : %s\n"+
"%s.\n"+
"%s.\n"+
"expects: %t, given %t\n"+
"Schema: %s\n"+
"Data: %s\n",
file.Name(),
test.Description,
testCase.Description,
testCase.Valid,
result.Valid(),
*schemaString,
*testCaseString)
for _, testCase := range test.Tests {
t.Run(testCase.Description, func(t *testing.T) {

result, err := testSchema.Validate(NewRawLoader(testCase.Data))
if err != nil {
t.Errorf("Error (%s)\n", err.Error())
}

if result.Valid() != testCase.Valid {
schemaString, _ := marshalToJSONString(test.Schema)
testCaseString, _ := marshalToJSONString(testCase.Data)

t.Errorf("Test failed : %s\n"+
"%s.\n"+
"%s.\n"+
"expects: %t, given %t\n"+
"Schema: %s\n"+
"Data: %s\n",
file.Name(),
test.Description,
testCase.Description,
testCase.Valid,
result.Valid(),
*schemaString,
*testCaseString)
}
})
}
}
})
}
return nil
}
Expand Down Expand Up @@ -155,35 +155,36 @@ func TestFormats(t *testing.T) {
wd = filepath.Join(wd, "testdata")

dirs, err := ioutil.ReadDir(wd)

if err != nil {
panic(err.Error())
}

for _, dir := range dirs {
if testDirectories.MatchString(dir.Name()) {
formatJSONFile := filepath.Join(wd, dir.Name(), "optional", "format.json")
if _, err = os.Stat(formatJSONFile); err == nil {
err = executeTests(t, formatJSONFile)
} else {
err = nil
}
t.Run(dir.Name(), func(t *testing.T) {
formatJSONFile := filepath.Join(wd, dir.Name(), "optional", "format.json")
if _, err = os.Stat(formatJSONFile); err == nil {
err = executeTests(t, formatJSONFile)
} else {
err = nil
}

if err != nil {
t.Errorf("Error (%s)\n", err.Error())
}
if err != nil {
t.Errorf("Error (%s)\n", err.Error())
}

formatsDirectory := filepath.Join(wd, dir.Name(), "optional", "format")
err = filepath.Walk(formatsDirectory, func(path string, fileInfo os.FileInfo, err error) error {
if fileInfo == nil || !strings.HasSuffix(fileInfo.Name(), ".json") {
return nil
}
return executeTests(t, path)
})

formatsDirectory := filepath.Join(wd, dir.Name(), "optional", "format")
err = filepath.Walk(formatsDirectory, func(path string, fileInfo os.FileInfo, err error) error {
if fileInfo == nil || !strings.HasSuffix(fileInfo.Name(), ".json") {
return nil
if err != nil {
t.Errorf("Error (%s)\n", err.Error())
}
return executeTests(t, path)
})

if err != nil {
t.Errorf("Error (%s)\n", err.Error())
}
}
}
}

0 comments on commit fd8e6d1

Please sign in to comment.