public void EnumerableConverter_DetectTargetItemTypeTest() { var converter = new EnumerableConverter() { ItemConverter = new ToStringConverter() }; TestEnumerableConverter <IList <string> >(converter); TestEnumerableConverter <ICollection <string> >(converter); TestEnumerableConverter <IEnumerable <string> >(converter); TestEnumerableConverter <List <string> >(converter); TestEnumerableConverter <Collection <string> >(converter); TestEnumerableConverter <ObservableCollection <string> >(converter); TestEnumerableConverter <ReadOnlyCollection <string> >(converter); }
public void EnumerableConverter_ConvertInfiniteEnumerableTest() { foreach (var targetType in new Type[] { null, typeof(IEnumerable <string>) }) { var converter = new EnumerableConverter() { TargetItemType = typeof(string), ItemConverter = new ToStringConverter() }; var result = (IEnumerable <string>)converter.Convert(GetInfiniteEnumerable(), null, null, null); string[] array500 = result.Take(500).ToArray(); for (int i = 0; i < 500; ++i) { Assert.AreEqual(i.ToString(), array500[i]); } } }
public void ShouldCallConverterChooserWithValuesAndReturnCreatedList() { var converterChooserMock = new Mock <ISettingsConverterChooser>(); converterChooserMock.Setup(x => x.ChooseAndConvert <string, int>(It.IsAny <string>())).Returns <string>(str => int.Parse(str, CultureInfo.InvariantCulture)); var converter = new EnumerableConverter(converterChooserMock.Object); var expectedList = new List <int> { 1, 2 }; var rawValue = new string[] { "1", "2" }; var convertedList = converter.Convert <string[], ICollection <int> >(rawValue); converterChooserMock.Verify(x => x.ChooseAndConvert <string, int>(rawValue[0]), Times.Once); converterChooserMock.Verify(x => x.ChooseAndConvert <string, int>(rawValue[1]), Times.Once); CollectionAssert.AreEqual(expectedList, (System.Collections.ICollection)convertedList); }
public void ConvertTest() { var converter = new EnumerableConverter(); var typeConverterOptions = new TypeConverterOptions { CultureInfo = CultureInfo.CurrentCulture }; try { converter.ConvertFromExcel(typeConverterOptions, ""); Assert.Fail(); } catch (ExcelTypeConverterException) { } try { converter.ConvertToExcel(typeConverterOptions, 5); Assert.Fail(); } catch (ExcelTypeConverterException) { } }
public void EnumerableConverter_ConvertToCollectionTest() { var converter = new EnumerableConverter() { TargetItemType = typeof(string), ItemConverter = new ToStringConverter() }; TestEnumerableConverter <IList <string> >(converter); TestEnumerableConverter <ICollection <string> >(converter); TestEnumerableConverter <IEnumerable <string> >(converter); TestEnumerableConverter <List <string> >(converter); TestEnumerableConverter <Collection <string> >(converter); TestEnumerableConverter <ObservableCollection <string> >(converter); TestEnumerableConverter <ReadOnlyCollection <string> >(converter); TestEnumerableConverter <StringCollection>(converter); TestEnumerableConverter <ArrayList>(converter); TestEnumerableConverter <Queue>(converter); TestEnumerableConverter <Queue <string> >(converter); }
public void ShouldCallConverterChooserWithValuesAndReturnCreatedSet() { var converterChooserMock = new Mock <ISettingsConverterChooser>(); converterChooserMock.Setup(x => x.ChooseAndConvert <string, int>(It.IsAny <string>())).Returns <string>(str => int.Parse(str, CultureInfo.InvariantCulture)); var converter = new EnumerableConverter(converterChooserMock.Object); var expectedList = new HashSet <int> { 1, 2 }; var rawValue = new ConcurrentBag <string> { "1", "2", "2" }; var convertedSet = converter.Convert <ConcurrentBag <string>, ISet <int> >(rawValue); converterChooserMock.Verify(x => x.ChooseAndConvert <string, int>(It.IsAny <string>()), Times.Exactly(3)); CollectionAssert.AllItemsAreUnique(convertedSet.ToList()); CollectionAssert.AreEquivalent(expectedList.ToList(), convertedSet.ToList()); }
public void Setup() { var enumerableConverter = new EnumerableConverter <ItemType, JsonStringEnumConverter>(); using var ms = new MemoryStream(); using (var writer = new Utf8JsonWriter(ms)) { var itemTypeList = new List <ItemType> { ItemType.Unknown, ItemType.Address }; writer.WriteStartArray(); enumerableConverter.Write(writer, itemTypeList, new JsonSerializerOptions()); writer.WriteEndArray(); } _result = System.Text.Encoding.UTF8.GetString(ms.ToArray()); }
/// <summary> /// returns files in a directory according to the specified search pattern /// </summary> /// <param name="searchPattern"></param> /// <param name="searchOption"></param> /// <param name="maxCount"></param> /// <param name="nextPageId"></param> /// <returns></returns> public IEnumerable <ObjectStorageFileInfo> EnumerateFiles(string searchPattern, SearchOption searchOption, int maxCount, string nextPageId) { IEnumerable <ObjectStorageFileInfo> files = null; if (String.IsNullOrEmpty(BucketName)) { files = new List <ObjectStorageFileInfo>(); } else { var metaFiles = GetOciObjects(ObjectKey, searchOption, maxCount, nextPageId).Where(f => !f.Name.EndsWith("/")).ToList(); files = new EnumerableConverter <ObjectSummary, ObjectStorageFileInfo> (metaFiles, o => new ObjectStorageFileInfo(Client, NamespaceName, BucketName, ObjectStorageHelper.DecodeKey(o.Name))); } var regEx = WildcardToRegex(searchPattern); files = files.Where(o => Regex.IsMatch(o.Name, regEx, RegexOptions.IgnoreCase)); return(files); }
public void Setup() { var enumerableConverter = new EnumerableConverter <ItemType, JsonStringEnumConverter>(); using var ms = new MemoryStream(); using (var writer = new Utf8JsonWriter(ms)) { writer.WriteStartArray(); writer.WriteStringValue("Unknown"); writer.WriteStringValue("Address"); writer.WriteEndArray(); } var json = System.Text.Encoding.UTF8.GetString(ms.ToArray()); var reader = new Utf8JsonReader(ms.ToArray()); reader.Read(); _result = enumerableConverter.Read(ref reader, typeof(IEnumerable <ItemType>), new JsonSerializerOptions()); }
public void ShouldCallConverterChooserWithValuesAndReturnCreatedDict() { var converterChooserMock = new Mock <ISettingsConverterChooser>(); converterChooserMock .Setup(x => x.ChooseAndConvert <KeyValuePair <string, string>, KeyValuePair <long, int> >(It.IsAny <KeyValuePair <string, string> >())) .Returns <KeyValuePair <string, string> >(kvp => new KeyValuePair <long, int>(long.Parse(kvp.Key, CultureInfo.InvariantCulture), int.Parse(kvp.Value, CultureInfo.InvariantCulture))); var converter = new EnumerableConverter(converterChooserMock.Object); var expectedDict = new Dictionary <long, int> { { 1, 1 }, { 2, 2 } }; var rawValue = new KeyValuePair <string, string>[] { new KeyValuePair <string, string>("1", "1"), new KeyValuePair <string, string>("2", "2"), }; var convertedDict = converter.Convert <KeyValuePair <string, string>[], IReadOnlyDictionary <long, int> >(rawValue); converterChooserMock.Verify(x => x.ChooseAndConvert <KeyValuePair <string, string>, KeyValuePair <long, int> >(It.IsAny <KeyValuePair <string, string> >()), Times.Exactly(2)); CollectionAssert.AreEqual(new KeyValuePair <long, int> [0], expectedDict.Except(convertedDict).ToList()); }
/// <summary> /// Converts the specified string to its <typeparamref name="T"/> equivalent using the specified <paramref name="context"/> and <paramref name="culture"/> information. /// </summary> /// <typeparam name="T">The type of the expected return <paramref name="value"/> after conversion.</typeparam> /// <param name="value">The string value to convert.</param> /// <param name="culture">The culture-specific formatting information about <paramref name="value"/>.</param> /// <param name="context">The type-specific formatting information about <paramref name="value"/>.</param> /// <returns>An object that is equivalent to <typeparamref name="T"/> contained in <paramref name="value"/>, as specified by <paramref name="culture"/> and <paramref name="context"/>.</returns> /// <exception cref="ArgumentException"> /// Invalid <paramref name="value"/> for <typeparamref name="T"/> specified. /// </exception> /// <exception cref="NotSupportedException"> /// The conversion cannot be performed. /// </exception> public static T FromString <T>(string value, CultureInfo culture, ITypeDescriptorContext context) { try { Type resultType = typeof(T); TypeConverter converter = TypeDescriptor.GetConverter(resultType); T result = (T)converter.ConvertFromString(context, culture, value); if (resultType == typeof(Uri)) // for reasons unknown to me, MS allows all sorts of string to be constructed on a Uri - check if valid (quick-fix until more knowledge of ITypeDescriptorContext) { Uri resultAsUri = result as Uri; string[] segments = resultAsUri?.Segments; } return(result); } catch (Exception ex) { if (ex.GetType() == typeof(NotSupportedException)) { throw; } throw ExceptionUtility.Refine(ExceptionUtility.CreateArgumentException(nameof(value), ex.Message, ex.InnerException), MethodBaseConverter.FromType(typeof(Converter), EnumerableConverter.AsArray(typeof(string), typeof(CultureInfo), typeof(ITypeDescriptorContext))), value, culture, context).Unwrap(); } }
public void EnumerableConverter_CanConvertString_IsFalse() { var converter = new EnumerableConverter <Image>(); Assert.IsFalse(converter.CanConvert(typeof(string))); }
/// <summary> /// Computes a hash value of the specified <paramref name="value"/>. /// </summary> /// <param name="value">The object to compute a hash code for.</param> /// <param name="setup">The <see cref="HashOptions"/> which need to be configured.</param> /// <returns>A <see cref="HashResult"/> containing the computed hash value of the specified <paramref name="value"/>.</returns> public static HashResult ComputeHash(object value, Action <HashOptions> setup = null) { var options = setup.ConfigureOptions(); return(ComputeHash(EnumerableConverter.AsArray(value), o => o.AlgorithmType = options.AlgorithmType)); }
/// <summary> /// Combines the specified form-data <paramref name="values"/> into one <see cref="NameValueCollection"/> equivalent. /// </summary> /// <param name="values">A variable number of form-data <paramref name="values"/>.</param> /// <returns>A <see cref="NameValueCollection"/> equivalent to the combined form-data <paramref name="values"/>.</returns> public static NameValueCollection Combine(params string[] values) { Validator.ThrowIfNull(values, nameof(values)); return(Combine(EnumerableConverter.Parse(values, FormConverter.FromString).ToArray())); }
/// <summary> /// Adds the specified <paramref name="key"/> and <paramref name="value"/> to the cache. /// </summary> /// <param name="key">The cache key used to identify the item.</param> /// <param name="value">The object to be inserted in the cache.</param> /// <param name="group">The group to associate the <paramref name="key"/> with.</param> /// <param name="dependencies">The dependencies for the <paramref name="value"/>. When any dependency changes, the <paramref name="value"/> becomes invalid and is removed from the cache.</param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="key"/> is null. /// </exception> /// <remarks> /// This method will not throw an <see cref="ArgumentException"/> in case of an existing cache item whose key matches the key parameter. /// </remarks> public void Add(string key, object value, string group, params IDependency[] dependencies) { Add(key, value, group, EnumerableConverter.FromArray(dependencies)); }
/// <summary> /// Gets the most significant (largest) value of either <see cref="Created"/> or <see cref="Modified"/>. /// </summary> /// <returns>The most significant (largest) value of either <see cref="Created"/> or <see cref="Modified"/>.</returns> public DateTime GetMostSignificant() { return(EnumerableConverter.FromArray(Created, Modified ?? DateTime.MinValue).Max()); }
internal ConvertingEnumerator(EnumerableConverter <T, U> ec) { getT = ec.baseEnum.GetEnumerator(); convert = ec.converter; }