~mna/zzcsi

ref: 773d5a48696b1058945be581b3befb0ab1527fba zzcsi/csi_test.go -rw-r--r-- 2.7 KiB
773d5a48Martin Angers doc and example 1 year, 2 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
113
114
115
116
117
package zztermcsi

import (
	"strings"
	"testing"
)

func TestFunc_NoArg(t *testing.T) {
	for i, seq := range csiSeqs {
		t.Run(string(seq), func(t *testing.T) {
			csi := CSI(i)
			want := strings.ReplaceAll(strings.ReplaceAll(string(seq), "\x01", ""), "\x02", "")
			got := csi.FuncString()
			if want != got {
				t.Fatalf("want %q, got %q", want, got)
			}
		})
	}
}

func TestFunc_Args(t *testing.T) {
	cases := []struct {
		csi  CSI
		args []int
		want string
	}{
		{InsCh, nil, "\x1b[@"},
		{InsCh, []int{1}, "\x1b[1@"},
		{InsCh, []int{123}, "\x1b[123@"},
		{InsCh, []int{2, 3, 4}, "\x1b[2@"},
		{CurPos, nil, "\x1b[;H"},
		{CurPos, []int{1}, "\x1b[1;H"},
		{CurPos, []int{1, 2}, "\x1b[1;2H"},
		{CurPos, []int{1, 2, 3}, "\x1b[1;2H"},
		{RstTitleMode, nil, "\x1b[>T"},
		{RstTitleMode, []int{1}, "\x1b[>1T"},
		{RstTitleMode, []int{123, 456}, "\x1b[>123;456T"},
		{RstTitleMode, []int{123, 456, 789}, "\x1b[>123;456;789T"},
		{ChAttrFgIRGB, nil, "\x1b[38;2;;;;m"},
		{ChAttrFgIRGB, []int{1}, "\x1b[38;2;1;;;m"},
		{ChAttrFgIRGB, []int{1, 2, 3}, "\x1b[38;2;1;2;3;m"},
		{ChAttrFgIRGB, []int{1, 2, 3, 4, 5}, "\x1b[38;2;1;2;3;4m"},
		{CSI(255), nil, ""},
	}

	buf := make([]byte, 128)
	for _, c := range cases {
		buf = buf[:0]
		t.Run(c.want, func(t *testing.T) {
			got := string(c.csi.AppendFunc(buf, c.args...))
			if c.want != got {
				t.Fatalf("want %q, got %q", c.want, got)
			}
		})
	}
}

func TestIsCSI(t *testing.T) {
	for _, seq := range csiSeqs {
		if len(seq) == 0 {
			continue
		}

		t.Run(string(seq), func(t *testing.T) {
			if !IsCSI(seq) {
				t.Fatalf("sequence not detected as CSI: %q", seq)
			}
			if !IsCSIString(string(seq)) {
				t.Fatalf("string sequence not detected as CSI: %q", seq)
			}
		})

		// invert the sequence, should NOT be CSI
		iseq := make([]byte, len(seq))
		copy(iseq, seq)
		for i := len(iseq)/2 - 1; i >= 0; i-- {
			opp := len(iseq) - 1 - i
			iseq[i], iseq[opp] = iseq[opp], iseq[i]
		}

		t.Run(string(iseq), func(t *testing.T) {
			if IsCSI(iseq) {
				t.Fatalf("inverted sequence detected as CSI: %q", iseq)
			}
			if IsCSIString(string(iseq)) {
				t.Fatalf("inverted string sequence detected as CSI: %q", iseq)
			}
		})
	}
}

var (
	BenchmarkResultString string
	BenchmarkResultBytes  []byte
)

func BenchmarkCSI(b *testing.B) {
	b.Run("Func", func(b *testing.B) {
		for i := 0; i < b.N; i++ {
			BenchmarkResultBytes = ChLnCol.Func(12, 80)
		}
	})

	b.Run("FuncString", func(b *testing.B) {
		for i := 0; i < b.N; i++ {
			BenchmarkResultString = ChLnCol.FuncString(12, 80)
		}
	})

	b.Run("AppendFunc", func(b *testing.B) {
		buf := make([]byte, 0, 8)
		b.ResetTimer()
		for i := 0; i < b.N; i++ {
			BenchmarkResultBytes = ChLnCol.AppendFunc(buf, 12, 80)
		}
	})
}