github.com/elves/elvish@v0.15.0/pkg/cli/widget_test_utils_test.go (about)

     1  package cli
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/elves/elvish/pkg/cli/term"
     7  	"github.com/elves/elvish/pkg/ui"
     8  )
     9  
    10  // Unable to test the failure branches, as we cannot construct a valid
    11  // *testing.T instance from outside the testing package :(
    12  
    13  func TestTestRender(t *testing.T) {
    14  	TestRender(t, []RenderTest{
    15  		{
    16  			Name:  "test",
    17  			Given: &testWidget{text: ui.T("test")},
    18  			Width: 10, Height: 10,
    19  
    20  			Want: term.NewBufferBuilder(10).Write("test"),
    21  		},
    22  	})
    23  }
    24  
    25  type testHandlerWithState struct {
    26  	State testHandlerState
    27  }
    28  
    29  type testHandlerState struct {
    30  	last  term.Event
    31  	total int
    32  }
    33  
    34  func (h *testHandlerWithState) Handle(e term.Event) bool {
    35  	if e == term.K('x') {
    36  		return false
    37  	}
    38  	h.State.last = e
    39  	h.State.total++
    40  	return true
    41  }
    42  
    43  func TestTestHandle(t *testing.T) {
    44  	TestHandle(t, []HandleTest{
    45  		{
    46  			Name:  "WantNewState",
    47  			Given: &testHandlerWithState{},
    48  			Event: term.K('a'),
    49  
    50  			WantNewState: testHandlerState{last: term.K('a'), total: 1},
    51  		},
    52  		{
    53  			Name:   "Multiple events",
    54  			Given:  &testHandlerWithState{},
    55  			Events: []term.Event{term.K('a'), term.K('b')},
    56  
    57  			WantNewState: testHandlerState{last: term.K('b'), total: 2},
    58  		},
    59  		{
    60  			Name:  "WantUnhaneld",
    61  			Given: &testHandlerWithState{},
    62  			Event: term.K('x'),
    63  
    64  			WantUnhandled: true,
    65  		},
    66  	})
    67  }