simscreen: unbreak the API

The API for NewSimulationScreen() was changed, which turned out to
break 3rd party libraries and applications.  (We didn't forgot that
this is part of the public API for tcell.)
This commit is contained in:
Garrett D'Amore 2023-12-05 14:21:05 -08:00
parent fb3659df9f
commit 26f328fb89
5 changed files with 30 additions and 27 deletions

View File

@ -35,11 +35,11 @@ func eventLoop(s Screen, evch chan Event) {
func TestMouseEvents(t *testing.T) { func TestMouseEvents(t *testing.T) {
s, ss := mkTestScreen(t, "") s := mkTestScreen(t, "")
defer s.Fini() defer s.Fini()
s.EnableMouse() s.EnableMouse()
ss.InjectMouse(4, 9, Button1, ModCtrl) s.InjectMouse(4, 9, Button1, ModCtrl)
evch := make(chan Event) evch := make(chan Event)
em := &EventMouse{} em := &EventMouse{}
done := false done := false
@ -71,11 +71,11 @@ func TestMouseEvents(t *testing.T) {
func TestChannelMouseEvents(t *testing.T) { func TestChannelMouseEvents(t *testing.T) {
s, ss := mkTestScreen(t, "") s := mkTestScreen(t, "")
defer s.Fini() defer s.Fini()
s.EnableMouse() s.EnableMouse()
ss.InjectMouse(4, 9, Button1, ModCtrl) s.InjectMouse(4, 9, Button1, ModCtrl)
evch := make(chan Event) evch := make(chan Event)
quit := make(chan struct{}) quit := make(chan struct{})
em := new(EventMouse) em := new(EventMouse)

View File

@ -19,7 +19,7 @@ import (
) )
func TestCanDisplayUTF8(t *testing.T) { func TestCanDisplayUTF8(t *testing.T) {
s, _ := mkTestScreen(t, "UTF-8") s := mkTestScreen(t, "UTF-8")
defer s.Fini() defer s.Fini()
if s.CharacterSet() != "UTF-8" { if s.CharacterSet() != "UTF-8" {
@ -39,7 +39,7 @@ func TestCanDisplayUTF8(t *testing.T) {
} }
} }
func TestCanDisplayASCII(t *testing.T) { func TestCanDisplayASCII(t *testing.T) {
s, _ := mkTestScreen(t, "US-ASCII") s := mkTestScreen(t, "US-ASCII")
defer s.Fini() defer s.Fini()
if s.CharacterSet() != "US-ASCII" { if s.CharacterSet() != "US-ASCII" {
@ -60,7 +60,7 @@ func TestCanDisplayASCII(t *testing.T) {
} }
func TestRuneFallbacks(t *testing.T) { func TestRuneFallbacks(t *testing.T) {
s, _ := mkTestScreen(t, "US-ASCII") s := mkTestScreen(t, "US-ASCII")
defer s.Fini() defer s.Fini()
if s.CharacterSet() != "US-ASCII" { if s.CharacterSet() != "US-ASCII" {
t.Errorf("Wrong character set: %v", s.CharacterSet()) t.Errorf("Wrong character set: %v", s.CharacterSet())

View File

@ -18,20 +18,20 @@ import (
"testing" "testing"
) )
func mkTestScreen(t *testing.T, charset string) (Screen, SimulationScreen) { func mkTestScreen(t *testing.T, charset string) SimulationScreen {
s, ss := NewSimulationScreen(charset) s := NewSimulationScreen(charset)
if s == nil || ss == nil { if s == nil {
t.Fatalf("Failed to get simulation screen") t.Fatalf("Failed to get simulation screen")
} }
if e := s.Init(); e != nil { if e := s.Init(); e != nil {
t.Fatalf("Failed to initialize screen: %v", e) t.Fatalf("Failed to initialize screen: %v", e)
} }
return s, ss return s
} }
func TestInitScreen(t *testing.T) { func TestInitScreen(t *testing.T) {
s, ss := mkTestScreen(t, "") s := mkTestScreen(t, "")
defer s.Fini() defer s.Fini()
if x, y := s.Size(); x != 80 || y != 25 { if x, y := s.Size(); x != 80 || y != 25 {
@ -40,16 +40,16 @@ func TestInitScreen(t *testing.T) {
if s.CharacterSet() != "UTF-8" { if s.CharacterSet() != "UTF-8" {
t.Fatalf("Character Set (%v) not UTF-8", s.CharacterSet()) t.Fatalf("Character Set (%v) not UTF-8", s.CharacterSet())
} }
if b, x, y := ss.GetContents(); len(b) != x*y || x != 80 || y != 25 { 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) t.Fatalf("Contents (%v, %v, %v) wrong", len(b), x, y)
} }
} }
func TestClearScreen(t *testing.T) { func TestClearScreen(t *testing.T) {
s, ss := mkTestScreen(t, "") s := mkTestScreen(t, "")
defer s.Fini() defer s.Fini()
s.Clear() s.Clear()
b, x, y := ss.GetContents() b, x, y := s.GetContents()
if len(b) != x*y || x != 80 || y != 25 { if len(b) != x*y || x != 80 || y != 25 {
t.Fatalf("Contents (%v, %v, %v) wrong", len(b), x, y) t.Fatalf("Contents (%v, %v, %v) wrong", len(b), x, y)
} }
@ -65,10 +65,10 @@ func TestClearScreen(t *testing.T) {
func TestSetCell(t *testing.T) { func TestSetCell(t *testing.T) {
st := StyleDefault.Background(ColorRed).Blink(true) st := StyleDefault.Background(ColorRed).Blink(true)
s, ss := mkTestScreen(t, "") s := mkTestScreen(t, "")
defer s.Fini() defer s.Fini()
s.SetCell(2, 5, st, '@') s.SetCell(2, 5, st, '@')
b, _, _ := ss.GetContents() b, _, _ := s.GetContents()
s.Show() s.Show()
if len(b) != 80*25 { if len(b) != 80*25 {
t.Fatalf("Wrong content size") t.Fatalf("Wrong content size")
@ -83,10 +83,10 @@ func TestSetCell(t *testing.T) {
func TestResize(t *testing.T) { func TestResize(t *testing.T) {
st := StyleDefault.Background(ColorYellow).Underline(true) st := StyleDefault.Background(ColorYellow).Underline(true)
s, ss := mkTestScreen(t, "") s := mkTestScreen(t, "")
defer s.Fini() defer s.Fini()
s.SetCell(2, 5, st, '&') s.SetCell(2, 5, st, '&')
b, x, y := ss.GetContents() b, x, y := s.GetContents()
s.Show() s.Show()
cell := &b[5*80+2] cell := &b[5*80+2]
@ -97,7 +97,7 @@ func TestResize(t *testing.T) {
} }
s.SetSize(30, 10) s.SetSize(30, 10)
s.Show() s.Show()
b2, x2, y2 := ss.GetContents() b2, x2, y2 := s.GetContents()
if len(b2) == len(b) || x2 == x || y2 == y { if len(b2) == len(b) || x2 == x || y2 == y {
t.Errorf("Screen parameters should not match") t.Errorf("Screen parameters should not match")
} }
@ -111,17 +111,17 @@ func TestResize(t *testing.T) {
} }
func TestBeep(t *testing.T) { func TestBeep(t *testing.T) {
s, ss := mkTestScreen(t, "") s := mkTestScreen(t, "")
defer s.Fini() defer s.Fini()
b0, x0, y0 := ss.GetContents() b0, x0, y0 := s.GetContents()
if err := s.Beep(); err != nil { if err := s.Beep(); err != nil {
t.Errorf("could not beep: %v", err) t.Errorf("could not beep: %v", err)
} }
s.Show() s.Show()
b1, x1, y1 := ss.GetContents() b1, x1, y1 := s.GetContents()
if x0 != x1 { if x0 != x1 {
t.Fatalf("screen width changed unexpectedly from %d to %d", x0, x1) t.Fatalf("screen width changed unexpectedly from %d to %d", x0, x1)
} }

View File

@ -23,19 +23,21 @@ import (
// NewSimulationScreen returns a SimulationScreen. Note that // NewSimulationScreen returns a SimulationScreen. Note that
// SimulationScreen is also a Screen. // SimulationScreen is also a Screen.
func NewSimulationScreen(charset string) (Screen, SimulationScreen) { func NewSimulationScreen(charset string) SimulationScreen {
if charset == "" { if charset == "" {
charset = "UTF-8" charset = "UTF-8"
} }
ss := &simscreen{charset: charset} ss := &simscreen{charset: charset}
s := &baseScreen{screenImpl: ss} ss.Screen = &baseScreen{screenImpl: ss}
return s, ss return ss
} }
// SimulationScreen represents a screen simulation. This is intended to // SimulationScreen represents a screen simulation. This is intended to
// be a superset of normal Screens, but also adds some important interfaces // be a superset of normal Screens, but also adds some important interfaces
// for testing. // for testing.
type SimulationScreen interface { type SimulationScreen interface {
Screen
// InjectKeyBytes injects a stream of bytes corresponding to // InjectKeyBytes injects a stream of bytes corresponding to
// the native encoding (see charset). It turns true if the entire // the native encoding (see charset). It turns true if the entire
// set of bytes were processed and delivered as KeyEvents, false // set of bytes were processed and delivered as KeyEvents, false
@ -97,6 +99,7 @@ type simscreen struct {
fillstyle Style fillstyle Style
fallback map[rune]string fallback map[rune]string
Screen
sync.Mutex sync.Mutex
} }

View File

@ -19,7 +19,7 @@ import (
) )
func TestStyle(t *testing.T) { func TestStyle(t *testing.T) {
s, _ := mkTestScreen(t, "") s := mkTestScreen(t, "")
defer s.Fini() defer s.Fini()
style := StyleDefault style := StyleDefault