formatter_test.go 2.2 KB

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