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

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