-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformatter_test.go
More file actions
165 lines (148 loc) · 4.16 KB
/
formatter_test.go
File metadata and controls
165 lines (148 loc) · 4.16 KB
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
164
165
package fracturedjson
import (
"encoding/json"
"os"
"path/filepath"
"testing"
)
type fileCase struct {
Name string `json:"name"`
Before string `json:"before"`
After string `json:"after"`
Options caseOptions `json:"options"`
}
type caseOptions struct {
MaxTotalLineLength *int `json:"maxTotalLineLength"`
MaxInlineComplexity *int `json:"maxInlineComplexity"`
MaxTableRowComplexity *int `json:"maxTableRowComplexity"`
AlwaysExpandDepth *int `json:"alwaysExpandDepth"`
SimpleBracketPadding *bool `json:"simpleBracketPadding"`
ColonBeforePropNamePadding *bool `json:"colonBeforePropNamePadding"`
AllowTrailingCommas *bool `json:"allowTrailingCommas"`
PreserveBlankLines *bool `json:"preserveBlankLines"`
CommentPolicy string `json:"commentPolicy"`
NumberListAlignment string `json:"numberListAlignment"`
JSONEolStyle string `json:"jsonEolStyle"`
}
func TestWikiGoldenCases(t *testing.T) {
casesPath := filepath.Join("testdata", "cases.json")
rawCases, err := os.ReadFile(casesPath)
if err != nil {
t.Fatalf("failed to read %s: %v", casesPath, err)
}
var cases []fileCase
if err := json.Unmarshal(rawCases, &cases); err != nil {
t.Fatalf("failed to parse %s: %v", casesPath, err)
}
for _, tc := range cases {
tc := tc
t.Run(tc.Name, func(t *testing.T) {
beforePath := filepath.Join("testdata", tc.Before)
afterPath := filepath.Join("testdata", tc.After)
before, err := os.ReadFile(beforePath)
if err != nil {
t.Fatalf("failed to read %s: %v", beforePath, err)
}
expected, err := os.ReadFile(afterPath)
if err != nil {
t.Fatalf("failed to read %s: %v", afterPath, err)
}
f := NewFormatter()
applyCaseOptions(&f.Options, tc.Options)
out, err := f.Reformat(string(before), 0)
if err != nil {
t.Fatalf("reformat failed: %v", err)
}
exp := string(expected)
if out != exp && out+"\n" != exp {
if !(f.Options.CommentPolicy == CommentPreserve && normalizeOutsideStrings(out) == normalizeOutsideStrings(exp)) {
t.Fatalf("output mismatch\nexpected:\n%s\nactual:\n%s", exp, out+"\n")
}
}
})
}
}
func normalizeOutsideStrings(s string) string {
b := make([]rune, 0, len(s))
inString := false
escaped := false
for _, r := range s {
if inString {
b = append(b, r)
if escaped {
escaped = false
continue
}
if r == '\\' {
escaped = true
continue
}
if r == '"' {
inString = false
}
continue
}
if r == '"' {
inString = true
b = append(b, r)
continue
}
if r == ' ' || r == '\t' || r == '\n' || r == '\r' {
continue
}
b = append(b, r)
}
return string(b)
}
func applyCaseOptions(o *Options, c caseOptions) {
if c.MaxTotalLineLength != nil {
o.MaxTotalLineLength = *c.MaxTotalLineLength
}
if c.MaxInlineComplexity != nil {
o.MaxInlineComplexity = *c.MaxInlineComplexity
}
if c.MaxTableRowComplexity != nil {
o.MaxTableRowComplexity = *c.MaxTableRowComplexity
}
if c.AlwaysExpandDepth != nil {
o.AlwaysExpandDepth = *c.AlwaysExpandDepth
}
if c.SimpleBracketPadding != nil {
o.SimpleBracketPadding = *c.SimpleBracketPadding
}
if c.ColonBeforePropNamePadding != nil {
o.ColonBeforePropNamePadding = *c.ColonBeforePropNamePadding
}
if c.AllowTrailingCommas != nil {
o.AllowTrailingCommas = *c.AllowTrailingCommas
}
if c.PreserveBlankLines != nil {
o.PreserveBlankLines = *c.PreserveBlankLines
}
switch c.CommentPolicy {
case "preserve", "PRESERVE":
o.CommentPolicy = CommentPreserve
case "remove", "REMOVE":
o.CommentPolicy = CommentRemove
case "error", "ERROR", "treataserror", "TreatAsError", "":
o.CommentPolicy = CommentTreatAsError
}
switch c.NumberListAlignment {
case "left", "LEFT":
o.NumberListAlignment = NumberLeft
case "right", "RIGHT":
o.NumberListAlignment = NumberRight
case "decimal", "DECIMAL", "":
o.NumberListAlignment = NumberDecimal
case "normalize", "NORMALIZE":
o.NumberListAlignment = NumberNormalize
}
switch c.JSONEolStyle {
case "lf", "LF":
o.JsonEolStyle = EolLF
case "crlf", "CRLF":
o.JsonEolStyle = EolCRLF
case "default", "DEFAULT", "":
o.JsonEolStyle = EolDefault
}
}