File: parser_test.go

package info (click to toggle)
golang-github-enescakir-emoji 1.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 472 kB
  • sloc: makefile: 3
file content (164 lines) | stat: -rw-r--r-- 4,367 bytes parent folder | download
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package emoji

import (
	"fmt"
	"testing"
)

func TestParse(t *testing.T) {
	tt := []struct {
		input    string
		expected string
	}{
		{
			input:    "I am :man_technologist: from :flag_for_turkey:. Tests are :thumbs_up:",
			expected: fmt.Sprintf("I am %v from %v. Tests are %v", ManTechnologist, FlagForTurkey, ThumbsUp),
		},
		{
			input:    "consecutive emojis :pizza::sushi::sweat:",
			expected: fmt.Sprintf("consecutive emojis %v%v%v", Pizza, Sushi, DowncastFaceWithSweat),
		},
		{
			input:    ":accordion::anguished_face: \n woman :woman_golfing:",
			expected: fmt.Sprintf("%v%v \n woman %v", Accordion, AnguishedFace, WomanGolfing),
		},
		{
			input:    "shared colon :angry_face_with_horns:anger_symbol:",
			expected: fmt.Sprintf("shared colon %vanger_symbol:", AngryFaceWithHorns),
		},
		{
			input:    ":not_exist_emoji: not exist emoji",
			expected: ":not_exist_emoji: not exist emoji",
		},
		{
			input:    ":dragon::",
			expected: fmt.Sprintf("%v:", Dragon),
		},
		{
			input:    "::+1:",
			expected: fmt.Sprintf(":%v", ThumbsUp),
		},
		{
			input:    "::anchor::",
			expected: fmt.Sprintf(":%v:", Anchor),
		},
		{
			input:    ":anguished:::",
			expected: fmt.Sprintf("%v::", AnguishedFace),
		},
		{
			input:    "too many colon::::closed_book:::: too many colon:",
			expected: fmt.Sprintf("too many colon:::%v::: too many colon:", ClosedBook),
		},
		{
			input:    "emoji with space :angry face_with_horns:anger_symbol:",
			expected: fmt.Sprintf("emoji with space :angry face_with_horns%v", AngerSymbol),
		},
		{
			input:    "flag testing :flag-tr: done",
			expected: fmt.Sprintf("flag testing %v done", FlagForTurkey),
		},
		{
			input:    "not valid flags :flag-tra: :flag-t: testing",
			expected: fmt.Sprintf("not valid flags :flag-tra: :flag-t: testing"),
		},
		{
			input:    "dummytext",
			expected: "dummytext",
		},
	}

	for i, tc := range tt {
		got := Parse(tc.input)
		if got != tc.expected {
			t.Fatalf("test case %v fail: got: %v, expected: %v", i+1, got, tc.expected)
		}
	}
}

func TestMap(t *testing.T) {
	expected := len(emojiMap)
	got := len(Map())

	if got != expected {
		t.Fatalf("test case fail: got: %v, expected: %v", got, expected)
	}
}

func TestAppendAlias(t *testing.T) {
	tt := []struct {
		alias string
		code  string
		err   bool
	}{
		{alias: ":my_car:", code: "\U0001f3ce\ufe0f", err: false},
		{alias: ":berserker:", code: "\U0001f621", err: false},
		{alias: ":potato:", code: "\U0001f423", err: true},
		{alias: ":not_valid alias:", code: "\U0001f423", err: true},
	}

	for i, tc := range tt {
		err := AppendAlias(tc.alias, tc.code)
		if (err != nil) != tc.err {
			t.Fatalf("test case %v fail: got: %v, expected: %v", i+1, err, tc.err)
		}

		if exist := Exist(tc.alias); !exist && !tc.err {
			t.Fatalf("test case %v fail: got: %v, expected: %v", i+1, !exist, exist)
		}
	}
}

func TestExist(t *testing.T) {
	tt := []struct {
		input    string
		expected bool
	}{
		{input: ":man_technologist:", expected: true},
		{input: ":registered:", expected: true},
		{input: ":robot_face:", expected: true},
		{input: ":wave:", expected: true},
		{input: ":sheaf_of_rice:", expected: true},
		{input: ":random_emoji:", expected: false},
		{input: ":test_emoji:", expected: false},
	}

	for i, tc := range tt {
		got := Exist(tc.input)
		if got != tc.expected {
			t.Fatalf("test case %v fail: got: %v, expected: %v", i+1, got, tc.expected)
		}
	}
}

func TestFind(t *testing.T) {
	tt := []struct {
		input    string
		expected string
		exist    bool
	}{
		{input: ":man_technologist:", expected: ManTechnologist.String(), exist: true},
		{input: ":robot_face:", expected: Robot.String(), exist: true},
		{input: ":wave:", expected: WavingHand.String(), exist: true},
		{input: ":sheaf_of_rice:", expected: SheafOfRice.String(), exist: true},
		{input: ":random_emoji:", expected: "", exist: false},
		{input: ":test_emoji:", expected: "", exist: false},
	}

	for i, tc := range tt {
		got, exist := Find(tc.input)
		if got != tc.expected {
			t.Fatalf("test case %v fail: got: %v, expected: %v", i+1, got, tc.expected)
		}

		if exist != tc.exist {
			t.Fatalf("test case %v fail: got: %v, expected: %v", i+1, exist, tc.exist)
		}
	}
}

func BenchmarkParse(b *testing.B) {
	for n := 0; n < b.N; n++ {
		_ = Parse("I am :man_technologist: from :flag_for_turkey:. Tests are :thumbs_up:")
	}
}