gopls/internal/lsp/protocol: move TestBytesOffset Change-Id: Ied6bbcc7a4da1a513e500f1e650ca9b1b51c6254 Reviewed-on: https://go-review.googlesource.com/c/tools/+/463685 Run-TryBot: Alan Donovan <adonovan@google.com> Auto-Submit: Alan Donovan <adonovan@google.com> Reviewed-by: Robert Findley <rfindley@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
diff --git a/gopls/internal/lsp/lsp_test.go b/gopls/internal/lsp/lsp_test.go index 5290e61..47f6b0a 100644 --- a/gopls/internal/lsp/lsp_test.go +++ b/gopls/internal/lsp/lsp_test.go
@@ -1332,43 +1332,6 @@ } } -func TestBytesOffset(t *testing.T) { - tests := []struct { - text string - pos protocol.Position - want int - }{ - // U+10400 encodes as [F0 90 90 80] in UTF-8 and [D801 DC00] in UTF-16. - {text: `a𐐀b`, pos: protocol.Position{Line: 0, Character: 0}, want: 0}, - {text: `a𐐀b`, pos: protocol.Position{Line: 0, Character: 1}, want: 1}, - {text: `a𐐀b`, pos: protocol.Position{Line: 0, Character: 2}, want: 1}, - {text: `a𐐀b`, pos: protocol.Position{Line: 0, Character: 3}, want: 5}, - {text: `a𐐀b`, pos: protocol.Position{Line: 0, Character: 4}, want: 6}, - {text: `a𐐀b`, pos: protocol.Position{Line: 0, Character: 5}, want: -1}, - {text: "aaa\nbbb\n", pos: protocol.Position{Line: 0, Character: 3}, want: 3}, - {text: "aaa\nbbb\n", pos: protocol.Position{Line: 0, Character: 4}, want: -1}, - {text: "aaa\nbbb\n", pos: protocol.Position{Line: 1, Character: 0}, want: 4}, - {text: "aaa\nbbb\n", pos: protocol.Position{Line: 1, Character: 3}, want: 7}, - {text: "aaa\nbbb\n", pos: protocol.Position{Line: 1, Character: 4}, want: -1}, - {text: "aaa\nbbb\n", pos: protocol.Position{Line: 2, Character: 0}, want: 8}, - {text: "aaa\nbbb\n", pos: protocol.Position{Line: 2, Character: 1}, want: -1}, - {text: "aaa\nbbb\n\n", pos: protocol.Position{Line: 2, Character: 0}, want: 8}, - } - - for i, test := range tests { - fname := fmt.Sprintf("test %d", i) - uri := span.URIFromPath(fname) - mapper := protocol.NewMapper(uri, []byte(test.text)) - got, err := mapper.PositionPoint(test.pos) - if err != nil && test.want != -1 { - t.Errorf("%d: unexpected error: %v", i, err) - } - if err == nil && got.Offset() != test.want { - t.Errorf("want %d for %q(Line:%d,Character:%d), but got %d", test.want, test.text, int(test.pos.Line), int(test.pos.Character), got.Offset()) - } - } -} - func (r *runner) collectDiagnostics(view *cache.View) { if r.diagnostics != nil { return
diff --git a/gopls/internal/lsp/protocol/mapper_test.go b/gopls/internal/lsp/protocol/mapper_test.go index 5e85481..0780491 100644 --- a/gopls/internal/lsp/protocol/mapper_test.go +++ b/gopls/internal/lsp/protocol/mapper_test.go
@@ -401,4 +401,41 @@ } } +func TestBytesOffset(t *testing.T) { + tests := []struct { + text string + pos protocol.Position + want int + }{ + // U+10400 encodes as [F0 90 90 80] in UTF-8 and [D801 DC00] in UTF-16. + {text: `a𐐀b`, pos: protocol.Position{Line: 0, Character: 0}, want: 0}, + {text: `a𐐀b`, pos: protocol.Position{Line: 0, Character: 1}, want: 1}, + {text: `a𐐀b`, pos: protocol.Position{Line: 0, Character: 2}, want: 1}, + {text: `a𐐀b`, pos: protocol.Position{Line: 0, Character: 3}, want: 5}, + {text: `a𐐀b`, pos: protocol.Position{Line: 0, Character: 4}, want: 6}, + {text: `a𐐀b`, pos: protocol.Position{Line: 0, Character: 5}, want: -1}, + {text: "aaa\nbbb\n", pos: protocol.Position{Line: 0, Character: 3}, want: 3}, + {text: "aaa\nbbb\n", pos: protocol.Position{Line: 0, Character: 4}, want: -1}, + {text: "aaa\nbbb\n", pos: protocol.Position{Line: 1, Character: 0}, want: 4}, + {text: "aaa\nbbb\n", pos: protocol.Position{Line: 1, Character: 3}, want: 7}, + {text: "aaa\nbbb\n", pos: protocol.Position{Line: 1, Character: 4}, want: -1}, + {text: "aaa\nbbb\n", pos: protocol.Position{Line: 2, Character: 0}, want: 8}, + {text: "aaa\nbbb\n", pos: protocol.Position{Line: 2, Character: 1}, want: -1}, + {text: "aaa\nbbb\n\n", pos: protocol.Position{Line: 2, Character: 0}, want: 8}, + } + + for i, test := range tests { + fname := fmt.Sprintf("test %d", i) + uri := span.URIFromPath(fname) + mapper := protocol.NewMapper(uri, []byte(test.text)) + got, err := mapper.PositionPoint(test.pos) + if err != nil && test.want != -1 { + t.Errorf("%d: unexpected error: %v", i, err) + } + if err == nil && got.Offset() != test.want { + t.Errorf("want %d for %q(Line:%d,Character:%d), but got %d", test.want, test.text, int(test.pos.Line), int(test.pos.Character), got.Offset()) + } + } +} + // -- end --