From 1ca1b7c246a608b161c4fe07ba271f4203e6db2c Mon Sep 17 00:00:00 2001 From: Max Risuhin Date: Wed, 6 Feb 2019 00:58:31 -0800 Subject: [PATCH] Introduce TestMain to run gui tests in goroutine locked to main app thread; Call LockOSThread only once. (#199) --- buffer/buffer.go | 12 ++++++------ gui/gui.go | 4 ---- main.go | 6 ++++-- main_test.go | 9 ++++++--- vttest/test-cursor-movement-1 | Bin 1950 -> 1920 bytes 5 files changed, 16 insertions(+), 15 deletions(-) diff --git a/buffer/buffer.go b/buffer/buffer.go index 9f8be82..b5f2a4b 100644 --- a/buffer/buffer.go +++ b/buffer/buffer.go @@ -1107,27 +1107,27 @@ func (buffer *Buffer) getMaxLines() uint64 { return result } -func (buffer *Buffer) Save(path string) { +func (buffer *Buffer) SaveViewLines(path string) { f, err := os.Create(path) if err != nil { panic(err) } defer f.Close() - for _, line := range buffer.lines { - f.WriteString(line.String()) + for i := uint16(0); i <= buffer.ViewHeight(); i++ { + f.WriteString(buffer.getViewLine(i).String()) } } -func (buffer *Buffer) Compare(path string) bool { +func (buffer *Buffer) CompareViewLines(path string) bool { f, err := ioutil.ReadFile(path) if err != nil { panic(err) } bufferContent := []byte{} - for _, line := range buffer.lines { - lineBytes := []byte(line.String()) + for i := uint16(0); i <= buffer.ViewHeight(); i++ { + lineBytes := []byte(buffer.getViewLine(i).String()) bufferContent = append(bufferContent, lineBytes...) } return bytes.Equal(f, bufferContent) diff --git a/gui/gui.go b/gui/gui.go index e309423..1b48ded 100644 --- a/gui/gui.go +++ b/gui/gui.go @@ -6,7 +6,6 @@ import ( "image/png" "math" "os" - "runtime" "strconv" "strings" "sync" @@ -296,9 +295,6 @@ func (gui *GUI) Close() { func (gui *GUI) Render() error { - gui.logger.Debugf("Locking OS thread...") - runtime.LockOSThread() - gui.logger.Debugf("Creating window...") var err error gui.window, err = gui.createWindow() diff --git a/main.go b/main.go index bf30570..66f257b 100644 --- a/main.go +++ b/main.go @@ -12,13 +12,15 @@ import ( type callback func(terminal *terminal.Terminal, g *gui.GUI) +func init() { + runtime.LockOSThread() +} + func main() { initialize(nil) } func initialize(fn callback) { - runtime.LockOSThread() - conf := getConfig() logger, err := getLogger(conf) if err != nil { diff --git a/main_test.go b/main_test.go index 152906f..c761761 100644 --- a/main_test.go +++ b/main_test.go @@ -53,8 +53,11 @@ func enter(terminal *terminal.Terminal) { terminal.Write([]byte("\n")) } -func TestCursorMovement(t *testing.T) { +func TestMain(m *testing.M) { + testCursorMovement() +} +func testCursorMovement() { testFunc := func(term *terminal.Terminal, g *gui.GUI) { sleep() send(term, "vttest\n") @@ -62,8 +65,8 @@ func TestCursorMovement(t *testing.T) { send(term, "1\n") sleep() - if term.ActiveBuffer().Compare("vttest/test-cursor-movement-1") == false { - t.Error(fmt.Sprint("ActiveBuffer doesn't match vttest template")) + if term.ActiveBuffer().CompareViewLines("vttest/test-cursor-movement-1") == false { + os.Exit(terminate(fmt.Sprintf("ActiveBuffer doesn't match vttest template vttest/test-cursor-movement-1"))) } g.Screenshot ("test-cursor-movement-1.png") compareImages("vttest/test-cursor-movement-1.png", "test-cursor-movement-1.png") diff --git a/vttest/test-cursor-movement-1 b/vttest/test-cursor-movement-1 index 054a1ab94fad4f654f3bb71378227c70a2f4b505..9c02d9825826bf2082c9cee9c3faa3908e2df430 100644 GIT binary patch literal 1920 zcmeH|&5pt#6oq}1vs39fiF-3Uwi9FY0cg1jnFy0(+qDukp=n7tk{^ID5=vYs zexNh8vp=!1wr+(Ho+Jl*1e^PQZpdX7S++?PD+9!kE9S4hpb_uvjl6g9yLY>!7X4Ms@g1DQ`Qku-WnbVfX!o jF<#;ODEwaXolmL5_cw);_r7^KiPvh%uc!+DEE)R)#45n5 literal 1950 zcmeH|!Ab)$5Qh6G|4mJ-;9b1+DpJ}9$Zr0c;5LyYtM%>8E*kZ)mk|VP+tSeX&Cj& z>|k_V@XtB1^I?qzo@RhQKwQMRR!(p5W5%T1iTa6|I)`{teHz|j-5grr9j(>_F{Ok& zX&F)B5Y&-Vug%XMHs)NO($aiyHW|P>;_~(?Gf2GnXp{BsYfzhRCY2w@XmGdP?DpG- ido1x9@1v+t@BG?tJ*HHic}0pU=P&eYd9NDZQt<&tg`AfF