zz_zerowidth_test.gno
1.30 Kb · 50 lines
1package guestbook
2
3import (
4 "testing"
5)
6
7// zwBlank holds one input per rune range chain/markdown.StripBidiAndZeroWidth
8// removes (U+200B..U+200F, U+202A..U+202E, U+2066..U+2069, U+FEFF), plus a mix
9// with real spaces. strings.TrimSpace keeps every one of them and the render
10// strips them all, so each must be refused.
11var zwBlank = []string{
12 "\u200b\u200b\u200b", // ZWSP
13 "\u200d", // ZWJ
14 "\u200e", // LRM
15 "\u202e", // RLO
16 "\u2066\u2069", // LRI PDI
17 "\ufeff", // BOM
18 " \u200b \u200b ", // mixed with real spaces
19}
20
21// zwOK must stay accepted: the stripper removes the marks and leaves text.
22var zwOK = []string{"a", "a\u200bb", " padded "}
23
24func zwPanics(f func()) (p bool) {
25 defer func() {
26 if recover() != nil {
27 p = true
28 }
29 }()
30 f()
31 return false
32}
33
34func TestZeroWidthMessageIsRefused(t *testing.T) {
35 resetState()
36 defer resetState()
37 for _, s := range zwBlank {
38 if !zwPanics(func() { addEntry("g1alice", s, 1) }) {
39 t.Errorf("addEntry(%q) accepted a message the render strips to blank", s)
40 }
41 }
42 for _, s := range zwOK {
43 if zwPanics(func() { addEntry("g1alice", s, 1) }) {
44 t.Errorf("addEntry(%q) refused a message that renders as text", s)
45 }
46 }
47 if Count() != len(zwOK) {
48 t.Errorf("Count = %d, want %d", Count(), len(zwOK))
49 }
50}