You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

emoji_test.go 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // Copyright 2015 Kenneth Shaw
  3. // SPDX-License-Identifier: MIT
  4. package emoji
  5. import (
  6. "reflect"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestDumpInfo(t *testing.T) {
  11. t.Logf("codes: %d", len(codeMap))
  12. t.Logf("aliases: %d", len(aliasMap))
  13. }
  14. func TestLookup(t *testing.T) {
  15. a := FromCode("\U0001f37a")
  16. b := FromCode("🍺")
  17. c := FromAlias(":beer:")
  18. d := FromAlias("beer")
  19. if !reflect.DeepEqual(a, b) {
  20. t.Errorf("a and b should equal")
  21. }
  22. if !reflect.DeepEqual(b, c) {
  23. t.Errorf("b and c should equal")
  24. }
  25. if !reflect.DeepEqual(c, d) {
  26. t.Errorf("c and d should equal")
  27. }
  28. if !reflect.DeepEqual(a, d) {
  29. t.Errorf("a and d should equal")
  30. }
  31. m := FromCode("\U0001f44d")
  32. n := FromAlias(":thumbsup:")
  33. o := FromAlias("+1")
  34. if !reflect.DeepEqual(m, n) {
  35. t.Errorf("m and n should equal")
  36. }
  37. if !reflect.DeepEqual(n, o) {
  38. t.Errorf("n and o should equal")
  39. }
  40. if !reflect.DeepEqual(m, o) {
  41. t.Errorf("m and o should equal")
  42. }
  43. }
  44. func TestReplacers(t *testing.T) {
  45. tests := []struct {
  46. f func(string) string
  47. v, exp string
  48. }{
  49. {ReplaceCodes, ":thumbsup: +1 for \U0001f37a! 🍺 \U0001f44d", ":thumbsup: +1 for :beer:! :beer: :+1:"},
  50. {ReplaceAliases, ":thumbsup: +1 :+1: :beer:", "\U0001f44d +1 \U0001f44d \U0001f37a"},
  51. }
  52. for i, x := range tests {
  53. s := x.f(x.v)
  54. if s != x.exp {
  55. t.Errorf("test %d `%s` expected `%s`, got: `%s`", i, x.v, x.exp, s)
  56. }
  57. }
  58. }
  59. func TestFindEmojiSubmatchIndex(t *testing.T) {
  60. type testcase struct {
  61. teststring string
  62. expected []int
  63. }
  64. testcases := []testcase{
  65. {
  66. "\U0001f44d",
  67. []int{0, len("\U0001f44d")},
  68. },
  69. {
  70. "\U0001f44d +1 \U0001f44d \U0001f37a",
  71. []int{0, 4},
  72. },
  73. {
  74. " \U0001f44d",
  75. []int{1, 1 + len("\U0001f44d")},
  76. },
  77. {
  78. string([]byte{'\u0001'}) + "\U0001f44d",
  79. []int{1, 1 + len("\U0001f44d")},
  80. },
  81. }
  82. for _, kase := range testcases {
  83. actual := FindEmojiSubmatchIndex(kase.teststring)
  84. assert.Equal(t, kase.expected, actual)
  85. }
  86. }