Skip to content

Commit

Permalink
Merge pull request #519 from icarus-consulting/i518-VersionMap
Browse files Browse the repository at this point in the history
Correct VersionMap behaviour with 2 or 3 digit versions
  • Loading branch information
MSE1188 authored Jul 11, 2022
2 parents 18d1088 + ba4faf8 commit 9ad6380
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/Yaapii.Atoms/Map/VersionMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,16 +179,22 @@ IEnumerator IEnumerable.GetEnumerator()

private Value Match(Version candidate)
{
var prettyCandidate = new Version(
candidate.Major,
candidate.Minor,
candidate.Build == -1 ? 0 : candidate.Build,
candidate.Revision == -1 ? 0 : candidate.Revision
);
var match = new Version(0, 0);
var matched = false;
foreach (var lowerBound in this.map.Keys)
{
if (candidate >= lowerBound)
if (prettyCandidate >= lowerBound)
{
match = lowerBound;
matched = true;
}
else if (match < candidate)
else if (match < prettyCandidate)
{
break;
}
Expand All @@ -202,10 +208,10 @@ private Value Match(Version candidate)
}
else
{
throw this.versionNotFound(candidate, this.map.Keys);
throw this.versionNotFound(prettyCandidate, this.map.Keys);
}
}
throw this.versionNotFound(candidate, this.map.Keys);
throw this.versionNotFound(prettyCandidate, this.map.Keys);
}
}
}
23 changes: 23 additions & 0 deletions tests/Yaapii.Atoms.Tests/Map/VersionMapTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,28 @@ public void MatchesWithinClosedEnd()
).ContainsKey(new Version(2, 0, 0, 0))
);
}

[Fact]
public void MatchesEndingZero()
{
Assert.True(
new VersionMap(false,
new KvpOf<Version, string>(new Version(1, 0, 0, 0), "ainz"),
new KvpOf<Version, string>(new Version(5, 0, 0, 0), "zway")
).ContainsKey(new Version(1, 0))
);
}

[Fact]
public void MatchesMajorMinorVersion()
{
Assert.Equal(
"zway",
new VersionMap(true,
new KvpOf<Version, string>(new Version(1, 0, 0, 0), "ainz"),
new KvpOf<Version, string>(new Version(5, 0, 0, 0), "zway")
)[new Version(5, 0)]
);
}
}
}

0 comments on commit 9ad6380

Please sign in to comment.