library/ui/ui_test.go

242 lines
6.0 KiB
Go
Raw Normal View History

package ui
import (
"fmt"
"testing"
2023-04-08 02:42:54 +00:00
"github.com/gdamore/tcell/v2"
)
func TestContainerOneBox(t *testing.T) {
expect := ` box one
`
m := &MockScreen{}
2023-04-08 02:42:54 +00:00
one := NewBox("box one", nil, Contents{}, tcell.Style{}, false)
container := NewContainer(
Contents{{Container: one}},
LayoutHorizontalEven,
)
m.Init()
m.Resize(0, 0, 5, 20)
container.SetSize(0, 0, 5, 20)
container.Draw(m)
result := m.DumpContents()
if result != expect {
fmt.Printf("expected:\n%+v", expect)
fmt.Printf("actual:\n%+v", result)
t.Fail()
}
}
func TestContainerTwoBoxesHStack(t *testing.T) {
expect := ` one two
`
m := &MockScreen{}
2023-04-08 02:42:54 +00:00
one := NewBox("one", nil, Contents{}, tcell.Style{}, false)
two := NewBox("two", nil, Contents{}, tcell.Style{}, false)
container := NewContainer(
Contents{{Container: one}, {Container: two}},
LayoutHorizontalEven,
)
m.Init()
m.Resize(0, 0, 5, 20)
container.SetSize(0, 0, 5, 20)
container.Draw(m)
result := m.DumpContents()
if result != expect {
fmt.Printf("expected:\n%+v", expect)
fmt.Printf("actual:\n%+v", result)
t.Fail()
}
}
2021-07-04 17:49:53 +00:00
2021-07-04 13:41:22 +00:00
func TestContainerThreeBoxesUnevenHStack(t *testing.T) {
expect := ` one two three
`
m := &MockScreen{}
2023-04-08 02:42:54 +00:00
one := NewBox("one", nil, Contents{}, tcell.Style{}, false)
two := NewBox("two", nil, Contents{}, tcell.Style{}, false)
three := NewBox("three", nil, Contents{}, tcell.Style{}, false)
2021-07-04 13:41:22 +00:00
container := NewContainer(
Contents{{Container: one}, {Container: two}, {Container: three}},
LayoutHorizontalEven,
)
m.Init()
m.Resize(0, 0, 5, 29)
container.SetSize(0, 0, 5, 29)
container.Draw(m)
result := m.DumpContents()
if result != expect {
fmt.Printf("expected:\n%+v", expect)
fmt.Printf("actual:\n%+v", result)
t.Fail()
}
}
2021-07-04 17:49:53 +00:00
func TestContainerTwoBoxesHPercentStack(t *testing.T) {
expect := ` one two
`
m := &MockScreen{}
2023-04-08 02:42:54 +00:00
one := NewBox("one", nil, Contents{}, tcell.Style{}, false)
two := NewBox("two", nil, Contents{}, tcell.Style{}, false)
2021-07-04 17:49:53 +00:00
container := NewContainer(
Contents{
{Container: one, Offsets: Offsets{Percent: 2}},
{Container: two, Offsets: Offsets{Percent: 1}}},
LayoutHorizontalPercent,
)
m.Init()
m.Resize(0, 0, 5, 22)
container.SetSize(0, 0, 5, 22)
container.Draw(m)
result := m.DumpContents()
if result != expect {
fmt.Printf("expected:\n%+v", expect)
fmt.Printf("actual:\n%+v", result)
t.Fail()
}
}
func TestContainerTwoBoxesVStack(t *testing.T) {
expect := ` one
two
`
m := &MockScreen{}
2023-04-08 02:42:54 +00:00
one := NewBox("one", nil, Contents{}, tcell.Style{}, false)
two := NewBox("two", nil, Contents{}, tcell.Style{}, false)
container := NewContainer(
Contents{{Container: one}, {Container: two}},
LayoutVerticalEven,
)
m.Init()
m.Resize(0, 0, 10, 10)
container.SetSize(0, 0, 10, 10)
container.Draw(m)
result := m.DumpContents()
if result != expect {
fmt.Printf("expected:\n%+v", expect)
fmt.Printf("actual:\n%+v", result)
t.Fail()
}
}
2021-07-04 17:49:53 +00:00
func TestContainerTwoBoxesPercentageVStack(t *testing.T) {
expect := ` one
two
`
m := &MockScreen{}
2023-04-08 02:42:54 +00:00
one := NewBox("one", nil, Contents{}, tcell.Style{}, false)
two := NewBox("two", nil, Contents{}, tcell.Style{}, false)
2021-07-04 17:49:53 +00:00
container := NewContainer(
Contents{
{Container: one, Offsets: Offsets{Percent: 2}},
{Container: two, Offsets: Offsets{Percent: 1}}},
LayoutVerticalPercent,
)
m.Init()
m.Resize(0, 0, 10, 10)
container.SetSize(0, 0, 10, 10)
container.Draw(m)
result := m.DumpContents()
if result != expect {
fmt.Printf("expected:\n%+v", expect)
fmt.Printf("actual:\n%+v", result)
t.Fail()
}
}
2021-08-01 15:41:02 +00:00
func TestNewEditableTextLine(t *testing.T) {
e := NewEditableTextLine("")
e.InsertAtCursor('a')
e.InsertAtCursor('b')
e.InsertAtCursor('c')
if e.text != "abc" {
fmt.Printf("expected: 'abc', actual: '%+v'", e.text)
t.Fail()
}
e.MoveCursor(-1)
e.InsertAtCursor('d')
if e.text != "abdc" {
fmt.Printf("expected: 'abdc', actual: '%+v'", e.text)
t.Fail()
}
e.MoveCursor(-20)
e.InsertAtCursor('e')
if e.text != "eabdc" {
fmt.Printf("expected: 'eabdc', actual: '%+v'", e.text)
t.Fail()
}
e.MoveCursor(20)
e.InsertAtCursor('f')
if e.text != "eabdcf" {
fmt.Printf("expected: 'eabdcf', actual: '%+v'", e.text)
t.Fail()
}
e.MoveCursor(1)
e.InsertAtCursor('g')
if e.text != "eabdcfg" {
fmt.Printf("expected: 'eabdcfg', actual: '%+v'", e.text)
t.Fail()
}
e.DeleteAtCursor()
e.DeleteAtCursor()
e.MoveCursor(-1)
e.DeleteAtCursor()
if e.text != "eabc" {
fmt.Printf("expected: 'eabc', actual: '%+v'", e.text)
t.Fail()
}
e.ResetCursor(false)
e.InsertAtCursor('h')
e.ResetCursor(true)
e.InsertAtCursor('g')
if e.text != "geabch" {
fmt.Printf("expected: 'geabch', actual: '%+v'", e.text)
t.Fail()
}
e.SetText("the rain in spain")
e.InsertAtCursor('s')
if e.text != "the rain in spains" {
fmt.Printf("expected: 'the rain in spains', actual: '%+v'", e.text)
t.Fail()
}
e.SetText("")
e.InsertAtCursor('s')
if e.text != "s" {
fmt.Printf("expected: 's', actual: '%+v'", e.text)
t.Fail()
}
}