1
1
package handler
2
2
3
3
import (
4
+ "fmt"
4
5
"io/ioutil"
5
6
"os"
6
7
"path/filepath"
@@ -11,62 +12,89 @@ import (
11
12
"github.com/lighttiger2505/sqls/internal/lsp"
12
13
)
13
14
14
- func TestFormatting (t * testing.T ) {
15
+ var formattingOptionTab = lsp.FormattingOptions {
16
+ TabSize : 0.0 ,
17
+ InsertSpaces : false ,
18
+ TrimTrailingWhitespace : false ,
19
+ InsertFinalNewline : false ,
20
+ TrimFinalNewlines : false ,
21
+ }
22
+
23
+ var formattingOptionIndentSpace2 = lsp.FormattingOptions {
24
+ TabSize : 2.0 ,
25
+ InsertSpaces : true ,
26
+ TrimTrailingWhitespace : false ,
27
+ InsertFinalNewline : false ,
28
+ TrimFinalNewlines : false ,
29
+ }
30
+
31
+ var formattingOptionIndentSpace4 = lsp.FormattingOptions {
32
+ TabSize : 4.0 ,
33
+ InsertSpaces : true ,
34
+ TrimTrailingWhitespace : false ,
35
+ InsertFinalNewline : false ,
36
+ TrimFinalNewlines : false ,
37
+ }
38
+
39
+ type formattingTestCase struct {
40
+ name string
41
+ input string
42
+ want string
43
+ }
44
+
45
+ func testFormatting (t * testing.T , testCases []formattingTestCase , options lsp.FormattingOptions ) {
15
46
tx := newTestContext ()
16
47
tx .initServer (t )
17
48
defer tx .tearDown ()
18
49
19
50
uri := "file:///Users/octref/Code/css-test/test.sql"
51
+ for _ , tt := range testCases {
52
+ t .Run (tt .name , func (t * testing.T ) {
53
+ // Open dummy file
54
+ didOpenParams := lsp.DidOpenTextDocumentParams {
55
+ TextDocument : lsp.TextDocumentItem {
56
+ URI : uri ,
57
+ LanguageID : "sql" ,
58
+ Version : 0 ,
59
+ Text : tt .input ,
60
+ },
61
+ }
62
+ if err := tx .conn .Call (tx .ctx , "textDocument/didOpen" , didOpenParams , nil ); err != nil {
63
+ t .Fatal ("conn.Call textDocument/didOpen:" , err )
64
+ }
65
+ tx .testFile (t , didOpenParams .TextDocument .URI , didOpenParams .TextDocument .Text )
66
+ // Create completion params
67
+ formattingParams := lsp.DocumentFormattingParams {
68
+ TextDocument : lsp.TextDocumentIdentifier {
69
+ URI : uri ,
70
+ },
71
+ Options : options ,
72
+ WorkDoneProgressParams : lsp.WorkDoneProgressParams {
73
+ WorkDoneToken : nil ,
74
+ },
75
+ }
20
76
21
- type formattingTestCase struct {
22
- name string
23
- input string
24
- want string
77
+ var got []lsp.TextEdit
78
+ if err := tx .conn .Call (tx .ctx , "textDocument/formatting" , formattingParams , & got ); err != nil {
79
+ t .Fatal ("conn.Call textDocument/formatting:" , err )
80
+ }
81
+ if diff := cmp .Diff (tt .want , got [0 ].NewText ); diff != "" {
82
+ t .Errorf ("unmatch (- want, + got):\n %s" , diff )
83
+ t .Errorf ("unmatch\n want: %q\n got : %q" , tt .want , got [0 ].NewText )
84
+ }
85
+ })
25
86
}
87
+ }
26
88
27
- testDir , err := os .Getwd ()
28
- if err != nil {
29
- t .Fatal (err )
30
- }
31
- testFileInfos , err := ioutil .ReadDir ("testdata" )
89
+ func TestFormattingBase (t * testing.T ) {
90
+ testCase , err := loadFormatTestCaseByTestdata ("format" )
32
91
if err != nil {
33
92
t .Fatal (err )
34
93
}
94
+ testFormatting (t , testCase , formattingOptionTab )
95
+ }
35
96
36
- testCase := []formattingTestCase {}
37
-
38
- // Add golden file test
39
- const (
40
- inputFileSuffix = ".input.sql"
41
- goldenFileSuffix = ".golden.sql"
42
- )
43
- for _ , testFileInfo := range testFileInfos {
44
- inputFileName := testFileInfo .Name ()
45
- if ! strings .HasSuffix (inputFileName , inputFileSuffix ) {
46
- continue
47
- }
48
-
49
- testName := testFileInfo .Name ()[:len (inputFileName )- len (inputFileSuffix )]
50
- inputPath := filepath .Join (testDir , "testdata" , inputFileName )
51
- goldenPath := filepath .Join (testDir , "testdata" , testName + goldenFileSuffix )
52
-
53
- input , err := ioutil .ReadFile (inputPath )
54
- if err != nil {
55
- t .Errorf ("Cannot read input file, Path=%s, Err=%+v" , inputPath , err )
56
- continue
57
- }
58
- golden , err := ioutil .ReadFile (goldenPath )
59
- if err != nil {
60
- t .Errorf ("Cannot read input file, Path=%s, Err=%+v" , goldenPath , err )
61
- continue
62
- }
63
- testCase = append (testCase , formattingTestCase {
64
- name : testName ,
65
- input : string (input ),
66
- want : string (golden )[:len (string (golden ))- len ("\n " )],
67
- })
68
- }
69
-
97
+ func TestFormattingMinimal (t * testing.T ) {
70
98
// Add minimal case test
71
99
minimalTestCase := []formattingTestCase {
72
100
{
@@ -105,48 +133,67 @@ func TestFormatting(t *testing.T) {
105
133
want : "1,\n 2,\n 3,\n 4" ,
106
134
},
107
135
}
108
- testCase = append (testCase , minimalTestCase ... )
136
+ testFormatting (t , minimalTestCase , formattingOptionTab )
137
+ }
109
138
110
- for _ , tt := range testCase {
111
- t .Run (tt .name , func (t * testing.T ) {
112
- // Open dummy file
113
- didOpenParams := lsp.DidOpenTextDocumentParams {
114
- TextDocument : lsp.TextDocumentItem {
115
- URI : uri ,
116
- LanguageID : "sql" ,
117
- Version : 0 ,
118
- Text : tt .input ,
119
- },
120
- }
121
- if err := tx .conn .Call (tx .ctx , "textDocument/didOpen" , didOpenParams , nil ); err != nil {
122
- t .Fatal ("conn.Call textDocument/didOpen:" , err )
123
- }
124
- tx .testFile (t , didOpenParams .TextDocument .URI , didOpenParams .TextDocument .Text )
125
- // Create completion params
126
- formattingParams := lsp.DocumentFormattingParams {
127
- TextDocument : lsp.TextDocumentIdentifier {
128
- URI : uri ,
129
- },
130
- Options : lsp.FormattingOptions {
131
- TabSize : 0.0 ,
132
- InsertSpaces : false ,
133
- TrimTrailingWhitespace : false ,
134
- InsertFinalNewline : false ,
135
- TrimFinalNewlines : false ,
136
- },
137
- WorkDoneProgressParams : lsp.WorkDoneProgressParams {
138
- WorkDoneToken : nil ,
139
- },
140
- }
139
+ func TestFormattingWithOptionSpace2 (t * testing.T ) {
140
+ testCase , err := loadFormatTestCaseByTestdata ("format_option_space2" )
141
+ if err != nil {
142
+ t .Fatal (err )
143
+ }
144
+ testFormatting (t , testCase , formattingOptionIndentSpace2 )
145
+ }
141
146
142
- var got []lsp.TextEdit
143
- if err := tx .conn .Call (tx .ctx , "textDocument/formatting" , formattingParams , & got ); err != nil {
144
- t .Fatal ("conn.Call textDocument/formatting:" , err )
145
- }
146
- if diff := cmp .Diff (tt .want , got [0 ].NewText ); diff != "" {
147
- t .Errorf ("unmatch (- want, + got):\n %s" , diff )
148
- t .Errorf ("unmatch\n want: %q\n got : %q" , tt .want , got [0 ].NewText )
149
- }
147
+ func TestFormattingWithOptionSpace4 (t * testing.T ) {
148
+ testCase , err := loadFormatTestCaseByTestdata ("format_option_space4" )
149
+ if err != nil {
150
+ t .Fatal (err )
151
+ }
152
+ testFormatting (t , testCase , formattingOptionIndentSpace4 )
153
+ }
154
+
155
+ func loadFormatTestCaseByTestdata (targetDir string ) ([]formattingTestCase , error ) {
156
+ packageDir , err := os .Getwd ()
157
+ if err != nil {
158
+ return nil , err
159
+ }
160
+ testDir := filepath .Join (packageDir , "testdata" , targetDir )
161
+ testFileInfos , err := ioutil .ReadDir (testDir )
162
+ if err != nil {
163
+ return nil , err
164
+ }
165
+
166
+ testCase := []formattingTestCase {}
167
+ const (
168
+ inputFileSuffix = ".input.sql"
169
+ goldenFileSuffix = ".golden.sql"
170
+ )
171
+
172
+ for _ , testFileInfo := range testFileInfos {
173
+ inputFileName := testFileInfo .Name ()
174
+ if ! strings .HasSuffix (inputFileName , inputFileSuffix ) {
175
+ continue
176
+ }
177
+
178
+ testName := testFileInfo .Name ()[:len (inputFileName )- len (inputFileSuffix )]
179
+ inputPath := filepath .Join (testDir , inputFileName )
180
+ goldenPath := filepath .Join (testDir , testName + goldenFileSuffix )
181
+
182
+ input , err := ioutil .ReadFile (inputPath )
183
+ if err != nil {
184
+ return nil , fmt .Errorf ("Cannot read input file, Path=%s, Err=%+v" , inputPath , err )
185
+ continue
186
+ }
187
+ golden , err := ioutil .ReadFile (goldenPath )
188
+ if err != nil {
189
+ return nil , fmt .Errorf ("Cannot read input file, Path=%s, Err=%+v" , goldenPath , err )
190
+ continue
191
+ }
192
+ testCase = append (testCase , formattingTestCase {
193
+ name : testName ,
194
+ input : string (input ),
195
+ want : string (golden )[:len (string (golden ))- len ("\n " )],
150
196
})
151
197
}
198
+ return testCase , nil
152
199
}
0 commit comments