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