1// Copyright 2019 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 15package terminfo 16 17import ( 18 "bytes" 19 "testing" 20 "time" 21) 22 23// This terminfo entry is a stripped down version from 24// xterm-256color, but I've added some of my own entries. 25var testTerminfo = &Terminfo{ 26 Name: "simulation_test", 27 Columns: 80, 28 Lines: 24, 29 Colors: 256, 30 Bell: "\a", 31 Blink: "\x1b2ms$<20>something", 32 Reverse: "\x1b[7m", 33 SetFg: "\x1b[%?%p1%{8}%<%t3%p1%d%e%p1%{16}%<%t9%p1%{8}%-%d%e38;5;%p1%d%;m", 34 SetBg: "\x1b[%?%p1%{8}%<%t4%p1%d%e%p1%{16}%<%t10%p1%{8}%-%d%e48;5;%p1%d%;m", 35 AltChars: "``aaffggiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~", 36 Mouse: "\x1b[M", 37 MouseMode: "%?%p1%{1}%=%t%'h'%Pa%e%'l'%Pa%;\x1b[?1000%ga%c\x1b[?1003%ga%c\x1b[?1006%ga%c", 38 SetCursor: "\x1b[%i%p1%d;%p2%dH", 39 PadChar: "\x00", 40} 41 42func TestTerminfoExpansion(t *testing.T) { 43 44 ti := testTerminfo 45 46 // Tests %i and basic parameter strings too 47 if ti.TGoto(7, 9) != "\x1b[10;8H" { 48 t.Error("TGoto expansion failed") 49 } 50 51 // This tests some conditionals 52 if ti.TParm("A[%p1%2.2X]B", 47) != "A[2F]B" { 53 t.Error("TParm conditionals failed") 54 } 55 56 // Color tests. 57 if ti.TParm(ti.SetFg, 7) != "\x1b[37m" { 58 t.Error("SetFg(7) failed") 59 } 60 if ti.TParm(ti.SetFg, 15) != "\x1b[97m" { 61 t.Error("SetFg(15) failed") 62 } 63 if ti.TParm(ti.SetFg, 200) != "\x1b[38;5;200m" { 64 t.Error("SetFg(200) failed") 65 } 66 67 if ti.TParm(ti.MouseMode, 1) != "\x1b[?1000h\x1b[?1003h\x1b[?1006h" { 68 t.Error("Enable mouse mode failed") 69 } 70 if ti.TParm(ti.MouseMode, 0) != "\x1b[?1000l\x1b[?1003l\x1b[?1006l" { 71 t.Error("Disable mouse mode failed") 72 } 73} 74 75func TestTerminfoDelay(t *testing.T) { 76 ti := testTerminfo 77 buf := bytes.NewBuffer(nil) 78 now := time.Now() 79 ti.TPuts(buf, ti.Blink) 80 then := time.Now() 81 s := string(buf.Bytes()) 82 if s != "\x1b2mssomething" { 83 t.Errorf("Terminfo delay failed: %s", s) 84 } 85 if then.Sub(now) < time.Millisecond*20 { 86 t.Error("Too short delay") 87 } 88 if then.Sub(now) > time.Millisecond*50 { 89 t.Error("Too late delay") 90 } 91} 92 93func BenchmarkSetFgBg(b *testing.B) { 94 ti := testTerminfo 95 96 for i := 0; i < b.N; i++ { 97 ti.TParm(ti.SetFg, 100, 200) 98 ti.TParm(ti.SetBg, 100, 200) 99 } 100} 101