Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit abb3ae0

Browse files
Making SoftwareVersions comparable
1 parent fdac15b commit abb3ae0

File tree

3 files changed

+89
-2
lines changed

3 files changed

+89
-2
lines changed

src/GitHub.Api/OutputProcessors/SoftwareVersion.cs

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,72 @@ public SoftwareVersion(int major, int minor, int build)
1313
Build = build;
1414
}
1515

16-
public SoftwareVersion(string major, string minor, string build):
16+
public SoftwareVersion(string major, string minor, string build) :
1717
this(int.Parse(major), int.Parse(minor), int.Parse(build))
1818
{
19-
19+
20+
}
21+
22+
public static bool operator <(SoftwareVersion lhs, SoftwareVersion rhs)
23+
{
24+
if (lhs.Major < rhs.Major)
25+
{
26+
return true;
27+
}
28+
29+
if (lhs.Major == rhs.Major)
30+
{
31+
if (lhs.Minor < rhs.Minor)
32+
{
33+
return true;
34+
}
35+
36+
if (lhs.Minor == rhs.Minor)
37+
{
38+
if (lhs.Build < rhs.Build)
39+
{
40+
return true;
41+
}
42+
}
43+
}
44+
45+
return false;
46+
}
47+
48+
public static bool operator >(SoftwareVersion lhs, SoftwareVersion rhs)
49+
{
50+
if (lhs.Major > rhs.Major)
51+
{
52+
return true;
53+
}
54+
55+
if (lhs.Major == rhs.Major)
56+
{
57+
if (lhs.Minor > rhs.Minor)
58+
{
59+
return true;
60+
}
61+
62+
if (lhs.Minor == rhs.Minor)
63+
{
64+
if (lhs.Build > rhs.Build)
65+
{
66+
return true;
67+
}
68+
}
69+
}
70+
71+
return false;
72+
}
73+
74+
public static bool operator <=(SoftwareVersion lhs, SoftwareVersion rhs)
75+
{
76+
return lhs.Equals(rhs) || lhs < rhs;
77+
}
78+
79+
public static bool operator >=(SoftwareVersion lhs, SoftwareVersion rhs)
80+
{
81+
return lhs.Equals(rhs) || lhs > rhs;
2082
}
2183
}
2284
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using FluentAssertions;
2+
using GitHub.Unity;
3+
using NUnit.Framework;
4+
5+
namespace UnitTests
6+
{
7+
[TestFixture]
8+
class SoftwareVersionTests
9+
{
10+
[Test]
11+
public void LessThanOrEqualTo()
12+
{
13+
(new SoftwareVersion(1, 1, 0) <= new SoftwareVersion(1, 1, 0)).Should().BeTrue();
14+
(new SoftwareVersion(1, 0, 0) <= new SoftwareVersion(1, 1, 0)).Should().BeTrue();
15+
}
16+
17+
[Test]
18+
public void LessThan()
19+
{
20+
(new SoftwareVersion(1, 1, 0) < new SoftwareVersion(1, 1, 0)).Should().BeFalse();
21+
(new SoftwareVersion(1, 0, 0) < new SoftwareVersion(1, 1, 0)).Should().BeTrue();
22+
}
23+
}
24+
}

src/tests/UnitTests/UnitTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
<Compile Include="Extensions\EnvironmentExtensionTests.cs" />
8585
<Compile Include="IO\LfsVersionOutputProcessorTests.cs" />
8686
<Compile Include="IO\LockOutputProcessorTests.cs" />
87+
<Compile Include="IO\SoftwareVersionTests.cs" />
8788
<Compile Include="IO\VersionOutputProcessorTests.cs" />
8889
<Compile Include="IO\RemoteListOutputProcessorTests.cs" />
8990
<Compile Include="IO\BaseOutputProcessorTests.cs" />

0 commit comments

Comments
 (0)