gitea源码

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. "testing"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestDumpInfo(t *testing.T) {
  10. t.Logf("codes: %d", len(codeMap))
  11. t.Logf("aliases: %d", len(aliasMap))
  12. }
  13. func TestLookup(t *testing.T) {
  14. a := FromCode("\U0001f37a")
  15. b := FromCode("🍺")
  16. c := FromAlias(":beer:")
  17. d := FromAlias("beer")
  18. assert.Equal(t, a, b)
  19. assert.Equal(t, b, c)
  20. assert.Equal(t, c, d)
  21. assert.Equal(t, a, d)
  22. m := FromCode("\U0001f44d")
  23. n := FromAlias(":thumbsup:")
  24. o := FromAlias("+1")
  25. assert.Equal(t, m, n)
  26. assert.Equal(t, m, o)
  27. assert.Equal(t, n, o)
  28. }
  29. func TestReplacers(t *testing.T) {
  30. tests := []struct {
  31. f func(string) string
  32. v, exp string
  33. }{
  34. {ReplaceCodes, ":thumbsup: +1 for \U0001f37a! 🍺 \U0001f44d", ":thumbsup: +1 for :beer:! :beer: :+1:"},
  35. {ReplaceAliases, ":thumbsup: +1 :+1: :beer:", "\U0001f44d +1 \U0001f44d \U0001f37a"},
  36. }
  37. for i, x := range tests {
  38. s := x.f(x.v)
  39. assert.Equalf(t, x.exp, s, "test %d `%s` expected `%s`, got: `%s`", i, x.v, x.exp, s)
  40. }
  41. }
  42. func TestFindEmojiSubmatchIndex(t *testing.T) {
  43. type testcase struct {
  44. teststring string
  45. expected []int
  46. }
  47. testcases := []testcase{
  48. {
  49. "\U0001f44d",
  50. []int{0, len("\U0001f44d")},
  51. },
  52. {
  53. "\U0001f44d +1 \U0001f44d \U0001f37a",
  54. []int{0, 4},
  55. },
  56. {
  57. " \U0001f44d",
  58. []int{1, 1 + len("\U0001f44d")},
  59. },
  60. {
  61. string([]byte{'\u0001'}) + "\U0001f44d",
  62. []int{1, 1 + len("\U0001f44d")},
  63. },
  64. }
  65. for _, kase := range testcases {
  66. actual := FindEmojiSubmatchIndex(kase.teststring)
  67. assert.Equal(t, kase.expected, actual)
  68. }
  69. }