-
Notifications
You must be signed in to change notification settings - Fork 3
/
test_test.go
155 lines (140 loc) · 3.32 KB
/
test_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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright 2020 by David A. Golden. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
package jibby
import (
"bufio"
"bytes"
"encoding/hex"
"io/ioutil"
"strings"
"testing"
"go.mongodb.org/mongo-driver/bson"
)
type unmarshalTestCase struct {
label string
input string
output string
errStr string
}
func testWithUnmarshal(t *testing.T, cases []unmarshalTestCase, extJSON bool) {
t.Helper()
for _, c := range cases {
c := c
t.Run(c.label, func(t *testing.T) {
t.Parallel()
var err error
buf := make([]byte, 0, 256)
if extJSON {
buf, err = UnmarshalExtJSON([]byte(c.input), buf)
} else {
buf, err = Unmarshal([]byte(c.input), buf)
}
if c.errStr != "" {
var got string
if err != nil {
got = err.Error()
t.Log(got)
}
if !strings.Contains(got, c.errStr) {
t.Errorf("expected error with '%s', but got %v", c.errStr, got)
}
} else {
if err != nil {
t.Errorf("unexpected error: %v", err)
} else {
c.output = strings.ToLower(c.output)
expect, err := hex.DecodeString(c.output)
if err != nil {
t.Fatalf("error decoding test output: %v", err)
}
if !bytes.Equal(expect, buf) {
t.Fatalf("Unmarshal doesn't match expected:\nGot: %v\nExpect: %v", hex.EncodeToString(buf), c.output)
}
}
}
})
}
}
func getTestFiles(t *testing.T, dir, prefix, suffix string) []string {
files, err := ioutil.ReadDir(dir)
if err != nil {
t.Fatal(err)
}
keep := make([]string, 0)
for _, file := range files {
name := file.Name()
if prefix != "" {
if !strings.HasPrefix(name, prefix) {
continue
}
}
if suffix != "" {
if !strings.HasSuffix(name, suffix) {
continue
}
}
keep = append(keep, name)
}
return keep
}
func convertWithJibby(input []byte) ([]byte, error) {
jsonReader := bufio.NewReader(bytes.NewReader(input))
jib, err := NewDecoder(jsonReader)
if err != nil {
return nil, err
}
jib.ExtJSON(true)
jib.MaxDepth(1000)
return jib.Decode(make([]byte, 0, 256))
}
func convertWithGoDriver(input []byte) ([]byte, error) {
var got bson.Raw
err := bson.UnmarshalExtJSON(input, false, &got)
return got, err
}
// objectify takes non-object top level value (e.g. `[ 42 ]`) and turns it into
// an object (e.g. `{ "a": [ 42 ] }`), as jibby only converts objects at the top
// level. A BOM and leading white space is preserved.
func objectify(input []byte) []byte {
// Skip over BOM and leading spaces
i := bomLength(input)
for i < len(input) {
if input[i] != ' ' {
break
}
i++
}
if input[i] != '{' {
object := make([]byte, 0)
object = append(object, input[0:i]...)
object = append(object, []byte(`{"a":`)...)
object = append(object, input[i:]...)
object = append(object, '}')
return object
}
return input
}
func bomLength(input []byte) int {
if len(input) < 2 {
return 0
}
if bytes.Equal(input[0:2], utf16BEBOM) || bytes.Equal(input[0:2], utf16LEBOM) {
return 2
}
if len(input) < 3 {
return 0
}
if bytes.Equal(input[0:3], utf8BOM) {
return 3
}
if len(input) < 4 {
return 0
}
if bytes.Equal(input[0:4], utf32BEBOM) || bytes.Equal(input[0:4], utf32LEBOM) {
return 4
}
return 0
}