This repository has been archived by the owner on Jun 6, 2018. It is now read-only.
forked from tomnomnom/gron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
163 lines (136 loc) · 3.43 KB
/
main_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
156
157
158
159
160
161
162
163
package main
import (
"bytes"
"encoding/json"
"io/ioutil"
"os"
"reflect"
"testing"
)
func TestGron(t *testing.T) {
cases := []struct {
inFile string
outFile string
}{
{"testdata/one.json", "testdata/one.gron"},
{"testdata/two.json", "testdata/two.gron"},
{"testdata/three.json", "testdata/three.gron"},
{"testdata/github.json", "testdata/github.gron"},
}
for _, c := range cases {
in, err := os.Open(c.inFile)
if err != nil {
t.Fatalf("failed to open input file: %s", err)
}
want, err := ioutil.ReadFile(c.outFile)
if err != nil {
t.Fatalf("failed to open want file: %s", err)
}
out := &bytes.Buffer{}
code, err := gron(in, out, optMonochrome)
if code != exitOK {
t.Errorf("want exitOK; have %d", code)
}
if err != nil {
t.Errorf("want nil error; have %s", err)
}
if !reflect.DeepEqual(want, out.Bytes()) {
t.Logf("want: %s", want)
t.Logf("have: %s", out.Bytes())
t.Errorf("gronned %s does not match %s", c.inFile, c.outFile)
}
}
}
func TestGronStream(t *testing.T) {
cases := []struct {
inFile string
outFile string
}{
{"testdata/stream.json", "testdata/stream.gron"},
}
for _, c := range cases {
in, err := os.Open(c.inFile)
if err != nil {
t.Fatalf("failed to open input file: %s", err)
}
want, err := ioutil.ReadFile(c.outFile)
if err != nil {
t.Fatalf("failed to open want file: %s", err)
}
out := &bytes.Buffer{}
code, err := gronStream(in, out, optMonochrome)
if code != exitOK {
t.Errorf("want exitOK; have %d", code)
}
if err != nil {
t.Errorf("want nil error; have %s", err)
}
if !reflect.DeepEqual(want, out.Bytes()) {
t.Logf("want: %s", want)
t.Logf("have: %s", out.Bytes())
t.Errorf("gronned %s does not match %s", c.inFile, c.outFile)
}
}
}
func TestUngron(t *testing.T) {
cases := []struct {
inFile string
outFile string
}{
{"testdata/one.gron", "testdata/one.json"},
{"testdata/two.gron", "testdata/two.json"},
{"testdata/three.gron", "testdata/three.json"},
{"testdata/grep-separators.gron", "testdata/grep-separators.json"},
{"testdata/github.gron", "testdata/github.json"},
}
for _, c := range cases {
wantF, err := ioutil.ReadFile(c.outFile)
if err != nil {
t.Fatalf("failed to open want file: %s", err)
}
var want interface{}
err = json.Unmarshal(wantF, &want)
if err != nil {
t.Fatalf("failed to unmarshal JSON from want file: %s", err)
}
in, err := os.Open(c.inFile)
if err != nil {
t.Fatalf("failed to open input file: %s", err)
}
out := &bytes.Buffer{}
code, err := ungron(in, out, optMonochrome)
if code != exitOK {
t.Errorf("want exitOK; have %d", code)
}
if err != nil {
t.Errorf("want nil error; have %s", err)
}
var have interface{}
err = json.Unmarshal(out.Bytes(), &have)
if err != nil {
t.Fatalf("failed to unmarshal JSON from ungron output: %s", err)
}
if !reflect.DeepEqual(want, have) {
t.Logf("want: %#v", want)
t.Logf("have: %#v", have)
t.Errorf("ungronned %s does not match %s", c.inFile, c.outFile)
}
}
}
func BenchmarkBigJSON(b *testing.B) {
in, err := os.Open("testdata/big.json")
if err != nil {
b.Fatalf("failed to open test data file: %s", err)
}
for i := 0; i < b.N; i++ {
out := &bytes.Buffer{}
_, err = in.Seek(0, 0)
if err != nil {
b.Fatalf("failed to rewind input: %s", err)
}
_, err := gron(in, out, optMonochrome|optNoSort)
if err != nil {
b.Fatalf("failed to gron: %s", err)
}
}
}