mirror of
https://github.com/rivo/tview.git
synced 2025-05-13 19:29:17 +08:00
box: add test
This commit is contained in:
parent
8e06c826b3
commit
28f45a4914
94
box_test.go
Normal file
94
box_test.go
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
package tview
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/gdamore/tcell"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
snapshotPath string = "screen"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestBox(t *testing.T) {
|
||||||
|
tcs := []struct {
|
||||||
|
name string
|
||||||
|
box *Box
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "box.Simple",
|
||||||
|
box: NewBox(),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "box.Bold.Border",
|
||||||
|
box: NewBox().SetBorder(true).SetBorderAttributes(tcell.AttrBold).SetTitle("Hello"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "box.AlignLeft",
|
||||||
|
box: NewBox().SetBorder(true).SetTitle("Left").SetTitleAlign(AlignLeft),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "box.AlignRight",
|
||||||
|
box: NewBox().SetBorder(true).SetTitle("Right").SetTitleAlign(AlignRight),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "box.AlignCenter",
|
||||||
|
box: NewBox().SetBorder(true).SetTitle("Center").SetTitleAlign(AlignCenter),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := range tcs {
|
||||||
|
i := i
|
||||||
|
t.Run(tcs[i].name, func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
simScreen := tcell.NewSimulationScreen("UTF-8")
|
||||||
|
simScreen.Init()
|
||||||
|
simScreen.SetSize(10, 5)
|
||||||
|
|
||||||
|
app := NewApplication()
|
||||||
|
app.SetScreen(simScreen)
|
||||||
|
app.SetRoot(tcs[i].box, true)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
if err := app.Run(); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
time.Sleep(time.Second)
|
||||||
|
|
||||||
|
cells, width, height := simScreen.GetContents()
|
||||||
|
var buf bytes.Buffer
|
||||||
|
for row := 0; row < height; row++ {
|
||||||
|
for col := 0; col < width; col++ {
|
||||||
|
position := row*width + col
|
||||||
|
fmt.Fprintf(&buf, "%s", string(cells[position].Runes))
|
||||||
|
}
|
||||||
|
fmt.Fprintf(&buf, "\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
snapshotFilename := filepath.Join(".", snapshotPath, tcs[i].name)
|
||||||
|
|
||||||
|
if os.Getenv("UPDATE") == "true" {
|
||||||
|
if err := ioutil.WriteFile(snapshotFilename, buf.Bytes(), 0644); err != nil {
|
||||||
|
t.Fatalf("Cannot write snapshot to file: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
content, err := ioutil.ReadFile(snapshotFilename)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Cannot read snapshot file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !bytes.Equal(buf.Bytes(), content) {
|
||||||
|
t.Errorf("Snapshots is not same")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
5
screen/box.AlignCenter
Normal file
5
screen/box.AlignCenter
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
╔═Center═╗
|
||||||
|
║ ║
|
||||||
|
║ ║
|
||||||
|
║ ║
|
||||||
|
╚════════╝
|
5
screen/box.AlignLeft
Normal file
5
screen/box.AlignLeft
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
╔Left════╗
|
||||||
|
║ ║
|
||||||
|
║ ║
|
||||||
|
║ ║
|
||||||
|
╚════════╝
|
5
screen/box.AlignRight
Normal file
5
screen/box.AlignRight
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
╔═══Right╗
|
||||||
|
║ ║
|
||||||
|
║ ║
|
||||||
|
║ ║
|
||||||
|
╚════════╝
|
5
screen/box.Bold.Border
Normal file
5
screen/box.Bold.Border
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
╔═Hello══╗
|
||||||
|
║ ║
|
||||||
|
║ ║
|
||||||
|
║ ║
|
||||||
|
╚════════╝
|
5
screen/box.Simple
Normal file
5
screen/box.Simple
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user