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

     1  package rty
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  
     9  	"github.com/gdamore/tcell"
    10  )
    11  
    12  func TestTextString(t *testing.T) {
    13  	i := NewInteractiveTester(t, screen)
    14  
    15  	i.Run("one-line text string", 20, 1, TextString("hello world"))
    16  	i.Run("two-line text string", 20, 2, TextString("hello\nworld"))
    17  	i.Run("two-line text string in one-line container", 20, 1, TextString("hello\nworld"))
    18  	i.Run("horizontally overflowed text string", 2, 1, TextString("hello world"))
    19  	i.Run("horizontally overflowed long text string", 20, 2, TextString(strings.Repeat("hello", 20)))
    20  	i.Run("vertically overflowed via newlines text string", 10, 10, TextString(strings.Repeat("hi\n", 20)))
    21  	i.Run("vertically overflowed via wrap text string", 5, 5, TextString(strings.Repeat("xxxxxxxxxx\n", 200)))
    22  }
    23  
    24  func TestStyledText(t *testing.T) {
    25  	i := NewInteractiveTester(t, screen)
    26  
    27  	i.Run("blue string", 20, 1, ColoredString("hello world", tcell.ColorBlue))
    28  	i.Run("black on white string", 20, 1, BgColoredString("hello world", tcell.ColorBlack, tcell.ColorWhite))
    29  	c := NewStringBuilder().Text("hello ").Fg(tcell.ColorBlue).Text("world").Build()
    30  	i.Run("multi-color string", 20, 1, c)
    31  }
    32  
    33  func TestLines(t *testing.T) {
    34  	i := NewInteractiveTester(t, screen)
    35  
    36  	fl := NewFlexLayout(DirHor)
    37  	for j := 0; j < 10; j++ {
    38  		fl.Add(TextString("x"))
    39  	}
    40  	i.Run("overflowed multi-string line", 3, 1, fl)
    41  
    42  	l := NewLines()
    43  	l.Add(NewStringBuilder().Text("hello").Build())
    44  	l.Add(NewStringBuilder().Text("hello").Text("goodbye").Build())
    45  	i.Run("lines of stringbuilders", 10, 10, l)
    46  
    47  	l = NewLines()
    48  	line := NewLine()
    49  	line.Add(TextString("hello"))
    50  	l.Add(line)
    51  	line = NewLine()
    52  	line.Add(TextString("hello"))
    53  	line.Add(TextString("goodbye"))
    54  	l.Add(line)
    55  	i.Run("lines of lines", 30, 10, l)
    56  
    57  	l = NewLines()
    58  	l.Add(TextString("the quick brown fox\njumped over the lazy dog"))
    59  	l.Add(TextString("here is another line"))
    60  	i.Run("wrapped line followed by another line", 10, 20, l)
    61  }
    62  
    63  func TestStringBuilder(t *testing.T) {
    64  	sb := NewStringBuilder()
    65  	sb.Text("hello")
    66  	w, h, err := sb.Build().Size(10, 1)
    67  	assert.NoError(t, err)
    68  	assert.Equal(t, 5, w)
    69  	assert.Equal(t, 1, h)
    70  
    71  	sb = NewStringBuilder()
    72  	sb.Text("hello world\ngoodbye")
    73  	w, h, err = sb.Build().Size(5, 10)
    74  	assert.NoError(t, err)
    75  	assert.Equal(t, 5, w)
    76  	assert.Equal(t, 6, h)
    77  
    78  	sb = NewStringBuilder()
    79  	sb.Text("hello world")
    80  	w, h, err = sb.Build().Size(3, 3)
    81  	assert.NoError(t, err)
    82  	assert.Equal(t, 3, w)
    83  	assert.Equal(t, 3, h)
    84  }
    85  
    86  func TestANSICodes(t *testing.T) {
    87  	i := NewInteractiveTester(t, screen)
    88  
    89  	sb := NewStringBuilder().Text("\x1b[31mhello \x1b[33mworld")
    90  	i.Run("red hello yellow world", 20, 1, sb.Build())
    91  
    92  	sb = NewStringBuilder().Text("\x1b[44mhello \x1bcworld")
    93  	i.Run("blue-bg hello, default-bg world", 20, 1, sb.Build())
    94  }