1 // Copyright 2016 The TCell Authors
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
22 . "github.com/smartystreets/goconvey/convey"
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",
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{{||}}~~",
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",
44 func TestTerminfo(t *testing.T) {
48 Convey("Terminfo parameter processing", t, func() {
49 // This tests %i, and basic parameter strings too
50 Convey("TGoto works", func() {
52 So(s, ShouldEqual, "\x1b[10;8H")
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")
61 // This tests some conditionals
62 Convey("TParm colors work", func() {
63 s := ti.TParm(ti.SetFg, 7)
64 So(s, ShouldEqual, "\x1b[37m")
66 s = ti.TParm(ti.SetFg, 15)
67 So(s, ShouldEqual, "\x1b[97m")
69 s = ti.TParm(ti.SetFg, 200)
70 So(s, ShouldEqual, "\x1b[38;5;200m")
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")
83 Convey("Terminfo delay handling", t, func() {
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")
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")
101 func TestTerminfoDatabase(t *testing.T) {
103 Convey("Database Lookups work", t, func() {
104 Convey("Basic lookup works", func() {
105 os.Setenv("TCELLDB", "testdata/test1")
106 ti, err := LookupTerminfo("test1")
108 So(ti, ShouldNotBeNil)
109 So(ti.Columns, ShouldEqual, 80)
111 ti, err = LookupTerminfo("alias1")
113 So(ti, ShouldNotBeNil)
114 So(ti.Columns, ShouldEqual, 80)
116 os.Setenv("TCELLDB", "testdata")
117 ti, err = LookupTerminfo("test2")
119 So(ti, ShouldNotBeNil)
120 So(ti.Columns, ShouldEqual, 80)
121 So(len(ti.Aliases), ShouldEqual, 1)
122 So(ti.Aliases[0], ShouldEqual, "alias2")
125 Convey("Incorrect primary name works", func() {
126 os.Setenv("TCELLDB", "testdata")
127 ti, err := LookupTerminfo("test3")
128 So(err, ShouldNotBeNil)
132 Convey("Loops fail", func() {
133 os.Setenv("TCELLDB", "testdata")
134 ti, err := LookupTerminfo("loop1")
136 So(err, ShouldNotBeNil)
139 Convey("Gzip database works", func() {
140 os.Setenv("TCELLDB", "testdata")
141 ti, err := LookupTerminfo("test-gzip")
143 So(ti, ShouldNotBeNil)
144 So(ti.Columns, ShouldEqual, 80)
147 Convey("Gzip alias lookup works", func() {
148 os.Setenv("TCELLDB", "testdata")
149 ti, err := LookupTerminfo("alias-gzip")
151 So(ti, ShouldNotBeNil)
152 So(ti.Columns, ShouldEqual, 80)
155 Convey("Broken alias works", func() {
156 os.Setenv("TCELLDB", "testdata")
157 ti, err := LookupTerminfo("alias-none")
158 So(err, ShouldNotBeNil)
162 Convey("Combined database works", func() {
163 os.Setenv("TCELLDB", "testdata/combined")
164 ti, err := LookupTerminfo("combined2")
166 So(ti, ShouldNotBeNil)
167 So(ti.Lines, ShouldEqual, 102)
169 ti, err = LookupTerminfo("alias-comb1")
171 So(ti, ShouldNotBeNil)
172 So(ti.Lines, ShouldEqual, 101)
174 ti, err = LookupTerminfo("combined3")
176 So(ti, ShouldNotBeNil)
177 So(ti.Lines, ShouldEqual, 103)
179 ti, err = LookupTerminfo("combined1")
181 So(ti, ShouldNotBeNil)
182 So(ti.Lines, ShouldEqual, 101)
187 func BenchmarkSetFgBg(b *testing.B) {
190 for i := 0; i < b.N; i++ {
191 ti.TParm(ti.SetFg, 100, 200)
192 ti.TParm(ti.SetBg, 100, 200)