scan_test.gno
2.08 Kb · 52 lines
1package ahocorasickdemo
2
3import (
4 "strings"
5 "testing"
6
7 "gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/md/v0"
8 "gno.land/p/nt/uassert/v0"
9)
10
11// The path is the visitor's text and it is echoed inside a code span, so a
12// backtick in it must not close that span and let a link through. A backslash
13// does not escape a backtick inside a span: only a wider fence does, which is
14// what md.InlineCode picks.
15func TestRenderArgCannotCloseItsCodeSpan(t *testing.T) {
16 arg := "x` [claim](https://evil.example) `"
17 out := Render("/" + arg)
18 uassert.True(t, strings.Contains(out, md.InlineCode(arg)), out)
19 uassert.False(t, strings.Contains(out, "`x\\` [claim](https://evil.example) \\``"), out)
20}
21
22// The match table reports offsets and dictionary words, never a slice of the
23// scanned text, so the argument reaches the page exactly once: in the heading
24// that echoes it. A pipe in it cannot open a column that is not there.
25func TestRenderEchoesTheArgumentOnlyOnce(t *testing.T) {
26 out := Render("/the|evil")
27 uassert.Equal(t, 1, strings.Count(out, "evil"))
28 uassert.True(t, strings.Contains(out, "| `0` | `3` | `the` |"), out)
29}
30
31// A text with nothing from the dictionary gets a sentence, not an empty table:
32// an empty table reads as a broken page rather than as an answer.
33func TestRenderSaysWhenNothingMatched(t *testing.T) {
34 out := Render("/zzz")
35 uassert.True(t, strings.Contains(out, "No pattern occurs in it."), out)
36 uassert.False(t, strings.Contains(out, "| start | end | pattern |"), out)
37}
38
39// Whitespace is not trimmed: trimming would report offsets into a string the
40// visitor did not submit. The leading space shifts every offset by one.
41func TestRenderDoesNotTrim(t *testing.T) {
42 out := Render("/ the")
43 uassert.True(t, strings.Contains(out, "| `1` | `4` | `the` |"), out)
44}
45
46// The realm holds no state, so two renders of the same path agree. This is
47// what makes the pinned example meaningful rather than a snapshot of whichever
48// test ran before it.
49func TestRenderIsDeterministic(t *testing.T) {
50 uassert.Equal(t, Render(""), Render(""))
51 uassert.Equal(t, Render("/thermos"), Render("/thermos"))
52}