summaryrefslogtreecommitdiffstats
path: root/helpers/emoji_test.go
blob: 6485bb5fee5d1aaf13764d32285a0033855bed49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright 2016 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package helpers

import (
	"math"
	"reflect"
	"strings"
	"testing"

	"github.com/gohugoio/hugo/bufferpool"
	"github.com/kyokomi/emoji/v2"
)

func TestEmojiCustom(t *testing.T) {
	for i, this := range []struct {
		input  string
		expect []byte
	}{
		{"A :smile: a day", []byte("A šŸ˜„ a day")},
		{"A few :smile:s a day", []byte("A few šŸ˜„s a day")},
		{"A :smile: and a :beer: makes the day for sure.", []byte("A šŸ˜„ and a šŸŗ makes the day for sure.")},
		{"A :smile: and: a :beer:", []byte("A šŸ˜„ and: a šŸŗ")},
		{"A :diamond_shape_with_a_dot_inside: and then some.", []byte("A šŸ’  and then some.")},
		{":smile:", []byte("šŸ˜„")},
		{":smi", []byte(":smi")},
		{"A :smile:", []byte("A šŸ˜„")},
		{":beer:!", []byte("šŸŗ!")},
		{"::smile:", []byte(":šŸ˜„")},
		{":beer::", []byte("šŸŗ:")},
		{" :beer: :", []byte(" šŸŗ :")},
		{":beer: and :smile: and another :beer:!", []byte("šŸŗ and šŸ˜„ and another šŸŗ!")},
		{" :beer: : ", []byte(" šŸŗ : ")},
		{"No smilies for you!", []byte("No smilies for you!")},
		{" The motto: no smiles! ", []byte(" The motto: no smiles! ")},
		{":hugo_is_the_best_static_gen:", []byte(":hugo_is_the_best_static_gen:")},
		{"ģ€ķ–‰ :smile: ģ€ķ–‰", []byte("ģ€ķ–‰ šŸ˜„ ģ€ķ–‰")},
		// #2198
		{"See: A :beer:!", []byte("See: A šŸŗ!")},
		{`Aaaaaaaaaa: aaaaaaaaaa aaaaaaaaaa aaaaaaaaaa.

:beer:`, []byte(`Aaaaaaaaaa: aaaaaaaaaa aaaaaaaaaa aaaaaaaaaa.

šŸŗ`)},
		{"test :\n```bash\nthis is a test\n```\n\ntest\n\n:cool::blush:::pizza:\\:blush : : blush: :pizza:", []byte("test :\n```bash\nthis is a test\n```\n\ntest\n\nšŸ†’šŸ˜Š:šŸ•\\:blush : : blush: šŸ•")},
		{
			// 2391
			"[a](http://gohugo.io) :smile: [r](http://gohugo.io/introduction/overview/) :beer:",
			[]byte(`[a](http://gohugo.io) šŸ˜„ [r](http://gohugo.io/introduction/overview/) šŸŗ`),
		},
	} {

		result := Emojify([]byte(this.input))

		if !reflect.DeepEqual(result, this.expect) {
			t.Errorf("[%d] got %q but expected %q", i, result, this.expect)
		}

	}
}

// The Emoji benchmarks below are heavily skewed in Hugo's direction:
//
// Hugo have a byte slice, wants a byte slice and doesn't mind if the original is modified.

func BenchmarkEmojiKyokomiFprint(b *testing.B) {
	f := func(in []byte) []byte {
		buff := bufferpool.GetBuffer()
		defer bufferpool.PutBuffer(buff)
		emoji.Fprint(buff, string(in))

		bc := make([]byte, buff.Len())
		copy(bc, buff.Bytes())
		return bc
	}

	doBenchmarkEmoji(b, f)
}

func BenchmarkEmojiKyokomiSprint(b *testing.B) {
	f := func(in []byte) []byte {
		return []byte(emoji.Sprint(string(in)))
	}

	doBenchmarkEmoji(b, f)
}

func BenchmarkHugoEmoji(b *testing.B) {
	doBenchmarkEmoji(b, Emojify)
}

func doBenchmarkEmoji(b *testing.B, f func(in []byte) []byte) {
	type input struct {
		in     []byte
		expect []byte
	}

	data := []struct {
		input  string
		expect string
	}{
		{"A :smile: a day", emoji.Sprint("A :smile: a day")},
		{"A :smile: and a :beer: day keeps the doctor away", emoji.Sprint("A :smile: and a :beer: day keeps the doctor away")},
		{"A :smile: a day and 10 " + strings.Repeat(":beer: ", 10), emoji.Sprint("A :smile: a day and 10 " + strings.Repeat(":beer: ", 10))},
		{"No smiles today.", "No smiles today."},
		{"No smiles for you or " + strings.Repeat("you ", 1000), "No smiles for you or " + strings.Repeat("you ", 1000)},
	}

	in := make([]input, b.N*len(data))
	cnt := 0
	for i := 0; i < b.N; i++ {
		for _, this := range data {
			in[cnt] = input{[]byte(this.input), []byte(this.expect)}
			cnt++
		}
	}

	b.ResetTimer()
	cnt = 0
	for i := 0; i < b.N; i++ {
		for j := range data {
			currIn := in[cnt]
			cnt++
			result := f(currIn.in)
			// The Emoji implementations gives slightly different output.
			diffLen := len(result) - len(currIn.expect)
			diffLen = int(math.Abs(float64(diffLen)))
			if diffLen > 30 {
				b.Fatalf("[%d] emoji std, got \n%q but expected \n%q", j, result, currIn.expect)
			}
		}
	}
}