diff --git a/box_test.go b/box_test.go new file mode 100644 index 0000000..625ee00 --- /dev/null +++ b/box_test.go @@ -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") + } + }) + } +} diff --git a/screen/box.AlignCenter b/screen/box.AlignCenter new file mode 100644 index 0000000..8ff3422 --- /dev/null +++ b/screen/box.AlignCenter @@ -0,0 +1,5 @@ +╔═Center═╗ +║ ║ +║ ║ +║ ║ +╚════════╝ diff --git a/screen/box.AlignLeft b/screen/box.AlignLeft new file mode 100644 index 0000000..68f63b5 --- /dev/null +++ b/screen/box.AlignLeft @@ -0,0 +1,5 @@ +╔Left════╗ +║ ║ +║ ║ +║ ║ +╚════════╝ diff --git a/screen/box.AlignRight b/screen/box.AlignRight new file mode 100644 index 0000000..4f59530 --- /dev/null +++ b/screen/box.AlignRight @@ -0,0 +1,5 @@ +╔═══Right╗ +║ ║ +║ ║ +║ ║ +╚════════╝ diff --git a/screen/box.Bold.Border b/screen/box.Bold.Border new file mode 100644 index 0000000..404ff50 --- /dev/null +++ b/screen/box.Bold.Border @@ -0,0 +1,5 @@ +╔═Hello══╗ +║ ║ +║ ║ +║ ║ +╚════════╝ diff --git a/screen/box.Simple b/screen/box.Simple new file mode 100644 index 0000000..612ee5a --- /dev/null +++ b/screen/box.Simple @@ -0,0 +1,5 @@ + + + + +