2021-01-25 07:18:06 +08:00
|
|
|
// Copyright 2021 The TCell Authors
|
2015-10-06 13:53:22 +08:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the license at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-11-24 04:31:44 +08:00
|
|
|
package terminfo
|
2015-10-06 13:53:22 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
2019-07-22 10:38:04 +08:00
|
|
|
"time"
|
2015-10-06 13:53:22 +08:00
|
|
|
)
|
|
|
|
|
2016-05-19 13:53:05 +08:00
|
|
|
// This terminfo entry is a stripped down version from
|
|
|
|
// xterm-256color, but I've added some of my own entries.
|
|
|
|
var testTerminfo = &Terminfo{
|
|
|
|
Name: "simulation_test",
|
|
|
|
Columns: 80,
|
|
|
|
Lines: 24,
|
|
|
|
Colors: 256,
|
|
|
|
Bell: "\a",
|
2019-07-22 10:38:04 +08:00
|
|
|
Blink: "\x1b2ms$<20>something",
|
2016-05-19 13:53:05 +08:00
|
|
|
Reverse: "\x1b[7m",
|
|
|
|
SetFg: "\x1b[%?%p1%{8}%<%t3%p1%d%e%p1%{16}%<%t9%p1%{8}%-%d%e38;5;%p1%d%;m",
|
|
|
|
SetBg: "\x1b[%?%p1%{8}%<%t4%p1%d%e%p1%{16}%<%t10%p1%{8}%-%d%e48;5;%p1%d%;m",
|
|
|
|
AltChars: "``aaffggiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~",
|
|
|
|
Mouse: "\x1b[M",
|
|
|
|
SetCursor: "\x1b[%i%p1%d;%p2%dH",
|
|
|
|
PadChar: "\x00",
|
|
|
|
}
|
|
|
|
|
2018-12-19 13:33:43 +08:00
|
|
|
func TestTerminfoExpansion(t *testing.T) {
|
|
|
|
|
|
|
|
ti := testTerminfo
|
|
|
|
|
|
|
|
// Tests %i and basic parameter strings too
|
|
|
|
if ti.TGoto(7, 9) != "\x1b[10;8H" {
|
|
|
|
t.Error("TGoto expansion failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
// This tests some conditionals
|
|
|
|
if ti.TParm("A[%p1%2.2X]B", 47) != "A[2F]B" {
|
|
|
|
t.Error("TParm conditionals failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Color tests.
|
|
|
|
if ti.TParm(ti.SetFg, 7) != "\x1b[37m" {
|
|
|
|
t.Error("SetFg(7) failed")
|
|
|
|
}
|
|
|
|
if ti.TParm(ti.SetFg, 15) != "\x1b[97m" {
|
|
|
|
t.Error("SetFg(15) failed")
|
|
|
|
}
|
|
|
|
if ti.TParm(ti.SetFg, 200) != "\x1b[38;5;200m" {
|
|
|
|
t.Error("SetFg(200) failed")
|
|
|
|
}
|
|
|
|
}
|
2015-10-06 13:53:22 +08:00
|
|
|
|
2019-07-22 10:38:04 +08:00
|
|
|
func TestTerminfoDelay(t *testing.T) {
|
2018-12-19 13:33:43 +08:00
|
|
|
ti := testTerminfo
|
|
|
|
buf := bytes.NewBuffer(nil)
|
2019-07-22 10:38:04 +08:00
|
|
|
now := time.Now()
|
2019-07-24 09:54:20 +08:00
|
|
|
ti.TPuts(buf, ti.Blink)
|
2019-07-22 10:38:04 +08:00
|
|
|
then := time.Now()
|
2018-12-19 13:33:43 +08:00
|
|
|
s := string(buf.Bytes())
|
2019-07-22 10:38:04 +08:00
|
|
|
if s != "\x1b2mssomething" {
|
|
|
|
t.Errorf("Terminfo delay failed: %s", s)
|
2018-12-19 13:33:43 +08:00
|
|
|
}
|
2019-07-22 10:38:04 +08:00
|
|
|
if then.Sub(now) < time.Millisecond*20 {
|
|
|
|
t.Error("Too short delay")
|
|
|
|
}
|
|
|
|
if then.Sub(now) > time.Millisecond*50 {
|
|
|
|
t.Error("Too late delay")
|
2018-12-19 13:33:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-19 13:53:05 +08:00
|
|
|
func BenchmarkSetFgBg(b *testing.B) {
|
|
|
|
ti := testTerminfo
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
ti.TParm(ti.SetFg, 100, 200)
|
|
|
|
ti.TParm(ti.SetBg, 100, 200)
|
|
|
|
}
|
|
|
|
}
|