Skip to content

Commit

Permalink
simscreen: unbreak the API
Browse files Browse the repository at this point in the history
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.)
  • Loading branch information
gdamore committed Dec 5, 2023
1 parent fb3659d commit 26f328f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 27 deletions.
8 changes: 4 additions & 4 deletions event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ func eventLoop(s Screen, evch chan Event) {

func TestMouseEvents(t *testing.T) {

s, ss := mkTestScreen(t, "")
s := mkTestScreen(t, "")
defer s.Fini()

s.EnableMouse()
ss.InjectMouse(4, 9, Button1, ModCtrl)
s.InjectMouse(4, 9, Button1, ModCtrl)
evch := make(chan Event)
em := &EventMouse{}
done := false
Expand Down Expand Up @@ -71,11 +71,11 @@ func TestMouseEvents(t *testing.T) {

func TestChannelMouseEvents(t *testing.T) {

s, ss := mkTestScreen(t, "")
s := mkTestScreen(t, "")
defer s.Fini()

s.EnableMouse()
ss.InjectMouse(4, 9, Button1, ModCtrl)
s.InjectMouse(4, 9, Button1, ModCtrl)
evch := make(chan Event)
quit := make(chan struct{})
em := new(EventMouse)
Expand Down
6 changes: 3 additions & 3 deletions runes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
)

func TestCanDisplayUTF8(t *testing.T) {
s, _ := mkTestScreen(t, "UTF-8")
s := mkTestScreen(t, "UTF-8")
defer s.Fini()

if s.CharacterSet() != "UTF-8" {
Expand All @@ -39,7 +39,7 @@ func TestCanDisplayUTF8(t *testing.T) {
}
}
func TestCanDisplayASCII(t *testing.T) {
s, _ := mkTestScreen(t, "US-ASCII")
s := mkTestScreen(t, "US-ASCII")
defer s.Fini()

if s.CharacterSet() != "US-ASCII" {
Expand All @@ -60,7 +60,7 @@ func TestCanDisplayASCII(t *testing.T) {
}

func TestRuneFallbacks(t *testing.T) {
s, _ := mkTestScreen(t, "US-ASCII")
s := mkTestScreen(t, "US-ASCII")
defer s.Fini()
if s.CharacterSet() != "US-ASCII" {
t.Errorf("Wrong character set: %v", s.CharacterSet())
Expand Down
32 changes: 16 additions & 16 deletions sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ import (
"testing"
)

func mkTestScreen(t *testing.T, charset string) (Screen, SimulationScreen) {
s, ss := NewSimulationScreen(charset)
if s == nil || ss == nil {
func mkTestScreen(t *testing.T, charset string) SimulationScreen {
s := NewSimulationScreen(charset)
if s == nil {
t.Fatalf("Failed to get simulation screen")
}
if e := s.Init(); e != nil {
t.Fatalf("Failed to initialize screen: %v", e)
}
return s, ss
return s
}

func TestInitScreen(t *testing.T) {

s, ss := mkTestScreen(t, "")
s := mkTestScreen(t, "")
defer s.Fini()

if x, y := s.Size(); x != 80 || y != 25 {
Expand All @@ -40,16 +40,16 @@ func TestInitScreen(t *testing.T) {
if s.CharacterSet() != "UTF-8" {
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)
}
}

func TestClearScreen(t *testing.T) {
s, ss := mkTestScreen(t, "")
s := mkTestScreen(t, "")
defer s.Fini()
s.Clear()
b, x, y := ss.GetContents()
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)
}
Expand All @@ -65,10 +65,10 @@ func TestClearScreen(t *testing.T) {

func TestSetCell(t *testing.T) {
st := StyleDefault.Background(ColorRed).Blink(true)
s, ss := mkTestScreen(t, "")
s := mkTestScreen(t, "")
defer s.Fini()
s.SetCell(2, 5, st, '@')
b, _, _ := ss.GetContents()
b, _, _ := s.GetContents()
s.Show()
if len(b) != 80*25 {
t.Fatalf("Wrong content size")
Expand All @@ -83,10 +83,10 @@ func TestSetCell(t *testing.T) {

func TestResize(t *testing.T) {
st := StyleDefault.Background(ColorYellow).Underline(true)
s, ss := mkTestScreen(t, "")
s := mkTestScreen(t, "")
defer s.Fini()
s.SetCell(2, 5, st, '&')
b, x, y := ss.GetContents()
b, x, y := s.GetContents()
s.Show()

cell := &b[5*80+2]
Expand All @@ -97,7 +97,7 @@ func TestResize(t *testing.T) {
}
s.SetSize(30, 10)
s.Show()
b2, x2, y2 := ss.GetContents()
b2, x2, y2 := s.GetContents()
if len(b2) == len(b) || x2 == x || y2 == y {
t.Errorf("Screen parameters should not match")
}
Expand All @@ -111,17 +111,17 @@ func TestResize(t *testing.T) {
}

func TestBeep(t *testing.T) {
s, ss := mkTestScreen(t, "")
s := mkTestScreen(t, "")
defer s.Fini()

b0, x0, y0 := ss.GetContents()
b0, x0, y0 := s.GetContents()

if err := s.Beep(); err != nil {
t.Errorf("could not beep: %v", err)
}
s.Show()

b1, x1, y1 := ss.GetContents()
b1, x1, y1 := s.GetContents()
if x0 != x1 {
t.Fatalf("screen width changed unexpectedly from %d to %d", x0, x1)
}
Expand Down
9 changes: 6 additions & 3 deletions simulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,21 @@ import (

// NewSimulationScreen returns a SimulationScreen. Note that
// SimulationScreen is also a Screen.
func NewSimulationScreen(charset string) (Screen, SimulationScreen) {
func NewSimulationScreen(charset string) SimulationScreen {
if charset == "" {
charset = "UTF-8"
}
ss := &simscreen{charset: charset}
s := &baseScreen{screenImpl: ss}
return s, ss
ss.Screen = &baseScreen{screenImpl: ss}
return ss
}

// SimulationScreen represents a screen simulation. This is intended to
// be a superset of normal Screens, but also adds some important interfaces
// for testing.
type SimulationScreen interface {
Screen

// InjectKeyBytes injects a stream of bytes corresponding to
// the native encoding (see charset). It turns true if the entire
// set of bytes were processed and delivered as KeyEvents, false
Expand Down Expand Up @@ -97,6 +99,7 @@ type simscreen struct {
fillstyle Style
fallback map[rune]string

Screen
sync.Mutex
}

Expand Down
2 changes: 1 addition & 1 deletion style_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
)

func TestStyle(t *testing.T) {
s, _ := mkTestScreen(t, "")
s := mkTestScreen(t, "")
defer s.Fini()

style := StyleDefault
Expand Down

0 comments on commit 26f328f

Please sign in to comment.