summaryrefslogtreecommitdiff
path: root/tui/termfo/termfo_test.go
blob: 60564ef62c76e91f116b184186d8cb4c4cf2fefd (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
package termfo

import (
	"fmt"
	"io"
	"os"
	"strings"
	"testing"
)

func BenchmarkNew(b *testing.B) {
	t := os.Getenv("TERM")
	b.ReportAllocs()
	for n := 0; n < b.N; n++ {
		New(t)
	}
}

func TestNew(t *testing.T) {
	tests := []struct {
		term    string
		intSize int
	}{
		{"xterm", 2},
		{"xterm-256color", 4},
	}

	for _, tt := range tests {
		t.Run(tt.term, func(t *testing.T) {
			ti, err := New(tt.term)
			if err != nil {
				t.Fatal(err)
			}

			if ti.Name != tt.term {
				t.Errorf("wrong name: %q", ti.Name)
			}
			if ti.IntSize != tt.intSize {
				t.Errorf("intsize: %d", ti.IntSize)
			}

			// Just a basic sanity check.
			if len(ti.Bools) < 10 || len(ti.Numbers) < 5 || len(ti.Strings) < 200 || len(ti.Extended) < 50 {
				t.Errorf("bools: %d  nums: %d  strs: %d  ext: %d", len(ti.Bools), len(ti.Numbers), len(ti.Strings), len(ti.Extended))
			}
		})
	}
}

func TestKeys(t *testing.T) {
	run := func(ti *Terminfo, seq string) []string {
		var collect []string
		ch := ti.FindKeys(strings.NewReader(seq))
		for e := <-ch; ; e = <-ch {
			if e.Err != nil {
				if e.Err == io.EOF {
					break
				}
				t.Fatal(e.Err)
			}
			collect = append(collect, e.Key.String())
		}
		return collect
	}

	{
		ti, err := New("xterm")
		if err != nil {
			t.Fatal(err)
		}
		f := run(ti, "\x1bOP\x1b[1;2P\x1bOA")
		if fmt.Sprintf("%s", f) != "[<F1> <S-F1> <Up>]" {
			t.Error(f)
		}
	}

	{
		ti, err := New("vi200")
		if err != nil {
			t.Fatal(err)
		}
		f := run(ti, "\x1b?q\x1bA")
		if fmt.Sprintf("%s", f) != "[<F1> <Up>]" {
			t.Error(f)
		}
	}
}