summaryrefslogtreecommitdiff
path: root/internal/lsp/document_test.go
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2025-08-16 15:35:02 +0300
committerPaul Buetow <paul@buetow.org>2025-08-16 15:35:02 +0300
commit6c8eb6876fe87553770de114ebd34649a0c6ec10 (patch)
tree064517edaf9d59522bec7191a61362a853c195bd /internal/lsp/document_test.go
parent1e1df8c204f6771719f85d8402128d72138bb863 (diff)
lsp: split monolithic server.go into modules; add configurable max tokens and context strategies (minimal|window|file-on-new-func|always-full); provide flags/env fallbacks; add unit tests for helpers and context; update README; remove obsolete files
Diffstat (limited to 'internal/lsp/document_test.go')
-rw-r--r--internal/lsp/document_test.go76
1 files changed, 76 insertions, 0 deletions
diff --git a/internal/lsp/document_test.go b/internal/lsp/document_test.go
new file mode 100644
index 0000000..8d81a99
--- /dev/null
+++ b/internal/lsp/document_test.go
@@ -0,0 +1,76 @@
+package lsp
+
+import (
+ "io"
+ "log"
+ "strings"
+ "testing"
+)
+
+func newTestServer() *Server {
+ return &Server{
+ logger: log.New(io.Discard, "", 0),
+ docs: make(map[string]*document),
+ }
+}
+
+func TestSplitLines(t *testing.T) {
+ in := "a\r\nb\nc"
+ got := splitLines(in)
+ want := []string{"a", "b", "c"}
+ if len(got) != len(want) {
+ t.Fatalf("len mismatch: got %d want %d", len(got), len(want))
+ }
+ for i := range want {
+ if got[i] != want[i] {
+ t.Fatalf("line %d: got %q want %q", i, got[i], want[i])
+ }
+ }
+}
+
+func TestLineContext(t *testing.T) {
+ s := newTestServer()
+ src := "package main\n\nfunc add(a, b int) int {\n\treturn a + b\n}\n"
+ uri := "file:///test.go"
+ s.setDocument(uri, src)
+
+ // Position on the return line (line 3, zero-based)
+ above, current, below, funcCtx := s.lineContext(uri, Position{Line: 3, Character: 0})
+
+ if want := "func add(a, b int) int {"; funcCtx != want {
+ t.Fatalf("funcCtx got %q want %q", funcCtx, want)
+ }
+ if want := "func add(a, b int) int {"; above != want {
+ t.Fatalf("above got %q want %q", above, want)
+ }
+ if want := "\treturn a + b"; current != want {
+ t.Fatalf("current got %q want %q", current, want)
+ }
+ if want := "}"; below != want {
+ t.Fatalf("below got %q want %q", below, want)
+ }
+}
+
+func TestLineContext_EmptyDoc(t *testing.T) {
+ s := newTestServer()
+ a, c, b, f := s.lineContext("file:///missing.go", Position{Line: 0, Character: 0})
+ if a != "" || b != "" || c != "" || f != "" {
+ t.Fatalf("expected all empty for missing doc; got above=%q current=%q below=%q func=%q", a, c, b, f)
+ }
+}
+
+func TestTrimLen(t *testing.T) {
+ long := strings.Repeat("a", 205)
+ got := trimLen(long)
+ want := strings.Repeat("a", 200) + "…"
+ if got != want {
+ t.Fatalf("trimLen got %q want %q", got, want)
+ }
+}
+
+func TestFirstLine(t *testing.T) {
+ s := "first line\r\nsecond line"
+ if got := firstLine(s); got != "first line" {
+ t.Fatalf("firstLine got %q want %q", got, "first line")
+ }
+}