forked from tomnomnom/gron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
103 lines (86 loc) · 2.13 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
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"},
}
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, true)
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"},
}
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, true)
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)
}
}
}