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