Skip to content

Commit

Permalink
Update SimScreen with new refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
gdamore committed Dec 4, 2023
1 parent ef4f9cc commit 602a473
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 44 deletions.
10 changes: 5 additions & 5 deletions event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"time"
)

func eventLoop(s SimulationScreen, evch chan Event) {
func eventLoop(s Screen, evch chan Event) {
for {
ev := s.PollEvent()
if ev == nil {
Expand All @@ -35,11 +35,11 @@ func eventLoop(s SimulationScreen, evch chan Event) {

func TestMouseEvents(t *testing.T) {

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

s.EnableMouse()
s.InjectMouse(4, 9, Button1, ModCtrl)
ss.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 := mkTestScreen(t, "")
s, ss := mkTestScreen(t, "")
defer s.Fini()

s.EnableMouse()
s.InjectMouse(4, 9, Button1, ModCtrl)
ss.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
34 changes: 17 additions & 17 deletions sim_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2018 The TCell Authors
// Copyright 2023 The TCell Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use file except in compliance with the License.
Expand All @@ -18,20 +18,20 @@ import (
"testing"
)

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

func TestInitScreen(t *testing.T) {

s := mkTestScreen(t, "")
s, ss := 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 := s.GetContents(); len(b) != x*y || x != 80 || y != 25 {
if b, x, y := ss.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 := mkTestScreen(t, "")
s, ss := mkTestScreen(t, "")
defer s.Fini()
s.Clear()
b, x, y := s.GetContents()
b, x, y := ss.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 := mkTestScreen(t, "")
s, ss := mkTestScreen(t, "")
defer s.Fini()
s.SetCell(2, 5, st, '@')
b, _, _ := s.GetContents()
b, _, _ := ss.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 := mkTestScreen(t, "")
s, ss := mkTestScreen(t, "")
defer s.Fini()
s.SetCell(2, 5, st, '&')
b, x, y := s.GetContents()
b, x, y := ss.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 := s.GetContents()
b2, x2, y2 := ss.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 := mkTestScreen(t, "")
s, ss := mkTestScreen(t, "")
defer s.Fini()

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

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

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

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

// SimulationScreen represents a screen simulation. This is intended to
Expand Down Expand Up @@ -57,8 +58,6 @@ type SimulationScreen interface {

// GetCursor returns the cursor details.
GetCursor() (x int, y int, visible bool)

Screen
}

// SimCell represents a simulated screen cell. The purpose of this
Expand Down Expand Up @@ -150,25 +149,12 @@ func (s *simscreen) SetStyle(style Style) {
s.Unlock()
}

func (s *simscreen) Clear() {
s.Fill(' ', s.style)
}

func (s *simscreen) Fill(r rune, style Style) {
s.Lock()
s.back.Fill(r, style)
s.Unlock()
}

func (s *simscreen) SetCell(x, y int, style Style, ch ...rune) {

if len(ch) > 0 {
s.SetContent(x, y, ch[0], ch[1:], style)
} else {
s.SetContent(x, y, ' ', nil, style)
}
}

func (s *simscreen) SetContent(x, y int, mainc rune, combc []rune, st Style) {

s.Lock()
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 602a473

Please sign in to comment.