Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Commit

Permalink
Always throw a FormatException when a version string is invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
0xced committed Aug 16, 2018
1 parent 2ba1f61 commit 004ef11
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,19 @@ public override bool CanConvertTo(ITypeDescriptorContext context, Type destinati
/// <summary>
/// <para>Converts the given object to a Version.</para>
/// </summary>
/// <exception cref="FormatException"><paramref name="value"/> is not a valid version string</exception>
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string versionString)
{
// Let the Version constructor throw any informative exceptions
return Version.Parse(versionString);
try
{
return Version.Parse(versionString);
}
catch (Exception e)
{
throw new FormatException(SR.Format(SR.ConvertInvalidPrimitive, versionString, nameof(Version)), e);
}
}

if (value is Version version)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,21 @@ public static void ConvertFrom_WithContext()
}

[Fact]
public static void ConvertFrom_WithContext_Negative()
public static void ConvertFromNull_WithContext_ThrowsNotSupportedException()
{
Assert.Throws<NotSupportedException>(
() => VersionConverterTests.s_converter.ConvertFrom(TypeConverterTests.s_context, null, null));
Assert.Throws<ArgumentException>(
() => VersionConverterTests.s_converter.ConvertFrom(TypeConverterTests.s_context, null, ""));
Assert.Throws<ArgumentException>(
() => VersionConverterTests.s_converter.ConvertFrom(TypeConverterTests.s_context, null, "1"));
}

[Theory]
[InlineData("")]
[InlineData("1")]
[InlineData("1.-2")]
[InlineData("1.9999999999")]
public static void ConvertFromInvalidVersion_WithContext_ThrowsFormatException(string version)
{
Assert.Throws<FormatException>(
() => VersionConverterTests.s_converter.ConvertFrom(TypeConverterTests.s_context, null, version));
}
}
}

0 comments on commit 004ef11

Please sign in to comment.