test: add basic tests
Enes Çakır enes@cakir.web.tr
Tue, 18 Feb 2020 00:17:09 +0300
4 files changed,
107 insertions(+),
13 deletions(-)
M
emoji.go
→
emoji.go
@@ -32,19 +32,23 @@ // EmojiWithTone defines an emoji object that has skin tone options.
type EmojiWithTone Emoji func (e EmojiWithTone) String() string { - return strings.ReplaceAll(string(e), "@", string(Default)) + return strings.ReplaceAll(string(e), "@", Default.String()) } // Tone returns an emoji object with given skin tone. func (e EmojiWithTone) Tone(tones ...Tone) EmojiWithTone { str := string(e) + if len(tones) == 0 { + tones = []Tone{Default} + } + for _, tone := range tones { - str = strings.Replace(str, "@", string(tone), 1) + str = strings.Replace(str, "@", tone.String(), 1) } if strings.Count(str, "@") > 0 { lastTone := tones[len(tones)-1] - str = strings.ReplaceAll(str, "@", string(lastTone)) + str = strings.ReplaceAll(str, "@", lastTone.String()) } return EmojiWithTone(str)
M
emoji_test.go
→
emoji_test.go
@@ -1,17 +1,107 @@
package emoji import ( - "fmt" "testing" ) -func TestColor(t *testing.T) { - fmt.Printf("hello %v from %v\n", WavingHand, FlagsForFlagTurkey) - fmt.Printf("different skin tones. default: %v light: %v dark: %v\n", ThumbsUp, OkHand.Tone(Light), CallMeHand.Tone(Dark)) - fmt.Printf("emoji with multiple skins: %v\n", PeopleHoldingHands.Tone(Light, Dark)) - flag, err := CountryFlag("tr") - if err != nil { - t.Fatal(err) +func TestEmoji(t *testing.T) { + tt := []struct { + input Emoji + expected string + }{ + {input: GrinningFace, expected: "\U0001F600"}, + {input: EyeInSpeechBubble, expected: "\U0001F441\uFE0F\u200D\U0001F5E8\uFE0F"}, + {input: ManGenie, expected: "\U0001F9DE\u200D\u2642\uFE0F"}, + {input: Badger, expected: "\U0001F9A1"}, + {input: FlagsForFlagTurkey, expected: "\U0001F1F9\U0001F1F7"}, + } + + for i, tc := range tt { + got := tc.input.String() + if got != tc.expected { + t.Fatalf("test case %v fail: got: %v, expected: %v", i+1, got, tc.expected) + } } - fmt.Println(flag) +} + +func TestEmojiWithTone(t *testing.T) { + tt := []struct { + input EmojiWithTone + tone Tone + expected string + }{ + {input: WavingHand, tone: Tone(""), expected: "\U0001F44B"}, + {input: WavingHand, tone: Default, expected: "\U0001F44B"}, + {input: WavingHand, tone: Light, expected: "\U0001F44B\U0001F3FB"}, + {input: WavingHand, tone: MediumLight, expected: "\U0001F44B\U0001F3FC"}, + {input: WavingHand, tone: Medium, expected: "\U0001F44B\U0001F3FD"}, + {input: WavingHand, tone: MediumDark, expected: "\U0001F44B\U0001F3FE"}, + {input: WavingHand, tone: Dark, expected: "\U0001F44B\U0001F3FF"}, + } + + for i, tc := range tt { + got := tc.input.Tone(tc.tone).String() + if got != tc.expected { + t.Fatalf("test case %v fail: got: %v, expected: %v", i+1, got, tc.expected) + } + } +} + +func TestEmojiWithToneTwo(t *testing.T) { + tt := []struct { + input EmojiWithTone + tones []Tone + expected string + }{ + {input: WomanAndManHoldingHandsWithTwoTone, tones: []Tone{}, expected: "\U0001F469\u200D\U0001F91D\u200D\U0001F468"}, + {input: WomanAndManHoldingHandsWithTwoTone, tones: []Tone{MediumLight}, expected: "\U0001F469\U0001F3FC\u200D\U0001F91D\u200D\U0001F468\U0001F3FC"}, + {input: WomanAndManHoldingHandsWithTwoTone, tones: []Tone{Medium, Dark}, expected: "\U0001F469\U0001F3FD\u200D\U0001F91D\u200D\U0001F468\U0001F3FF"}, + } + + for i, tc := range tt { + got := tc.input.Tone(tc.tones...).String() + if got != tc.expected { + t.Fatalf("test case %v fail: got: %v, expected: %v", i+1, got, tc.expected) + } + } +} + +func TestCountryFlag(t *testing.T) { + tt := []struct { + input string + expected Emoji + }{ + {input: "tr", expected: FlagsForFlagTurkey}, + {input: "TR", expected: FlagsForFlagTurkey}, + {input: "us", expected: FlagsForFlagUnitedStates}, + {input: "gb", expected: FlagsForFlagUnitedKingdom}, + } + + for i, tc := range tt { + got, err := CountryFlag(tc.input) + if err != nil { + t.Fatalf("test case %v fail: %v", i+1, err) + } + if got != tc.expected { + t.Fatalf("test case %v fail: got: %v, expected: %v", i+1, got, tc.expected) + } + } +} + +func TestCountryFlagError(t *testing.T) { + tt := []struct { + input string + fail bool + }{ + {input: "tr", fail: false}, + {input: "a", fail: true}, + {input: "tur", fail: true}, + } + + for i, tc := range tt { + _, err := CountryFlag(tc.input) + if (err != nil) != tc.fail { + t.Fatalf("test case %v fail: %v", i+1, err) + } + } }
A
go.sum