summaryrefslogtreecommitdiff
path: root/internal/lsp/coverage_add_test.go
blob: b3b7322d65a263bedced3981036f89ef4b039d28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package lsp

import (
	"encoding/json"
	"testing"
)

func TestInParamListAndComputeWordStart(t *testing.T) {
	line := "func add(a int, b int) int { return a + b }"
	if !inParamList(line, 15) { // inside params
		t.Fatalf("expected inParamList true")
	}
	if inParamList("not a func", 3) {
		t.Fatalf("expected inParamList false")
	}
	if n := computeWordStart("helloWorld", 10); n != 0 {
		t.Fatalf("computeWordStart wrong: %d", n)
	}
}

func TestStripInlineAndLabel(t *testing.T) {
	if got := stripInlineCodeSpan("`abc`def"); got != "abc" {
		t.Fatalf("stripInlineCodeSpan: %q", got)
	}
	if lbl := labelForCompletion("First line\nSecond", "fir"); lbl != "First line" {
		t.Fatalf("labelForCompletion: %q", lbl)
	}
	if lbl := labelForCompletion("Other", "zzz"); lbl != "zzz" {
		t.Fatalf("label fallback: %q", lbl)
	}
}

func TestRangeComparators(t *testing.T) {
	a := Range{Start: Position{Line: 1, Character: 5}, End: Position{Line: 3, Character: 0}}
	b := Range{Start: Position{Line: 2, Character: 0}, End: Position{Line: 4, Character: 0}}
	if !rangesOverlap(a, b) {
		t.Fatalf("expected overlap")
	}
	if !lessPos(Position{Line: 1, Character: 0}, Position{Line: 1, Character: 1}) {
		t.Fatalf("lessPos")
	}
	if !greaterPos(Position{Line: 2, Character: 0}, Position{Line: 1, Character: 10}) {
		t.Fatalf("greaterPos")
	}
	if !isIdentChar('A') || isIdentChar('-') {
		t.Fatalf("isIdentChar")
	}
}

func TestFindGoFunctionAtLine_NoBody(t *testing.T) {
	lines := []string{"func X(a int)", "// comment"}
	start, end := findGoFunctionAtLine(lines, 0)
	if start != 0 || end != 0 {
		t.Fatalf("expected single-line prototype, got %d,%d", start, end)
	}
}

func TestLineHasInlinePrompt(t *testing.T) {
	if !lineHasInlinePrompt(">do>", '>', '>') {
		t.Fatalf("expected inline prompt")
	}
}

func TestDiagnosticsInRange_Overlap(t *testing.T) {
	s := &Server{}
	initServerDefaults(s)
	ctx := CodeActionContext{Diagnostics: []Diagnostic{{
		Range:   Range{Start: Position{Line: 10, Character: 0}, End: Position{Line: 12, Character: 0}},
		Message: "x",
	}}}
	raw, _ := json.Marshal(ctx)
	sel := Range{Start: Position{Line: 11, Character: 0}, End: Position{Line: 11, Character: 1}}
	out := s.diagnosticsInRange(raw, sel)
	if len(out) != 1 {
		t.Fatalf("expected 1 diag overlap, got %d", len(out))
	}
	// no diagnostics
	var empty json.RawMessage
	if o2 := s.diagnosticsInRange(empty, sel); len(o2) != 0 {
		t.Fatalf("expected 0 with empty ctx")
	}
}

func TestIndentHelpersAndPromptRemoval(t *testing.T) {
	if ind := leadingIndent("\t  ab"); ind == "" {
		t.Fatalf("expected indent")
	}
	if out := applyIndent("  ", "x\ny"); out != "  x\n  y" {
		t.Fatalf("applyIndent: %q", out)
	}
	// double-open trigger removes whole line
	edits := promptRemovalEditsForLine(">>ask>", 3, '>', '>')
	if len(edits) != 1 || edits[0].Range.Start.Line != 3 {
		t.Fatalf("unexpected edits: %#v", edits)
	}
	// semicolon tags collect correctly when provided explicitly
	edits2 := collectSemicolonMarkers("pre;do;post", 1, ';', ';')
	if len(edits2) != 1 {
		t.Fatalf("expected one semicolon edit, got %#v", edits2)
	}
}