all repos — emoji @ 0235a1371768f43c7c89286e0881464088241b54

A minimalistic emoji package for Go (golang)

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:    "flag testing :flag-tr: done",
 59			expected: fmt.Sprintf("flag testing %v done", FlagForTurkey),
 60		},
 61		{
 62			input:    "not valid flags :flag-tra: :flag-t: testing",
 63			expected: fmt.Sprintf("not valid flags :flag-tra: :flag-t: testing"),
 64		},
 65		{
 66			input:    "dummytext",
 67			expected: "dummytext",
 68		},
 69	}
 70
 71	for i, tc := range tt {
 72		got := Parse(tc.input)
 73		if got != tc.expected {
 74			t.Fatalf("test case %v fail: got: %v, expected: %v", i+1, got, tc.expected)
 75		}
 76	}
 77}
 78
 79func TestMap(t *testing.T) {
 80	expected := len(emojiMap)
 81	got := len(Map())
 82
 83	if got != expected {
 84		t.Fatalf("test case fail: got: %v, expected: %v", got, expected)
 85	}
 86}
 87
 88func TestAppendAlias(t *testing.T) {
 89	tt := []struct {
 90		alias string
 91		code  string
 92		err   bool
 93	}{
 94		{alias: ":my_car:", code: "\U0001f3ce\ufe0f", err: false},
 95		{alias: ":berserker:", code: "\U0001f621", err: false},
 96		{alias: ":potato:", code: "\U0001f423", err: true},
 97		{alias: ":not_valid alias:", code: "\U0001f423", err: true},
 98	}
 99
100	for i, tc := range tt {
101		err := AppendAlias(tc.alias, tc.code)
102		if (err != nil) != tc.err {
103			t.Fatalf("test case %v fail: got: %v, expected: %v", i+1, err, tc.err)
104		}
105
106		if exist := Exist(tc.alias); !exist && !tc.err {
107			t.Fatalf("test case %v fail: got: %v, expected: %v", i+1, !exist, exist)
108		}
109	}
110}
111
112func TestExist(t *testing.T) {
113	tt := []struct {
114		input    string
115		expected bool
116	}{
117		{input: ":man_technologist:", expected: true},
118		{input: ":registered:", expected: true},
119		{input: ":robot_face:", expected: true},
120		{input: ":wave:", expected: true},
121		{input: ":sheaf_of_rice:", expected: true},
122		{input: ":random_emoji:", expected: false},
123		{input: ":test_emoji:", expected: false},
124	}
125
126	for i, tc := range tt {
127		got := Exist(tc.input)
128		if got != tc.expected {
129			t.Fatalf("test case %v fail: got: %v, expected: %v", i+1, got, tc.expected)
130		}
131	}
132}
133
134func TestFind(t *testing.T) {
135	tt := []struct {
136		input    string
137		expected string
138		exist    bool
139	}{
140		{input: ":man_technologist:", expected: ManTechnologist.String(), exist: true},
141		{input: ":robot_face:", expected: Robot.String(), exist: true},
142		{input: ":wave:", expected: WavingHand.String(), exist: true},
143		{input: ":sheaf_of_rice:", expected: SheafOfRice.String(), exist: true},
144		{input: ":random_emoji:", expected: "", exist: false},
145		{input: ":test_emoji:", expected: "", exist: false},
146	}
147
148	for i, tc := range tt {
149		got, exist := Find(tc.input)
150		if got != tc.expected {
151			t.Fatalf("test case %v fail: got: %v, expected: %v", i+1, got, tc.expected)
152		}
153
154		if exist != tc.exist {
155			t.Fatalf("test case %v fail: got: %v, expected: %v", i+1, exist, tc.exist)
156		}
157	}
158}
159
160func BenchmarkParse(b *testing.B) {
161	for n := 0; n < b.N; n++ {
162		_ = Parse("I am :man_technologist: from :flag_for_turkey:. Tests are :thumbs_up:")
163	}
164}