This repository has been archived by the owner on Apr 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
format_test.go
115 lines (105 loc) · 1.93 KB
/
format_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
package vendorfmt
import (
"encoding/json"
"reflect"
"strings"
"testing"
)
func TestFmt(t *testing.T) {
decode := func(s string) map[string]interface{} {
var m map[string]interface{}
if err := json.NewDecoder(strings.NewReader(s)).Decode(&m); err != nil {
t.Fatalf("got error %s want nil", err)
}
return m
}
in := `{
"comment": "",
"ignore": "test",
"package": [
{
"checksum": "a",
"path": "x",
"revision": true
},
{
"checksum": "b",
"path": "y",
"revision": false
},
{
"checksum": "c",
"path": "z/x",
"revision": true
}
]
}`
want := `{
"comment": "",
"ignore": "test",
"package": [
{"path":"x","checksum":"a","revision":true},
{"path":"y","checksum":"b","revision":false},
{"path":"z/x","checksum":"c","revision":true}
]
}
`
got, err := FormatString(in)
if err != nil {
t.Fatalf("got %v want nil", err)
}
// verify content is equal
if got, want := decode(got), decode(in); !reflect.DeepEqual(got, want) {
t.Fatalf("got %#v want %#v", got, want)
}
// verify format is ok
if got != want {
t.Fatalf("got:\n%s\nwant:\n%s", got, want)
}
}
func TestPkgSort(t *testing.T) {
in := `{
"comment": "",
"ignore": "test",
"package": [
{
"checksum": "b",
"path": "y",
"revision":false
},
{
"checksum": "a",
"path": "x",
"revision":true
},
{
"checksum": "c",
"path": "z/x",
"revision":true
}
]
}`
want := `{
"comment": "",
"ignore": "test",
"package": [
{"path":"x","checksum":"a","revision":true},
{"path":"y","checksum":"b","revision":false},
{"path":"z/x","checksum":"c","revision":true}
]
}
`
got, err := FormatString(in)
if err != nil {
t.Fatalf("got %v want nil", err)
}
// verify packages are sorted
if got != want {
t.Fatalf("got:\n%s\nwant:\n%s", got, want)
}
// verify json is still legal
var v interface{}
if err := json.Unmarshal([]byte(got), &v); err != nil {
t.Fatalf("json.Unmarshal failed: %s", err)
}
}