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.

bech32demo_test.gno

1.80 Kb · 48 lines
 1package bech32demo
 2
 3import (
 4	"strings"
 5	"testing"
 6
 7	"gno.land/p/nt/uassert/v0"
 8)
 9
10// The argument path decodes a string of the caller's choosing.
11func TestRenderArgument(t *testing.T) {
12	got := Render("/a1lqfn3a")
13	uassert.True(t, strings.Contains(got, "| variant | bech32m |"), got)
14	uassert.True(t, strings.Contains(got, "| payload | 0 bytes |"))
15	// The full demo's sections are not rendered for an argument.
16	uassert.False(t, strings.Contains(got, "## Why Bech32m exists"))
17}
18
19// A string that does not decode says why, instead of rendering half a table.
20func TestRenderRejects(t *testing.T) {
21	got := Render("/g1manfred47kzduec920z88wfr64ylksmdcedlf4")
22	uassert.True(t, strings.Contains(got, "bech32: bad checksum"), got)
23	uassert.False(t, strings.Contains(got, "| field | value |"))
24}
25
26// A caller controls the whole argument, so anything echoed back is escaped.
27// Without it, "|" opens a column and "[x](y)" renders as a link.
28func TestRenderEscapesArgument(t *testing.T) {
29	got := Render("/*boom*")
30	uassert.False(t, strings.Contains(got, "Decoding *boom*"))
31	uassert.True(t, strings.Contains(got, "Decoding \\*boom\\*"), got)
32}
33
34// A slash is legal in a human-readable part, so the argument is taken whole
35// rather than cut at the first one.
36func TestRenderSlashInHRP(t *testing.T) {
37	got := Render("/a/b1mqd7sj")
38	uassert.True(t, strings.Contains(got, "human-readable part | a/b |"), got)
39}
40
41// Nothing is trimmed. A trailing non-breaking space is part of the string the
42// caller submitted, and the library refuses it: trimming it first would render
43// a clean decode of a string nobody typed.
44func TestRenderDoesNotTrim(t *testing.T) {
45	got := Render("/g1manfred47kzduec920z88wfr64ylksmdcedlf5 ")
46	uassert.True(t, strings.Contains(got, "bech32: invalid data character"), got)
47	uassert.False(t, strings.Contains(got, "20 bytes"))
48}