Skip to content

Commit

Permalink
SemVersion.Parse and TryParse should throw exception for negative max…
Browse files Browse the repository at this point in the history
…Length (issue #72)
  • Loading branch information
WalkerCodeRanger committed Jan 22, 2023
1 parent e6adb31 commit 2b19b96
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 19 deletions.
15 changes: 6 additions & 9 deletions Semver.Test/SemVersionParsingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -378,12 +378,10 @@ public void ParseWithInvalidStyle(SemVersionStyles styles)
[MemberData(nameof(InvalidMaxLength))]
public void ParseWithInvalidMaxLength(int maxLength)
{
// TODO change in v3.0.0 for issue #72
var ex = Assert.Throws<FormatException>(() => SemVersion.Parse("ignored", Strict, maxLength));
var ex = Assert.Throws<ArgumentOutOfRangeException>(() => SemVersion.Parse("ignored", Strict, maxLength));

var expected = ExceptionMessages.InjectValue(ExceptionMessages.TooLongVersion, maxLength.ToString());
expected = ExceptionMessages.InjectVersion(expected, "ignored");
Assert.Equal(expected, ex.Message);
Assert.StartsWith(ExceptionMessages.InvalidMaxLengthStart, ex.Message);
Assert.Equal("maxLength", ex.ParamName);
}

[Theory]
Expand Down Expand Up @@ -429,11 +427,10 @@ public void TryParseWithInvalidStyle(SemVersionStyles styles)
[MemberData(nameof(InvalidMaxLength))]
public void TryParseWithInvalidMaxLength(int maxLength)
{
// TODO change in v3.0.0 for issue #72
var result = SemVersion.TryParse("1.2.3", Strict, out var version, maxLength);
var ex = Assert.Throws<ArgumentOutOfRangeException>(() => SemVersion.TryParse("ignored", Strict, out _, maxLength));

Assert.False(result);
Assert.Null(version);
Assert.StartsWith(ExceptionMessages.InvalidMaxLengthStart, ex.Message);
Assert.Equal("maxLength", ex.ParamName);
}

[Theory]
Expand Down
9 changes: 3 additions & 6 deletions Semver/SemVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ public sealed class SemVersion : IEquatable<SemVersion>
private const string InvalidPatchVersionMessage = "Patch version must be greater than or equal to zero.";
private const string PrereleaseIdentifierIsDefaultMessage = "Prerelease identifier cannot be default/null.";
private const string MetadataIdentifierIsDefaultMessage = "Metadata identifier cannot be default/null.";
// TODO include in v3.0.0 for issue #72
//internal const string InvalidMaxLengthMessage = "Must not be negative.";
private const string InvalidMaxLengthMessage = "Must not be negative.";
internal const int MaxVersionLength = 1024;

#if SERIALIZABLE
Expand Down Expand Up @@ -407,8 +406,7 @@ public Version ToVersion()
public static SemVersion Parse(string version, SemVersionStyles style, int maxLength = MaxVersionLength)
{
if (!style.IsValid()) throw new ArgumentException(InvalidSemVersionStylesMessage, nameof(style));
// TODO include in v3.0.0 for issue #72
//if (maxLength < 0) throw new ArgumentOutOfRangeException(InvalidMaxLengthMessage, nameof(maxLength));
if (maxLength < 0) throw new ArgumentOutOfRangeException(nameof(maxLength), InvalidMaxLengthMessage);
var ex = SemVersionParser.Parse(version, style, null, maxLength, out var semver);

return ex is null ? semver : throw ex;
Expand Down Expand Up @@ -439,8 +437,7 @@ public static bool TryParse(string version, SemVersionStyles style,
out SemVersion semver, int maxLength = MaxVersionLength)
{
if (!style.IsValid()) throw new ArgumentException(InvalidSemVersionStylesMessage, nameof(style));
// TODO include in v3.0.0 for issue #72
//if (maxLength < 0) throw new ArgumentOutOfRangeException(InvalidMaxLengthMessage, nameof(maxLength));
if (maxLength < 0) throw new ArgumentOutOfRangeException(nameof(maxLength), InvalidMaxLengthMessage);
var exception = SemVersionParser.Parse(version, style, Parsing.FailedException, maxLength, out semver);

#if DEBUG
Expand Down
6 changes: 2 additions & 4 deletions Semver/SemVersionParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ public static Exception Parse(
out SemVersion semver)
{
DebugChecks.IsValid(style, nameof(style));
// TODO include in v3.0.0 for issue #72
// DebugChecks.IsValidMaxLength(maxLength, nameof(maxLength));
DebugChecks.IsValidMaxLength(maxLength, nameof(maxLength));

if (version != null)
return Parse(version, style, SemVersionParsingOptions.None, ex, maxLength, out semver, out _);
Expand Down Expand Up @@ -89,8 +88,7 @@ public static Exception Parse(
out WildcardVersion wildcardVersion)
{
DebugChecks.IsValid(style, nameof(style));
// TODO include in v3.0.0 for issue #72
// DebugChecks.IsValidMaxLength(maxLength, nameof(maxLength));
DebugChecks.IsValidMaxLength(maxLength, nameof(maxLength));

// Assign once so it doesn't have to be done any time parse fails
semver = null;
Expand Down

0 comments on commit 2b19b96

Please sign in to comment.