-
-
Notifications
You must be signed in to change notification settings - Fork 6
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
Drommedhar
committed
Nov 10, 2024
1 parent
6833a97
commit d661c2e
Showing
6 changed files
with
287 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>net8.0-windows</TargetFrameworks> | ||
|
||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" /> | ||
<PackageReference Include="NUnit" Version="4.2.2" /> | ||
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\DlssUpdater\DLSSUpdater.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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,87 @@ | ||
using System; | ||
using DLSSUpdater.Helpers; | ||
using NUnit.Framework; | ||
|
||
namespace DLSSUpdater.Tests.Helpers; | ||
|
||
[TestFixture] | ||
[TestOf(typeof(SafeVersion))] | ||
public class SafeVersionTest | ||
{ | ||
[Test] | ||
public void StringToSafeVersion() | ||
{ | ||
var version = "1.2.3.4"; | ||
var safeVersion = new SafeVersion(version); | ||
Assert.That(safeVersion.ToString(), Is.EqualTo("1.2.3.4")); | ||
|
||
version = "10.20.30.40"; | ||
safeVersion = new SafeVersion(version); | ||
Assert.That(safeVersion.ToString(), Is.EqualTo("10.20.30.40")); | ||
|
||
version = "1.2.3.4.u"; | ||
safeVersion = new SafeVersion(version); | ||
Assert.That(safeVersion.ToString(), Is.EqualTo("1.2.3.4")); | ||
|
||
version = "1-2-3-4"; | ||
safeVersion = new SafeVersion(version); | ||
Assert.That(safeVersion.ToString(), Is.EqualTo("1.2.3.4")); | ||
|
||
version = "1a2ab3ac4aa"; | ||
safeVersion = new SafeVersion(version); | ||
Assert.That(safeVersion.ToString(), Is.EqualTo("1.2.3.4")); | ||
|
||
version = "xx1a-2abx3ac*4aa+"; | ||
safeVersion = new SafeVersion(version); | ||
Assert.That(safeVersion.ToString(), Is.EqualTo("1.2.3.4")); | ||
|
||
version = "xx1a-2abx3ac*4aa+5rtuz;"; | ||
safeVersion = new SafeVersion(version); | ||
Assert.That(safeVersion.ToString(), Is.EqualTo("1.2.3.4")); | ||
} | ||
|
||
[Test] | ||
public void VersionToSafeVersion() | ||
{ | ||
var version = new Version(1, 2, 3,4); | ||
var safeVersion = new SafeVersion(version); | ||
Assert.That((Version)safeVersion, Is.EqualTo(version)); | ||
|
||
version = new Version(1, 0, 0, 1); | ||
safeVersion = new SafeVersion(version); | ||
Assert.That((Version)safeVersion, Is.EqualTo(version)); | ||
|
||
version = new Version(1, 0); | ||
safeVersion = new SafeVersion(version); | ||
Assert.That((Version)safeVersion, Is.EqualTo(version)); | ||
|
||
version = new Version(1, 0, 0, 0); | ||
safeVersion = new SafeVersion(version); | ||
Assert.That((Version)safeVersion, Is.EqualTo(version)); | ||
|
||
version = new Version(1, 2, 3, 0); | ||
safeVersion = new SafeVersion(version); | ||
Assert.That((Version)safeVersion, Is.EqualTo(version)); | ||
|
||
version = new Version(1, 2, 3); | ||
safeVersion = new SafeVersion(version); | ||
Assert.That((Version)safeVersion, Is.EqualTo(version)); | ||
|
||
version = new Version(1, 2); | ||
safeVersion = new SafeVersion(version); | ||
Assert.That((Version)safeVersion, Is.EqualTo(version)); | ||
} | ||
|
||
[Test] | ||
public void CheckIfVersionIsSafe() | ||
{ | ||
var safeVersion = new SafeVersion("1"); | ||
Assert.That(safeVersion.ToString(), Is.EqualTo("1.0.0.0")); | ||
|
||
safeVersion = new SafeVersion("1.2"); | ||
Assert.That(safeVersion.ToString(), Is.EqualTo("1.2.0.0")); | ||
|
||
safeVersion = new SafeVersion("1.2.3"); | ||
Assert.That(safeVersion.ToString(), Is.EqualTo("1.2.3.0")); | ||
} | ||
} |
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,18 @@ | ||
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> | ||
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AAssert_002Ecs_002Fl_003AC_0021_003FUsers_003Fdomin_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E2_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F53539022a55f40bd95b3200c33b231c074e00_003F65_003F7669dcf3_003FAssert_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> | ||
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AEnumerable_002Ecs_002Fl_003AC_0021_003FUsers_003Fdomin_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E2_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Fcca5cfb955e146648d91eb22ffe4627a84930_003Fa0_003Ff6ea3ed0_003FEnumerable_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> | ||
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AFileSystemEnumerator_002EWindows_002Ecs_002Fl_003AC_0021_003FUsers_003Fdomin_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E2_003Fresharper_002Dhost_003FSourcesCache_003F422299c1e1233f1a20b8c0654281f74792424fe87e29abaf4ecfcd0cd64d718_003FFileSystemEnumerator_002EWindows_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> | ||
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AVersion_002Ecs_002Fl_003AC_0021_003FUsers_003Fdomin_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E2_003Fresharper_002Dhost_003FSourcesCache_003Fbda1a8aceb88de87254065a2ded795c3953da7c42a269ae5bbb443973798b75_003FVersion_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> | ||
<s:Boolean x:Key="/Default/Environment/UnitTesting/CreateUnitTestDialog/ShowAdvancedOptions/@EntryValue">True</s:Boolean> | ||
<s:String x:Key="/Default/Environment/UnitTesting/CreateUnitTestDialog/TestProjectMapping/=891674A2_002D74FE_002D4E71_002DA18E_002D859680E25E98/@EntryIndexedValue">DLSSUpdater.Tests</s:String> | ||
<s:String x:Key="/Default/Environment/UnitTesting/CreateUnitTestDialog/TestTemplateMapping/=NUnit3x/@EntryIndexedValue">db4927dd-2e12-48a7-9a84-2b7e3e31b9c8</s:String> | ||
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=35466c2e_002Df322_002D4413_002D8d9b_002D0b19d0251add/@EntryIndexedValue"><SessionState ContinuousTestingMode="0" IsActive="True" Name="StringToSafeVersion" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session">
 | ||
