File: screenbuf_test.go

package info (click to toggle)
golang-github-manifoldco-promptui 0.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 256 kB
  • sloc: makefile: 33
file content (134 lines) | stat: -rw-r--r-- 3,014 bytes parent folder | download | duplicates (2)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
package screenbuf

import (
	"bytes"
	"testing"
)

func TestScreen(t *testing.T) {
	// overwrite regular movement codes for easier visualization
	clearLine = []byte("\\c")
	moveUp = []byte("\\u")
	moveDown = []byte("\\d")

	var buf bytes.Buffer
	s := New(&buf)

	tcs := []struct {
		scenario string
		lines    []string
		expect   string
		cursor   int
		height   int
		flush    bool
		reset    bool
		clear    bool
	}{
		{
			scenario: "initial write",
			lines:    []string{"Line One"},
			expect:   "\\cLine One\n",
			cursor:   1,
			height:   1,
		},
		{
			scenario: "write of with same number of lines",
			lines:    []string{"Line One"},
			expect:   "\\u\\cLine One\\d",
			cursor:   1,
			height:   1,
		},
		{
			scenario: "write of with more lines",
			lines:    []string{"Line One", "Line Two"},
			expect:   "\\u\\cLine One\\d\\cLine Two\n",
			cursor:   2,
			height:   2,
		},
		{
			scenario: "write of with fewer lines",
			lines:    []string{"line One"},
			expect:   "\\u\\u\\cline One\\d\\c\\d",
			cursor:   1,
			height:   2,
		},
		{
			scenario: "write of way more lines",
			lines:    []string{"line one", "line two", "line three", "line four", "line five"},
			expect:   "\\u\\u\\cline one\\d\\cline two\\d\\cline three\n\\cline four\n\\cline five\n",
			cursor:   5,
			height:   5,
		},
		{
			scenario: "write of way less lines",
			lines:    []string{"line one", "line two"},
			expect:   "\\u\\u\\u\\u\\u\\cline one\\d\\cline two\\d\\c\\d\\c\\d\\c\\d",
			cursor:   2,
			height:   5,
		},
		{
			scenario: "write of way more lines",
			lines:    []string{"line one", "line two", "line three", "line four", "line five"},
			expect:   "\\u\\u\\u\\u\\u\\cline one\\d\\cline two\\d\\cline three\\d\\cline four\\d\\cline five\\d",
			cursor:   5,
			height:   5,
		},
		{
			scenario: "reset and write",
			lines:    []string{"line one", "line two"},
			expect:   "\\u\\c\\u\\c\\u\\c\\u\\c\\u\\c\\cline one\n\\cline two\n",
			cursor:   2,
			height:   2,
			reset:    true,
		},
		{
			scenario: "clear all previous lines",
			lines:    []string{"line one", "line two"},
			expect:   "\\u\\u\\cline one\\d\\cline two\\d\\u\\c\\u\\c",
			cursor:   0,
			height:   0,
			clear:    true,
		},
	}

	for _, tc := range tcs {
		t.Run(tc.scenario, func(t *testing.T) {
			buf.Reset()
			if tc.reset {
				s.Reset()
			}

			for _, line := range tc.lines {
				_, err := s.WriteString(line)
				if err != nil {
					t.Fatalf("expected no error, got %v", err)
				}
			}

			if tc.clear {
				if err := s.Clear(); err != nil {
					t.Errorf("expected no error, got %d", err)
				}
			}

			if tc.cursor != s.cursor {
				t.Errorf("expected cursor %d, got %d", tc.cursor, s.cursor)
			}

			err := s.Flush()
			if err != nil {
				t.Fatalf("expected no error, got %v", err)
			}

			got := buf.String()

			if tc.expect != got {
				t.Errorf("expected %q, got %q", tc.expect, got)
			}

			if tc.height != s.height {
				t.Errorf("expected height %d, got %d", tc.height, s.height)
			}
		})
	}
}