myersdiffdemo_test.gno
2.89 Kb · 77 lines
1package myersdiffdemo
2
3import (
4 "strings"
5 "testing"
6
7 "gno.land/p/nt/uassert/v0"
8)
9
10func TestUnknownPathFallsBackToTheIndex(t *testing.T) {
11 index := Render("")
12 uassert.Equal(t, index, Render("/"))
13 uassert.Equal(t, index, Render("/nope"))
14 uassert.Equal(t, index, Render("/patch2"))
15}
16
17// TestNoCallerEchoOnThePage is the escaping question asked the only way that
18// settles it: feed the path every construct the sanitizer exists to stop, then
19// assert none of it reached the page. This realm answers by never rendering a
20// caller string at all, so the assertion is that the output is byte-identical
21// to the index.
22func TestNoCallerEchoOnThePage(t *testing.T) {
23 index := Render("")
24 hostile := []string{
25 "/[click](https://evil.example)",
26 "/",
27 "/<gno-columns>",
28 "/a`b",
29 "/x\ny",
30 "/patch/[link](https://evil.example)",
31 }
32 for _, p := range hostile {
33 got := Render(p)
34 uassert.False(t, strings.Contains(got, "evil.example"), "a caller URL reached the page")
35 uassert.False(t, strings.Contains(got, "<gno-"), "caller chrome reached the page")
36 if !strings.HasPrefix(p, "/patch/") {
37 uassert.Equal(t, index, got)
38 }
39 }
40 // A hostile context segment is parsed as an integer, fails, and leaves the
41 // default width, so the page is the ordinary one.
42 uassert.Equal(t, Render("/patch"), Render("/patch/[link](https://evil.example)"))
43}
44
45func TestContextIsClamped(t *testing.T) {
46 uassert.Equal(t, Render("/patch/0"), Render("/patch/-3"))
47 uassert.Equal(t, Render("/patch/6"), Render("/patch/99"))
48 uassert.Equal(t, Render("/patch/2"), Render("/patch"))
49
50 // Context 0 prints changed lines only, so no unchanged line of the fixture
51 // survives into the diff body.
52 narrow := Render("/patch/0")
53 uassert.False(t, strings.Contains(narrow, " package main"), "context 0 printed an unchanged line")
54 wide := Render("/patch/6")
55 uassert.True(t, strings.Contains(wide, " package main"), "context 6 dropped an unchanged line")
56}
57
58func TestStarvedScenarioSaysSo(t *testing.T) {
59 page := Render("/starved")
60 uassert.True(t, strings.Contains(page, "| minimal | no |"), "the starved page should report a non-minimal script")
61 uassert.True(t, strings.Contains(page, "reproduces the right one **yes**"), "a non-minimal script still applies")
62
63 ok := Render("/move")
64 uassert.True(t, strings.Contains(ok, "| minimal | yes |"), "the move page should report a minimal script")
65}
66
67// TestEveryScenarioApplies walks the whole table rather than the one scenario a
68// hand-written test would pick: a fixture added later is covered the day it is
69// added, not the day someone remembers it.
70func TestEveryScenarioApplies(t *testing.T) {
71 for _, sc := range scenarios {
72 s := diffOf(sc)
73 uassert.True(t, applies(sc, s), "scenario "+sc.key+" does not replay into its right-hand document")
74 page := Render("/" + sc.key)
75 uassert.True(t, strings.Contains(page, "```diff"), "scenario "+sc.key+" rendered no diff block")
76 }
77}