-
Notifications
You must be signed in to change notification settings - Fork 0
/
unflatten_test.go
64 lines (58 loc) · 1.4 KB
/
unflatten_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
package main
import (
"bytes"
"io"
"os"
"strings"
"testing"
"github.com/matryer/is"
)
func TestUnflatten(t *testing.T) {
tts := []struct {
in string
out string
}{
{"json.key\t42", `{"key":42}`},
{"json[11]\t42", `[42]`},
{"json[\"11\"]\t42", `{"11":42}`},
{"json.k1.k2[11]\t42", `{"k1":{"k2":[42]}}`},
{"json.k1\t\"41\"\njson.k2\t42\njson.k3\t43", `{"k1":"41","k2":42,"k3":43}`},
{"json.k1.sub\t41\njson.k2.sub\t42\njson.k2.sub2\t422\njson.k3\t43", `{"k1":{"sub":41},"k2":{"sub":42,"sub2":422},"k3":43}`},
{"json[1].sub\t41\njson[2].sub\t42\njson[2].sub2\t422\njson[33]\t43", `[{"sub":41},{"sub":42,"sub2":422},43]`},
{
strings.Join([]string{
"json.!@#$%^&*()-_=+|{}привет,<>/?\t1",
"json[\"x x\"]\t2",
"json[\"x][x\"]\t3",
"json[\"x.x\"]\t4",
}, "\n"),
`{"!@#$%^&*()-_=+|{}привет,<>/?":1,"x x":2,"x][x":3,"x.x":4}`,
},
}
for _, tt := range tts {
t.Run(tt.out, func(t *testing.T) {
var (
is = is.New(t)
w strings.Builder
)
unflatten(strings.NewReader(tt.in), &w, false)
is.Equal(tt.out, w.String())
})
}
}
func BenchmarkUnflatten(b *testing.B) {
var (
f, _ = os.Open("testdata/big.fj")
data, err = io.ReadAll(f)
buf = bytes.NewReader(data)
)
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, _ = buf.Seek(0, io.SeekStart)
unflatten(buf, io.Discard, false)
}
}