gitstack

grindlemire/go-tui code browser

2.0 KB Go 82 lines 2026-06-12 · 0ad9a5a raw
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
package lsp

import (
	"testing"

	"github.com/grindlemire/go-tui/internal/tuigen"
)

func TestDocumentManagerUpdate_UnopenedDocument(t *testing.T) {
	dm := NewDocumentManager()
	uri := "file:///fresh.gsx"

	doc := dm.Update(uri, "package main\n\ntempl Fresh() {\n\t<span>hi</span>\n}\n", 3)
	if doc == nil {
		t.Fatal("Update returned nil")
	}
	if doc.Version != 3 {
		t.Errorf("Version = %d, want 3", doc.Version)
	}
	if doc.AST == nil {
		t.Error("expected document to be parsed")
	}
	if dm.Get(uri) != doc {
		t.Error("document not registered in the manager")
	}
}

func TestURIToPath(t *testing.T) {
	type tc struct {
		uri  string
		want string
	}

	tests := map[string]tc{
		"file uri":        {uri: "file:///tmp/a.gsx", want: "/tmp/a.gsx"},
		"plain path":      {uri: "relative/a.gsx", want: "relative/a.gsx"},
		"bare prefix":     {uri: "file://", want: "file://"},
		"non-file scheme": {uri: "untitled:Untitled-1", want: "untitled:Untitled-1"},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			if got := uriToPath(tt.uri); got != tt.want {
				t.Errorf("uriToPath(%q) = %q, want %q", tt.uri, got, tt.want)
			}
		})
	}
}

func TestPositionToOffset_PastLineEnd(t *testing.T) {
	// Requesting a character beyond the line end falls back to start + character.
	content := "ab\ncd"
	got := PositionToOffset(content, Position{Line: 0, Character: 10})
	if got != 10 {
		t.Errorf("PositionToOffset = %d, want 10", got)
	}
}

func TestTuigenPosToRange(t *testing.T) {
	got := TuigenPosToRange(tuigen.Position{Line: 3, Column: 5}, 4)
	want := Range{
		Start: Position{Line: 2, Character: 4},
		End:   Position{Line: 2, Character: 8},
	}
	if got != want {
		t.Errorf("TuigenPosToRange = %+v, want %+v", got, want)
	}
}

func TestTuigenPosToRangeWithEnd(t *testing.T) {
	got := TuigenPosToRangeWithEnd(
		tuigen.Position{Line: 1, Column: 2},
		tuigen.Position{Line: 4, Column: 7},
	)
	want := Range{
		Start: Position{Line: 0, Character: 1},
		End:   Position{Line: 3, Character: 6},
	}
	if got != want {
		t.Errorf("TuigenPosToRangeWithEnd = %+v, want %+v", got, want)
	}
}