charmbracelet / lipgloss

Style definitions for nice terminal layouts ๐Ÿ‘„
MIT License
8.24k stars 231 forks source link

Initializing t.data with new content instead of leaving it as nil #329

Open zveinn opened 4 months ago

zveinn commented 4 months ago

leaving t.data as nil causes a panic if the table is re-rendered again without adding new rows

aymanbagabas commented 4 months ago

@zveinn Could you please add a test case?

zveinn commented 4 months ago

Not really, Iยดm too busy to write that for you :S sorry @aymanbagabas

siennathesane commented 2 months ago

Here's the test code to be added...

func TestEmptyTable(t *testing.T) {
    table := New().
        StyleFunc(TableStyle).
        Headers("Fruit", "Color", "Code")

    expected := strings.TrimSpace(`
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Fruit โ”‚ Color โ”‚ Code โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”˜
`)

    if stripString(table.String()) != expected {
        t.Fatalf("expected:\n\n%s\n\ngot:\n\n%s", expected, stripString(table.String()))
    }
}