parser_test.go (view raw)
1package emoji
2
3import (
4 "fmt"
5 "testing"
6)
7
8func TestParse(t *testing.T) {
9 tt := []struct {
10 input string
11 expected string
12 }{
13 {
14 input: "I am :man_technologist: from :flag_for_turkey:. Tests are :thumbs_up:",
15 expected: fmt.Sprintf("I am %v from %v. Tests are %v", ManTechnologist, FlagForTurkey, ThumbsUp),
16 },
17 {
18 input: "consecutive emojis :pizza::sushi::sweat:",
19 expected: fmt.Sprintf("consecutive emojis %v%v%v", Pizza, Sushi, DowncastFaceWithSweat),
20 },
21 {
22 input: ":accordion::anguished_face: \n woman :woman_golfing:",
23 expected: fmt.Sprintf("%v%v \n woman %v", Accordion, AnguishedFace, WomanGolfing),
24 },
25 {
26 input: "shared colon :angry_face_with_horns:anger_symbol:",
27 expected: fmt.Sprintf("shared colon %vanger_symbol:", AngryFaceWithHorns),
28 },
29 {
30 input: ":not_exist_emoji: not exist emoji",
31 expected: ":not_exist_emoji: not exist emoji",
32 },
33 {
34 input: ":dragon::",
35 expected: fmt.Sprintf("%v:", Dragon),
36 },
37 {
38 input: "::+1:",
39 expected: fmt.Sprintf(":%v", ThumbsUp),
40 },
41 {
42 input: "::anchor::",
43 expected: fmt.Sprintf(":%v:", Anchor),
44 },
45 {
46 input: ":anguished:::",
47 expected: fmt.Sprintf("%v::", AnguishedFace),
48 },
49 {
50 input: "too many colon::::closed_book:::: too many colon:",
51 expected: fmt.Sprintf("too many colon:::%v::: too many colon:", ClosedBook),
52 },
53 {
54 input: "emoji with space :angry face_with_horns:anger_symbol:",
55 expected: fmt.Sprintf("emoji with space :angry face_with_horns%v", AngerSymbol),
56 },
57 {
58 input: "dummytext",
59 expected: "dummytext",
60 },
61 }
62
63 for i, tc := range tt {
64 got := Parse(tc.input)
65 if got != tc.expected {
66 t.Fatalf("test case %v fail: got: %v, expected: %v", i+1, got, tc.expected)
67 }
68 }
69}
70
71func TestMap(t *testing.T) {
72 expected := len(emojiMap)
73 got := len(Map())
74
75 if got != expected {
76 t.Fatalf("test case fail: got: %v, expected: %v", got, expected)
77 }
78}
79
80func TestAppendAlias(t *testing.T) {
81 tt := []struct {
82 alias string
83 code string
84 err bool
85 }{
86 {alias: ":my_car:", code: "\U0001f3ce\ufe0f", err: false},
87 {alias: ":berserker:", code: "\U0001f621", err: false},
88 {alias: ":potato:", code: "\U0001f423", err: true},
89 {alias: ":not_valid alias:", code: "\U0001f423", err: true},
90 }
91
92 for i, tc := range tt {
93 err := AppendAlias(tc.alias, tc.code)
94 if (err != nil) != tc.err {
95 t.Fatalf("test case %v fail: got: %v, expected: %v", i+1, err, tc.err)
96 }
97
98 if exist := Exist(tc.alias); !exist && !tc.err {
99 t.Fatalf("test case %v fail: got: %v, expected: %v", i+1, !exist, exist)
100 }
101 }
102}
103
104func TestExist(t *testing.T) {
105 tt := []struct {
106 input string
107 expected bool
108 }{
109 {input: ":man_technologist:", expected: true},
110 {input: ":registered:", expected: true},
111 {input: ":robot_face:", expected: true},
112 {input: ":wave:", expected: true},
113 {input: ":sheaf_of_rice:", expected: true},
114 {input: ":random_emoji:", expected: false},
115 {input: ":test_emoji:", expected: false},
116 }
117
118 for i, tc := range tt {
119 got := Exist(tc.input)
120 if got != tc.expected {
121 t.Fatalf("test case %v fail: got: %v, expected: %v", i+1, got, tc.expected)
122 }
123 }
124}
125
126func TestFind(t *testing.T) {
127 tt := []struct {
128 input string
129 expected string
130 exist bool
131 }{
132 {input: ":man_technologist:", expected: ManTechnologist.String(), exist: true},
133 {input: ":robot_face:", expected: Robot.String(), exist: true},
134 {input: ":wave:", expected: WavingHand.String(), exist: true},
135 {input: ":sheaf_of_rice:", expected: SheafOfRice.String(), exist: true},
136 {input: ":random_emoji:", expected: "", exist: false},
137 {input: ":test_emoji:", expected: "", exist: false},
138 }
139
140 for i, tc := range tt {
141 got, exist := Find(tc.input)
142 if got != tc.expected {
143 t.Fatalf("test case %v fail: got: %v, expected: %v", i+1, got, tc.expected)
144 }
145
146 if exist != tc.exist {
147 t.Fatalf("test case %v fail: got: %v, expected: %v", i+1, exist, tc.exist)
148 }
149 }
150}
151
152func BenchmarkParse(b *testing.B) {
153 for n := 0; n < b.N; n++ {
154 _ = Parse("I am :man_technologist: from :flag_for_turkey:. Tests are :thumbs_up:")
155 }
156}