public void ShouldRunAllDefaultCasts() { IConverterRegistry converterRegistry = new ConverterRegistry(); CastTestRunner.RunTests((testCase) => { // Arrange var value = CastTestRunner.GenerateValueForType(testCase.SourceType); var generatedTestSuccessful = CastTestRunner.CastValueWithGeneratedCode(value, testCase.SourceType, testCase.TargetType, testCase.CastFlag); // Act var convertedObject = converterRegistry.TryConvert( sourceType: testCase.SourceType, targetType: testCase.TargetType, value: value, defaultReturnValue: null); // Assert var castResult = new CastResult(convertedObject, testCase.CastFlag); var isSuccessful = CastTestRunner.AreEqual( this.testOutputHelper, testCase.SourceType, testCase.TargetType, generatedTestSuccessful, castResult, testCase.CastFlag); return(isSuccessful); }); }
/// <summary> /// Converts an object to the target type. /// </summary> /// <param name="sourceInstance">The object to convert to the target type.</param> /// <param name="targetType">The type to convert to.</param> /// <returns>The converted object.</returns> /// <remarks> /// <para> /// Converts an object to the target type. /// </para> /// </remarks> public static object ConvertTypeTo(object sourceInstance, Type targetType) { Type sourceType = sourceInstance.GetType(); // Check if we can assign directly from the source type to the target type if (targetType.IsAssignableFrom(sourceType)) { return(sourceInstance); } // Look for a TO converter IConvertTo tcSource = ConverterRegistry.GetConvertTo(sourceType, targetType); if (tcSource != null) { if (tcSource.CanConvertTo(targetType)) { return(tcSource.ConvertTo(sourceInstance, targetType)); } } // Look for a FROM converter IConvertFrom tcTarget = ConverterRegistry.GetConvertFrom(targetType); if (tcTarget != null) { if (tcTarget.CanConvertFrom(sourceType)) { return(tcTarget.ConvertFrom(sourceInstance)); } } throw new ArgumentException("Cannot convert source object [" + sourceInstance.ToString() + "] to target type [" + targetType.Name + "]", "sourceInstance"); }
public async Task WriteCsvFile() { await using var fileStream = new FileStream(_tempFilePath, FileMode.Create); await using var fileWriter = new StreamWriter(fileStream); for (var i = 0; i < LineCount; i++) { await fileWriter.WriteAsync(Line); } var stateMachine = new TokenizerStateMachine(StateHolder.DefaultConfiguration); _tokenizer = new StateMachineTokenizer(stateMachine); var map = new ColumnMapBuilder <BenchmarkDataClass>() .WithColumn(0, c => c.Field1) .WithColumn(1, c => c.Field2) .WithColumn(2, c => c.Field3) .WithColumn(3, c => c.Field4) .WithColumn(4, c => c.Field5) .WithColumn(5, c => c.Field6) .WithColumn(6, c => c.Field7) .Build(); _mapper = new MapperFactory <BenchmarkDataClass>(ConverterRegistry.CreateDefaultInstance()).CreateForMap(map); }
/// <summary> /// Checks if there is an appropriate type conversion from the source type to the target type. /// </summary> /// <param name="sourceType">The type to convert from.</param> /// <param name="targetType">The type to convert to.</param> /// <returns><c>true</c> if there is a conversion from the source type to the target type.</returns> /// <remarks> /// Checks if there is an appropriate type conversion from the source type to the target type. /// <para> /// </para> /// </remarks> public static bool CanConvertTypeTo(Type sourceType, Type targetType) { if (sourceType == null || targetType == null) { return(false); } // Check if we can assign directly from the source type to the target type if (targetType.IsAssignableFrom(sourceType)) { return(true); } // Look for a From converter IConvertFrom tcTarget = ConverterRegistry.GetConvertFrom(targetType); if (tcTarget != null) { if (tcTarget.CanConvertFrom(sourceType)) { return(true); } } return(false); }
/// <summary> /// Converts a string to an object. /// </summary> /// <param name="target">The target type to convert to.</param> /// <param name="txt">The string to convert to an object.</param> /// <returns> /// The object converted from a string or <c>null</c> when the /// conversion failed. /// </returns> /// <remarks> /// <para> /// Converts a string to an object. Uses the converter registry to try /// to convert the string value into the specified target type. /// </para> /// </remarks> public static object ConvertStringTo(Type target, string txt) { if ((object)target == null) { throw new ArgumentNullException("target"); } if ((object)typeof(string) == target || (object)typeof(object) == target) { return(txt); } IConvertFrom convertFrom = ConverterRegistry.GetConvertFrom(target); if (convertFrom != null && convertFrom.CanConvertFrom(typeof(string))) { return(convertFrom.ConvertFrom(txt)); } if (target.GetTypeInfo().IsEnum) { return(ParseEnum(target, txt, ignoreCase: true)); } return(target.GetMethod("Parse", new Type[1] { typeof(string) })?.Invoke(target, new string[1] { txt })); }
public void CustomConverterViaRegistry() { var input = new SerializationTestData.GuidPair2 { Name = "test1", Guid = Guid.Parse("a7dc91a0-ef9b-4fc7-9f03-1763d9688dfa"), GuidOrNull = Guid.Parse("5e124acc-c53e-4d47-bec8-a6618cf0b2d9") }; var expectedMap = new Dictionary <string, Value> { { "Name", new Value { StringValue = "test1" } }, { "Guid", new Value { StringValue = "a7dc91a0ef9b4fc79f031763d9688dfa" } }, { "GuidOrNull", new Value { StringValue = "5e124accc53e4d47bec8a6618cf0b2d9" } } }; var expectedValue = new Value { MapValue = new MapValue { Fields = { expectedMap } } }; var registry = new ConverterRegistry { new SerializationTestData.GuidConverter() }; var context = new SerializationContext(registry); var actualValue = ValueSerializer.Serialize(context, input); Assert.Equal(expectedValue, actualValue); }
public InfluxAppender() { ConverterRegistry.AddConverter(typeof(AppName), new ConvertStringToAppName()); ConverterRegistry.AddConverter(typeof(Facility), new ConvertStringToFacility()); //https://github.com/dotnet/extensions/issues/1345 HttpClient = new HttpClient(); }
public void DeserializeCustomPropertyConversion_ConverterRegistry() { Guid guid1 = Guid.NewGuid(); Guid guid2 = Guid.NewGuid(); var value = new Value { MapValue = new MapValue { Fields = { { "Name", new Value { StringValue = "test" } }, { "Guid", new Value { StringValue = guid1.ToString("N") } }, { "GuidOrNull", new Value { StringValue = guid2.ToString("N") } }, } } }; var registry = new ConverterRegistry() { new SerializationTestData.GuidConverter() }; var db = FirestoreDb.Create("proj", "db", new FakeFirestoreClient(), converterRegistry: registry); var snapshot = GetSampleSnapshot(db, "doc1"); var context = new DeserializationContext(snapshot); var pair = (SerializationTestData.GuidPair2)ValueDeserializer.Deserialize(context, value, typeof(SerializationTestData.GuidPair2)); Assert.Equal("test", pair.Name); Assert.Equal(guid1, pair.Guid); Assert.Equal(guid2, pair.GuidOrNull); }
public static object ConvertStringTo(Type target, string txt) { if (target == null) { throw new ArgumentNullException("target"); } if (ReferenceEquals(typeof(string), target) || ReferenceEquals(typeof(object), target)) { return(txt); } IConvertFrom convertFrom = ConverterRegistry.GetConvertFrom(target); if ((convertFrom != null) && convertFrom.CanConvertFrom(typeof(string))) { return(convertFrom.ConvertFrom(txt)); } if (target.IsEnum) { return(ParseEnum(target, txt, true)); } Type[] types = new Type[] { typeof(string) }; MethodInfo method = target.GetMethod("Parse", types); if (method == null) { return(null); } object[] parameters = new object[] { txt }; return(method.Invoke(null, BindingFlags.InvokeMethod, null, parameters, CultureInfo.InvariantCulture)); }
/// <summary> /// Converts a string to an object. /// </summary> /// <param name="target">The target type to convert to.</param> /// <param name="txt">The string to convert to an object.</param> /// <returns> /// The object converted from a string or <c>null</c> when the /// conversion failed. /// </returns> /// <remarks> /// <para> /// Converts a string to an object. Uses the converter registry to try /// to convert the string value into the specified target type. /// </para> /// </remarks> public static object ConvertStringTo(Type target, string txt) { if (target == null) { throw new ArgumentNullException("target"); } // If we want a string we already have the correct type if (typeof(string) == target || typeof(object) == target) { return(txt); } // First lets try to find a type converter IConvertFrom typeConverter = ConverterRegistry.GetConvertFrom(target); if (typeConverter != null && typeConverter.CanConvertFrom(typeof(string))) { // Found appropriate converter return(typeConverter.ConvertFrom(txt)); } else { #if NETSTANDARD1_3 if (target.GetTypeInfo().IsEnum) #else if (target.IsEnum) #endif { // Target type is an enum. // Use the Enum.Parse(EnumType, string) method to get the enum value return(ParseEnum(target, txt, true)); } else { // We essentially make a guess that to convert from a string // to an arbitrary type T there will be a static method defined on type T called Parse // that will take an argument of type string. i.e. T.Parse(string)->T we call this // method to convert the string to the type required by the property. System.Reflection.MethodInfo meth = target.GetMethod("Parse", new Type[] { typeof(string) }); if (meth != null) { // Call the Parse method #if NETSTANDARD1_3 return(meth.Invoke(target, new[] { txt })); #else return(meth.Invoke(null, BindingFlags.InvokeMethod, null, new object[] { txt }, CultureInfo.InvariantCulture)); #endif } else { // No Parse() method found. } } } return(null); }
public void DuplicateKey() { var registry = new ConverterRegistry { new GuidConverter() }; Assert.Throws <ArgumentException>(() => registry.Add(new AnotherGuidConverter())); }
/// <summary> /// Looks up the <see cref="IConvertFrom"/> for the target type. /// </summary> /// <param name="target">The type to lookup the converter for.</param> /// <returns>The converter for the specified type.</returns> public static IConvertFrom GetTypeConverter(Type target) { IConvertFrom converter = ConverterRegistry.GetConverter(target); if (converter == null) { throw new InvalidOperationException("No type converter defined for [" + target + "]"); } return(converter); }
public void ShouldThrowConversionNotSupportedExceptionWhenTryingToConvertToOpenGenericType() { // Arrange IGenericOperators <string> value = new Operators(); IConverterRegistry converterRegistry = new ConverterRegistry(); // Act Action action = () => converterRegistry.Convert(typeof(IGenericOperators <string>), typeof(IGenericOperators <>), value); // Assert Assert.Throws <ConversionNotSupportedException>(action); }
public void ShouldConvertFromOpenGenericTypeToGenericType() { // Arrange IGenericOperators <string> inputValue = new Operators(); IConverterRegistry converterRegistry = new ConverterRegistry(); // Act var convertedValue = converterRegistry.Convert(typeof(IGenericOperators <>), typeof(IGenericOperators <string>), inputValue); // Assert convertedValue.Should().Be(inputValue); }
public void ShouldConvertULongToDecimalImplicitly() { // Arrange const ulong UlongValue = 999UL; IConverterRegistry converterRegistry = new ConverterRegistry(); // Act var convertedValue = converterRegistry.Convert <decimal>(UlongValue); // Assert convertedValue.Should().Be(Convert.ToDecimal(UlongValue)); }
public void ShouldConvertDoubleToIntegerExplicitly() { // Arrange const double DoubleValue = 999.99d; IConverterRegistry converterRegistry = new ConverterRegistry(); // Act var convertedValue = converterRegistry.Convert <int>(DoubleValue); // Assert convertedValue.Should().Be((int)DoubleValue); }
public void ShouldConvertValueTypeToNullableType() { // Arrange const bool ValueType = true; IConverterRegistry converterRegistry = new ConverterRegistry(); // Act var nullableValue = converterRegistry.Convert <bool?>(ValueType); // Assert nullableValue.Should().Be(ValueType); }
public void ShouldConvertNullableTypeToValueType() { // Arrange bool?nullableValue = true; IConverterRegistry converterRegistry = new ConverterRegistry(); // Act var valueType = converterRegistry.Convert <bool>(nullableValue); // Assert valueType.Should().Be(nullableValue.Value); }
public void ShouldThrowConversionNotSupportedExceptionWhenTryingToConvertGenericWithoutValidRegistration() { // Arrange const string InputString = "http://www.superdev.ch/"; IConverterRegistry converterRegistry = new ConverterRegistry(); // Act Action action = () => converterRegistry.Convert <string, Uri>(InputString); // Assert Assert.Throws <ConversionNotSupportedException>(action); }
public void ShouldConvertIfSourceTypeIsEqualToTargetType() { // Arrange const string InputString = "999"; IConverterRegistry converterRegistry = new ConverterRegistry(); // Act var convertedObject = (string)converterRegistry.Convert(typeof(string), typeof(string), InputString); // Assert convertedObject.Should().Be(InputString); }
public void IEnumerableImplementation() { var guidConverter = new GuidConverter(); var emailConverter = new EmailConverter(); var registry = new ConverterRegistry { guidConverter, emailConverter }; Assert.Equal(new object[] { guidConverter, emailConverter }, registry.Cast <object>().ToList()); }
public void ShouldReturnDefaultValueWhenTryConvertToReferenceTypeFails() { // Arrange const string InputString = "http://www.superdev.ch/"; IConverterRegistry converterRegistry = new ConverterRegistry(); // Act var convertedObject = converterRegistry.TryConvert <Uri>(InputString, null); // Assert convertedObject.Should().BeNull(); }
public void ShouldConvertEnumerableToArray() { // Arrange string[] stringArray = { "a", "b", "c" }; IConverterRegistry converterRegistry = new ConverterRegistry(); // Act var convertedList = (IEnumerable <string>)converterRegistry.Convert(typeof(IEnumerable <string>), stringArray); // Assert convertedList.Should().BeEquivalentTo(stringArray); }
public void ShouldConvertFormatBStringToGuid() { // Arrange const string InputString = "{1E20D9BB-D64C-4449-AC1B-36CB690601ED}"; IConverterRegistry converterRegistry = new ConverterRegistry(); converterRegistry.RegisterConverter<string, Guid>(() => new StringToGuidConverter()); // Act var outputGuid = converterRegistry.Convert<string, Guid>(InputString); // Assert outputGuid.Should().Be(new Guid(InputString)); }
public void ShouldConvertFloatMinValueToString() { // Arrange float inputFloat = float.MinValue; IConverterRegistry converterRegistry = new ConverterRegistry(); converterRegistry.RegisterConverter<float, string>(() => new StringToFloatConverter()); // Act var outputString = converterRegistry.Convert<float, string>(inputFloat); // Assert outputString.Should().Be("-3.40282347E+38"); }
public void ShouldConvertStringToDateTime_Universal() { // Arrange const string InputString = "1999-12-31T23:59:59.0000000+00:00"; IConverterRegistry converterRegistry = new ConverterRegistry(); converterRegistry.RegisterConverter<string, DateTimeOffset>(() => new StringToDateTimeOffsetConverter()); // Act var outputDateTime = converterRegistry.Convert<string, DateTimeOffset>(InputString); // Assert outputDateTime.Should().Be(new DateTimeOffset(new DateTime(1999, 12, 31, 23, 59, 59, DateTimeKind.Utc))); }
public void ShouldConvertDateTimeToString_Local() { // Arrange DateTimeOffset intputDateTime = new DateTimeOffset(new DateTime(1999, 12, 31, 23, 59, 59), new TimeSpan(-7, 0, 0)); IConverterRegistry converterRegistry = new ConverterRegistry(); converterRegistry.RegisterConverter<DateTimeOffset, string>(() => new StringToDateTimeOffsetConverter()); // Act var outputString = converterRegistry.Convert<DateTimeOffset, string>(intputDateTime); // Assert outputString.Should().Be("1999-12-31T23:59:59.0000000-07:00"); }
public void ShouldConvertDecimalMinValueToString() { // Arrange decimal inputDecimal = decimal.MinValue; IConverterRegistry converterRegistry = new ConverterRegistry(); converterRegistry.RegisterConverter<decimal, string>(() => new StringToDecimalConverter()); // Act var outputString = converterRegistry.Convert<decimal, string>(inputDecimal); // Assert outputString.Should().Be("-79228162514264337593543950335"); }
public void ShouldConvertStringToFloatMinValue() { // Arrange const string InputString = "-3.40282347E+38"; IConverterRegistry converterRegistry = new ConverterRegistry(); converterRegistry.RegisterConverter<string, float>(() => new StringToFloatConverter()); // Act var outputFloat = converterRegistry.Convert<string, float>(InputString); // Assert outputFloat.Should().Be(float.MinValue); }
public void ShouldConvertIfSourceTypeEqualsTargetType() { // Arrange var inputUri = new Uri("http://www.superdev.ch/"); IConverterRegistry converterRegistry = new ConverterRegistry(); // Act var outputUri = (Uri)converterRegistry.Convert(typeof(Uri), typeof(Uri), inputUri); // Assert outputUri.Should().NotBeNull(); outputUri.Should().Be(inputUri); }
public void ShouldConvertStringToDoubleMinValue() { // Arrange const string InputString = "-1.7976931348623157E+308"; IConverterRegistry converterRegistry = new ConverterRegistry(); converterRegistry.RegisterConverter<string, double>(() => new StringToDoubleConverter()); // Act var outputDouble = converterRegistry.Convert<string, double>(InputString); // Assert outputDouble.Should().Be(double.MinValue); }
public void ShouldConvertFormatNStringToGuid() { // Arrange const string InputString = "4568CA6400E742BAAA41E76916DE7118"; IConverterRegistry converterRegistry = new ConverterRegistry(); converterRegistry.RegisterConverter<string, Guid>(() => new StringToGuidConverter()); // Act var outputGuid = converterRegistry.Convert<string, Guid>(InputString); // Assert outputGuid.Should().Be(new Guid(InputString)); }
public void ShouldTryConvertEnumImplicitlyWithNonGenericMethod() { // Arrange object sourceObject = MyEnum.TestValue; const MyEnum DefaultValue = default(MyEnum); IConverterRegistry converterRegistry = new ConverterRegistry(); // Act object convertedObject = converterRegistry.TryConvert(typeof(object), typeof(MyEnum), sourceObject, DefaultValue); // Assert convertedObject.Should().Be(sourceObject); }
public void SetUp() { var stateMachine = new TokenizerStateMachine(StateHolder.DefaultConfiguration); _tokenizer = new StateMachineTokenizer(stateMachine); var map = new ColumnMapBuilder <StatePopulation>() .WithColumn(0, s => s.Name) .WithColumn(1, s => s.Population) .Build(); _sut = new MapperFactory <StatePopulation>(ConverterRegistry.CreateDefaultInstance()).CreateForMap(map); }
public void ShouldConvertStringToDecimalMinValue() { // Arrange const string InputString = "-79228162514264337593543950335"; IConverterRegistry converterRegistry = new ConverterRegistry(); converterRegistry.RegisterConverter<string, decimal>(() => new StringToDecimalConverter()); // Act var outputDecimal = converterRegistry.Convert<string, decimal>(InputString); // Assert outputDecimal.Should().Be(decimal.MinValue); }
public void ShouldConvertDateTimeToString_Universal() { // Arrange DateTime intputDateTime = new DateTime(1999, 12, 31, 23, 59, 59, DateTimeKind.Utc); IConverterRegistry converterRegistry = new ConverterRegistry(); converterRegistry.RegisterConverter<DateTime, string>(() => new StringToDateTimeConverter()); // Act var outputString = converterRegistry.Convert<DateTime, string>(intputDateTime); // Assert outputString.Should().Be("1999-12-31T23:59:59.0000000Z"); }
public void ShouldConvertUriToString() { // Arrange var inputUri = new Uri("http://www.superdev.ch/"); IConverterRegistry converterRegistry = new ConverterRegistry(); converterRegistry.RegisterConverter<Uri, string>(() => new StringToUriConverter()); // Act var outputString = converterRegistry.Convert<Uri, string>(inputUri); // Assert outputString.Should().Be(inputUri.AbsoluteUri); }
public void ShouldConvertUsingChangeType() { // Arrange bool? nullableBool = true; string valueTypeString = nullableBool.ToString(); IConverterRegistry converterRegistry = new ConverterRegistry(); // Act var nullableValue = converterRegistry.Convert <bool?>(valueTypeString); // Assert nullableValue.Should().Be(nullableBool.Value); }
public void ShouldConvertGuidToBFormatString() { // Arrange var inputGuid = new Guid("83EDDA8A-4538-4BA8-8D40-E82C561CD745"); IConverterRegistry converterRegistry = new ConverterRegistry(); converterRegistry.RegisterConverter<Guid,string>(() => new StringToGuidConverter()); // Act var outputString = converterRegistry.Convert<Guid, string>(inputGuid); // Assert outputString.Should().Be("{83edda8a-4538-4ba8-8d40-e82c561cd745}"); }
public void ShouldConvertDoubleMinValueToString() { // Arrange double inputDouble = double.MinValue; IConverterRegistry converterRegistry = new ConverterRegistry(); converterRegistry.RegisterConverter<double, string>(() => new StringToDoubleConverter()); // Act var outputString = converterRegistry.Convert<double, string>(inputDouble); // Assert outputString.Should().Be("-1.7976931348623157E+308"); }
public void ShouldConvertStringToDateTime_Local() { // Arrange const string InputString = "1999-12-31T23:59:59.0000000-07:00"; IConverterRegistry converterRegistry = new ConverterRegistry(); converterRegistry.RegisterConverter <string, DateTimeOffset>(() => new StringToDateTimeOffsetConverter()); // Act var outputDateTime = converterRegistry.Convert <string, DateTimeOffset>(InputString); // Assert outputDateTime.Should().Be(new DateTimeOffset(new DateTime(1999, 12, 31, 23, 59, 59), new TimeSpan(-7, 0, 0))); }
public void ShouldConvertDateTimeToString_Local() { // Arrange DateTimeOffset intputDateTime = new DateTimeOffset(new DateTime(1999, 12, 31, 23, 59, 59), new TimeSpan(-7, 0, 0)); IConverterRegistry converterRegistry = new ConverterRegistry(); converterRegistry.RegisterConverter <DateTimeOffset, string>(() => new StringToDateTimeOffsetConverter()); // Act var outputString = converterRegistry.Convert <DateTimeOffset, string>(intputDateTime); // Assert outputString.Should().Be("1999-12-31T23:59:59.0000000-07:00"); }
public void ShouldConvertDateTimeToString_Universal() { // Arrange DateTimeOffset intputDateTime = new DateTimeOffset(new DateTime(1999, 12, 31, 23, 59, 59, DateTimeKind.Utc)); IConverterRegistry converterRegistry = new ConverterRegistry(); converterRegistry.RegisterConverter <DateTimeOffset, string>(() => new StringToDateTimeOffsetConverter()); // Act var outputString = converterRegistry.Convert <DateTimeOffset, string>(intputDateTime); // Assert outputString.Should().Be("1999-12-31T23:59:59.0000000+00:00"); }
public void ShouldConvertStringToDateTime_Local() { // Arrange const string InputString = "1999-12-31T23:59:59.0000000+01:00"; IConverterRegistry converterRegistry = new ConverterRegistry(); converterRegistry.RegisterConverter<string, DateTime>(() => new StringToDateTimeConverter()); // Act var outputDateTime = converterRegistry.Convert<string, DateTime>(InputString); // Assert outputDateTime.Should().Be(new DateTime(1999, 12, 31, 23, 59, 59, DateTimeKind.Local)); outputDateTime.Kind.Should().Be(DateTimeKind.Local); }
public void ShouldConvertStringToUri() { // Arrange const string InputString = "http://www.superdev.ch/"; IConverterRegistry converterRegistry = new ConverterRegistry(); converterRegistry.RegisterConverter<string, Uri>(() => new StringToUriConverter()); converterRegistry.RegisterConverter<Uri, string>(() => new StringToUriConverter()); // Act var outputUri = converterRegistry.Convert<string, Uri>(InputString); // Assert outputUri.Should().NotBeNull(); outputUri.AbsoluteUri.Should().Be(InputString); }
public void ShouldConvertBothWays() { // Arrange const string InputString = "999"; IConverterRegistry converterRegistry = new ConverterRegistry(); converterRegistry.RegisterConverter<string, int>(() => new StringToIntegerConverter()); converterRegistry.RegisterConverter<int, string>(() => new StringToIntegerConverter()); // Act var convertedObject = converterRegistry.Convert<string, int>(InputString); var outputString = converterRegistry.Convert<int, string>(convertedObject); // Assert convertedObject.Should().Be(999); outputString.Should().NotBeNullOrEmpty(); outputString.Should().Be(InputString); }
public void ShouldConvertBothWays() { // Arrange const string InputString = "True"; IConverterRegistry converterRegistry = new ConverterRegistry(); converterRegistry.RegisterConverter<string, bool>(() => new StringToBoolConverter()); converterRegistry.RegisterConverter<bool, string>(() => new StringToBoolConverter()); // Act var convertedObject = converterRegistry.Convert<string, bool>(InputString); var outputString = converterRegistry.Convert<bool, string>(convertedObject); // Assert convertedObject.Should().BeTrue(); outputString.Should().NotBeNullOrEmpty(); outputString.Should().Be(InputString); }
public void ShouldRegisterMultipleConvertables() { // Arrange const string InputUriString = "http://www.superdev.ch/"; const string InputBoolString = "True"; IConverterRegistry converterRegistry = new ConverterRegistry(); IConvertable[] converters = { new StringToUriConverter(), new StringToBoolConverter() }; converterRegistry.RegisterConverters(converters); // Act var outputUri = converterRegistry.Convert<string, Uri>(InputUriString); var outputUriString = converterRegistry.Convert<Uri, string>(outputUri); var outputBool = converterRegistry.Convert<string, bool>(InputBoolString); var outputBoolString = converterRegistry.Convert<bool, string>(outputBool); // Assert outputUriString.Should().Be(InputUriString); outputBoolString.Should().Be(InputBoolString); }