-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoffscreen_test.go
More file actions
143 lines (130 loc) · 3.75 KB
/
offscreen_test.go
File metadata and controls
143 lines (130 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright (c) 2022-2023 The Go-Curses Authors
// Copyright 2018 The TCell Authors
//
// 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.
package cdk
import (
"testing"
"github.com/go-curses/cdk/lib/paint"
)
func TestInitScreen(t *testing.T) {
s := NewTestingScreen(t, "")
defer s.Close()
if x, y := s.Size(); x != 80 || y != 25 {
t.Fatalf("Size should be 80, 25, was %v, %v", x, y)
}
if s.CharacterSet() != "UTF-8" {
t.Fatalf("Character Set (%v) not UTF-8", s.CharacterSet())
}
if b, x, y := s.GetContents(); len(b) != x*y || x != 80 || y != 25 {
t.Fatalf("Contents (%v, %v, %v) wrong", len(b), x, y)
}
}
func TestClearScreen(t *testing.T) {
s := NewTestingScreen(t, "")
defer s.Close()
s.Clear()
b, x, y := s.GetContents()
if len(b) != x*y || x != 80 || y != 25 {
t.Fatalf("Contents (%v, %v, %v) wrong", len(b), x, y)
}
for i := 0; i < x*y; i++ {
if len(b[i].Runes) == 1 && b[i].Runes[0] != ' ' {
t.Errorf("Incorrect contents at %v: %v", i, b[i].Runes)
}
if b[i].Style != paint.StyleDefault {
t.Errorf("Incorrect style at %v: %v", i, b[i].Style)
}
}
}
func TestSetCell(t *testing.T) {
st := paint.StyleDefault.Background(paint.ColorRed).Blink(true)
s := NewTestingScreen(t, "")
defer s.Close()
s.SetCell(2, 5, st, '@')
b, _, _ := s.GetContents()
s.Show()
if len(b) != 80*25 {
s.Close()
t.Fatalf("Wrong content size")
}
cell := &b[5*80+2]
if len(cell.Runes) != 1 || len(cell.Bytes) != 1 ||
cell.Runes[0] != '@' || cell.Bytes[0] != '@' ||
cell.Style != st {
t.Errorf("Incorrect cell content: %v", cell)
}
}
func TestResize(t *testing.T) {
st := paint.StyleDefault.Background(paint.ColorYellow).Underline(true)
s := NewTestingScreen(t, "")
defer s.Close()
s.SetCell(2, 5, st, '&')
b, x, y := s.GetContents()
s.Show()
cell := &b[5*80+2]
if len(cell.Runes) != 1 || len(cell.Bytes) != 1 ||
cell.Runes[0] != '&' || cell.Bytes[0] != '&' ||
cell.Style != st {
t.Errorf("Incorrect cell content: %v", cell)
}
s.SetSize(30, 10)
s.Show()
b2, x2, y2 := s.GetContents()
if len(b2) == len(b) || x2 == x || y2 == y {
t.Errorf("Display parameters should not match")
}
cell2 := &b[5*80+2]
if len(cell2.Runes) != 1 || len(cell2.Bytes) != 1 ||
cell2.Runes[0] != '&' || cell2.Bytes[0] != '&' ||
cell2.Style != st {
t.Errorf("Incorrect cell content after resize: %v", cell2)
}
}
func TestBeep(t *testing.T) {
s := NewTestingScreen(t, "")
defer s.Close()
b0, x0, y0 := s.GetContents()
if err := s.Beep(); err != nil {
t.Errorf("could not beep: %v", err)
}
s.Show()
b1, x1, y1 := s.GetContents()
if x0 != x1 {
t.Fatalf("display width changed unexpectedly from %d to %d", x0, x1)
}
if y0 != y1 {
t.Fatalf("display height changed unexpectedly from %d to %d", y0, y1)
}
if len(b0) != len(b1) {
t.Fatalf("display size changed unexpectedly (had %d cells, has %d cells)",
len(b0), len(b1))
}
for i := 0; i < len(b0); i++ {
cell0 := b0[i]
cell1 := b1[i]
if len(cell0.Runes) != len(cell1.Runes) {
t.Errorf("incorrect cell content: had %d runes, has %d runes",
len(cell0.Runes), len(cell1.Runes))
continue
}
for j := 0; j < len(cell0.Runes); j++ {
r0 := cell0.Runes[j]
r1 := cell1.Runes[j]
if r0 != r1 {
t.Errorf("incorrect content: cell %d rune %d changed from %v to %v",
i, j, r0, r1)
}
}
}
}