diff options
| author | Paul Buetow <paul@buetow.org> | 2026-03-16 04:03:44 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-03-16 04:03:44 +0200 |
| commit | 8f31040cc388943601cfd8a026ea85f0790e66c2 (patch) | |
| tree | 682ab9d8e0cf698f754866ec63e4eb955eb684bd /internal/lsp/extract_range_text_test.go | |
| parent | 030ce454f330871a6be729d7ca8c6ac33e1bbbb5 (diff) | |
Add bounds checks to extractRangeText and split into helper functions
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Diffstat (limited to 'internal/lsp/extract_range_text_test.go')
| -rw-r--r-- | internal/lsp/extract_range_text_test.go | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/internal/lsp/extract_range_text_test.go b/internal/lsp/extract_range_text_test.go new file mode 100644 index 0000000..1fe29ae --- /dev/null +++ b/internal/lsp/extract_range_text_test.go @@ -0,0 +1,83 @@ +package lsp + +import "testing" + +func TestExtractRangeText_NilDocument(t *testing.T) { + got := extractRangeText(nil, Range{}) + if got != "" { + t.Fatalf("expected empty, got %q", got) + } +} + +func TestExtractRangeText_EmptyLines(t *testing.T) { + d := &document{lines: []string{}} + got := extractRangeText(d, Range{Start: Position{0, 0}, End: Position{0, 5}}) + if got != "" { + t.Fatalf("expected empty, got %q", got) + } +} + +func TestExtractRangeText_NegativeStartLine(t *testing.T) { + d := &document{lines: []string{"hello"}} + got := extractRangeText(d, Range{Start: Position{-1, 0}, End: Position{0, 3}}) + if got != "" { + t.Fatalf("expected empty, got %q", got) + } +} + +func TestExtractRangeText_NegativeEndLine(t *testing.T) { + d := &document{lines: []string{"hello"}} + got := extractRangeText(d, Range{Start: Position{0, 0}, End: Position{-1, 3}}) + if got != "" { + t.Fatalf("expected empty, got %q", got) + } +} + +func TestExtractRangeText_StartLineBeyondEnd(t *testing.T) { + d := &document{lines: []string{"hello", "world"}} + got := extractRangeText(d, Range{Start: Position{5, 0}, End: Position{6, 0}}) + if got != "" { + t.Fatalf("expected empty, got %q", got) + } +} + +func TestExtractRangeText_EndLineBeyondEnd(t *testing.T) { + // End line is clamped to the last valid line. + d := &document{lines: []string{"hello", "world"}} + got := extractRangeText(d, Range{Start: Position{0, 0}, End: Position{10, 5}}) + if got != "hello\nworld" { + t.Fatalf("expected clamped result, got %q", got) + } +} + +func TestExtractRangeText_StartAfterEnd(t *testing.T) { + d := &document{lines: []string{"aaa", "bbb"}} + got := extractRangeText(d, Range{Start: Position{1, 0}, End: Position{0, 2}}) + if got != "" { + t.Fatalf("expected empty, got %q", got) + } +} + +func TestExtractRangeText_SingleLine(t *testing.T) { + d := &document{lines: []string{"hello world"}} + got := extractRangeText(d, Range{Start: Position{0, 6}, End: Position{0, 11}}) + if got != "world" { + t.Fatalf("expected %q, got %q", "world", got) + } +} + +func TestExtractRangeText_SingleLineNegativeChar(t *testing.T) { + d := &document{lines: []string{"hello"}} + got := extractRangeText(d, Range{Start: Position{0, -3}, End: Position{0, 3}}) + if got != "hel" { + t.Fatalf("expected %q, got %q", "hel", got) + } +} + +func TestExtractRangeText_MultiLine(t *testing.T) { + d := &document{lines: []string{"aaa", "bbb", "ccc"}} + got := extractRangeText(d, Range{Start: Position{0, 1}, End: Position{2, 2}}) + if got != "aa\nbbb\ncc" { + t.Fatalf("expected %q, got %q", "aa\nbbb\ncc", got) + } +} |
