public static Dictionary <int, string> GetLanguageNames( int displayLanguageLcid, List <string> includeOnlyCultureNames, DropDownFirstItemType dropDownFirstItemType = DropDownFirstItemType.None) { CultureInfo?backupCultureInfo = null; if (Thread.CurrentThread.CurrentUICulture.LCID != displayLanguageLcid) { backupCultureInfo = Thread.CurrentThread.CurrentUICulture; Thread.CurrentThread.CurrentUICulture = new CultureInfo(displayLanguageLcid); } var cultures = GetCultures(); var data = DataFactory.CreateKeyValueDictionaryOfIntString(dropDownFirstItemType); if (backupCultureInfo != null) { Thread.CurrentThread.CurrentUICulture = backupCultureInfo; } var languageDisplayNameCount = new Dictionary <string, int>(StringComparer.Ordinal); foreach (var culture in cultures) { if (includeOnlyCultureNames != null && includeOnlyCultureNames.Count > 0 && !includeOnlyCultureNames.Contains(culture.Name)) { continue; } if (languageDisplayNameCount.ContainsKey(culture.LanguageDisplayName)) { languageDisplayNameCount[culture.LanguageDisplayName]++; } else { languageDisplayNameCount.Add(culture.LanguageDisplayName, 1); } } foreach (var culture in cultures.OrderBy(x => x.LanguageDisplayName)) { if (data.ContainsKey(culture.Lcid)) { continue; } if (includeOnlyCultureNames == null || includeOnlyCultureNames.Count == 0 || includeOnlyCultureNames.Contains(culture.Name)) { var text = languageDisplayNameCount[culture.LanguageDisplayName] > 1 ? $"{culture.LanguageDisplayName} ({culture.CountryDisplayName})" : culture.LanguageDisplayName; data.Add(culture.Lcid, text); } } return(data); }
/// <summary> /// Create the key/value dictionary of global identifier and string. /// </summary> /// <param name="dropDownFirstItemType">Type of the drop down first item.</param> /// <returns> /// The Dictionary. /// </returns> public static Dictionary <Guid, string> CreateKeyValueDictionaryOfGuidString(DropDownFirstItemType dropDownFirstItemType = DropDownFirstItemType.None) { var dictionary = new Dictionary <Guid, string>(); switch (dropDownFirstItemType) { case DropDownFirstItemType.None: break; case DropDownFirstItemType.Blank: dictionary.Add( DropDownFirstItemTypeHelper.GetEnumGuid(DropDownFirstItemType.Blank), string.Empty); break; case DropDownFirstItemType.PleaseSelect: dictionary.Add( DropDownFirstItemTypeHelper.GetEnumGuid(DropDownFirstItemType.PleaseSelect), EnumResources.DropDownFirstItemTypePleaseSelect); break; case DropDownFirstItemType.IncludeAll: dictionary.Add( DropDownFirstItemTypeHelper.GetEnumGuid(DropDownFirstItemType.IncludeAll), EnumResources.DropDownFirstItemTypeIncludeAll); break; default: throw new SwitchCaseDefaultException(dropDownFirstItemType); } return(dictionary); }
public void ConvertEnumToArray <T>( T dummyForT, int expectedCount, DropDownFirstItemType dropDownFirstItemType, bool useDescriptionAttribute, bool includeDefault, SortDirectionType sortDirectionType, bool byFlagIncludeBase, bool byFlagIncludeCombined) where T : Enum { // Arrange var enumType = dummyForT.GetType(); // Act var actual = EnumHelper.ConvertEnumToArray( enumType, dropDownFirstItemType, useDescriptionAttribute, includeDefault, sortDirectionType, byFlagIncludeBase, byFlagIncludeCombined); // Assert actual.Should().NotBeNull().And.HaveCount(expectedCount); }
public void GetEnumGuid(string expected, DropDownFirstItemType input) { // Act var actual = DropDownFirstItemTypeHelper.GetEnumGuid(input); // Assert Assert.Equal(expected, actual.ToString().ToUpperInvariant()); }
public static List <string> EnsureFirstItemType(List <string> list, DropDownFirstItemType dropDownFirstItemType) { if (list == null) { throw new ArgumentNullException(nameof(list)); } var s = list.FirstOrDefault(x => x.Equals("None", StringComparison.Ordinal)); if (s != null) { list.Remove(s); } switch (dropDownFirstItemType) { case DropDownFirstItemType.None: break; case DropDownFirstItemType.Blank: s = list.FirstOrDefault(x => x == string.Empty); if (s != null) { list.Remove(s); } list.Insert(0, string.Empty); break; case DropDownFirstItemType.PleaseSelect: s = list.FirstOrDefault(x => x == EnumResources.DropDownFirstItemTypePleaseSelect); if (s != null) { list.Remove(s); } list.Insert(0, EnumResources.DropDownFirstItemTypePleaseSelect); break; case DropDownFirstItemType.IncludeAll: s = list.FirstOrDefault(x => x == EnumResources.DropDownFirstItemTypeIncludeAll); if (s != null) { list.Remove(s); } list.Insert(0, EnumResources.DropDownFirstItemTypeIncludeAll); break; default: throw new SwitchCaseDefaultException(dropDownFirstItemType); } return(list); }
public void GetItemFromEnumGuid(DropDownFirstItemType expected, string input) { // Arrange var guid = Guid.Parse(input); // Act var actual = DropDownFirstItemTypeHelper.GetItemFromEnumGuid(guid); // Assert Assert.Equal(expected, actual); }
public void GetLanguageNames_IncludeOnlyNames(int expected, DropDownFirstItemType dropDownFirstItemType, string[] includeNames) { // Arrange var inputList = includeNames.ToList(); // Act var actual = CultureHelper.GetLanguageNames(inputList, dropDownFirstItemType); // Assert actual.Should().NotBeNull() .And.BeOfType <Dictionary <int, string> >() .And.HaveCount(expected); }
public void GetLanguageNames(int expectedExtraOnCultureCount, DropDownFirstItemType input) { // Arrange var culturesCount = CultureHelper.GetCultures().Count; // Act var actual = CultureHelper.GetLanguageNames(input); // Assert actual.Should().NotBeNull() .And.BeOfType <Dictionary <int, string> >() .And.HaveCount(culturesCount + expectedExtraOnCultureCount); }
/// <summary> /// Gets the language names. /// </summary> /// <param name="displayLanguageLcid">The display language lcid.</param> /// <param name="includeOnlyLcids">The include only lcids.</param> /// <param name="dropDownFirstItemType">Type of the drop down first item.</param> public static Dictionary <int, string> GetLanguageNames( int displayLanguageLcid, List <int>?includeOnlyLcids, DropDownFirstItemType dropDownFirstItemType = DropDownFirstItemType.None) { if (includeOnlyLcids == null || includeOnlyLcids.Count == 0) { return(GetLanguageNames(displayLanguageLcid, dropDownFirstItemType)); } var includeOnlyCultureNames = includeOnlyLcids.Select(lcid => new CultureInfo(lcid).Name).ToList(); return(GetLanguageNames(displayLanguageLcid, includeOnlyCultureNames, dropDownFirstItemType)); }
public void EnsureFirstItemType(int expectedCount, string expectedFirstItem, DropDownFirstItemType input) { // Arrange var list = new List <string> { "John", "Bob", }; // Act var actual = DropDownFirstItemTypeHelper.EnsureFirstItemType(list, input); // Assert actual.Should().HaveCount(expectedCount); Assert.Equal(expectedFirstItem, actual.First()); }
/// <summary> /// Gets the enumeration GUID. /// </summary> /// <param name="dropDownFirstItemType">Type of the drop down first item.</param> /// <returns> /// The <see cref="Guid" />. /// </returns> /// <exception cref="System.ArgumentException"> /// Enumeration ' + dropDownFirstItemType + ' has no EnumGuid defined. /// </exception> public static Guid GetEnumGuid(DropDownFirstItemType dropDownFirstItemType) { var type = dropDownFirstItemType.GetType(); var memberInfos = type.GetMember(dropDownFirstItemType.ToString()); if (memberInfos.Length > 0) { var attrs = memberInfos[0].GetCustomAttributes(typeof(EnumGuidAttribute), false); if (attrs.Length > 0) { return(((EnumGuidAttribute)attrs[0]).GlobalIdentifier); } } throw new ArgumentException("Enumeration '" + dropDownFirstItemType + "' has no EnumGuid defined."); }
/// <summary> /// Create the key/value data table of global identifier and string. /// </summary> /// <param name="dropDownFirstItemType">Type of the drop down first item.</param> /// <returns> /// The <see cref="DataTable" />. /// </returns> public static DataTable CreateKeyValueDataTableOfGuidString(DropDownFirstItemType dropDownFirstItemType = DropDownFirstItemType.None) { var dt = new DataTable { Locale = CultureInfo.InvariantCulture, }; dt.Columns.Add("key", typeof(Guid)); dt.Columns.Add("value", typeof(string)); dt.PrimaryKey = new[] { dt.Columns["key"] }; dt.AcceptChanges(); DataRow dr; switch (dropDownFirstItemType) { case DropDownFirstItemType.None: break; case DropDownFirstItemType.Blank: dr = dt.NewRow(); dr["key"] = DropDownFirstItemTypeHelper.GetEnumGuid(DropDownFirstItemType.Blank); dr["value"] = string.Empty; dt.Rows.Add(dr); break; case DropDownFirstItemType.PleaseSelect: dr = dt.NewRow(); dr["key"] = DropDownFirstItemTypeHelper.GetEnumGuid(DropDownFirstItemType.PleaseSelect); dr["value"] = EnumResources.DropDownFirstItemTypePleaseSelect; dt.Rows.Add(dr); break; case DropDownFirstItemType.IncludeAll: dr = dt.NewRow(); dr["key"] = DropDownFirstItemTypeHelper.GetEnumGuid(DropDownFirstItemType.IncludeAll); dr["value"] = EnumResources.DropDownFirstItemTypeIncludeAll; dt.Rows.Add(dr); break; default: throw new SwitchCaseDefaultException(dropDownFirstItemType); } return(dt); }
public void ToArray <T>( T dummyForT, int expectedCount, DropDownFirstItemType dropDownFirstItemType, bool useDescriptionAttribute, bool includeDefault, SortDirectionType sortDirectionType, bool byFlagIncludeBase, bool byFlagIncludeCombined) where T : Enum { // ReSharper disable once UnusedVariable object dummyAssignment = dummyForT; // Act var actual = Enum <T> .ToArray(dropDownFirstItemType, useDescriptionAttribute, includeDefault, sortDirectionType, byFlagIncludeBase, byFlagIncludeCombined); // Assert actual.Should().NotBeNull().And.HaveCount(expectedCount); }
/// <summary> /// Creates the key value dictionary of string string. /// </summary> /// <param name="dropDownFirstItemType">Type of the drop down first item.</param> /// <returns> /// The Dictionary. /// </returns> public static Dictionary <string, string> CreateKeyValueDictionaryOfStringString(DropDownFirstItemType dropDownFirstItemType = DropDownFirstItemType.None) { var dictionary = new Dictionary <string, string>(StringComparer.Ordinal); switch (dropDownFirstItemType) { case DropDownFirstItemType.None: break; case DropDownFirstItemType.Blank: dictionary.Add( DropDownFirstItemType.Blank.ToString(), string.Empty); break; case DropDownFirstItemType.PleaseSelect: dictionary.Add( DropDownFirstItemType.PleaseSelect.ToString(), EnumResources.DropDownFirstItemTypePleaseSelect); break; case DropDownFirstItemType.IncludeAll: dictionary.Add( DropDownFirstItemType.IncludeAll.ToString(), EnumResources.DropDownFirstItemTypeIncludeAll); break; default: throw new SwitchCaseDefaultException(dropDownFirstItemType); } return(dictionary); }
/// <summary> /// Gets the language names. /// </summary> /// <param name="includeOnlyCultureNames">The include only culture names.</param> /// <param name="dropDownFirstItemType">Type of the drop down first item.</param> public static Dictionary <int, string> GetLanguageNames( List <string> includeOnlyCultureNames, DropDownFirstItemType dropDownFirstItemType = DropDownFirstItemType.None) { return(GetLanguageNames(Thread.CurrentThread.CurrentUICulture.LCID, includeOnlyCultureNames, dropDownFirstItemType)); }
/// <summary> /// Gets the language names. /// </summary> /// <param name="displayLanguageLcid">The display language lcid.</param> /// <param name="dropDownFirstItemType">Type of the drop down first item.</param> public static Dictionary <int, string> GetLanguageNames( int displayLanguageLcid, DropDownFirstItemType dropDownFirstItemType = DropDownFirstItemType.None) { return(GetLanguageNames(displayLanguageLcid, new List <string>(), dropDownFirstItemType)); }
/// <summary> /// Gets the language names. /// </summary> /// <param name="dropDownFirstItemType">Type of the drop down first item.</param> public static Dictionary <int, string> GetLanguageNames( DropDownFirstItemType dropDownFirstItemType = DropDownFirstItemType.None) { return(GetLanguageNames(Thread.CurrentThread.CurrentUICulture.LCID, new List <string>(), dropDownFirstItemType)); }
/// <summary> /// Gets the country names. /// </summary> /// <param name="includeOnlyLcids">The include only lcids.</param> /// <param name="dropDownFirstItemType">Type of the drop down first item.</param> public static Dictionary <int, string> GetCountryNames( List <int> includeOnlyLcids, DropDownFirstItemType dropDownFirstItemType = DropDownFirstItemType.None) { return(GetCountryNames(Thread.CurrentThread.CurrentUICulture.LCID, includeOnlyLcids, dropDownFirstItemType)); }
public void CreateKeyValueDataTableOfGuidString_DropDownFirstItemType(int expectedCount, DropDownFirstItemType dropDownFirstItemType) { // Act var actual = DataFactory.CreateKeyValueDataTableOfGuidString(dropDownFirstItemType); // Assert actual.Should() .NotBeNull() .And.BeOfType <DataTable>(); actual.Rows.Should().HaveCount(expectedCount); }
public void GetCountryNames_DisplayLanguageLcid(int expectedExtraOnCultureCount, DropDownFirstItemType dropDownFirstItemType, int displayLanguageLcid) { // Arrange var culturesCount = CultureHelper.GetCultures().Count; // Act var actual = CultureHelper.GetCountryNames(displayLanguageLcid, dropDownFirstItemType); // Assert actual.Should().NotBeNull() .And.BeOfType <Dictionary <int, string> >() .And.HaveCount(culturesCount + expectedExtraOnCultureCount); }
public void CreateKeyValueDictionaryOfStringString_DropDownFirstItemType(int expectedCount, DropDownFirstItemType dropDownFirstItemType) { // Act var actual = DataFactory.CreateKeyValueDictionaryOfStringString(dropDownFirstItemType); // Assert actual.Should() .NotBeNull() .And.BeOfType <Dictionary <string, string> >(); actual.Should().HaveCount(expectedCount); }