gallery_test.gno
4.45 Kb · 113 lines
1package gallery
2
3import (
4 "strings"
5 "testing"
6
7 "gno.land/p/nt/uassert/v0"
8)
9
10func TestRenderIndexMarkdown(t *testing.T) {
11 out := Render("")
12 uassert.True(t, strings.Contains(out, "# moul's gallery"), "heading")
13 uassert.True(t, strings.Contains(out, "```"), "the banner is fenced in markdown")
14 uassert.True(t, strings.Contains(out, "| Piece |"), "a real markdown table")
15 // The path is spelled out rather than taken from realmPath: a test that
16 // reads the same constant the code does would pass with the version
17 // dropped, which is the exact regression this pins.
18 for _, p := range catalog() {
19 uassert.True(t, strings.Contains(out, "/r/moul/gallery/v0:piece/"+p.Slug), p.Slug+" is linked")
20 }
21}
22
23// The two targets are different documents, not one with characters deleted.
24func TestTextTargetStripsMarkdownAndDefersLinks(t *testing.T) {
25 out := Render("piece/millipede?fmt=txt")
26
27 uassert.False(t, strings.Contains(out, "```"), "no code fences")
28 uassert.False(t, strings.Contains(out, "**"), "no bold markers")
29 uassert.False(t, strings.Contains(out, "]("), "no inline link syntax")
30 uassert.False(t, strings.Contains(out, "\n#"), "no heading hashes")
31
32 // A heading is underlined instead.
33 uassert.True(t, strings.Contains(out, "millipede\n========="), "underlined heading")
34 // And the URL is a numbered footnote, not inline noise.
35 uassert.True(t, strings.Contains(out, "the source realm [1]"), "inline marker")
36 uassert.True(t, strings.Contains(out, "\nLinks:\n [1] https://gno.land/r/moul/demo/millipede/v0"), "footnote block")
37}
38
39func TestTextTableColumnsLineUp(t *testing.T) {
40 out := Render("?fmt=txt")
41 var header, rule string
42 for _, line := range strings.Split(out, "\n") {
43 if strings.HasPrefix(line, " Piece ") {
44 header = line
45 }
46 if strings.HasPrefix(line, " ---") {
47 rule = line
48 }
49 }
50 uassert.True(t, header != "", "found the header")
51 uassert.True(t, rule != "", "found the rule")
52
53 // Every column boundary in the rule is a pair of spaces in the header at
54 // the same rune offset. That is the property an unpadded pipe table does
55 // not have, and the reason the text target has its own table at all.
56 uassert.Equal(t, gutters(rule), gutters(header), "columns start at the same offsets")
57}
58
59// gutters returns the rune offsets at which a column starts: a non-space
60// preceded by at least two spaces. Measuring where the PADDING starts instead
61// would compare "Piece" against eleven dashes and report a difference that is
62// not one.
63func gutters(s string) string {
64 r := []rune(s)
65 out := ""
66 for i := 2; i < len(r); i++ {
67 if r[i] != ' ' && r[i-1] == ' ' && r[i-2] == ' ' {
68 out += itoa(i) + ","
69 }
70 }
71 return out
72}
73
74func TestArtIsNeverEscapedOrWrapped(t *testing.T) {
75 // The millipede is box-drawing and parentheses. A text target that ran it
76 // through an escaper or a wrapper would ruin it, and it would still look
77 // like a picture, which is what makes this worth pinning.
78 out := Render("piece/millipede?fmt=txt")
79 uassert.True(t, strings.Contains(out, "╚═(███)═╝"), "art survives verbatim")
80 uassert.False(t, strings.Contains(out, "\\("), "no escaping")
81}
82
83func TestUnknownPiece(t *testing.T) {
84 uassert.True(t, strings.Contains(Render("piece/nope"), "no piece called nope"), "markdown 404")
85 uassert.True(t, strings.Contains(Render("piece/nope?fmt=txt"), "No piece called nope."), "text 404")
86 // An empty slug is the index, not a 404.
87 uassert.True(t, strings.Contains(Render("piece/"), "# moul's gallery"), "empty slug falls back to the index")
88}
89
90func TestLinkedPiecesSayWhyTheyAreNotRendered(t *testing.T) {
91 p, ok := find("asciiart")
92 uassert.True(t, ok, "found")
93 uassert.False(t, p.Live(), "asciiart is private = true, so it cannot be imported")
94
95 out := Render("piece/asciiart")
96 uassert.True(t, strings.Contains(out, "cannot import the source"), "says so rather than showing nothing")
97}
98
99func TestEveryPieceRenders(t *testing.T) {
100 for _, p := range catalog() {
101 out := Render("piece/" + p.Slug)
102 uassert.True(t, strings.Contains(out, p.Title), p.Slug+" markdown")
103 uassert.True(t, len(Render("piece/"+p.Slug+"?fmt=txt")) > 0, p.Slug+" text")
104 }
105}
106
107func TestWrapKeepsLongWordsIntact(t *testing.T) {
108 long := "see gno.land/r/g17khqpukees4237dtn3astzapmp462vjhsz6st4/settlers/nft now"
109 got := wrap(long, 20)
110 // A path is worse broken than wide, so it overflows rather than splitting.
111 uassert.True(t, strings.Contains(got, "gno.land/r/g17khqpukees4237dtn3astzapmp462vjhsz6st4/settlers/nft"), "unbroken")
112 uassert.Equal(t, 3, len(strings.Split(got, "\n")), "three lines")
113}