Skip to content

Commit

Permalink
Language names and make codes unique
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshKeegan committed Aug 4, 2016
1 parent b28eb60 commit 690bda0
Show file tree
Hide file tree
Showing 7 changed files with 280 additions and 235 deletions.
1 change: 1 addition & 0 deletions Testinvi/Testinvi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
<Compile Include="SetupHelpers\UserQuerValidatorHelper.cs" />
<Compile Include="SetupHelpers\UserQueryGeneratorHelper.cs" />
<Compile Include="Tweetinvi.Core\EnumerableExtensionsTests.cs" />
<Compile Include="Tweetinvi.Core\LanguageTests.cs" />
<Compile Include="Tweetinvi.Logic\ExtendedTweetTests.cs" />
<Compile Include="Tweetinvi.Core\StringExtensionsTests.cs" />
<Compile Include="Tweetinvi.Core\TokenRateLimitsTests.cs" />
Expand Down
41 changes: 41 additions & 0 deletions Testinvi/Tweetinvi.Core/LanguageTests.cs
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);
}
}
}
}
}
}
31 changes: 23 additions & 8 deletions Tweetinvi.Core/Core/Attributes/LanguageAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,46 @@ namespace Tweetinvi.Core.Attributes
/// </summary>
public class LanguageAttribute : Attribute
{
/// <summary>
/// Name of the language in English
/// </summary>
public string Name { get; private set; }

/// <summary>
/// Primary language code.
/// </summary>
public string Language { get; private set; }
public string Code { get; private set; }

/// <summary>
/// All available language codes.
/// </summary>
public string[] Languages { get; private set; }
public string[] Codes { get; private set; }

/// <summary>
/// Does Twitter represent this language with different codes.
/// </summary>
public bool HasMultipleCodes { get; private set; }

public LanguageAttribute(params string[] languages)
public LanguageAttribute(string name, params string[] codes)
{
if (languages == null || languages.Length == 0)
// Validation
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
if (name.Trim() == "")
{
throw new ArgumentException("name must not be whitespace", nameof(name));
}
if (codes == null || codes.Length == 0)
{
throw new ArgumentException("You must specify a language code to a Language");
throw new ArgumentException("You must specify a language code to a Language", nameof(codes));
}

Language = languages[0];
Languages = languages;
HasMultipleCodes = languages.Length > 1;
Name = name;
Code = codes[0];
Codes = codes;
HasMultipleCodes = codes.Length > 1;
}
}
}
22 changes: 22 additions & 0 deletions Tweetinvi.Core/Core/Extensions/EnumExtensions.cs
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;
}
}
}
6 changes: 3 additions & 3 deletions Tweetinvi.Core/Core/Extensions/LanguageExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static string GetLanguageCode(this Language language)
{
var field = language.GetType().GetField(language.ToString());
var descriptionAttribute = (LanguageAttribute)Attribute.GetCustomAttribute(field, typeof(LanguageAttribute));
return descriptionAttribute != null ? descriptionAttribute.Language : language.ToString();
return descriptionAttribute != null ? descriptionAttribute.Code : language.ToString();
}

public static Language GetLangFromDescription(string descriptionValue)
Expand Down Expand Up @@ -58,11 +58,11 @@ private static bool IsValidDescriptionField(string descriptionValue, FieldInfo f
var attribute = ((LanguageAttribute) descriptionAttribute);
if (!attribute.HasMultipleCodes)
{
return attribute.Language == descriptionValue;
return attribute.Code == descriptionValue;
}
else
{
return attribute.Languages.Any(x => x == descriptionValue);
return attribute.Codes.Any(x => x == descriptionValue);
}
}
}
Expand Down
Loading

0 comments on commit 690bda0

Please sign in to comment.