Skip to content
Myr

screen_test.go

This file

Path

screen_test.go

207 lines · 3.9 KiB · Go

Last changed by

fix: prevent data race with cursedRenderer.onMouse (#1691)Liam Stanleybefore adoptionc60f0c5

History of this file

Take it

1package tea23import (4	"bytes"5	"image/color"6	"testing"78	"github.com/charmbracelet/colorprofile"9	"github.com/charmbracelet/x/exp/golden"10)1112type testViewOpts struct {13	altScreen   bool14	mouseMode   MouseMode15	showCursor  bool16	disableBp   bool17	keyReleases bool18	bgColor     color.Color19}2021func testViewOptsCmds(opts ...testViewOpts) []Cmd {22	cmds := make([]Cmd, len(opts))23	for i, o := range opts {24		o := o25		cmds[i] = func() Msg {26			return o27		}28	}29	return cmds30}3132type testViewModel struct {33	*testModel34	opts testViewOpts35}3637func (m *testViewModel) Update(msg Msg) (Model, Cmd) {38	switch msg := msg.(type) {39	case testViewOpts:40		m.opts = msg41		return m, nil42	}43	tm, cmd := m.testModel.Update(msg)44	m.testModel = tm.(*testModel)45	return m, cmd46}4748func (m *testViewModel) View() View {49	v := m.testModel.View()50	v.AltScreen = m.opts.altScreen51	v.MouseMode = m.opts.mouseMode52	v.DisableBracketedPasteMode = m.opts.disableBp53	v.KeyboardEnhancements.ReportEventTypes = m.opts.keyReleases54	v.BackgroundColor = m.opts.bgColor55	if m.opts.showCursor {56		v.Cursor = NewCursor(0, 0)57	}58	return v59}6061func TestViewModel(t *testing.T) {62	tests := []struct {63		name string64		opts []testViewOpts65	}{66		{67			name: "altscreen",68			opts: []testViewOpts{69				{altScreen: true},70				{altScreen: false},71			},72		},73		{74			name: "altscreen_autoexit",75			opts: []testViewOpts{76				{altScreen: true},77			},78		},79		{80			name: "mouse_cellmotion",81			opts: []testViewOpts{82				{mouseMode: MouseModeCellMotion},83			},84		},85		{86			name: "mouse_allmotion",87			opts: []testViewOpts{88				{mouseMode: MouseModeAllMotion},89			},90		},91		{92			name: "mouse_disable",93			opts: []testViewOpts{94				{mouseMode: MouseModeAllMotion},95				{mouseMode: MouseModeNone},96			},97		},98		{99			name: "cursor_hide",100			opts: []testViewOpts{101				{},102			},103		},104		{105			name: "cursor_hideshow",106			opts: []testViewOpts{107				{showCursor: false},108				{showCursor: true},109			},110		},111		{112			name: "bp_stop_start",113			opts: []testViewOpts{114				{disableBp: true},115				{disableBp: false},116			},117		},118		{119			name: "kitty_stop_startreleases",120			opts: []testViewOpts{121				{},122				{keyReleases: true},123			},124		},125		{126			name: "bg_set_color",127			opts: []testViewOpts{128				{bgColor: color.RGBA{255, 255, 255, 255}},129			},130		},131	}132133	for _, test := range tests {134		t.Run(test.name, func(t *testing.T) {135			t.Parallel()136			var buf bytes.Buffer137			var in bytes.Buffer138139			m := &testViewModel{testModel: &testModel{}}140			p := NewProgram(m,141				// Set the initial window size for the program.142				WithWindowSize(80, 24),143				// Use ANSI256 to increase test coverage.144				WithColorProfile(colorprofile.ANSI256),145				// always use xterm and 256 colors for tests146				WithEnvironment([]string{"TERM=xterm-256color"}),147				WithInput(&in),148				WithOutput(&buf),149			)150151			go p.Send(append(sequenceMsg(testViewOptsCmds(test.opts...)), Quit))152153			if _, err := p.Run(); err != nil {154				t.Fatal(err)155			}156			golden.RequireEqual(t, buf.Bytes())157		})158	}159}160161func TestClearMsg(t *testing.T) {162	type test struct {163		name string164		cmds sequenceMsg165	}166	tests := []test{167		{168			name: "clear_screen",169			cmds: []Cmd{ClearScreen},170		},171		{172			name: "read_set_clipboard",173			cmds: []Cmd{ReadClipboard, SetClipboard("success")},174		},175		{176			name: "bg_fg_cur_color",177			cmds: []Cmd{RequestForegroundColor, RequestBackgroundColor, RequestCursorColor},178		},179	}180181	for _, test := range tests {182		t.Run(test.name, func(t *testing.T) {183			t.Parallel()184			var buf bytes.Buffer185			var in bytes.Buffer186187			m := &testModel{}188			p := NewProgram(m,189				// Set the initial window size for the program.190				WithWindowSize(80, 24),191				// Use ANSI256 to increase test coverage.192				WithColorProfile(colorprofile.ANSI256),193				// always use xterm and 256 colors for tests194				WithEnvironment([]string{"TERM=xterm-256color"}),195				WithInput(&in),196				WithOutput(&buf),197			)198199			go p.Send(append(test.cmds, Quit))200201			if _, err := p.Run(); err != nil {202				t.Fatal(err)203			}204			golden.RequireEqual(t, buf.Bytes())205		})206	}207}