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

     1  package cli
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/elves/elvish/pkg/cli/term"
     8  	"github.com/elves/elvish/pkg/ui"
     9  )
    10  
    11  var textViewRenderTests = []RenderTest{
    12  	{
    13  		Name: "text fits entirely",
    14  		Given: NewTextView(TextViewSpec{State: TextViewState{
    15  			Lines: []string{"line 1", "line 2", "line 3"}}}),
    16  		Width: 10, Height: 4,
    17  		Want: bb(10).
    18  			Write("line 1").Newline().
    19  			Write("line 2").Newline().
    20  			Write("line 3").Buffer(),
    21  	},
    22  	{
    23  		Name: "text cropped horizontally",
    24  		Given: NewTextView(TextViewSpec{State: TextViewState{
    25  			Lines: []string{"a very long line"}}}),
    26  		Width: 10, Height: 4,
    27  		Want: bb(10).
    28  			Write("a very lon").Buffer(),
    29  	},
    30  	{
    31  		Name: "text cropped vertically",
    32  		Given: NewTextView(TextViewSpec{State: TextViewState{
    33  			Lines: []string{"line 1", "line 2", "line 3"}}}),
    34  		Width: 10, Height: 2,
    35  		Want: bb(10).
    36  			Write("line 1").Newline().
    37  			Write("line 2").Buffer(),
    38  	},
    39  	{
    40  		Name: "text cropped vertically, with scrollbar",
    41  		Given: NewTextView(TextViewSpec{
    42  			Scrollable: true,
    43  			State: TextViewState{
    44  				Lines: []string{"line 1", "line 2", "line 3", "line 4"}}}),
    45  		Width: 10, Height: 2,
    46  		Want: bb(10).
    47  			Write("line 1   ").
    48  			Write(" ", ui.Inverse, ui.FgMagenta).Newline().
    49  			Write("line 2   ").
    50  			Write("│", ui.FgMagenta).Buffer(),
    51  	},
    52  	{
    53  		Name: "State.First adjusted to fit text",
    54  		Given: NewTextView(TextViewSpec{State: TextViewState{
    55  			First: 2,
    56  			Lines: []string{"line 1", "line 2", "line 3"}}}),
    57  		Width: 10, Height: 3,
    58  		Want: bb(10).
    59  			Write("line 1").Newline().
    60  			Write("line 2").Newline().
    61  			Write("line 3").Buffer(),
    62  	},
    63  }
    64  
    65  func TestTextView_Render(t *testing.T) {
    66  	TestRender(t, textViewRenderTests)
    67  }
    68  
    69  var textViewHandleTests = []HandleTest{
    70  	{
    71  		Name: "up doing nothing when not scrollable",
    72  		Given: NewTextView(TextViewSpec{
    73  			State: TextViewState{Lines: []string{"1", "2", "3", "4"}, First: 1}}),
    74  		Event: term.K(ui.Up),
    75  
    76  		WantUnhandled: true,
    77  	},
    78  	{
    79  		Name: "up moving window up when scrollable",
    80  		Given: NewTextView(TextViewSpec{
    81  			Scrollable: true,
    82  			State:      TextViewState{Lines: []string{"1", "2", "3", "4"}, First: 1}}),
    83  		Event: term.K(ui.Up),
    84  
    85  		WantNewState: TextViewState{Lines: []string{"1", "2", "3", "4"}, First: 0},
    86  	},
    87  	{
    88  		Name: "up doing nothing when already at top",
    89  		Given: NewTextView(TextViewSpec{
    90  			Scrollable: true,
    91  			State:      TextViewState{Lines: []string{"1", "2", "3", "4"}, First: 0}}),
    92  		Event: term.K(ui.Up),
    93  
    94  		WantNewState: TextViewState{Lines: []string{"1", "2", "3", "4"}, First: 0},
    95  	},
    96  	{
    97  		Name: "down moving window down when scrollable",
    98  		Given: NewTextView(TextViewSpec{
    99  			Scrollable: true,
   100  			State:      TextViewState{Lines: []string{"1", "2", "3", "4"}, First: 1}}),
   101  		Event: term.K(ui.Down),
   102  
   103  		WantNewState: TextViewState{Lines: []string{"1", "2", "3", "4"}, First: 2},
   104  	},
   105  	{
   106  		Name: "down doing nothing when already at bottom",
   107  		Given: NewTextView(TextViewSpec{
   108  			Scrollable: true,
   109  			State:      TextViewState{Lines: []string{"1", "2", "3", "4"}, First: 3}}),
   110  		Event: term.K(ui.Down),
   111  
   112  		WantNewState: TextViewState{Lines: []string{"1", "2", "3", "4"}, First: 3},
   113  	},
   114  	{
   115  		Name: "overlay",
   116  		Given: NewTextView(TextViewSpec{
   117  			OverlayHandler: MapHandler{term.K('a'): func() {}}}),
   118  		Event: term.K('a'),
   119  
   120  		WantNewState: TextViewState{},
   121  	},
   122  }
   123  
   124  func TestTextView_Handle(t *testing.T) {
   125  	TestHandle(t, textViewHandleTests)
   126  }
   127  
   128  func TestTextView_CopyState(t *testing.T) {
   129  	state := TextViewState{Lines: []string{"a", "b", "c"}, First: 1}
   130  	w := NewTextView(TextViewSpec{State: state})
   131  	copied := w.CopyState()
   132  	if !reflect.DeepEqual(copied, state) {
   133  		t.Errorf("Got copied state %v, want %v", copied, state)
   134  	}
   135  }