Fix trailing zeros regex, add unit tests

This commit is contained in:
Tyfon
2024-04-30 16:34:07 -07:00
parent 17cea8e958
commit 107d598b7e
5 changed files with 87 additions and 5 deletions

View File

@@ -14,7 +14,7 @@ using UnityEngine;
namespace UIFixes
{
internal class ItemPanelPatches
public class ItemPanelPatches
{
private static FieldInfo AttributeCompactPanelDictionaryField;
private static FieldInfo AttributeCompactDropdownDictionaryField;
@@ -329,7 +329,7 @@ namespace UIFixes
}
// Remove trailing 0s
text = Regex.Replace(text, @"(\d)(\.[0-9]*[^0])?\.?0+\b", "$1$2");
text = RemoveTrailingZeros(text);
// Fix spacing
text = text.Replace(" %", "%");
@@ -412,5 +412,16 @@ namespace UIFixes
}
}
}
public static string RemoveTrailingZeros(string input)
{
// This matches: a number (so it doesn't apply to periods in words), named "integer"
// Followed by either
// a) a dot, some digits, and then a non-zero digit (named "significantDigits"), which is followed by one or more trailing 0
// b) a dot and some trailing 0
// And all that is replaced to the original integer, and the significantDigits (if they exist)
// If neither matches this doesn't match and does nothing
return Regex.Replace(input, @"(?<integer>\d)((?<significantDecimals>\.[0-9]*[^0])0*\b)?(\.0+\b)?", "${integer}${significantDecimals}");
}
}
}

View File

@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\UIFixes.csproj" />
</ItemGroup>
<ItemGroup>
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" />
</ItemGroup>
</Project>

34
UIFixes.Test/UnitTests.cs Normal file
View File

@@ -0,0 +1,34 @@
namespace UIFixes.Test
{
[TestClass]
public class UnitTests
{
[TestMethod]
public void RemoveTrailingZeroTest()
{
var testCases = new Dictionary<string, string>
{
{ "10", "10" },
{ "400", "400" },
{ "4.01", "4.01" },
{ "5.060", "5.06" },
{ "3.000", "3" },
{ "2.0000001000", "2.0000001" },
{ "0.5", "0.5" },
{ "0.05", "0.05" },
{ "0.50", "0.5" },
{ "400sec", "400sec" },
{ "del. 2sec", "del. 2sec" },
{ "Hello.world", "Hello.world" },
{ "2Fast20Furious0", "2Fast20Furious0" },
{ "1.0.2", "1.0.2" }
};
foreach (var testCase in testCases)
{
string result = ItemPanelPatches.RemoveTrailingZeros(testCase.Key);
Assert.AreEqual(result, testCase.Value);
}
}
}
}

View File

@@ -1,14 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net472</TargetFramework>
<TargetFramework>net471</TargetFramework>
<AssemblyName>Tyfon.UIFixes</AssemblyName>
<Description>SPT UI Fixes</Description>
<Version>1.3.1</Version>
<Version>1.3.2</Version>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<PropertyGroup>
<DefaultItemExcludes>$(DefaultItemExcludes);UIFixes.Test\**</DefaultItemExcludes>
</PropertyGroup>
<PropertyGroup>
<PathToSPT Condition="'$(Configuration)'=='Debug'">..\..\..\..\SPT\3.8.0-debug</PathToSPT>
<PathToSPT Condition="'$(Configuration)'=='Release'">..\..\..\..\SPT\3.8.0</PathToSPT>

View File

@@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.9.34723.18
MinimumVisualStudioVersion = 10.0.40219.1
Project("{A1684561-953B-4240-9F71-E812C02803E4}") = "UIFixes", "UIFixes.csproj", "{240CCF3A-4A9E-4C7D-96BB-AD7C375892BF}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UIFixes", "UIFixes.csproj", "{240CCF3A-4A9E-4C7D-96BB-AD7C375892BF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UIFixes.Test", "UIFixes.Test\UIFixes.Test.csproj", "{FE27D858-0061-4BAD-BE4D-FABBDB53213F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -15,6 +17,10 @@ Global
{240CCF3A-4A9E-4C7D-96BB-AD7C375892BF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{240CCF3A-4A9E-4C7D-96BB-AD7C375892BF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{240CCF3A-4A9E-4C7D-96BB-AD7C375892BF}.Release|Any CPU.Build.0 = Release|Any CPU
{FE27D858-0061-4BAD-BE4D-FABBDB53213F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FE27D858-0061-4BAD-BE4D-FABBDB53213F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FE27D858-0061-4BAD-BE4D-FABBDB53213F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FE27D858-0061-4BAD-BE4D-FABBDB53213F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE