code.gitea.io/gitea@v1.19.3/modules/emoji/emoji_test.go (about)

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