hwylterm/tests/tbbansi.nim

83 lines
2.7 KiB
Nim
Raw Normal View History

2024-09-17 17:28:21 -05:00
import std/[strutils,unittest]
import hwylterm/bbansi
template bbCheck(input: string, output: string): untyped =
check escape($bb(input)) == escape(output)
suite "basic":
test "simple":
2024-09-17 18:54:25 -05:00
bbCheck "[red][/red]", ""
bbCheck "[red]red text", "\e[38;5;1mred text\e[0m"
bbCheck "[red]Red Text", "\e[38;5;1mRed Text\e[0m"
bbCheck "[yellow]Yellow Text", "\e[38;5;3mYellow Text\e[0m"
bbCheck "[bold red]Bold Red Text", "\e[1;38;5;1mBold Red Text\e[0m"
bbCheck "[red]5[/]", "\e[38;5;1m5\e[0m"
bbCheck "[bold][red]5","\e[1;38;5;1m5\e[0m"
2024-09-17 17:28:21 -05:00
test "closing":
bbCheck "[bold]Bold[red] Bold Red[/red] Bold Only",
"\e[1mBold\e[0m\e[1;38;5;1m Bold Red\e[0m\e[1m Bold Only\e[0m"
2024-09-17 17:28:21 -05:00
test "abbreviated":
bbCheck "[b]Bold[/] Not Bold", "\e[1mBold\e[0m Not Bold"
test "noop":
bbCheck "No Style", "No Style"
bbCheck "[unknown]Unknown Style", "Unknown Style"
test "escaped":
bbCheck "[[red] ignored pattern", "[red] ignored pattern"
2024-09-24 11:53:44 -05:00
bbCheck "\\[red] ignored pattern","[red] ignored pattern"
2024-09-17 17:28:21 -05:00
test "newlines":
bbCheck "[red]Red Text[/]\nNext Line", "\e[38;5;1mRed Text\e[0m\nNext Line"
2024-09-17 17:28:21 -05:00
test "on color":
bbCheck "[red on yellow]Red on Yellow", "\e[38;5;1;48;5;3mRed on Yellow\e[0m"
2024-09-17 17:28:21 -05:00
test "concat-ops":
check "[red]RED[/]".bb & " plain string" == "[red]RED[/] plain string".bb
check "[red]RED[/]".bb.len == 3
check bb("[blue]Blue[/]") & " " & bb("[red]Red[/]") ==
"[blue]Blue[/] [red]Red[/]".bb
check "a plain string" & "[blue] a blue string".bb ==
"a plain string[blue] a blue string".bb
test "style insensitive":
bbCheck "[red]no case sensitivity[/RED]", "\e[38;5;1mno case sensitivity\e[0m"
bbCheck "[bright_red]should be BrightRed[/]", "\e[38;5;9mshould be BrightRed\e[0m"
2024-09-17 17:28:21 -05:00
test "style full":
check "[red]Red[/red]".bb == bb("Red", "red")
check "[b][yellow]not yellow[/][/b]".bb == bb("[yellow]not yellow[/]", "b")
2024-09-25 13:25:09 -05:00
check "[9]color 9[/9]".bb == bb("color 9", 9) # syntax will change to [color(9)]
2024-09-17 17:28:21 -05:00
2024-09-17 18:54:25 -05:00
test "escape":
check bbEscape("[info] brackets") == "[[info] brackets"
bbCheck bbEscape("[info] brackets"), "[info] brackets"
test "fmt":
let x = 5
check $bbfmt"[red]{x}" == "\e[38;5;1m5\e[0m"
test "hex":
bbCheck "[#FF0000]red", "\e[38;2;255;0;0mred\e[0m"
suite "strutils":
test "&":
check "plain string" & bb"[red]red string" == bb"plain string[red]red string"
test "truncate":
let tester = bb"[red]a red[/] [blue on red]blue on red part"
check tester.truncate(50) == tester
check tester.truncate(5) == bb"[red]a red"
check tester.truncate(10) == bb"[red]a red[/] [blue on red]blue"
test "align":
check (bb"[red]red").align(10) == bb" [red]red"
check (bb"[red]red").alignLeft(10) == bb"[red]red[/] "