5
5
"flag"
6
6
"os"
7
7
"path"
8
+ "path/filepath"
8
9
"testing"
9
10
"unicode/utf8"
10
11
@@ -17,118 +18,145 @@ import (
17
18
18
19
var update = flag .Bool ("u" , false , "update golden files" )
19
20
21
+ var optionSets = []struct {
22
+ name string
23
+ opts []formatter.FormatterOption
24
+ }{
25
+ {"default" , nil },
26
+ {"spaceIndent" , []formatter.FormatterOption {formatter .WithIndent (" " )}},
27
+ {"comments" , []formatter.FormatterOption {formatter .WithComments ()}},
28
+ }
29
+
20
30
func TestFormatter_FormatSchema (t * testing.T ) {
21
31
const testSourceDir = "./testdata/source/schema"
22
32
const testBaselineDir = "./testdata/baseline/FormatSchema"
23
33
24
- executeGoldenTesting (t , & goldenConfig {
25
- SourceDir : testSourceDir ,
26
- BaselineFileName : func (cfg * goldenConfig , f os.DirEntry ) string {
27
- return path .Join (testBaselineDir , f .Name ())
28
- },
29
- Run : func (t * testing.T , cfg * goldenConfig , f os.DirEntry ) []byte {
30
- // load stuff
31
- schema , gqlErr := gqlparser .LoadSchema (& ast.Source {
32
- Name : f .Name (),
33
- Input : mustReadFile (path .Join (testSourceDir , f .Name ())),
34
+ for _ , optionSet := range optionSets {
35
+ testBaselineDir := filepath .Join (testBaselineDir , optionSet .name )
36
+ opts := optionSet .opts
37
+ t .Run (optionSet .name , func (t * testing.T ) {
38
+ executeGoldenTesting (t , & goldenConfig {
39
+ SourceDir : testSourceDir ,
40
+ BaselineFileName : func (cfg * goldenConfig , f os.DirEntry ) string {
41
+ return path .Join (testBaselineDir , f .Name ())
42
+ },
43
+ Run : func (t * testing.T , cfg * goldenConfig , f os.DirEntry ) []byte {
44
+ // load stuff
45
+ schema , gqlErr := gqlparser .LoadSchema (& ast.Source {
46
+ Name : f .Name (),
47
+ Input : mustReadFile (path .Join (testSourceDir , f .Name ())),
48
+ })
49
+ if gqlErr != nil {
50
+ t .Fatal (gqlErr )
51
+ }
52
+
53
+ // exec format
54
+ var buf bytes.Buffer
55
+ formatter .NewFormatter (& buf , opts ... ).FormatSchema (schema )
56
+
57
+ // validity check
58
+ _ , gqlErr = gqlparser .LoadSchema (& ast.Source {
59
+ Name : f .Name (),
60
+ Input : buf .String (),
61
+ })
62
+ if gqlErr != nil {
63
+ t .Log (buf .String ())
64
+ t .Fatal (gqlErr )
65
+ }
66
+
67
+ return buf .Bytes ()
68
+ },
34
69
})
35
- if gqlErr != nil {
36
- t .Fatal (gqlErr )
37
- }
38
-
39
- // exec format
40
- var buf bytes.Buffer
41
- formatter .NewFormatter (& buf ).FormatSchema (schema )
42
-
43
- // validity check
44
- _ , gqlErr = gqlparser .LoadSchema (& ast.Source {
45
- Name : f .Name (),
46
- Input : buf .String (),
47
- })
48
- if gqlErr != nil {
49
- t .Log (buf .String ())
50
- t .Fatal (gqlErr )
51
- }
52
-
53
- return buf .Bytes ()
54
- },
55
- })
70
+ })
71
+ }
56
72
}
57
73
58
74
func TestFormatter_FormatSchemaDocument (t * testing.T ) {
59
75
const testSourceDir = "./testdata/source/schema"
60
76
const testBaselineDir = "./testdata/baseline/FormatSchemaDocument"
61
77
62
- executeGoldenTesting (t , & goldenConfig {
63
- SourceDir : testSourceDir ,
64
- BaselineFileName : func (cfg * goldenConfig , f os.DirEntry ) string {
65
- return path .Join (testBaselineDir , f .Name ())
66
- },
67
- Run : func (t * testing.T , cfg * goldenConfig , f os.DirEntry ) []byte {
68
- // load stuff
69
- doc , gqlErr := parser .ParseSchema (& ast.Source {
70
- Name : f .Name (),
71
- Input : mustReadFile (path .Join (testSourceDir , f .Name ())),
72
- })
73
- if gqlErr != nil {
74
- t .Fatal (gqlErr )
75
- }
76
-
77
- // exec format
78
- var buf bytes.Buffer
79
- formatter .NewFormatter (& buf ).FormatSchemaDocument (doc )
80
-
81
- // validity check
82
- _ , gqlErr = parser .ParseSchema (& ast.Source {
83
- Name : f .Name (),
84
- Input : buf .String (),
78
+ for _ , optionSet := range optionSets {
79
+ testBaselineDir := filepath .Join (testBaselineDir , optionSet .name )
80
+ opts := optionSet .opts
81
+ t .Run (optionSet .name , func (t * testing.T ) {
82
+ executeGoldenTesting (t , & goldenConfig {
83
+ SourceDir : testSourceDir ,
84
+ BaselineFileName : func (cfg * goldenConfig , f os.DirEntry ) string {
85
+ return path .Join (testBaselineDir , f .Name ())
86
+ },
87
+ Run : func (t * testing.T , cfg * goldenConfig , f os.DirEntry ) []byte {
88
+ // load stuff
89
+ doc , gqlErr := parser .ParseSchema (& ast.Source {
90
+ Name : f .Name (),
91
+ Input : mustReadFile (path .Join (testSourceDir , f .Name ())),
92
+ })
93
+ if gqlErr != nil {
94
+ t .Fatal (gqlErr )
95
+ }
96
+
97
+ // exec format
98
+ var buf bytes.Buffer
99
+ formatter .NewFormatter (& buf , opts ... ).FormatSchemaDocument (doc )
100
+
101
+ // validity check
102
+ _ , gqlErr = parser .ParseSchema (& ast.Source {
103
+ Name : f .Name (),
104
+ Input : buf .String (),
105
+ })
106
+ if gqlErr != nil {
107
+ t .Log (buf .String ())
108
+ t .Fatal (gqlErr )
109
+ }
110
+
111
+ return buf .Bytes ()
112
+ },
85
113
})
86
- if gqlErr != nil {
87
- t .Log (buf .String ())
88
- t .Fatal (gqlErr )
89
- }
90
-
91
- return buf .Bytes ()
92
- },
93
- })
114
+ })
115
+ }
94
116
}
95
117
96
118
func TestFormatter_FormatQueryDocument (t * testing.T ) {
97
119
const testSourceDir = "./testdata/source/query"
98
120
const testBaselineDir = "./testdata/baseline/FormatQueryDocument"
99
121
100
- executeGoldenTesting (t , & goldenConfig {
101
- SourceDir : testSourceDir ,
102
- BaselineFileName : func (cfg * goldenConfig , f os.DirEntry ) string {
103
- return path .Join (testBaselineDir , f .Name ())
104
- },
105
- Run : func (t * testing.T , cfg * goldenConfig , f os.DirEntry ) []byte {
106
- // load stuff
107
- doc , gqlErr := parser .ParseQuery (& ast.Source {
108
- Name : f .Name (),
109
- Input : mustReadFile (path .Join (testSourceDir , f .Name ())),
122
+ for _ , optionSet := range optionSets {
123
+ testBaselineDir := filepath .Join (testBaselineDir , optionSet .name )
124
+ opts := optionSet .opts
125
+ t .Run (optionSet .name , func (t * testing.T ) {
126
+ executeGoldenTesting (t , & goldenConfig {
127
+ SourceDir : testSourceDir ,
128
+ BaselineFileName : func (cfg * goldenConfig , f os.DirEntry ) string {
129
+ return path .Join (testBaselineDir , f .Name ())
130
+ },
131
+ Run : func (t * testing.T , cfg * goldenConfig , f os.DirEntry ) []byte {
132
+ // load stuff
133
+ doc , gqlErr := parser .ParseQuery (& ast.Source {
134
+ Name : f .Name (),
135
+ Input : mustReadFile (path .Join (testSourceDir , f .Name ())),
136
+ })
137
+ if gqlErr != nil {
138
+ t .Fatal (gqlErr )
139
+ }
140
+
141
+ // exec format
142
+ var buf bytes.Buffer
143
+ formatter .NewFormatter (& buf , opts ... ).FormatQueryDocument (doc )
144
+
145
+ // validity check
146
+ _ , gqlErr = parser .ParseQuery (& ast.Source {
147
+ Name : f .Name (),
148
+ Input : buf .String (),
149
+ })
150
+ if gqlErr != nil {
151
+ t .Log (buf .String ())
152
+ t .Fatal (gqlErr )
153
+ }
154
+
155
+ return buf .Bytes ()
156
+ },
110
157
})
111
- if gqlErr != nil {
112
- t .Fatal (gqlErr )
113
- }
114
-
115
- // exec format
116
- var buf bytes.Buffer
117
- formatter .NewFormatter (& buf ).FormatQueryDocument (doc )
118
-
119
- // validity check
120
- _ , gqlErr = parser .ParseQuery (& ast.Source {
121
- Name : f .Name (),
122
- Input : buf .String (),
123
- })
124
- if gqlErr != nil {
125
- t .Log (buf .String ())
126
- t .Fatal (gqlErr )
127
- }
128
-
129
- return buf .Bytes ()
130
- },
131
- })
158
+ })
159
+ }
132
160
}
133
161
134
162
type goldenConfig struct {
@@ -178,11 +206,11 @@ func executeGoldenTesting(t *testing.T, cfg *goldenConfig) {
178
206
179
207
expected , err := os .ReadFile (expectedFilePath )
180
208
if os .IsNotExist (err ) {
181
- err = os .MkdirAll (path .Dir (expectedFilePath ), 0755 )
209
+ err = os .MkdirAll (path .Dir (expectedFilePath ), 0o755 )
182
210
if err != nil {
183
211
t .Fatal (err )
184
212
}
185
- err = os .WriteFile (expectedFilePath , result , 0444 )
213
+ err = os .WriteFile (expectedFilePath , result , 0o444 )
186
214
if err != nil {
187
215
t .Fatal (err )
188
216
}
0 commit comments