@@ -89,7 +89,17 @@ func parsePos(s string) lsp.Position {
8989 return lsp.Position {Line : line , Character : char }
9090}
9191
92- func TestDocument_ApplyChange (t * testing.T ) {
92+ func TestDocument (t * testing.T ) {
93+ for _ , impl := range bufferImplementations {
94+ t .Run (bufferName (impl ), func (t * testing.T ) {
95+ t .Run ("ApplyChange" , func (t * testing.T ) { testApplyChange (t , impl ) })
96+ t .Run ("ApplyChange_Error" , func (t * testing.T ) { testApplyChangeError (t , impl ) })
97+ t .Run ("PositionToOffset" , func (t * testing.T ) { testPositionToOffset (t , impl ) })
98+ })
99+ }
100+ }
101+
102+ func testApplyChange (t * testing.T , impl lsp.Buffer ) {
93103 tests := []struct {
94104 name string
95105 initial string
@@ -164,25 +174,71 @@ func TestDocument_ApplyChange(t *testing.T) {
164174 },
165175 }
166176
167- for _ , impl := range bufferImplementations {
168- t .Run (bufferName (impl ), func (t * testing.T ) {
169- for _ , tt := range tests {
170- t .Run (tt .name , func (t * testing.T ) {
171- g := NewWithT (t )
172- testutil .SetupLogger (t )
173- doc := lsp .NewDocument ([]byte (tt .initial ), newBuffer (impl ))
174- for i , change := range tt .changes {
175- err := doc .ApplyChange (change )
176- g .Expect (err ).ToNot (HaveOccurred (), "ApplyChange #%d failed" , i + 1 )
177- }
178- g .Expect (string (doc .Bytes ())).To (BeComparableTo (tt .want ))
179- })
177+ for _ , tt := range tests {
178+ t .Run (tt .name , func (t * testing.T ) {
179+ g := NewWithT (t )
180+ testutil .SetupLogger (t )
181+ doc := lsp .NewDocument ([]byte (tt .initial ), newBuffer (impl ))
182+ for i , change := range tt .changes {
183+ err := doc .ApplyChange (change )
184+ g .Expect (err ).ToNot (HaveOccurred (), "ApplyChange #%d failed" , i + 1 )
185+ }
186+ g .Expect (string (doc .Bytes ())).To (BeComparableTo (tt .want ))
187+ })
188+ }
189+ }
190+
191+ func testPositionToOffset (t * testing.T , impl lsp.Buffer ) {
192+ tests := []struct {
193+ name string
194+ initial string
195+ position lsp.Position
196+ want int
197+ wantErr bool
198+ }{
199+ {
200+ name : "position in single line" ,
201+ initial : "hello world" ,
202+ position : lsp.Position {Line : 0 , Character : 5 },
203+ want : 5 ,
204+ },
205+ {
206+ name : "position in multi-line" ,
207+ initial : "hello\n world" ,
208+ position : lsp.Position {Line : 1 , Character : 2 },
209+ want : 8 ,
210+ },
211+ {
212+ name : "position in multi-line with CRLF" ,
213+ initial : "hello\r \n world" ,
214+ position : lsp.Position {Line : 1 , Character : 2 },
215+ want : 9 ,
216+ },
217+ {
218+ name : "position at end of document" ,
219+ initial : "hello world" ,
220+ position : lsp.Position {Line : 0 , Character : 11 },
221+ want : 11 ,
222+ },
223+ }
224+
225+ for _ , tt := range tests {
226+ t .Run (tt .name , func (t * testing.T ) {
227+ g := NewWithT (t )
228+ testutil .SetupLogger (t )
229+ doc := lsp .NewDocument ([]byte (tt .initial ), newBuffer (impl ))
230+ offset , err := doc .PositionToOffset (tt .position )
231+ if tt .wantErr {
232+ g .Expect (err ).To (HaveOccurred (), "expected error for position %v" , tt .position )
233+ } else {
234+ g .Expect (err ).ToNot (HaveOccurred (), "unexpected error for position %v" , tt .position )
235+ g .Expect (offset ).To (BeEquivalentTo (tt .want ), "unexpected offset for position %v" , tt .position )
180236 }
181237 })
182238 }
183239}
184240
185- func TestDocument_ApplyChange_Error (t * testing.T ) {
241+ func testApplyChangeError (t * testing.T , impl lsp. Buffer ) {
186242 tests := []struct {
187243 name string
188244 initial string
@@ -200,14 +256,10 @@ func TestDocument_ApplyChange_Error(t *testing.T) {
200256 },
201257 }
202258
203- for _ , impl := range bufferImplementations {
204- t .Run (bufferName (impl ), func (t * testing.T ) {
205- for _ , tt := range tests {
206- t .Run (tt .name , func (t * testing.T ) {
207- doc := lsp .NewDocument ([]byte (tt .initial ), newBuffer (impl ))
208- NewWithT (t ).Expect (doc .ApplyChange (tt .change )).ToNot (Succeed ())
209- })
210- }
259+ for _ , tt := range tests {
260+ t .Run (tt .name , func (t * testing.T ) {
261+ doc := lsp .NewDocument ([]byte (tt .initial ), newBuffer (impl ))
262+ NewWithT (t ).Expect (doc .ApplyChange (tt .change )).ToNot (Succeed ())
211263 })
212264 }
213265}
0 commit comments