errata_test.gno
1.91 Kb · 63 lines
1package errata
2
3import (
4 "strings"
5 "testing"
6)
7
8func TestFormatUgnot(t *testing.T) {
9 tests := []struct {
10 amount int64
11 want string
12 }{
13 {0, "0 GNOT (0 ugnot)"},
14 {1, "0.000001 GNOT (1 ugnot)"},
15 {2560000, "2.56 GNOT (2560000 ugnot)"},
16 {2000000, "2 GNOT (2000000 ugnot)"},
17 {-1, "-0.000001 GNOT (-1 ugnot)"},
18 {-9223372036854775808, "-9223372036854.775808 GNOT (-9223372036854775808 ugnot)"},
19 }
20 for _, test := range tests {
21 if got := FormatUgnot(test.amount); got != test.want {
22 t.Errorf("FormatUgnot(%d) = %q, want %q", test.amount, got, test.want)
23 }
24 }
25}
26
27func TestShortAddr(t *testing.T) {
28 if got := ShortAddr("g1abcdefghijklmnopqrstuvwxy"); got != "g1abcd…vwxy" {
29 t.Errorf("unexpected abbreviated address: %q", got)
30 }
31 if got := ShortAddr("g1abcd"); got != "g1abcd" {
32 t.Errorf("short address changed: %q", got)
33 }
34}
35
36func TestRenderSection(t *testing.T) {
37 if got := RenderSection("", nil); got != "## Errata\n\nNo errata yet.\n" {
38 t.Errorf("unexpected empty section: %q", got)
39 }
40 entries := []Entry{
41 {
42 Location: "p. 42 | 43", Text: "*fix*\n[x](javascript:boom) <img>",
43 Finder: "[reader]|test", FoundAt: 12, Paid: 2560000, Link: "javascript:boom",
44 },
45 {Location: "p. 50", Text: "Fix the example.", Finder: "reader",
46 FoundAt: 13, Link: "/r/reader/errata/v0:finder/1"},
47 }
48 got := RenderSection("Errata\n# <unsafe>", entries)
49 for _, want := range []string{"p\\. 42 \\| 43", "\\*fix\\*", "\\<img\\>",
50 "2.56 GNOT (2560000 ugnot)", "[reader](/r/reader/errata/v0:finder/1)"} {
51 if !strings.Contains(got, want) {
52 t.Errorf("table missing %q in %q", want, got)
53 }
54 }
55 for _, unwanted := range []string{"\n# <unsafe>", "[x](javascript:", "](javascript:", "<img>"} {
56 if strings.Contains(got, unwanted) {
57 t.Errorf("unsafe content %q in %q", unwanted, got)
58 }
59 }
60 if lines := strings.Count(got, "\n"); lines != 6 {
61 t.Errorf("expected one line per table row, got %d lines in %q", lines, got)
62 }
63}