From ac9b575230f1504725ba4ba6fe64f9def5c61d85 Mon Sep 17 00:00:00 2001 From: Christopher F Date: Tue, 15 Aug 2017 18:25:40 -0400 Subject: [PATCH] Clean up Emoji validation --- src/Discord.Net.Core/Entities/Emotes/Emoji.cs | 24 ++++++++++------------ test/Discord.Net.Tests/Tests.Emotes.cs | 29 +++++++++++++++++++++++---- 2 files changed, 36 insertions(+), 17 deletions(-) diff --git a/src/Discord.Net.Core/Entities/Emotes/Emoji.cs b/src/Discord.Net.Core/Entities/Emotes/Emoji.cs index 2106c2bac..4feb4ea6e 100644 --- a/src/Discord.Net.Core/Entities/Emotes/Emoji.cs +++ b/src/Discord.Net.Core/Entities/Emotes/Emoji.cs @@ -23,13 +23,14 @@ namespace Discord /// The pure UTF-8 encoding of an emoji public Emoji(string unicode) { - byte[] utf32 = Encoding.Unicode.GetBytes(unicode); - bool safe = false, any = false; - int codepoint = 0; + // NETStandard1.1 doesn't support UTF32 +#if !NETSTANDARD1_1 + byte[] utf32 = Encoding.UTF32.GetBytes(unicode); for (var i = 0; i < utf32.Length; i += 4) { - codepoint = BitConverter.ToInt32(utf32, i); - any = false; + int codepoint = BitConverter.ToInt32(utf32, i); + bool any = false; + for (var j = 0; j < Codepoints.Length; j++) { if (Codepoints[j] == codepoint) @@ -38,16 +39,13 @@ namespace Discord break; } } - if (any) - safe = true; + + if (any) continue; else - { - safe = false; - break; - } + throw new ArgumentException("One or more characters was not a valid Emoji", nameof(unicode)); } - if (!safe) - throw new ArgumentException("One or more characters was not a valid Emoji", nameof(unicode)); +#endif + Name = unicode; } diff --git a/test/Discord.Net.Tests/Tests.Emotes.cs b/test/Discord.Net.Tests/Tests.Emotes.cs index 986de20d1..740c2849d 100644 --- a/test/Discord.Net.Tests/Tests.Emotes.cs +++ b/test/Discord.Net.Tests/Tests.Emotes.cs @@ -5,11 +5,32 @@ namespace Discord { public class EmoteTests { - [Fact] - public void Emoji() + const string Smiley = "\U0001F603"; + const string Man = "\U0001F468"; + const string Woman = "\U0001F469"; + const string Girl = "\U0001F467"; + const string Boy = "\U0001F466"; + const string Join = "\u200D"; + + [Fact] + public void Single_Emoji() + { + Assert.Equal(Smiley, new Emoji(Smiley).Name); + Assert.Equal(Man, new Emoji(Man).Name); + Assert.Equal(Woman, new Emoji(Woman).Name); + Assert.Equal(Girl, new Emoji(Girl).Name); + Assert.Equal(Boy, new Emoji(Boy).Name); + } + [Fact] + public void Multipart_Emoji() + { + string family = string.Concat(Man, Join, Woman, Join, Girl, Join, Boy); + Assert.Equal(family, new Emoji(family).Name); + } + [Fact] + public void Emoji_Fail() { - // Future: Validate emoji parsing - Assert.Equal("🦅", new Emoji("🦅").Name); + Assert.Throws(() => new Emoji("foxDab")); } [Fact]