github.com/grahambrereton-form3/tilt@v0.10.18/internal/rty/layouts_test.go (about)

     1  package rty
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/gdamore/tcell"
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestFlexLayoutOverflow(t *testing.T) {
    13  	sc := tcell.NewSimulationScreen("")
    14  	err := sc.Init()
    15  	assert.NoError(t, err)
    16  
    17  	r := NewRTY(sc)
    18  
    19  	f := NewFlexLayout(DirHor)
    20  	f.Add(TextString("hello"))
    21  	f.Add(TextString("world"))
    22  	err = r.Render(f)
    23  	assert.NoError(t, err)
    24  
    25  	i := NewInteractiveTester(t, screen)
    26  	i.Run("text overflow", 8, 1, f)
    27  }
    28  
    29  func TestStyles(t *testing.T) {
    30  	i := NewInteractiveTester(t, screen)
    31  
    32  	var c Component
    33  	c = NewGrowingBox()
    34  	c = Fg(c, tcell.ColorGreen)
    35  	c = Bg(c, tcell.ColorWhite)
    36  	i.Run("green on white box", 10, 10, c)
    37  
    38  	b := NewGrowingBox()
    39  	b.SetInner(TextString("hello world"))
    40  	c = Fg(b, tcell.ColorGreen)
    41  	c = Bg(c, tcell.ColorWhite)
    42  	i.Run("green on white box with text inside", 10, 10, c)
    43  
    44  	b = NewGrowingBox()
    45  	b.SetInner(BgColoredString("hello world", tcell.ColorBlue, tcell.ColorGreen))
    46  	c = Fg(b, tcell.ColorGreen)
    47  	c = Bg(c, tcell.ColorWhite)
    48  	i.Run("green on white box with blue on green text inside", 10, 10, c)
    49  
    50  	l := NewFlexLayout(DirHor)
    51  	l.Add(Bg(NewGrowingBox(), tcell.ColorBlue))
    52  	l.Add(Bg(NewGrowingBox(), tcell.ColorWhite))
    53  	l.Add(Bg(NewGrowingBox(), tcell.ColorRed))
    54  	i.Run("blue, white, red boxes horizontally", 30, 30, l)
    55  
    56  	l = NewFlexLayout(DirVert)
    57  	l.Add(Bg(NewGrowingBox(), tcell.ColorBlue))
    58  	l.Add(Bg(NewGrowingBox(), tcell.ColorWhite))
    59  	l.Add(Bg(NewGrowingBox(), tcell.ColorRed))
    60  	i.Run("blue, white, red boxes vertically", 30, 30, l)
    61  }
    62  
    63  func TestConcatLayout(t *testing.T) {
    64  	i := NewInteractiveTester(t, screen)
    65  
    66  	cl := NewConcatLayout(DirVert)
    67  	cl.Add(TextString("hello"))
    68  	cl.Add(TextString("goodbye"))
    69  	i.Run("two strings in ConcatLayout", 15, 15, cl)
    70  
    71  	cl = NewConcatLayout(DirHor)
    72  	cl.Add(TextString("HEADER"))
    73  	cl.AddDynamic(TextString(strings.Repeat("helllllo", 20)))
    74  	i.Run("wrapping on right of ConcatLayout", 20, 20, cl)
    75  }
    76  
    77  func TestAlignEnd(t *testing.T) {
    78  	i := NewInteractiveTester(t, screen)
    79  	l := NewMinLengthLayout(10, DirHor).
    80  		SetAlign(AlignEnd).
    81  		Add(TextString("hello"))
    82  	i.Run("align right on min-length layout", 15, 15, NewBox(l))
    83  }
    84  
    85  func TestNestedConcatLayoutOverflow(t *testing.T) {
    86  	sc := tcell.NewSimulationScreen("")
    87  	err := sc.Init()
    88  	assert.NoError(t, err)
    89  
    90  	r := NewRTY(sc)
    91  
    92  	f1 := NewConcatLayout(DirHor)
    93  	for i := 0; i < 10; i++ {
    94  		f1.Add(TextString("x"))
    95  		f1.AddDynamic(NewFillerString(' '))
    96  	}
    97  
    98  	f2 := NewConcatLayout(DirHor)
    99  	f1.Add(f2)
   100  	for i := 0; i < 10; i++ {
   101  		f2.Add(TextString("y"))
   102  		f2.AddDynamic(NewFillerString(' '))
   103  	}
   104  
   105  	err = r.Render(NewFixedSize(f1, 8, 1))
   106  	assert.NoError(t, err)
   107  
   108  	i := NewInteractiveTester(t, screen)
   109  	i.Run("concat text overflow", 8, 1, f1)
   110  }
   111  
   112  func TestMinWidthInNestedConcatLayoutOverflow(t *testing.T) {
   113  	sc := tcell.NewSimulationScreen("")
   114  	err := sc.Init()
   115  	assert.NoError(t, err)
   116  
   117  	r := NewRTY(sc)
   118  
   119  	f1 := NewConcatLayout(DirHor)
   120  	for i := 0; i < 10; i++ {
   121  		f1.Add(NewMinLengthLayout(3, DirHor).Add(TextString("x")))
   122  		f1.AddDynamic(NewFillerString(' '))
   123  	}
   124  
   125  	f2 := NewConcatLayout(DirHor)
   126  	f2.Add(f1)
   127  
   128  	err = r.Render(f2)
   129  	assert.NoError(t, err)
   130  
   131  	i := NewInteractiveTester(t, screen)
   132  	i.Run("min width concat text overflow", 8, 1, f2)
   133  }
   134  
   135  func TestTailLayout(t *testing.T) {
   136  	sc := tcell.NewSimulationScreen("")
   137  	err := sc.Init()
   138  	assert.NoError(t, err)
   139  
   140  	f := NewConcatLayout(DirVert)
   141  	for i := 0; i < 15; i++ {
   142  		f.Add(TextString(fmt.Sprintf("line %d text text", i)))
   143  	}
   144  
   145  	tail := NewBox(NewTailLayout(f))
   146  	i := NewInteractiveTester(t, screen)
   147  	i.Run("tail layout no overflow", 20, 20, tail)
   148  	i.Run("tail layout overflow-y", 20, 10, tail)
   149  	i.Run("tail layout overflow-x", 15, 40, tail)
   150  	i.Run("tail layout overflow-xy", 15, 10, tail)
   151  }
   152  
   153  func TestMaxLengthLayout(t *testing.T) {
   154  	sc := tcell.NewSimulationScreen("")
   155  	err := sc.Init()
   156  	assert.NoError(t, err)
   157  
   158  	f := NewConcatLayout(DirVert)
   159  	for i := 0; i < 15; i++ {
   160  		f.Add(TextString(fmt.Sprintf("line %d text text", i)))
   161  	}
   162  
   163  	box := NewBox(NewMaxLengthLayout(f, DirVert, 20))
   164  	i := NewInteractiveTester(t, screen)
   165  	i.Run("max layout no overflow", 20, 20, box)
   166  	i.Run("max layout overflow", 15, 40, box)
   167  }