~taiite/senpai

ref: a532aae30446d7d76c74780bebaf0a40c775694e senpai/ui/editor_test.go -rw-r--r-- 2.3 KiB
a532aae3Hubert Hirtz Fix history not being loaded on channel re-JOIN 8 months ago
                                                                                
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
package ui

import "testing"

var hell = Editor{
	text:      [][]rune{{'h', 'e', 'l', 'l'}},
	textWidth: []int{0, 1, 2, 3, 4},
	cursorIdx: 4,
	offsetIdx: 0,
	width:     5,
}

func assertEditorEq(t *testing.T, actual, expected Editor) {
	if len(actual.text) != len(expected.text) {
		t.Errorf("expected text len to be %d, got %d\n", len(expected.text), len(actual.text))
	} else {
		for i := 0; i < len(actual.text); i++ {
			a := actual.text[0][i]
			e := expected.text[0][i]

			if a != e {
				t.Errorf("expected rune #%d to be '%c', got '%c'\n", i, e, a)
			}
		}
	}

	if len(actual.textWidth) != len(expected.textWidth) {
		t.Errorf("expected textWidth len to be %d, got %d\n", len(expected.textWidth), len(actual.textWidth))
	} else {
		for i := 0; i < len(actual.textWidth); i++ {
			a := actual.textWidth[i]
			e := expected.textWidth[i]

			if a != e {
				t.Errorf("expected width #%d to be %d, got %d\n", i, e, a)
			}
		}
	}

	if actual.cursorIdx != expected.cursorIdx {
		t.Errorf("expected cursorIdx to be %d, got %d\n", expected.cursorIdx, actual.cursorIdx)
	}

	if actual.offsetIdx != expected.offsetIdx {
		t.Errorf("expected offsetIdx to be %d, got %d\n", expected.offsetIdx, actual.offsetIdx)
	}

	if actual.width != expected.width {
		t.Errorf("expected width to be %d, got %d\n", expected.width, actual.width)
	}
}

func TestOneLetter(t *testing.T) {
	e := NewEditor(nil)
	e.Resize(5)
	e.PutRune('h')
	assertEditorEq(t, e, Editor{
		text:      [][]rune{{'h'}},
		textWidth: []int{0, 1},
		cursorIdx: 1,
		offsetIdx: 0,
		width:     5,
	})
}

func TestFourLetters(t *testing.T) {
	e := NewEditor(nil)
	e.Resize(5)
	e.PutRune('h')
	e.PutRune('e')
	e.PutRune('l')
	e.PutRune('l')
	assertEditorEq(t, e, hell)
}

func TestOneLeft(t *testing.T) {
	e := NewEditor(nil)
	e.Resize(5)
	e.PutRune('h')
	e.PutRune('l')
	e.Left()
	e.PutRune('e')
	e.PutRune('l')
	e.Right()
	assertEditorEq(t, e, hell)
}

func TestOneRem(t *testing.T) {
	e := NewEditor(nil)
	e.Resize(5)
	e.PutRune('h')
	e.PutRune('l')
	e.RemRune()
	e.PutRune('e')
	e.PutRune('l')
	e.PutRune('l')
	assertEditorEq(t, e, hell)
}

func TestLeftAndRem(t *testing.T) {
	e := NewEditor(nil)
	e.Resize(5)
	e.PutRune('h')
	e.PutRune('l')
	e.PutRune('e')
	e.Left()
	e.RemRune()
	e.Right()
	e.PutRune('l')
	e.PutRune('l')
	assertEditorEq(t, e, hell)
}