-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
formatter_test.go
57 lines (51 loc) · 2.22 KB
/
formatter_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// mautrix-discord - A Matrix-Discord puppeting bridge.
// Copyright (C) 2022 Tulir Asokan
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestEscapeDiscordMarkdown(t *testing.T) {
type escapeTest struct {
name string
input string
expected string
}
tests := []escapeTest{
{"Simple text", "Lorem ipsum dolor sit amet, consectetuer adipiscing elit.", "Lorem ipsum dolor sit amet, consectetuer adipiscing elit."},
{"Backslash", `foo\bar`, `foo\\bar`},
{"Underscore", `foo_bar`, `foo\_bar`},
{"Asterisk", `foo*bar`, `foo\*bar`},
{"Tilde", `foo~bar`, `foo\~bar`},
{"Backtick", "foo`bar", "foo\\`bar"},
{"Forward tick", `foo´bar`, `foo´bar`},
{"Pipe", `foo|bar`, `foo\|bar`},
{"Less than", `foo<bar`, `foo\<bar`},
{"Greater than", `foo>bar`, `foo>bar`},
{"Multiple things", `\_*~|`, `\\\_\*\~\|`},
{"URL", `https://example.com/foo_bar`, `https://example.com/foo_bar`},
{"Multiple URLs", `hello_world https://example.com/foo_bar *testing* https://a_b_c/*def*`, `hello\_world https://example.com/foo_bar \*testing\* https://a_b_c/*def*`},
{"URL ends with no-break zero-width space", "https://example.com\ufefffoo_bar", "https://example.com\ufefffoo\\_bar"},
{"URL ends with less than", `https://example.com<foo_bar`, `https://example.com<foo\_bar`},
{"Short URL", `https://_`, `https://_`},
{"Insecure URL", `http://example.com/foo_bar`, `http://example.com/foo_bar`},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
assert.Equal(t, test.expected, escapeDiscordMarkdown(test.input))
})
}
}