Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
qdraw committed Mar 14, 2024
1 parent 2c01325 commit 2142cf1
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ private static int CompareComponent(string a, string? b, bool nonemptyIsLower =
return nonemptyIsLower ? -1 : 1;

var aComps = a.Split('.');
var bComps = b.Split('.');
var bComps = b!.Split('.');

return CompareComponentCompareLoop(aComps, bComps);
}
Expand Down Expand Up @@ -228,7 +228,7 @@ public override int GetHashCode()
unchecked
{
// verify this. Some versions start result = 17. Some use 37 instead of 31
int result = Major.GetHashCode();
var result = Major.GetHashCode();
result = result * 31 + Minor.GetHashCode();
result = result * 31 + Patch.GetHashCode();
result = result * 31 + Prerelease.GetHashCode();
Expand Down Expand Up @@ -315,7 +315,7 @@ private static int CompareComponentCompareOther(bool aIsNum, bool bIsNum, string
/// <param name="versionB">The second version to compare.</param>
/// <returns>A signed number indicating the relative values of <paramref name="versionA"/>
/// and <paramref name="versionB"/>.</returns>
private static int Compare(SemVersion versionA, SemVersion versionB)
internal static int Compare(SemVersion? versionA, SemVersion? versionB)
{
if ( ReferenceEquals(versionA, versionB) ) return 0;
if ( versionA is null ) return -1;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
Expand Down Expand Up @@ -31,6 +32,33 @@ public async Task CheckForUpdates_Disabled()
Assert.AreEqual(208, actionResult?.StatusCode);
}

private class TestOverWriteEnumModel
{
public UpdateStatus Value { get; set; }
}


[TestMethod]
[ExpectedException(typeof(NotSupportedException))]
public async Task CheckForUpdates_NotSupportedException()
{
var input = new TestOverWriteEnumModel { Value = UpdateStatus.Disabled };

// Use reflection to set the updateStatus field to UpdateAvailable
// overwrite enum value
var propertyInfo = input.GetType().GetProperty("Value");
Assert.IsNotNull(propertyInfo);
propertyInfo.SetValue(input, 44, null); // <-- this could not happen

var fakeService = new FakeICheckForUpdates(
new KeyValuePair<UpdateStatus, string?>(input.Value, string.Empty));

Assert.IsNotNull(input.Value);

await new HealthCheckForUpdatesController(fakeService,
new FakeISpecificVersionReleaseInfo()).CheckForUpdates();
}

[TestMethod]
public async Task CheckForUpdates_HttpError()
{
Expand Down Expand Up @@ -102,5 +130,31 @@ public async Task CheckForUpdates_CurrentVersionIsLatest()
ObjectResult;
Assert.AreEqual(200, actionResult?.StatusCode);
}

[TestMethod]
public async Task SpecificVersionReleaseInfo()
{
var fakeService = new FakeICheckForUpdates(new KeyValuePair<UpdateStatus, string?>());
var service2 = new FakeISpecificVersionReleaseInfo(
new Dictionary<string, Dictionary<string, string>>
{
{
"1.0.0",
new Dictionary<string, string>
{
{ "en", " # 1.0.0\n\n- [x] test\n- [ ] test2\n\n" }
}
}
}
);

var actionResult =
await new HealthCheckForUpdatesController(fakeService,
service2)
.SpecificVersionReleaseInfo("1.0.0") as JsonResult;

Assert.AreEqual(" # 1.0.0\n\n- [x] test\n- [ ] test2\n\n",
actionResult?.Value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ public void NonThrowError()
Assert.AreEqual(new SemVersion(0), result);
}

[TestMethod]
public void CompareTo_Null()
{
var result = new SemVersion(0).CompareTo(null);
Assert.AreEqual(1, result);
}

[TestMethod]
public void WithVPrefix()
{
Expand Down Expand Up @@ -535,5 +542,26 @@ public void Inequality_Operator_Returns_False_For_Same_Version()
// Assert
Assert.IsFalse(result);
}

[TestMethod]
public void Compare_FirstNull()
{
var result = SemVersion.Compare(null, new SemVersion(1));
Assert.AreEqual(-1, result);
}

[TestMethod]
public void Compare_SecondNull()
{
var result = SemVersion.Compare(new SemVersion(1), null);
Assert.AreEqual(1, result);
}

[TestMethod]
public void Compare_BothNull()
{
var result = SemVersion.Compare(null, null);
Assert.AreEqual(0, result);
}
}
}

0 comments on commit 2142cf1

Please sign in to comment.