Skip to content Search Apps Documentation Source Content File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

printfdebugging_test.gno

2.77 Kb · 120 lines
  1package printfdebugging
  2
  3import (
  4	"testing"
  5
  6	"gno.land/p/nt/uassert/v0"
  7)
  8
  9// The SGR codes are transcribed from fatih/color. They are plain `iota` blocks,
 10// so a single inserted or removed line silently shifts every constant below it
 11// and the terminal renders the wrong colour. Pin the boundaries and the offsets.
 12
 13func TestBaseAttributes(t *testing.T) {
 14	cases := []struct {
 15		name string
 16		got  Attribute
 17		want int
 18	}{
 19		{"Reset", Reset, 0},
 20		{"Bold", Bold, 1},
 21		{"Faint", Faint, 2},
 22		{"Italic", Italic, 3},
 23		{"Underline", Underline, 4},
 24		{"BlinkSlow", BlinkSlow, 5},
 25		{"BlinkRapid", BlinkRapid, 6},
 26		{"ReverseVideo", ReverseVideo, 7},
 27		{"Concealed", Concealed, 8},
 28		{"CrossedOut", CrossedOut, 9},
 29	}
 30	for _, c := range cases {
 31		uassert.Equal(t, c.want, int(c.got), c.name)
 32	}
 33}
 34
 35// TestResetAttributes covers the block with a deliberate `_` gap at 26.
 36func TestResetAttributes(t *testing.T) {
 37	cases := []struct {
 38		name string
 39		got  Attribute
 40		want int
 41	}{
 42		{"ResetBold", ResetBold, 22},
 43		{"ResetItalic", ResetItalic, 23},
 44		{"ResetUnderline", ResetUnderline, 24},
 45		{"ResetBlinking", ResetBlinking, 25},
 46		// 26 is skipped with `_`
 47		{"ResetReversed", ResetReversed, 27},
 48		{"ResetConcealed", ResetConcealed, 28},
 49		{"ResetCrossedOut", ResetCrossedOut, 29},
 50	}
 51	for _, c := range cases {
 52		uassert.Equal(t, c.want, int(c.got), c.name)
 53	}
 54}
 55
 56func TestForegroundColors(t *testing.T) {
 57	cases := []struct {
 58		name string
 59		got  Attribute
 60		want int
 61	}{
 62		{"FgBlack", FgBlack, 30},
 63		{"FgRed", FgRed, 31},
 64		{"FgGreen", FgGreen, 32},
 65		{"FgYellow", FgYellow, 33},
 66		{"FgBlue", FgBlue, 34},
 67		{"FgMagenta", FgMagenta, 35},
 68		{"FgCyan", FgCyan, 36},
 69		{"FgWhite", FgWhite, 37},
 70	}
 71	for _, c := range cases {
 72		uassert.Equal(t, c.want, int(c.got), c.name)
 73	}
 74}
 75
 76func TestBackgroundColors(t *testing.T) {
 77	cases := []struct {
 78		name string
 79		got  Attribute
 80		want int
 81	}{
 82		{"BgBlack", BgBlack, 40},
 83		{"BgRed", BgRed, 41},
 84		{"BgWhite", BgWhite, 47},
 85	}
 86	for _, c := range cases {
 87		uassert.Equal(t, c.want, int(c.got), c.name)
 88	}
 89}
 90
 91func TestHiIntensityColors(t *testing.T) {
 92	cases := []struct {
 93		name string
 94		got  Attribute
 95		want int
 96	}{
 97		{"FgHiBlack", FgHiBlack, 90},
 98		{"FgHiWhite", FgHiWhite, 97},
 99		{"BgHiBlack", BgHiBlack, 100},
100		{"BgHiWhite", BgHiWhite, 107},
101	}
102	for _, c := range cases {
103		uassert.Equal(t, c.want, int(c.got), c.name)
104	}
105}
106
107func TestEscape(t *testing.T) {
108	uassert.Equal(t, "\x1b", Escape)
109	uassert.Equal(t, 1, len(Escape))
110}
111
112// TestOutputHelpersDoNotPanic exercises the two exported helpers. They only
113// println, so there is no return value to assert — this pins that they build
114// and run (BigRedLine formats Escape/Attribute through ufmt).
115func TestOutputHelpersDoNotPanic(t *testing.T) {
116	BigRedLine()
117	BigRedLine("one")
118	BigRedLine("several", "args", "joined")
119	Success()
120}