2022-11-05 11:52:56 -07:00
|
|
|
package termui
|
|
|
|
|
|
|
|
import (
|
2022-11-08 06:09:14 -08:00
|
|
|
"fmt"
|
2022-11-07 08:54:53 -08:00
|
|
|
"strings"
|
2022-11-05 11:52:56 -07:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2022-11-08 06:09:14 -08:00
|
|
|
func TestBreakByStylesComplex(t *testing.T) {
|
|
|
|
items := BreakByStyles("test [blue](fg:blue,mod:bold) and [red](fg:red) and maybe even [foo](bg:red)!")
|
|
|
|
// "test [blue](fg:blue,mod:bold) and [red](fg:red) and maybe even [foo](bg:red)!"
|
|
|
|
// 01234567890123456789012345678
|
|
|
|
// 0- 4 normal
|
|
|
|
// 5-28 style
|
|
|
|
// 29-
|
2022-11-07 08:54:53 -08:00
|
|
|
if len(items) != 10 {
|
2022-11-07 08:26:51 -08:00
|
|
|
t.Fatal("wrong length", len(items))
|
|
|
|
}
|
2022-11-07 08:54:53 -08:00
|
|
|
text := strings.Join(items, " ")
|
|
|
|
if text != "test blue fg:blue,bg:white,mod:bold and red fg:red and maybe even foo bg:red !" {
|
|
|
|
t.Fatal("wrong text", text)
|
2022-11-07 04:21:40 -08:00
|
|
|
}
|
|
|
|
}
|
2022-11-08 06:09:14 -08:00
|
|
|
func TestBreakByStylesSimpler(t *testing.T) {
|
|
|
|
items := BreakByStyles("[blue](fg:blue) [1]")
|
|
|
|
// [blue](fg:blue) [1]
|
|
|
|
// 012345678901234
|
|
|
|
// 0-14, rest
|
|
|
|
fmt.Println(items, len(items))
|
|
|
|
}
|
2022-11-07 04:21:40 -08:00
|
|
|
|
2022-11-05 11:52:56 -07:00
|
|
|
func TestParseStyles(t *testing.T) {
|
2022-11-07 03:46:07 -08:00
|
|
|
cells := ParseStyles("test nothing", NewStyle(ColorWhite))
|
|
|
|
cells = ParseStyles("test [blue](fg:blue,bg:white,mod:bold) and [red](fg:red)", NewStyle(ColorWhite))
|
|
|
|
if len(cells) != 17 {
|
2022-11-07 03:00:20 -08:00
|
|
|
t.Fatal("wrong length", len(cells))
|
|
|
|
}
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
if cells[i].Style.Fg != ColorWhite {
|
|
|
|
t.Fatal("wrong fg color", cells[i])
|
|
|
|
}
|
|
|
|
if cells[i].Style.Bg != ColorClear {
|
|
|
|
t.Fatal("wrong bg color", cells[i])
|
|
|
|
}
|
|
|
|
if cells[i].Style.Modifier != ModifierClear {
|
|
|
|
t.Fatal("wrong mod", cells[i])
|
|
|
|
}
|
|
|
|
}
|
2022-11-07 03:46:07 -08:00
|
|
|
for i := 5; i < 9; i++ {
|
2022-11-07 03:00:20 -08:00
|
|
|
if cells[i].Style.Fg != ColorBlue {
|
|
|
|
t.Fatal("wrong fg color", cells[i])
|
|
|
|
}
|
|
|
|
if cells[i].Style.Bg != ColorWhite {
|
|
|
|
t.Fatal("wrong bg color", cells[i])
|
|
|
|
}
|
|
|
|
if cells[i].Style.Modifier != ModifierBold {
|
|
|
|
t.Fatal("wrong mod", cells[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-07 02:43:40 -08:00
|
|
|
text := textFromCells(cells)
|
2022-11-07 03:46:07 -08:00
|
|
|
if text != "test blue and red" {
|
2022-11-07 02:43:40 -08:00
|
|
|
t.Fatal("wrong text", text)
|
2022-11-05 11:52:56 -07:00
|
|
|
}
|
2022-11-07 02:43:40 -08:00
|
|
|
|
2022-11-07 02:44:37 -08:00
|
|
|
cells = ParseStyles("[blue](fg:blue) [1]", NewStyle(ColorWhite))
|
2022-11-07 02:43:40 -08:00
|
|
|
text = textFromCells(cells)
|
2022-11-07 02:44:37 -08:00
|
|
|
if text != "blue [1]" {
|
2022-11-07 02:43:40 -08:00
|
|
|
t.Fatal("wrong text", text)
|
|
|
|
}
|
|
|
|
|
|
|
|
cells = ParseStyles("[0]", NewStyle(ColorWhite))
|
|
|
|
text = textFromCells(cells)
|
|
|
|
if text != "[0]" {
|
|
|
|
t.Fatal("wrong text", text)
|
2022-11-05 11:52:56 -07:00
|
|
|
}
|
|
|
|
|
2022-11-07 02:43:40 -08:00
|
|
|
cells = ParseStyles("[", NewStyle(ColorWhite))
|
|
|
|
text = textFromCells(cells)
|
|
|
|
if text != "[" {
|
|
|
|
t.Fatal("wrong text", text)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func textFromCells(cells []Cell) string {
|
|
|
|
buff := []string{}
|
|
|
|
for _, cell := range cells {
|
|
|
|
buff = append(buff, string(cell.Rune))
|
2022-11-05 11:56:04 -07:00
|
|
|
}
|
2022-11-07 02:43:40 -08:00
|
|
|
return strings.Join(buff, "")
|
2022-11-07 08:54:53 -08:00
|
|
|
}
|