]> git.lizzy.rs Git - micro.git/blob - cmd/micro/terminfo/terminfo_test.go
Update to use new mkinfo from tcell
[micro.git] / cmd / micro / terminfo / terminfo_test.go
1 // Copyright 2016 The TCell Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use file except in compliance with the License.
5 // You may obtain a copy of the license at
6 //
7 //    http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package terminfo
16
17 import (
18         "bytes"
19         "os"
20         "testing"
21
22         . "github.com/smartystreets/goconvey/convey"
23 )
24
25 // This terminfo entry is a stripped down version from
26 // xterm-256color, but I've added some of my own entries.
27 var testTerminfo = &Terminfo{
28         Name:      "simulation_test",
29         Columns:   80,
30         Lines:     24,
31         Colors:    256,
32         Bell:      "\a",
33         Blink:     "\x1b2ms$<2>",
34         Reverse:   "\x1b[7m",
35         SetFg:     "\x1b[%?%p1%{8}%<%t3%p1%d%e%p1%{16}%<%t9%p1%{8}%-%d%e38;5;%p1%d%;m",
36         SetBg:     "\x1b[%?%p1%{8}%<%t4%p1%d%e%p1%{16}%<%t10%p1%{8}%-%d%e48;5;%p1%d%;m",
37         AltChars:  "``aaffggiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~",
38         Mouse:     "\x1b[M",
39         MouseMode: "%?%p1%{1}%=%t%'h'%Pa%e%'l'%Pa%;\x1b[?1000%ga%c\x1b[?1003%ga%c\x1b[?1006%ga%c",
40         SetCursor: "\x1b[%i%p1%d;%p2%dH",
41         PadChar:   "\x00",
42 }
43
44 func TestTerminfo(t *testing.T) {
45
46         ti := testTerminfo
47
48         Convey("Terminfo parameter processing", t, func() {
49                 // This tests %i, and basic parameter strings too
50                 Convey("TGoto works", func() {
51                         s := ti.TGoto(7, 9)
52                         So(s, ShouldEqual, "\x1b[10;8H")
53                 })
54
55                 // This tests some conditionals
56                 Convey("TParm extended formats work", func() {
57                         s := ti.TParm("A[%p1%2.2X]B", 47)
58                         So(s, ShouldEqual, "A[2F]B")
59                 })
60
61                 // This tests some conditionals
62                 Convey("TParm colors work", func() {
63                         s := ti.TParm(ti.SetFg, 7)
64                         So(s, ShouldEqual, "\x1b[37m")
65
66                         s = ti.TParm(ti.SetFg, 15)
67                         So(s, ShouldEqual, "\x1b[97m")
68
69                         s = ti.TParm(ti.SetFg, 200)
70                         So(s, ShouldEqual, "\x1b[38;5;200m")
71                 })
72
73                 // This tests variables
74                 Convey("TParm mouse mode works", func() {
75                         s := ti.TParm(ti.MouseMode, 1)
76                         So(s, ShouldEqual, "\x1b[?1000h\x1b[?1003h\x1b[?1006h")
77                         s = ti.TParm(ti.MouseMode, 0)
78                         So(s, ShouldEqual, "\x1b[?1000l\x1b[?1003l\x1b[?1006l")
79                 })
80
81         })
82
83         Convey("Terminfo delay handling", t, func() {
84
85                 Convey("19200 baud", func() {
86                         buf := bytes.NewBuffer(nil)
87                         ti.TPuts(buf, ti.Blink, 19200)
88                         s := string(buf.Bytes())
89                         So(s, ShouldEqual, "\x1b2ms\x00\x00\x00\x00")
90                 })
91
92                 Convey("50 baud", func() {
93                         buf := bytes.NewBuffer(nil)
94                         ti.TPuts(buf, ti.Blink, 50)
95                         s := string(buf.Bytes())
96                         So(s, ShouldEqual, "\x1b2ms")
97                 })
98         })
99 }
100
101 func TestTerminfoDatabase(t *testing.T) {
102
103         Convey("Database Lookups work", t, func() {
104                 Convey("Basic lookup works", func() {
105                         os.Setenv("TCELLDB", "testdata/test1")
106                         ti, err := LookupTerminfo("test1")
107                         So(err, ShouldBeNil)
108                         So(ti, ShouldNotBeNil)
109                         So(ti.Columns, ShouldEqual, 80)
110
111                         ti, err = LookupTerminfo("alias1")
112                         So(err, ShouldBeNil)
113                         So(ti, ShouldNotBeNil)
114                         So(ti.Columns, ShouldEqual, 80)
115
116                         os.Setenv("TCELLDB", "testdata")
117                         ti, err = LookupTerminfo("test2")
118                         So(err, ShouldBeNil)
119                         So(ti, ShouldNotBeNil)
120                         So(ti.Columns, ShouldEqual, 80)
121                         So(len(ti.Aliases), ShouldEqual, 1)
122                         So(ti.Aliases[0], ShouldEqual, "alias2")
123                 })
124
125                 Convey("Incorrect primary name works", func() {
126                         os.Setenv("TCELLDB", "testdata")
127                         ti, err := LookupTerminfo("test3")
128                         So(err, ShouldNotBeNil)
129                         So(ti, ShouldBeNil)
130                 })
131
132                 Convey("Loops fail", func() {
133                         os.Setenv("TCELLDB", "testdata")
134                         ti, err := LookupTerminfo("loop1")
135                         So(ti, ShouldBeNil)
136                         So(err, ShouldNotBeNil)
137                 })
138
139                 Convey("Gzip database works", func() {
140                         os.Setenv("TCELLDB", "testdata")
141                         ti, err := LookupTerminfo("test-gzip")
142                         So(err, ShouldBeNil)
143                         So(ti, ShouldNotBeNil)
144                         So(ti.Columns, ShouldEqual, 80)
145                 })
146
147                 Convey("Gzip alias lookup works", func() {
148                         os.Setenv("TCELLDB", "testdata")
149                         ti, err := LookupTerminfo("alias-gzip")
150                         So(err, ShouldBeNil)
151                         So(ti, ShouldNotBeNil)
152                         So(ti.Columns, ShouldEqual, 80)
153                 })
154
155                 Convey("Broken alias works", func() {
156                         os.Setenv("TCELLDB", "testdata")
157                         ti, err := LookupTerminfo("alias-none")
158                         So(err, ShouldNotBeNil)
159                         So(ti, ShouldBeNil)
160                 })
161
162                 Convey("Combined database works", func() {
163                         os.Setenv("TCELLDB", "testdata/combined")
164                         ti, err := LookupTerminfo("combined2")
165                         So(err, ShouldBeNil)
166                         So(ti, ShouldNotBeNil)
167                         So(ti.Lines, ShouldEqual, 102)
168
169                         ti, err = LookupTerminfo("alias-comb1")
170                         So(err, ShouldBeNil)
171                         So(ti, ShouldNotBeNil)
172                         So(ti.Lines, ShouldEqual, 101)
173
174                         ti, err = LookupTerminfo("combined3")
175                         So(err, ShouldBeNil)
176                         So(ti, ShouldNotBeNil)
177                         So(ti.Lines, ShouldEqual, 103)
178
179                         ti, err = LookupTerminfo("combined1")
180                         So(err, ShouldBeNil)
181                         So(ti, ShouldNotBeNil)
182                         So(ti.Lines, ShouldEqual, 101)
183                 })
184         })
185 }
186
187 func BenchmarkSetFgBg(b *testing.B) {
188         ti := testTerminfo
189
190         for i := 0; i < b.N; i++ {
191                 ti.TParm(ti.SetFg, 100, 200)
192                 ti.TParm(ti.SetBg, 100, 200)
193         }
194 }