This repository has been archived by the owner on Oct 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
102 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace TypeCode.Business.Version; | ||
|
||
public interface ISemanticVersionComparer | ||
{ | ||
bool IsNewer(string current, string newest); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
namespace TypeCode.Business.Version; | ||
|
||
public class SemanticVersionComparer : ISemanticVersionComparer | ||
{ | ||
public bool IsNewer(string current, string newest) | ||
{ | ||
var currentSplitted = current.Split(".").Select(int.Parse); | ||
var newestSplitted = newest.Split(".").Select(int.Parse); | ||
var zipped = currentSplitted.Zip(newestSplitted).Reverse().ToList(); | ||
|
||
var isHigher = false; | ||
foreach (var (currentPart, newestPart) in zipped) | ||
{ | ||
if (newestPart > currentPart) | ||
{ | ||
isHigher = true; | ||
} | ||
else if (newestPart < currentPart) | ||
{ | ||
isHigher = false; | ||
} | ||
} | ||
|
||
return isHigher; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
tests/TypeCode.Business.Tests/Version/SemanticVersionComparerTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using FluentAssertions; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using TypeCode.Business.Version; | ||
|
||
namespace TypeCode.Business.Tests.Version; | ||
|
||
[TestClass] | ||
public class SemanticVersionComparerTest | ||
{ | ||
private SemanticVersionComparer _testee = null!; | ||
|
||
[TestInitialize] | ||
public void Initialize() | ||
{ | ||
_testee = new SemanticVersionComparer(); | ||
} | ||
|
||
[TestMethod] | ||
[DataRow("1.1.1", "1.1.1", false)] | ||
[DataRow("1.1.1", "1.1.2", true)] | ||
[DataRow("1.1.1", "1.2.1", true)] | ||
[DataRow("1.1.1", "2.1.1", true)] | ||
[DataRow("1.1.1", "2.2.1", true)] | ||
[DataRow("1.1.1", "2.2.2", true)] | ||
|
||
[DataRow("1.1.0", "1.1.1", true)] | ||
[DataRow("1.1.2", "1.1.1", false)] | ||
[DataRow("1.2.1", "1.1.1", false)] | ||
[DataRow("2.1.1", "1.1.1", false)] | ||
[DataRow("2.2.1", "1.1.1", false)] | ||
[DataRow("1.1.2", "1.1.1", false)] | ||
public void IsNewer_WhenData_ThenReturn(string current, string newer, bool expected) | ||
{ | ||
var result = _testee.IsNewer(current, newer); | ||
result.Should().Be(expected); | ||
} | ||
} |