gitstack

grindlemire/go-tui code browser

2.6 KB Go 121 lines 2026-01-31 · e47a4fd 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package lsp

import (
	"testing"
)

func TestDocumentManager(t *testing.T) {
	type tc struct {
		operations []func(dm *DocumentManager)
		wantDocs   int
	}

	tests := map[string]tc{
		"open single": {
			operations: []func(dm *DocumentManager){
				func(dm *DocumentManager) {
					dm.Open("file:///a.gsx", "package main", 1)
				},
			},
			wantDocs: 1,
		},
		"open multiple": {
			operations: []func(dm *DocumentManager){
				func(dm *DocumentManager) {
					dm.Open("file:///a.gsx", "package main", 1)
				},
				func(dm *DocumentManager) {
					dm.Open("file:///b.gsx", "package main", 1)
				},
			},
			wantDocs: 2,
		},
		"open and close": {
			operations: []func(dm *DocumentManager){
				func(dm *DocumentManager) {
					dm.Open("file:///a.gsx", "package main", 1)
				},
				func(dm *DocumentManager) {
					dm.Close("file:///a.gsx")
				},
			},
			wantDocs: 0,
		},
		"update": {
			operations: []func(dm *DocumentManager){
				func(dm *DocumentManager) {
					dm.Open("file:///a.gsx", "package main", 1)
				},
				func(dm *DocumentManager) {
					dm.Update("file:///a.gsx", "package updated", 2)
				},
			},
			wantDocs: 1,
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			dm := NewDocumentManager()

			for _, op := range tt.operations {
				op(dm)
			}

			docs := dm.All()
			if len(docs) != tt.wantDocs {
				t.Errorf("got %d documents, want %d", len(docs), tt.wantDocs)
			}
		})
	}
}

func TestPositionConversion(t *testing.T) {
	type tc struct {
		content  string
		pos      Position
		wantOff  int
		wantBack Position
	}

	tests := map[string]tc{
		"start of file": {
			content:  "hello\nworld",
			pos:      Position{Line: 0, Character: 0},
			wantOff:  0,
			wantBack: Position{Line: 0, Character: 0},
		},
		"middle of first line": {
			content:  "hello\nworld",
			pos:      Position{Line: 0, Character: 3},
			wantOff:  3,
			wantBack: Position{Line: 0, Character: 3},
		},
		"start of second line": {
			content:  "hello\nworld",
			pos:      Position{Line: 1, Character: 0},
			wantOff:  6,
			wantBack: Position{Line: 1, Character: 0},
		},
		"middle of second line": {
			content:  "hello\nworld",
			pos:      Position{Line: 1, Character: 2},
			wantOff:  8,
			wantBack: Position{Line: 1, Character: 2},
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			offset := PositionToOffset(tt.content, tt.pos)
			if offset != tt.wantOff {
				t.Errorf("PositionToOffset = %d, want %d", offset, tt.wantOff)
			}

			back := OffsetToPosition(tt.content, offset)
			if back != tt.wantBack {
				t.Errorf("OffsetToPosition = %+v, want %+v", back, tt.wantBack)
			}
		})
	}
}