forked from linvi/tweetinvi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Language names and make codes unique
- Loading branch information
1 parent
b28eb60
commit 690bda0
Showing
7 changed files
with
280 additions
and
235 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Tweetinvi.Core.Attributes; | ||
using Tweetinvi.Core.Extensions; | ||
using Tweetinvi.Models; | ||
|
||
namespace Testinvi.Tweetinvi.Core | ||
{ | ||
[TestClass] | ||
public class LanguageTests | ||
{ | ||
[TestMethod] | ||
public void AllCodesAreUniqueToOneLanguage() | ||
{ | ||
// Test that each language code only appears against one enum entry. | ||
// If more than one entry had the same code, the behaviour of which one will be returned | ||
// is undefined, so lets avoid it. | ||
Dictionary<string, Language> taken = new Dictionary<string, Language>(); | ||
|
||
foreach (Language lang in Enum.GetValues(typeof(Language))) | ||
{ | ||
LanguageAttribute attr = lang.GetAttributeOfType<LanguageAttribute>(); | ||
|
||
foreach (string code in attr.Codes) | ||
{ | ||
// Rather than using assert, conditional & write details to console, making fixing any problem easier | ||
if (taken.ContainsKey(code)) | ||
{ | ||
Console.WriteLine("Code {0} is against multiple languages: {1} & {2}", code, taken[code], lang); | ||
Assert.Fail(); | ||
} | ||
else | ||
{ | ||
taken.Add(code, lang); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System; | ||
using System.Reflection; | ||
|
||
namespace Tweetinvi.Core.Extensions | ||
{ | ||
public static class EnumExtensions | ||
{ | ||
/// <summary> | ||
/// Gets an attribute on an enum field value | ||
/// </summary> | ||
/// <typeparam name="T">The type of the attribute you want to retrieve</typeparam> | ||
/// <param name="enumVal">The enum value</param> | ||
/// <returns>The attribute of type T that exists on the enum value</returns> | ||
public static T GetAttributeOfType<T>(this Enum enumVal) where T : Attribute | ||
{ | ||
Type type = enumVal.GetType(); | ||
MemberInfo[] memInfo = type.GetMember(enumVal.ToString()); | ||
object[] attributes = memInfo[0].GetCustomAttributes(typeof(T), false); | ||
return attributes.Length > 0 ? (T)attributes[0] : null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.