github.com/elves/elvish@v0.15.0/pkg/cli/layout_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 layoutRenderTests = []struct {
    12  	name     string
    13  	renderer Renderer
    14  	width    int
    15  	height   int
    16  	wantBuf  *term.BufferBuilder
    17  }{
    18  	{
    19  		"empty widget",
    20  		Empty{},
    21  		10, 24,
    22  		bb(10),
    23  	},
    24  	{
    25  		"Label showing all",
    26  		Label{ui.T("label")},
    27  		10, 24,
    28  		bb(10).Write("label"),
    29  	},
    30  	{
    31  		"Label cropping",
    32  		Label{ui.T("label")},
    33  		4, 1,
    34  		bb(4).Write("labe"),
    35  	},
    36  	{
    37  		"VScrollbar showing full thumb",
    38  		VScrollbar{4, 0, 3},
    39  		10, 2,
    40  		bb(1).WriteStyled(vscrollbarThumb).WriteStyled(vscrollbarThumb),
    41  	},
    42  	{
    43  		"VScrollbar showing thumb in first half",
    44  		VScrollbar{4, 0, 1},
    45  		10, 2,
    46  		bb(1).WriteStyled(vscrollbarThumb).WriteStyled(vscrollbarTrough),
    47  	},
    48  	{
    49  		"VScrollbar showing a minimal 1-size thumb at beginning",
    50  		VScrollbar{4, 0, 0},
    51  		10, 2,
    52  		bb(1).WriteStyled(vscrollbarThumb).WriteStyled(vscrollbarTrough),
    53  	},
    54  	{
    55  		"VScrollbar showing a minimal 1-size thumb at end",
    56  		VScrollbar{4, 3, 3},
    57  		10, 2,
    58  		bb(1).WriteStyled(vscrollbarTrough).WriteStyled(vscrollbarThumb),
    59  	},
    60  	{
    61  		"VScrollbarContainer",
    62  		VScrollbarContainer{Label{ui.T("abcd1234")},
    63  			VScrollbar{4, 0, 1}},
    64  		5, 2,
    65  		bb(5).Write("abcd").WriteStyled(vscrollbarThumb).
    66  			Newline().Write("1234").WriteStyled(vscrollbarTrough),
    67  	},
    68  	{
    69  		"HScrollbar showing full thumb",
    70  		HScrollbar{4, 0, 3},
    71  		2, 10,
    72  		bb(2).WriteStyled(hscrollbarThumb).WriteStyled(hscrollbarThumb),
    73  	},
    74  	{
    75  		"HScrollbar showing thumb in first half",
    76  		HScrollbar{4, 0, 1},
    77  		2, 10,
    78  		bb(2).WriteStyled(hscrollbarThumb).WriteStyled(hscrollbarTrough),
    79  	},
    80  	{
    81  		"HScrollbar showing a minimal 1-size thumb at beginning",
    82  		HScrollbar{4, 0, 0},
    83  		2, 10,
    84  		bb(2).WriteStyled(hscrollbarThumb).WriteStyled(hscrollbarTrough),
    85  	},
    86  	{
    87  		"HScrollbar showing a minimal 1-size thumb at end",
    88  		HScrollbar{4, 3, 3},
    89  		2, 10,
    90  		bb(2).WriteStyled(hscrollbarTrough).WriteStyled(hscrollbarThumb),
    91  	},
    92  }
    93  
    94  func TestLayout_Render(t *testing.T) {
    95  	for _, test := range layoutRenderTests {
    96  		t.Run(test.name, func(t *testing.T) {
    97  			buf := test.renderer.Render(test.width, test.height)
    98  			wantBuf := test.wantBuf.Buffer()
    99  			if !reflect.DeepEqual(buf, wantBuf) {
   100  				t.Errorf("got buf %v, want %v", buf, wantBuf)
   101  			}
   102  		})
   103  	}
   104  }
   105  
   106  var nopHandlers = []Handler{
   107  	Empty{}, Label{ui.T("label")},
   108  }
   109  
   110  func TestLayout_Handle(t *testing.T) {
   111  	for _, handler := range nopHandlers {
   112  		handled := handler.Handle(term.K('a'))
   113  		if handled {
   114  			t.Errorf("%v handles event when it shouldn't", handler)
   115  		}
   116  	}
   117  }