okami.mom/ultraviolet
git clone https://git.nkp.pet/okami.mom/ultraviolet
2ac17c4
main
1package uv 2 3import ( 4"testing" 5) 6 7func TestBorderConstructors (t * testing.T ) { 8t .Run ("NormalBorder" ,func (t * testing.T ) { 9b := NormalBorder () 10if b .Top .Content != "─" || b .Bottom .Content != "─" || b .Left .Content != "│" || b .Right .Content != "│" { 11t .Fatalf ("unexpected sides for NormalBorder" ) 12 } 13if b .TopLeft .Content != "┌" || b .TopRight .Content != "┐" || b .BottomLeft .Content != "└" || b .BottomRight .Content != "┘" { 14t .Fatalf ("unexpected corners for NormalBorder" ) 15 } 16 }) 17 18t .Run ("RoundedBorder" ,func (t * testing.T ) { 19b := RoundedBorder () 20if b .Top .Content != "─" || b .Bottom .Content != "─" || b .Left .Content != "│" || b .Right .Content != "│" { 21t .Fatalf ("unexpected sides for RoundedBorder" ) 22 } 23if b .TopLeft .Content != "╭" || b .TopRight .Content != "╮" || b .BottomLeft .Content != "╰" || b .BottomRight .Content != "╯" { 24t .Fatalf ("unexpected corners for RoundedBorder" ) 25 } 26 }) 27 28t .Run ("BlockBorder" ,func (t * testing.T ) { 29b := BlockBorder () 30if b .Top .Content != "█" || b .Bottom .Content != "█" || b .Left .Content != "█" || b .Right .Content != "█" { 31t .Fatalf ("unexpected sides for BlockBorder" ) 32 } 33if b .TopLeft .Content != "█" || b .TopRight .Content != "█" || b .BottomLeft .Content != "█" || b .BottomRight .Content != "█" { 34t .Fatalf ("unexpected corners for BlockBorder" ) 35 } 36 }) 37 38t .Run ("OuterHalfBlockBorder" ,func (t * testing.T ) { 39b := OuterHalfBlockBorder () 40if b .Top .Content != "▀" || b .Bottom .Content != "▄" || b .Left .Content != "▌" || b .Right .Content != "▐" { 41t .Fatalf ("unexpected sides for OuterHalfBlockBorder" ) 42 } 43if b .TopLeft .Content != "▛" || b .TopRight .Content != "▜" || b .BottomLeft .Content != "▙" || b .BottomRight .Content != "▟" { 44t .Fatalf ("unexpected corners for OuterHalfBlockBorder" ) 45 } 46 }) 47 48t .Run ("InnerHalfBlockBorder" ,func (t * testing.T ) { 49b := InnerHalfBlockBorder () 50if b .Top .Content != "▄" || b .Bottom .Content != "▀" || b .Left .Content != "▐" || b .Right .Content != "▌" { 51t .Fatalf ("unexpected sides for InnerHalfBlockBorder" ) 52 } 53if b .TopLeft .Content != "▗" || b .TopRight .Content != "▖" || b .BottomLeft .Content != "▝" || b .BottomRight .Content != "▘" { 54t .Fatalf ("unexpected corners for InnerHalfBlockBorder" ) 55 } 56 }) 57 58t .Run ("ThickBorder" ,func (t * testing.T ) { 59b := ThickBorder () 60if b .Top .Content != "━" || b .Bottom .Content != "━" || b .Left .Content != "┃" || b .Right .Content != "┃" { 61t .Fatalf ("unexpected sides for ThickBorder" ) 62 } 63if b .TopLeft .Content != "┏" || b .TopRight .Content != "┓" || b .BottomLeft .Content != "┗" || b .BottomRight .Content != "┛" { 64t .Fatalf ("unexpected corners for ThickBorder" ) 65 } 66 }) 67 68t .Run ("DoubleBorder" ,func (t * testing.T ) { 69b := DoubleBorder () 70if b .Top .Content != "═" || b .Bottom .Content != "═" || b .Left .Content != "║" || b .Right .Content != "║" { 71t .Fatalf ("unexpected sides for DoubleBorder" ) 72 } 73if b .TopLeft .Content != "╔" || b .TopRight .Content != "╗" || b .BottomLeft .Content != "╚" || b .BottomRight .Content != "╝" { 74t .Fatalf ("unexpected corners for DoubleBorder" ) 75 } 76 }) 77 78t .Run ("HiddenBorder" ,func (t * testing.T ) { 79b := HiddenBorder () 80if b .Top .Content != " " || b .Bottom .Content != " " || b .Left .Content != " " || b .Right .Content != " " { 81t .Fatalf ("unexpected sides for HiddenBorder" ) 82 } 83if b .TopLeft .Content != " " || b .TopRight .Content != " " || b .BottomLeft .Content != " " || b .BottomRight .Content != " " { 84t .Fatalf ("unexpected corners for HiddenBorder" ) 85 } 86 }) 87 88t .Run ("MarkdownBorder" ,func (t * testing.T ) { 89b := MarkdownBorder () 90if b .Left .Content != "|" || b .Right .Content != "|" { 91t .Fatalf ("unexpected sides for MarkdownBorder left/right" ) 92 } 93if b .TopLeft .Content != "|" || b .TopRight .Content != "|" || b .BottomLeft .Content != "|" || b .BottomRight .Content != "|" { 94t .Fatalf ("unexpected corners for MarkdownBorder" ) 95 } 96if b .Top .Content != "" || b .Bottom .Content != "" { 97t .Fatalf ("unexpected top/bottom content for MarkdownBorder" ) 98 } 99 }) 100 101t .Run ("ASCIIBorder" ,func (t * testing.T ) { 102b := ASCIIBorder () 103if b .Top .Content != "-" || b .Bottom .Content != "-" || b .Left .Content != "|" || b .Right .Content != "|" { 104t .Fatalf ("unexpected sides for ASCIIBorder" ) 105 } 106if b .TopLeft .Content != "+" || b .TopRight .Content != "+" || b .BottomLeft .Content != "+" || b .BottomRight .Content != "+" { 107t .Fatalf ("unexpected corners for ASCIIBorder" ) 108 } 109 }) 110} 111 112func TestBorderStyleAndLink (t * testing.T ) { 113base := NormalBorder () 114style := Style {Attrs :AttrBold } 115link := NewLink ("https://example.com" ,"id=1" ) 116 117b := base .Style (style ).Link (link ) 118 119if ! b .Top .Style .Equal (& style )|| ! b .Bottom .Style .Equal (& style )|| ! b .Left .Style .Equal (& style )|| ! b .Right .Style .Equal (& style ) { 120t .Fatalf ("style not applied to all sides" ) 121 } 122if ! b .TopLeft .Style .Equal (& style )|| ! b .TopRight .Style .Equal (& style )|| ! b .BottomLeft .Style .Equal (& style )|| ! b .BottomRight .Style .Equal (& style ) { 123t .Fatalf ("style not applied to all corners" ) 124 } 125if ! b .Top .Link .Equal (& link )|| ! b .Bottom .Link .Equal (& link )|| ! b .Left .Link .Equal (& link )|| ! b .Right .Link .Equal (& link ) { 126t .Fatalf ("link not applied to all sides" ) 127 } 128if ! b .TopLeft .Link .Equal (& link )|| ! b .TopRight .Link .Equal (& link )|| ! b .BottomLeft .Link .Equal (& link )|| ! b .BottomRight .Link .Equal (& link ) { 129t .Fatalf ("link not applied to all corners" ) 130 } 131 132if ! base .Top .Style .IsZero ()|| ! base .Top .Link .IsZero () { 133t .Fatalf ("base border modified by Style/Link" ) 134 } 135} 136 137func TestBorderDrawNormal (t * testing.T ) { 138dst := NewScreenBuffer (20 ,10 ) 139area := Rect (1 ,1 ,6 ,4 ) 140b := NormalBorder () 141b .Draw (& dst ,area ) 142 143if c := dst .CellAt (1 ,1 );c == nil || c .Content != "┌" { 144t .Fatalf ("expected top-left corner" ) 145 } 146if c := dst .CellAt (6 ,1 );c == nil || c .Content != "┐" { 147t .Fatalf ("expected top-right corner" ) 148 } 149if c := dst .CellAt (1 ,4 );c == nil || c .Content != "└" { 150t .Fatalf ("expected bottom-left corner" ) 151 } 152if c := dst .CellAt (6 ,4 );c == nil || c .Content != "┘" { 153t .Fatalf ("expected bottom-right corner" ) 154 } 155 156for x := 2 ;x <= 5 ;x ++ { 157if c := dst .CellAt (x ,1 );c == nil || c .Content != "─" { 158t .Fatalf ("expected top edge at x=%d" ,x ) 159 } 160if c := dst .CellAt (x ,4 );c == nil || c .Content != "─" { 161t .Fatalf ("expected bottom edge at x=%d" ,x ) 162 } 163 } 164for y := 2 ;y <= 3 ;y ++ { 165if c := dst .CellAt (1 ,y );c == nil || c .Content != "│" { 166t .Fatalf ("expected left edge at y=%d" ,y ) 167 } 168if c := dst .CellAt (6 ,y );c == nil || c .Content != "│" { 169t .Fatalf ("expected right edge at y=%d" ,y ) 170 } 171 } 172 173for y := 2 ;y <= 3 ;y ++ { 174for x := 2 ;x <= 5 ;x ++ { 175if c := dst .CellAt (x ,y );c == nil || c .Content != " " { 176t .Fatalf ("expected interior to be blank at %d,%d" ,x ,y ) 177 } 178 } 179 } 180} 181 182func TestBorderDrawHiddenStyleLink (t * testing.T ) { 183dst := NewScreenBuffer (10 ,6 ) 184area := Rect (2 ,2 ,5 ,3 ) 185style := Style {Attrs :AttrBold } 186link := NewLink ("https://example.com" ) 187b := HiddenBorder ().Style (style ).Link (link ) 188b .Draw (& dst ,area ) 189 190checkPos := []struct {x ,y int }{ 191 {2 ,2 }, {6 ,2 }, {2 ,4 }, {6 ,4 }, 192 } 193for x := 3 ;x <= 5 ;x ++ { 194checkPos = append (checkPos ,struct {x ,y int }{x ,2 }) 195checkPos = append (checkPos ,struct {x ,y int }{x ,4 }) 196 } 197for y := 3 ;y <= 3 ;y ++ { 198checkPos = append (checkPos ,struct {x ,y int }{2 ,y }) 199checkPos = append (checkPos ,struct {x ,y int }{6 ,y }) 200 } 201 202for _ ,p := range checkPos { 203c := dst .CellAt (p .x ,p .y ) 204if c == nil || c .Content != " " || ! c .Style .Equal (& style )|| ! c .Link .Equal (& link ) { 205t .Fatalf ("expected styled/link space at %d,%d" ,p .x ,p .y ) 206 } 207 } 208 209c := dst .CellAt (4 ,3 ) 210if c == nil || c .Content != " " || ! c .Style .IsZero ()|| ! c .Link .IsZero () { 211t .Fatalf ("interior should be default space without style/link" ) 212 } 213} 214 215func TestBorderDrawSmallAreas (t * testing.T ) { 216dst := NewScreenBuffer (3 ,3 ) 217b := NormalBorder () 218 219area1 := Rect (0 ,0 ,1 ,1 ) 220b .Draw (& dst ,area1 ) 221if c := dst .CellAt (0 ,0 );c == nil || c .Content != "┌" { 222t .Fatalf ("expected single-cell area to use top-left corner" ) 223 } 224 225area2 := Rect (0 ,1 ,1 ,2 ) 226b .Draw (& dst ,area2 ) 227if c := dst .CellAt (0 ,1 );c == nil || c .Content != "┌" { 228t .Fatalf ("expected top-left at 0,1" ) 229 } 230if c := dst .CellAt (0 ,2 );c == nil || c .Content != "└" { 231t .Fatalf ("expected bottom-left at 0,2" ) 232 } 233}