123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- package door
- import (
- "bytes"
- "fmt"
- "strings"
- "testing"
- )
- func TestPanel(t *testing.T) {
- var p Panel = Panel{X: 5, Y: 7, Width: 10, Style: DOUBLE, Title: "Test"}
- p.Lines = append(p.Lines, NewLine("1234567890"))
- p.Lines = append(p.Lines, NewLine("abcdefghij"))
- var expected string = string(Goto(5, 7)) + "╔Test══════╗" +
- string(Goto(5, 8)) + "║1234567890║" +
- string(Goto(5, 9)) + "║abcdefghij║" +
- string(Goto(5, 10)) + "╚══════════╝"
- var got string = string(p.Output())
- var es []string = strings.SplitAfter(expected, "5H")
- var gs []string = strings.SplitAfter(got, "5H")
- for idx, exp := range es {
- var g string = gs[idx]
- if g != exp {
- t.Errorf("Panel expected %#v, got %#v", exp, g)
- }
- }
- p.TitleOffset = 3
- got = string(p.Output())
- gs = strings.SplitAfter(got, "5H")
- es[0] = "╔═══Test═══╗" + string(Goto(5, 8))
- if gs[1] != es[0] {
- t.Errorf("Panel TitleOffset=3 expected %#v, got %#v", es[0], gs[1])
- }
- }
- func TestPanelSpacer(t *testing.T) {
- var p Panel = Panel{X: 1, Y: 1, Width: 5, Style: SINGLE}
- p.Lines = append(p.Lines, p.Spacer())
- var expected string = string(Goto(1, 1)) + "┌─────┐" +
- string(Goto(1, 2)) + "├─────┤" +
- string(Goto(1, 3)) + "└─────┘"
- var got string = string(p.Output())
- if expected != got {
- t.Errorf("Panel Spacer expected %#v, got %#v", expected, got)
- }
- }
- func TestPanelUpdate(t *testing.T) {
- var TestX int = 2
- var TestY int = 2
- var p Panel = Panel{X: TestX, Y: TestY, Width: 3, Style: DOUBLE_SINGLE}
- var x int = 0
- var updater Updater = func(u *bytes.Buffer) {
- u.Reset()
- fmt.Fprintf(u, "%3d", x)
- }
- var l *Line = &Line{UpdateF: updater}
- l.Update()
- p.Lines = append(p.Lines, l)
- var expected string = string(Goto(TestX, TestY)) + "╒═══╕" +
- string(Goto(TestX, TestY+1)) + "│ 0│" +
- string(Goto(TestX, TestY+2)) + "╘═══╛"
- var got string = string(p.Output())
- if expected != got {
- t.Errorf("Panel Update expected %#v, got %#v", expected, got)
- }
- x += 3
- got = string(p.Update())
- expected = string(Goto(TestX+1, TestY+1)) + " 3"
- if expected != got {
- t.Errorf("Panel Update expected %#v, got %#v", expected, got)
- }
- got = string(p.Update())
- if got != "" {
- t.Errorf("Panel Update expected '', got %#v", got)
- }
- got = string(p.UpdateLine(0))
- if expected != got {
- t.Errorf("Panel UpdateLine expected %#v, got %#v", expected, got)
- }
- // Expected: Width = 3, Length = 1
- if p.Width != 3 {
- t.Errorf("Panel Width, expected %d, got %d\n", 3, p.Width)
- }
- if p.Length() != 1 {
- t.Errorf("Panel Length, expected %d, got %d\n", 1, p.Length())
- }
- got = string(p.GotoEnd())
- expected = string(Goto(p.X+p.Width+2, p.Y+p.Length()+1))
- if got != expected {
- t.Errorf("Panel GotoEnd expected %#v, got %#v", expected, got)
- }
- }
- func BenchmarkPanel(b *testing.B) {
- Unicode = false
- var TestX int = 2
- var TestY int = 2
- var p Panel = Panel{X: TestX, Y: TestY, Width: 3, Style: DOUBLE_SINGLE}
- var x int = 0
- var updater Updater = func(u *bytes.Buffer) {
- u.Reset()
- fmt.Fprintf(u, "%3d", x)
- }
- var l *Line = &Line{UpdateF: updater}
- l.Update()
- p.Lines = append(p.Lines, l)
- p.Lines = append(p.Lines, l)
- p.Lines = append(p.Lines, l)
- for n := 0; n < b.N; n++ {
- x = n
- p.Update()
- }
- }
- func BenchmarkPanelRender(b *testing.B) {
- Unicode = false
- var TestX int = 2
- var TestY int = 2
- var p Panel = Panel{X: TestX, Y: TestY, Width: 8, Style: DOUBLE_SINGLE}
- var x int = 0
- var updater Updater = func(u *bytes.Buffer) {
- u.Reset()
- // Give this text that it will have to do something with (MiXeD CaSe)
- if x%2 == 0 {
- fmt.Fprintf(u, "TeSt %3d", x)
- } else {
- fmt.Fprintf(u, "tEsT %3d", x)
- }
- }
- var l *Line = &Line{UpdateF: updater, RenderF: RenderUppercase("BRI WHI ON BLUE", "GREEN")}
- l.Update()
- p.Lines = append(p.Lines, l)
- p.Lines = append(p.Lines, l)
- p.Lines = append(p.Lines, l)
- for n := 0; n < b.N; n++ {
- x = n
- p.Update()
- }
- }
|