-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_suite_validate.go
More file actions
77 lines (64 loc) · 1.55 KB
/
test_suite_validate.go
File metadata and controls
77 lines (64 loc) · 1.55 KB
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
// +build none
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"reflect"
"strings"
"github.com/twmb/chkjson"
)
func chk(err error) {
if err != nil {
panic(err)
}
}
// This file checks that all y_ and n_ files in
// github.com/nst/JSONTestSuite/test_parsing
// are validated and compacted properly.
//
// The repo is expected to be in the dir this runs in.
func main() {
const path = "JSONTestSuite/test_parsing/"
dirents, err := ioutil.ReadDir(path)
chk(err)
for _, dirent := range dirents {
name := dirent.Name()
raw, err := ioutil.ReadFile(path + name)
chk(err)
gotValid := chkjson.Valid(raw)
should := strings.HasPrefix(name, "y_")
shouldNot := strings.HasPrefix(name, "n_")
if should && !gotValid {
fmt.Printf("FAIL %s\n", name)
}
if shouldNot && gotValid {
fmt.Printf("FAIL %s\n", name)
}
officialValid := json.Valid(raw)
if officialValid != gotValid {
fmt.Printf("MISMATCH %s (us: %v, stdlib: %v)\n", name, gotValid, officialValid)
}
if !should {
continue
}
var exp interface{}
chk(json.Unmarshal(raw, &exp))
for _, fn := range []struct {
name string
fn func([]byte, []byte) ([]byte, bool)
}{
{"AppendCompact", chkjson.AppendCompact},
{"AppendCompactJSONP", chkjson.AppendCompactJSONP},
{"Compact", func(_, in []byte) ([]byte, bool) { return chkjson.Compact(in) }},
} {
dup := append([]byte(nil), raw...)
dup, _ = fn.fn(dup[:0], dup)
var got interface{}
chk(json.Unmarshal(dup, &got))
if !reflect.DeepEqual(exp, got) {
fmt.Printf("FAIL %s\n", name)
}
}
}
}