...

Source file src/github.com/gdamore/tcell/v2/terminfo/terminfo_test.go

Documentation: github.com/gdamore/tcell/v2/terminfo

     1  // Copyright 2022 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  	"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.
    25  var 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  	SetCursor: "\x1b[%i%p1%d;%p2%dH",
    38  	PadChar:   "\x00",
    39  	EnterUrl:  "\x1b]8;%p2%s;%p1%s\x1b\\",
    40  }
    41  
    42  func TestTerminfoExpansion(t *testing.T) {
    43  	ti := testTerminfo
    44  
    45  	// Tests %i and basic parameter strings too
    46  	if ti.TGoto(7, 9) != "\x1b[10;8H" {
    47  		t.Error("TGoto expansion failed")
    48  	}
    49  
    50  	// This tests some conditionals
    51  	if ti.TParm("A[%p1%2.2X]B", 47) != "A[2F]B" {
    52  		t.Error("TParm conditionals failed")
    53  	}
    54  
    55  	// Color tests.
    56  	if ti.TParm(ti.SetFg, 7) != "\x1b[37m" {
    57  		t.Error("SetFg(7) failed")
    58  	}
    59  	if ti.TParm(ti.SetFg, 15) != "\x1b[97m" {
    60  		t.Error("SetFg(15) failed")
    61  	}
    62  	if ti.TParm(ti.SetFg, 200) != "\x1b[38;5;200m" {
    63  		t.Error("SetFg(200) failed")
    64  	}
    65  
    66  	type testCase struct {
    67  		expect string
    68  		format string
    69  		params []interface{}
    70  	}
    71  
    72  	cases := []testCase{
    73  		{expect: "0a", format: "%p1%02x", params: []interface{}{10}},
    74  		{expect: "0A", format: "%p1%02X", params: []interface{}{10}},
    75  		{expect: "A", format: "%p1%c", params: []interface{}{65}},
    76  		{expect: "A", format: "%'A'%c", params: []interface{}{}},
    77  		{expect: "65", format: "%'A'%d", params: []interface{}{}},
    78  		{expect: "7", format: "%i%p1%p2%+%d", params: []interface{}{2, 3}},
    79  		{expect: "abc", format: "%p1%s", params: []interface{}{"abc"}},
    80  		{expect: "1%d", format: "1%%d", params: []interface{}{}},
    81  		{expect: "abc", format: "%p1%s%", params: []interface{}{"abc"}}, // unterminated %
    82  		{expect: "  abc", format: "%p1%5s", params: []interface{}{"abc"}},
    83  		{expect: "abc  ", format: "%p1%:-5s", params: []interface{}{"abc"}},
    84  		{expect: "15", format: "%{3}%p1%*%d", params: []interface{}{5}},
    85  		{expect: " A", format: "%p1%2c", params: []interface{}{65}},
    86  		{expect: "4", format: "%p1%l%d", params: []interface{}{"four"}},
    87  		{expect: "0", format: "%pA%d", params: []interface{}{}}, // missing/invalid parameter
    88  		{expect: "5", format: "%p1%p2%/%d", params: []interface{}{15, 3}},
    89  		{expect: "0", format: "%p1%p2%/%d", params: []interface{}{3, 15}},
    90  		{expect: "0", format: "%p1%p2%/%d", params: []interface{}{3, 0}},
    91  		{expect: "3", format: "%p1%p2%m%d", params: []interface{}{15, 4}},
    92  		{expect: "0", format: "%p1%p2%m%d", params: []interface{}{3, 0}},
    93  		{expect: "2", format: "%p1%Pa%{4}%{3}%ga%d", params: []interface{}{2}},
    94  		{expect: "2", format: "%p1%PA%{4}%{3}%gA%d", params: []interface{}{2}},
    95  		{expect: "0", format: "%p1%PA%{4}%{3}%ga%d", params: []interface{}{2}},
    96  		{expect: "0", format: "%p1%Pz%{4}%{3}%gZ%d", params: []interface{}{2}},
    97  		{expect: "0", format: "%d", params: []interface{}{}}, // underflow
    98  		{expect: "", format: "%s", params: []interface{}{}},  // underflow
    99  		{expect: "1", format: "%p1%p2%=%d", params: []interface{}{3, 3}},
   100  		{expect: "0", format: "%p1%p2%=%d", params: []interface{}{3, 4}},
   101  		{expect: "1", format: "%p1%p2%=%!%d", params: []interface{}{3, 4}},
   102  		{expect: "1", format: "%p1%p2%>%d", params: []interface{}{4, 3}},
   103  		{expect: "3", format: "%p1%p2%|%d", params: []interface{}{1, 2}},
   104  		{expect: "2", format: "%p1%p2%&%d", params: []interface{}{2, 3}},
   105  		{expect: "1", format: "%p1%p2%^%d", params: []interface{}{2, 3}},
   106  		{expect: "f", format: "%p1%~%{255}%&%x", params: []interface{}{0xf0}},
   107  		{expect: "%Z", format: "%Z", params: []interface{}{2, 3}}, // unknown sequence
   108  	}
   109  
   110  	for i := range cases {
   111  		if res := ti.TParm(cases[i].format, cases[i].params...); res != cases[i].expect {
   112  			t.Errorf("Format case %d failed: Format %q got %q", i, cases[i].format, res)
   113  		}
   114  	}
   115  	t.Logf("Tested %d cases", len(cases))
   116  }
   117  
   118  func TestTerminfoDelay(t *testing.T) {
   119  	ti := testTerminfo
   120  	buf := bytes.NewBuffer(nil)
   121  	now := time.Now()
   122  	ti.TPuts(buf, ti.Blink)
   123  	then := time.Now()
   124  	s := string(buf.Bytes())
   125  	if s != "\x1b2mssomething" {
   126  		t.Errorf("Terminfo delay failed: %s", s)
   127  	}
   128  	if then.Sub(now) < time.Millisecond*20 {
   129  		t.Error("Too short delay")
   130  	}
   131  	if then.Sub(now) > time.Millisecond*50 {
   132  		t.Error("Too late delay")
   133  	}
   134  }
   135  
   136  func TestStringParameter(t *testing.T) {
   137  	ti := testTerminfo
   138  	s := ti.TParm(ti.EnterUrl, "https://example.org/test")
   139  	if s != "\x1b]8;;https://example.org/test\x1b\\" {
   140  		t.Errorf("Result string failed: %s", s)
   141  	}
   142  	s = ti.TParm(ti.EnterUrl, "https://example.org/test", "id=1234")
   143  	if s != "\x1b]8;id=1234;https://example.org/test\x1b\\" {
   144  		t.Errorf("Result string failed: %s", s)
   145  	}
   146  }
   147  
   148  func BenchmarkSetFgBg(b *testing.B) {
   149  	ti := testTerminfo
   150  
   151  	for i := 0; i < b.N; i++ {
   152  		ti.TParm(ti.SetFg, 100, 200)
   153  		ti.TParm(ti.SetBg, 100, 200)
   154  	}
   155  }
   156  

View as plain text