Compare commits

..
14 Commits
Author SHA1 Message Date
Jeremy Pritts 182fca60ca Add test case for custom china version 2023-03-16 00:04:38 -04:00
Jeremy Pritts d4609533b2 Bump version to 1.4.0 2023-03-15 23:58:57 -04:00
Jeremy Pritts 46e53d834f NUnit constraint model 2023-03-15 23:56:25 -04:00
Jeremy Pritts 99157d5433 Support parsing custom versions 2023-03-15 23:52:00 -04:00
Jeremy Pritts 5ec33f0518 Simplify ToString 2023-03-15 23:32:35 -04:00
Jeremy Pritts c4a5ab77d4 Refactor parsing tests 2023-03-15 23:10:59 -04:00
Jeremy Pritts ab4df10c87 Remove ParseFromDllName 2023-03-15 23:02:59 -04:00
dependabot[bot] 2d00a1259a Bump NUnit3TestAdapter from 4.4.0 to 4.4.2
Bumps [NUnit3TestAdapter](https://github.com/nunit/nunit3-vs-adapter) from 4.4.0 to 4.4.2.
- [Release notes](https://github.com/nunit/nunit3-vs-adapter/releases)
- [Commits](https://github.com/nunit/nunit3-vs-adapter/compare/V4.4.0...V4.4.2)

---
updated-dependencies:
- dependency-name: NUnit3TestAdapter
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <[email protected]>
2023-03-01 14:05:44 -05:00
dependabot[bot] b65056d08f Bump NUnit3TestAdapter from 4.3.1 to 4.4.0
Bumps [NUnit3TestAdapter](https://github.com/nunit/nunit3-vs-adapter) from 4.3.1 to 4.4.0.
- [Release notes](https://github.com/nunit/nunit3-vs-adapter/releases)
- [Commits](https://github.com/nunit/nunit3-vs-adapter/compare/V4.3.1...V4.4.0)

---
updated-dependencies:
- dependency-name: NUnit3TestAdapter
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <[email protected]>
2023-02-27 14:18:13 -05:00
dependabot[bot] 42c5957429 Bump Microsoft.NET.Test.Sdk from 17.4.1 to 17.5.0
Bumps [Microsoft.NET.Test.Sdk](https://github.com/microsoft/vstest) from 17.4.1 to 17.5.0.
- [Release notes](https://github.com/microsoft/vstest/releases)
- [Changelog](https://github.com/microsoft/vstest/blob/main/docs/releases.md)
- [Commits](https://github.com/microsoft/vstest/compare/v17.4.1...v17.5.0)

---
updated-dependencies:
- dependency-name: Microsoft.NET.Test.Sdk
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <[email protected]>
2023-02-22 13:56:50 -05:00
dependabot[bot] 606e439721 Bump PolySharp from 1.11.0 to 1.12.1
Bumps [PolySharp](https://github.com/Sergio0694/PolySharp) from 1.11.0 to 1.12.1.
- [Release notes](https://github.com/Sergio0694/PolySharp/releases)
- [Commits](https://github.com/Sergio0694/PolySharp/compare/1.11.0...1.12.1)

---
updated-dependencies:
- dependency-name: PolySharp
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <[email protected]>
2023-01-23 17:29:41 -05:00
dependabot[bot] b9a57bd765 Bump PolySharp from 1.10.0 to 1.11.0
Bumps [PolySharp](https://github.com/Sergio0694/PolySharp) from 1.10.0 to 1.11.0.
- [Release notes](https://github.com/Sergio0694/PolySharp/releases)
- [Commits](https://github.com/Sergio0694/PolySharp/compare/1.10.0...1.11.0)

---
updated-dependencies:
- dependency-name: PolySharp
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <[email protected]>
2023-01-19 13:10:01 -05:00
Jeremy Pritts 0ed4298585 Bump version to 1.3.1 2023-01-17 01:10:40 -05:00
Jeremy Pritts 3e5ee80680 Fix #21 2023-01-17 01:09:16 -05:00
6 changed files with 102 additions and 124 deletions

No files matched your search

+26 -14
View File
@@ -9,55 +9,67 @@ public class ComparisonTests
[Test]
public void LessThanIsTransitive()
{
Assert.IsTrue(lowValue < mediumValue);
Assert.IsTrue(mediumValue < highValue);
Assert.IsTrue(lowValue < highValue);
Assert.Multiple(() =>
{
Assert.That(lowValue, Is.LessThan(mediumValue));
Assert.That(mediumValue, Is.LessThan(highValue));
Assert.That(lowValue, Is.LessThan(highValue));
});
}
[Test]
public void GreaterThanIsTransitive()
{
Assert.IsTrue(mediumValue > lowValue);
Assert.IsTrue(highValue > mediumValue);
Assert.IsTrue(highValue > lowValue);
Assert.Multiple(() =>
{
Assert.That(mediumValue, Is.GreaterThan(lowValue));
Assert.That(highValue, Is.GreaterThan(mediumValue));
Assert.That(highValue, Is.GreaterThan(lowValue));
});
}
[Test]
public void WeakInequalityHolds()
{
UnityVersion low = lowValue;
Assert.IsTrue(lowValue <= low);
Assert.That(lowValue, Is.LessThanOrEqualTo(low));
}
[Test]
public void MinimumValueDefinition()
{
Assert.IsTrue(UnityVersion.MinVersion == new UnityVersion(ushort.MinValue, ushort.MinValue, ushort.MinValue, UnityVersionType.Alpha, byte.MinValue));
Assert.That(UnityVersion.MinVersion, Is.EqualTo(new UnityVersion(ushort.MinValue, ushort.MinValue, ushort.MinValue, UnityVersionType.Alpha, byte.MinValue)));
}
[Test]
public void MaximumValueDefinition()
{
Assert.IsTrue(UnityVersion.MaxVersion == new UnityVersion(ushort.MaxValue, ushort.MaxValue, ushort.MaxValue, (UnityVersionType)byte.MaxValue, byte.MaxValue));
Assert.That(UnityVersion.MaxVersion, Is.EqualTo(new UnityVersion(ushort.MaxValue, ushort.MaxValue, ushort.MaxValue, (UnityVersionType)byte.MaxValue, byte.MaxValue)));
}
[Test]
public void AnIncreaseOfTypeNumberMakesInequal()
{
Assert.IsTrue(UnityVersion.MinVersion < new UnityVersion(ushort.MinValue, ushort.MinValue, ushort.MinValue, UnityVersionType.Alpha, 1));
Assert.IsTrue(lowValue < new UnityVersion(lowValue.Major, lowValue.Minor, lowValue.Build, lowValue.Type, (byte)(lowValue.TypeNumber + 1)));
Assert.Multiple(() =>
{
Assert.That(UnityVersion.MinVersion, Is.LessThan(new UnityVersion(ushort.MinValue, ushort.MinValue, ushort.MinValue, UnityVersionType.Alpha, 1)));
Assert.That(lowValue, Is.LessThan(new UnityVersion(lowValue.Major, lowValue.Minor, lowValue.Build, lowValue.Type, (byte)(lowValue.TypeNumber + 1))));
});
}
[Test]
public void AlphaZeroIsTheDefault()
{
Assert.IsTrue(new UnityVersion(2019, 1, 0) < new UnityVersion(2019, 1, 0, UnityVersionType.Final, 1));
Assert.That(new UnityVersion(2019, 1, 0), Is.LessThan(new UnityVersion(2019, 1, 0, UnityVersionType.Final, 1)));
}
[Test]
public void ComparisonMethodsIgnoreUnspecifiedValues()
{
Assert.IsTrue(new UnityVersion(2019, 1, 0, UnityVersionType.Final, 1).IsGreaterEqual(2019, 1, 0));
Assert.IsTrue(new UnityVersion(2019, 1, 0, UnityVersionType.Final, 1).IsLessEqual(2019, 1, 0));
Assert.Multiple(() =>
{
Assert.That(new UnityVersion(2019, 1, 0, UnityVersionType.Final, 1).IsGreaterEqual(2019, 1, 0), Is.True);
Assert.That(new UnityVersion(2019, 1, 0, UnityVersionType.Final, 1).IsLessEqual(2019, 1, 0), Is.True);
});
}
}
+4 -4
View File
@@ -16,14 +16,14 @@ public class DistanceTests
{
ulong distance1 = UnityVersion.Distance(version4, version5);
ulong distance2 = UnityVersion.Distance(version5, version4);
Assert.IsTrue(distance1 == distance2);
Assert.That(distance1, Is.EqualTo(distance2));
}
[Test]
public void DistanceIsZeroForEquivalentVersions()
{
ulong distance = UnityVersion.Distance(version4, version4);
Assert.IsTrue(distance == 0);
Assert.That(distance, Is.EqualTo(0));
}
[Test]
@@ -31,7 +31,7 @@ public class DistanceTests
{
UnityVersion version = new UnityVersion(6, 1);
UnityVersion closest = version.GetClosestVersion(versionArray);
Assert.IsTrue(closest == version6);
Assert.That(closest, Is.EqualTo(version6));
}
[Test]
@@ -39,6 +39,6 @@ public class DistanceTests
{
UnityVersion version = new UnityVersion(5, 4, 9);
UnityVersion closest = version.GetClosestVersion(versionArray);
Assert.IsTrue(closest == version5);
Assert.That(closest, Is.EqualTo(version5));
}
}
+43 -33
View File
@@ -1,45 +1,43 @@
namespace AssetRipper.VersionUtilities.Tests;
using NUnit.Framework.Internal;
using System.Collections.Generic;
namespace AssetRipper.VersionUtilities.Tests;
public class ParsingTests
{
[Test]
public void UnityVersionParsesCorrectly()
[TestCase("4.2.2f1", 4, 2, 2, UnityVersionType.Final, 1, false)]
[TestCase("4.2.2f1\n1", 4, 2, 2, UnityVersionType.Final, 1, true)]
[TestCase("2343.4.5f7", 2343, 4, 5, UnityVersionType.Final, 7, false)]
[TestCase("2019.2.2f1c2\n2", 2019, 2, 2, UnityVersionType.China, 2, true)]//Not a real version
public void UnityVersionParsesCorrectly(string versionString, int major, int minor, int build, UnityVersionType type, int typeNumber, bool customEngine)
{
string version = "2343.4.5f7";
UnityVersion expected = new UnityVersion(2343, 4, 5, UnityVersionType.Final, 7);
Assert.AreEqual(expected, UnityVersion.Parse(version));
UnityVersion expected = new UnityVersion((ushort)major, (ushort)minor, (ushort)build, type, (byte)typeNumber);
UnityVersion parsedVersion = UnityVersion.Parse(versionString, out bool parsedCustomEngine);
Assert.Multiple(() =>
{
Assert.That(parsedCustomEngine, Is.EqualTo(customEngine));
Assert.That(parsedVersion, Is.EqualTo(expected));
});
}
[Test]
public void UnityVersionParsesDllCorrectly()
[TestCaseSource(nameof(GenerateRandomVersions), new object[] { 20 })]
public void UnityVersionToStringParsesCorrectly(UnityVersion version)
{
string dllName = "_2343_4_5f7.dll";
UnityVersion expected = new UnityVersion(2343, 4, 5, UnityVersionType.Final, 7);
Assert.AreEqual(expected, UnityVersion.ParseFromDllName(dllName));
string versionString = version.ToString();
UnityVersion parsedVersion = UnityVersion.Parse(versionString, out bool customEngine);
Assert.Multiple(() =>
{
Assert.That(customEngine, Is.False);
Assert.That(parsedVersion, Is.EqualTo(version));
});
}
[Test]
public void UnityVersionParsesDllCorrectlyWithoutExtension()
{
string dllName = "_2343_4_5f7";
UnityVersion expected = new UnityVersion(2343, 4, 5, UnityVersionType.Final, 7);
Assert.AreEqual(expected, UnityVersion.ParseFromDllName(dllName));
}
[Test]
public void UnityVersionParsesDllCorrectlyWithoutLeadingUnderscore()
{
string dllName = "2343_4_5f7.dll";
UnityVersion expected = new UnityVersion(2343, 4, 5, UnityVersionType.Final, 7);
Assert.AreEqual(expected, UnityVersion.ParseFromDllName(dllName));
}
[Test]
public void MajorMinorOnly()
{
string version = "2019.4";
UnityVersion expected = new UnityVersion(2019, 4, 0, UnityVersionType.Final, 1);
Assert.AreEqual(expected, UnityVersion.Parse(version));
Assert.That(UnityVersion.Parse(version), Is.EqualTo(expected));
}
[Test]
@@ -47,18 +45,30 @@ public class ParsingTests
{
string version = "2019.4.3";
UnityVersion expected = new UnityVersion(2019, 4, 3, UnityVersionType.Final, 1);
Assert.AreEqual(expected, UnityVersion.Parse(version));
Assert.That(UnityVersion.Parse(version), Is.EqualTo(expected));
}
[Test]
public void ChinaVersionString()
{
string version = "2020.3.15f2c1";
UnityVersion expected = new UnityVersion(2020, 3, 15, UnityVersionType.China, 2);
//This is an actual version from a game.
//Versions like 2019.3.0f2 and 2020.3.15f2 don't have corresponding Chinese versions.
//Source: https://unity.cn/releases
string version = "2021.3.11f1c2";
UnityVersion expected = new UnityVersion(2021, 3, 11, UnityVersionType.China, 2);
Assert.Multiple(() =>
{
Assert.AreEqual(expected, UnityVersion.Parse(version));
Assert.AreEqual(version, expected.ToString());
Assert.That(UnityVersion.Parse(version), Is.EqualTo(expected));
Assert.That(expected.ToString(), Is.EqualTo(version));
});
}
private static IEnumerable<UnityVersion> GenerateRandomVersions(int count)
{
Randomizer random = TestContext.CurrentContext.Random;
for (int i = 0; i < count; i++)
{
yield return new UnityVersion(random.NextUShort(), random.NextUShort(), random.NextUShort(), random.NextEnum<UnityVersionType>(), random.NextByte());
}
}
}
@@ -10,14 +10,18 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.3.1" />
<PackageReference Include="NUnit3TestAdapter" Version="4.4.2" />
<PackageReference Include="NUnit.Analyzers" Version="3.6.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.2.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\VersionUtilities\VersionUtilities.csproj" />
<ProjectReference Include="..\VersionUtilities\VersionUtilities.csproj" />
</ItemGroup>
</Project>
+20 -68
View File
@@ -8,8 +8,8 @@ public readonly partial struct UnityVersion
{
private static readonly Regex majorMinorRegex = new Regex(@"^([0-9]+)\.([0-9]+)$", RegexOptions.Compiled);
private static readonly Regex majorMinorBuildRegex = new Regex(@"^([0-9]+)\.([0-9]+)\.([0-9]+)$", RegexOptions.Compiled);
private static readonly Regex normalRegex = new Regex(@"^([0-9]+)\.([0-9]+)\.([0-9]+)\.?([abfpx])([0-9]+)$", RegexOptions.Compiled);
private static readonly Regex chinaRegex = new Regex(@"^([0-9]+)\.([0-9]+)\.([0-9]+)\.?f([0-9]+)c1$", RegexOptions.Compiled);
private static readonly Regex normalRegex = new Regex(@"^([0-9]+)\.([0-9]+)\.([0-9]+)\.?([abfpx])([0-9]+)(\n[0-9]+)?$", RegexOptions.Compiled);
private static readonly Regex chinaRegex = new Regex(@"^([0-9]+)\.([0-9]+)\.([0-9]+)\.?f1c([0-9]+)(\n[0-9]+)?$", RegexOptions.Compiled);
/// <summary>
/// Serialize the version as a string
@@ -17,51 +17,9 @@ public readonly partial struct UnityVersion
/// <returns>A new string like 2019.4.3f1</returns>
public override string ToString()
{
return ToString(false, false, false, false);
}
/// <summary>
/// Serialize the version as a string
/// </summary>
/// <param name="hasUnderscorePrefix">Include the _ prefix</param>
/// <param name="useUnderscoresAsSeparators">Use underscores as separators instead of periods</param>
/// <param name="hasDllExtension">Include the .dll extension</param>
/// <param name="hasSeparatorAfterBuild">Include a separator between <see cref="Build"/> and <see cref="Type"/></param>
/// <returns>A new string generated with those parameters</returns>
public string ToString(bool hasUnderscorePrefix, bool useUnderscoresAsSeparators, bool hasDllExtension, bool hasSeparatorAfterBuild)
{
StringBuilder sb = new();
char separator = useUnderscoresAsSeparators ? '_' : '.';
if(hasUnderscorePrefix)
sb.Append('_');
sb.Append(Major);
sb.Append(separator);
sb.Append(Minor);
sb.Append(separator);
sb.Append(Build);
if (hasSeparatorAfterBuild)
{
sb.Append(separator);
}
if (Type == UnityVersionType.China)
{
sb.Append('f');
sb.Append(TypeNumber);
sb.Append("c1");
}
else
{
sb.Append(Type.ToCharacter());
sb.Append(TypeNumber);
}
if(hasDllExtension)
sb.Append(".dll");
return sb.ToString();
return Type == UnityVersionType.China
? $"{Major}.{Minor}.{Build}f1c{TypeNumber}"
: $"{Major}.{Minor}.{Build}{Type.ToCharacter()}{TypeNumber}";
}
/// <summary>
@@ -73,26 +31,6 @@ public readonly partial struct UnityVersion
return $"{Major}.{Minor}.{Build}";
}
/// <summary>
/// Parse a dll name like _2019_4_3f1.dll
/// </summary>
/// <param name="dllName">The name of a dll file</param>
/// <returns>The parsed Unity version</returns>
/// <exception cref="ArgumentNullException">If the string is null or empty</exception>
public static UnityVersion ParseFromDllName(string dllName)
{
if (string.IsNullOrEmpty(dllName))
{
throw new ArgumentNullException(nameof(dllName));
}
if (dllName[0] == '_')
{
dllName = dllName.Substring(1);
}
return Parse(dllName.Replace('_', '.').Replace(".dll", ""));
}
/// <summary>
/// Parse a normal Unity version string
/// </summary>
@@ -100,7 +38,17 @@ public readonly partial struct UnityVersion
/// <returns>The parsed Unity version</returns>
/// <exception cref="ArgumentNullException">If the string is null or empty</exception>
/// <exception cref="ArgumentException">If the string is in an invalid format</exception>
public static UnityVersion Parse(string version)
public static UnityVersion Parse(string version) => Parse(version, out _);
/// <summary>
/// Parse a normal Unity version string
/// </summary>
/// <param name="version">A string to parse</param>
/// <param name="customEngine">True if this version was generated by a custom Unity Engine.</param>
/// <returns>The parsed Unity version</returns>
/// <exception cref="ArgumentNullException">If the string is null or empty</exception>
/// <exception cref="ArgumentException">If the string is in an invalid format</exception>
public static UnityVersion Parse(string version, out bool customEngine)
{
if (string.IsNullOrEmpty(version))
{
@@ -114,6 +62,7 @@ public readonly partial struct UnityVersion
int build = int.Parse(match.Groups[3].Value);
char type = match.Groups[4].Value[0];
int typeNumber = int.Parse(match.Groups[5].Value);
customEngine = match.Groups[6].Length > 0;
return new UnityVersion((ushort)major, (ushort)minor, (ushort)build, type.ToUnityVersionType(), (byte)typeNumber);
}
else if (majorMinorBuildRegex.TryMatch(version, out match))
@@ -121,12 +70,14 @@ public readonly partial struct UnityVersion
int major = int.Parse(match.Groups[1].Value);
int minor = int.Parse(match.Groups[2].Value);
int build = int.Parse(match.Groups[3].Value);
customEngine = false;
return new UnityVersion((ushort)major, (ushort)minor, (ushort)build, UnityVersionType.Final, 1);
}
else if (majorMinorRegex.TryMatch(version, out match))
{
int major = int.Parse(match.Groups[1].Value);
int minor = int.Parse(match.Groups[2].Value);
customEngine = false;
return new UnityVersion((ushort)major, (ushort)minor, 0, UnityVersionType.Final, 1);
}
else if (chinaRegex.TryMatch(version, out match))
@@ -135,6 +86,7 @@ public readonly partial struct UnityVersion
int minor = int.Parse(match.Groups[2].Value);
int build = int.Parse(match.Groups[3].Value);
int typeNumber = int.Parse(match.Groups[4].Value);
customEngine = match.Groups[5].Length > 0;
return new UnityVersion((ushort)major, (ushort)minor, (ushort)build, UnityVersionType.China, (byte)typeNumber);
}
else
+2 -2
View File
@@ -8,7 +8,7 @@
<AssemblyName>AssetRipper.VersionUtilities</AssemblyName>
<Authors>ds5678</Authors>
<Company>AssetRipper</Company>
<Version>1.3.0.0</Version>
<Version>1.4.0.0</Version>
<AssemblyVersion>$(Version)</AssemblyVersion>
<PackageId>AssetRipper.VersionUtilities</PackageId>
<PackageTags>C# version unity unity3d</PackageTags>
@@ -32,7 +32,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="PolySharp" Version="1.10.0">
<PackageReference Include="PolySharp" Version="1.12.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>