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.

cowsay_test.gno

2.09 Kb · 75 lines
 1package cowsay
 2
 3// handrolled-table: the cow's horn line "||----w |" is ASCII art, not a GFM header
 4// separator. This package renders no table at all, and its production source is live
 5// on an immutable public path, so the opt-out has to live here.
 6
 7import (
 8	"strings"
 9	"testing"
10
11	"gno.land/p/nt/uassert/v0"
12)
13
14func TestWrap(t *testing.T) {
15	tests := []struct {
16		name  string
17		text  string
18		width int
19		want  []string
20	}{
21		{"empty", "", 10, []string{""}},
22		{"single", "hello", 10, []string{"hello"}},
23		{"wrap two", "aaa bbb ccc", 7, []string{"aaa bbb", "ccc"}},
24		{"hard split", "abcdefghij", 4, []string{"abcd", "efgh", "ij"}},
25	}
26	for _, tt := range tests {
27		got := wrap(tt.text, tt.width)
28		uassert.Equal(t, len(tt.want), len(got), tt.name+": line count")
29		for i := range tt.want {
30			uassert.Equal(t, tt.want[i], got[i], tt.name+": line "+tt.want[i])
31		}
32		// no wrapped line exceeds width
33		for _, ln := range got {
34			uassert.True(t, len(ln) <= tt.width, tt.name+": within width")
35		}
36	}
37}
38
39func TestBorderChars(t *testing.T) {
40	l, r := borderChars(0, 1)
41	uassert.Equal(t, "<", l)
42	uassert.Equal(t, ">", r)
43
44	l, r = borderChars(0, 3)
45	uassert.Equal(t, "/", l)
46	uassert.Equal(t, "\\", r)
47
48	l, r = borderChars(1, 3)
49	uassert.Equal(t, "|", l)
50	uassert.Equal(t, "|", r)
51
52	l, r = borderChars(2, 3)
53	uassert.Equal(t, "\\", l)
54	uassert.Equal(t, "/", r)
55}
56
57func TestMaxLen(t *testing.T) {
58	uassert.Equal(t, 0, maxLen([]string{""}))
59	uassert.Equal(t, 5, maxLen([]string{"ab", "cde", "fghij"}))
60}
61
62func TestSayDefault(t *testing.T) {
63	got := Say("")
64	uassert.True(t, strings.Contains(got, defaultMessage), "uses default message")
65	uassert.True(t, strings.Contains(got, "(oo)"), "contains cow face")
66	uassert.True(t, strings.Contains(got, "< Moo! >"), "single-line bubble")
67}
68
69func TestSayWrapsMultiline(t *testing.T) {
70	msg := "the quick brown fox jumps over the lazy dog many many times over"
71	got := Say(msg)
72	// multi-line bubbles use slashes, never the single-line angle brackets
73	uassert.True(t, strings.Contains(got, "/"), "multi-line top border char")
74	uassert.False(t, strings.Contains(got, "< "), "no single-line marker")
75}