<TestAncestor>
 | ||
<TestId>NUnit3x::43C6713F-2567-4400-A617-0684C22F987C::net8.0-windows::DLSSUpdater.Tests.Helpers.SafeVersionTest</TestId>
 | ||
</TestAncestor>
 | ||
</SessionState></s:String> | ||
|
||
|
||
|
||
|
||
</wpf:ResourceDictionary> |
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,151 @@ | ||
using System.Text.RegularExpressions; | ||
|
||
namespace DLSSUpdater.Helpers; | ||
|
||
public partial class SafeVersion : IComparable<SafeVersion>, IEquatable<SafeVersion?>, ICloneable, IComparable | ||
{ | ||
public int Major => _versionParts.Count > 0 ? _versionParts[0] : 0; | ||
public int Minor => _versionParts.Count > 1 ? _versionParts[1] : 0; | ||
public int Build => _versionParts.Count > 2 ? _versionParts[2] : 0; | ||
public int Revision => _versionParts.Count > 3 ? _versionParts[3] : 0; | ||
|
||
private readonly List<int> _versionParts = []; | ||
|
||
public SafeVersion(string? version) | ||
{ | ||
if (string.IsNullOrEmpty(version)) | ||
{ | ||
return; | ||
} | ||
|
||
// We need to parse all elements that might be there | ||
// Use regex to extract digits and group into version format | ||
_versionParts = VersionRegex().Matches(version) | ||
.Select(m => int.Parse(m.Value)) | ||
.ToList(); | ||
} | ||
|
||
public SafeVersion(Version? version) | ||
{ | ||
if (version == null) | ||
{ | ||
return; | ||
} | ||
|
||
FillParts(version.Major); | ||
FillParts(version.Minor); | ||
FillParts(version.Build); | ||
FillParts(version.Revision); | ||
} | ||
|
||
private SafeVersion(SafeVersion version) | ||
{ | ||
foreach (var part in version._versionParts) | ||
{ | ||
_versionParts.Add(part); | ||
} | ||
} | ||
|
||
public void AddVersionPart(int part) | ||
{ | ||
_versionParts.Add(part); | ||
} | ||
|
||
public int CompareTo(SafeVersion? other) | ||
{ | ||
return other == null ? 1 : ((Version)this).CompareTo((Version)other); | ||
} | ||
|
||
public int CompareTo(object? obj) | ||
{ | ||
return CompareTo(obj as SafeVersion); | ||
} | ||
|
||
public bool Equals(SafeVersion? other) | ||
{ | ||
return other != null && ((Version)this).Equals((Version)other); | ||
} | ||
|
||
public object Clone() | ||
{ | ||
return new SafeVersion(this); | ||
} | ||
|
||
public override bool Equals(object? obj) | ||
{ | ||
return Equals(obj as SafeVersion); | ||
} | ||
|
||
public static bool operator ==(SafeVersion? v1, SafeVersion? v2) | ||
{ | ||
// Test "right" first to allow branch elimination when inlined for null checks (== null) | ||
// so it can become a simple test | ||
if (v2 is null) | ||
{ | ||
return v1 is null; | ||
} | ||
|
||
// Quick reference equality test prior to calling the virtual Equality | ||
return ReferenceEquals(v2, v1) || v2.Equals(v1); | ||
} | ||
|
||
public static bool operator !=(SafeVersion? v1, SafeVersion? v2) => !(v1 == v2); | ||
|
||
public static bool operator <(SafeVersion? v1, SafeVersion? v2) | ||
{ | ||
if (v1 is null) | ||
{ | ||
return v2 is not null; | ||
} | ||
|
||
return v1.CompareTo(v2) < 0; | ||
} | ||
|
||
public static bool operator <=(SafeVersion? v1, SafeVersion? v2) | ||
{ | ||
if (v1 is null) | ||
{ | ||
return true; | ||
} | ||
|
||
return v1.CompareTo(v2) <= 0; | ||
} | ||
|
||
public static bool operator >(SafeVersion? v1, SafeVersion? v2) => v2 < v1; | ||
|
||
public static bool operator >=(SafeVersion? v1, SafeVersion? v2) => v2 <= v1; | ||
|
||
public override int GetHashCode() | ||
{ | ||
return HashCode.Combine(_versionParts); | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
return $"{Major}.{Minor}.{Build}.{Revision}"; | ||
} | ||
|
||
public static implicit operator Version(SafeVersion version) | ||
{ | ||
return version._versionParts.Count switch | ||
{ | ||
2 => new Version(version.Major, version.Minor), | ||
3 => new Version(version.Major, version.Minor, version.Build), | ||
4 => new Version(version.Major, version.Minor, version.Build, version.Revision), | ||
_ => new Version() | ||
}; | ||
} | ||
|
||
private void FillParts(int part) | ||
{ | ||
if (part == -1) | ||
{ | ||
return; | ||
} | ||
|
||
_versionParts.Add(part); | ||
} | ||
|
||
[GeneratedRegex(@"\d+")] | ||
private static partial Regex VersionRegex(); | ||
} |
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