From b80ce910b3c7065d48abcde2aa4967c55122537f Mon Sep 17 00:00:00 2001 From: Timerix Date: Sat, 5 Apr 2025 04:03:06 +0500 Subject: [PATCH] transformed my shitty test code into nuint tests --- .../ParadoxSaveParser.Lib.Tests.csproj | 29 ++++++++ .../SearchExpressionTests.cs | 49 ++++++++++++++ ParadoxSaveParser.WebAPI/Program.cs | 66 ------------------- ParadoxSaveParser.sln | 6 ++ 4 files changed, 84 insertions(+), 66 deletions(-) create mode 100644 ParadoxSaveParser.Lib.Tests/ParadoxSaveParser.Lib.Tests.csproj create mode 100644 ParadoxSaveParser.Lib.Tests/SearchExpressionTests.cs diff --git a/ParadoxSaveParser.Lib.Tests/ParadoxSaveParser.Lib.Tests.csproj b/ParadoxSaveParser.Lib.Tests/ParadoxSaveParser.Lib.Tests.csproj new file mode 100644 index 0000000..79e7bca --- /dev/null +++ b/ParadoxSaveParser.Lib.Tests/ParadoxSaveParser.Lib.Tests.csproj @@ -0,0 +1,29 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + + + + + + + + + + + + + + + diff --git a/ParadoxSaveParser.Lib.Tests/SearchExpressionTests.cs b/ParadoxSaveParser.Lib.Tests/SearchExpressionTests.cs new file mode 100644 index 0000000..a2c0180 --- /dev/null +++ b/ParadoxSaveParser.Lib.Tests/SearchExpressionTests.cs @@ -0,0 +1,49 @@ +using System.Text.Encodings.Web; +using System.Text.Json; +using DTLib.Extensions; + +namespace ParadoxSaveParser.Lib.Tests; + +[TestFixture] +[TestOf(typeof(ISearchExpression))] +public class SearchExpressionTests +{ + byte[] _smallSaveData; + + [SetUp] + public void Setup() + { + _smallSaveData = "EU4txt a={ b={ c=0 d=1 e=2 } f=3 }".ToBytes(); + } + + + private static JsonSerializerOptions _smallSaveSerializerOptions = new() + { + WriteIndented = false, + Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, + MaxDepth = 1024, + }; + + internal static string JsonToPdx(string json) => + json.Substring(1, json.Length - 2) + .Replace(",", " ").Replace("{", "{ ").Replace("}", " }") + .Replace("\"", "").Replace("[", "").Replace("]", "").Replace(":", "="); + + [TestCase("a", "a={ b={ c=0 d=1 e=2 } f=3 }")] + [TestCase("a.*", "a={ b={ c=0 d=1 e=2 } f=3 }")] + [TestCase("a.b", "a={ b={ c=0 d=1 e=2 } }")] + [TestCase("a.[0].c", "a={ b={ c=0 } }")] + [TestCase("a.[1]", "a={ f=3 }")] + [TestCase("a.b.(c|d)", "a={ b={ c=0 d=1 } }")] + [TestCase("a.(b.e|f)", "a={ b={ e=2 } f=3 }")] + public void TestSearchOnSmallData(string input, string expectedOutput) + { + using var saveStream = new MemoryStream(_smallSaveData, false); + var se = SearchExpressionCompiler.Compile(input); + var parser = new SaveParserEU4(saveStream, se); + var rootNode = parser.Parse(); + string json = JsonSerializer.Serialize(rootNode, _smallSaveSerializerOptions); + string pdx = JsonToPdx(json); + Assert.That(pdx, Is.EqualTo(expectedOutput)); + } +} \ No newline at end of file diff --git a/ParadoxSaveParser.WebAPI/Program.cs b/ParadoxSaveParser.WebAPI/Program.cs index 0638964..72aaa2e 100644 --- a/ParadoxSaveParser.WebAPI/Program.cs +++ b/ParadoxSaveParser.WebAPI/Program.cs @@ -15,7 +15,6 @@ using System.Collections.Concurrent; using System.IO; using System.Text.Encodings.Web; using DTLib.Dtsod; -using DTLib.Extensions; using DTLib.Web; using DTLib.Web.Routes; @@ -39,73 +38,9 @@ public partial class Program Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, MaxDepth = 1024, }; - - static void TestSearchExpression(Stream saveStream, TestCase tc) - { - try - { - - saveStream.Seek(0, SeekOrigin.Begin); - var se = SearchExpressionCompiler.Compile(tc.q); - var parser = new SaveParserEU4(saveStream, se); - var rootNode = parser.Parse(); - string json = JsonSerializer.Serialize(rootNode, _saveSerializerOptions); - string pdx = json.Substring(1, json.Length - 2) - .Replace(",", " ").Replace("{", "{ ").Replace("}", " }") - .Replace("\"", "").Replace("[", "").Replace("]", "").Replace(":", "="); - if (pdx == tc.a) - { - Console.WriteLine($"[OK] q:'{tc.q}' a:'{tc.a}'"); - } - else - { - Console.WriteLine($"[Invalid] q:'{tc.q}' a:'{tc.a}' r:'{pdx}'"); - } - } - catch (Exception ex) - { - Console.WriteLine($"[Error] q:'{tc.q}' a:'{tc.a}' e:\n" + ex.ToStringDemystified()); - } - } - - record TestCase(string q, string a); public static void Main(string[] args) { - - using var saveStream = new MemoryStream( - "EU4txt a={ b={ c=0 d=1 e=2 } f=3 }".ToBytes(), - false); - - TestCase[] testCases = [ - new("a", - "a={ b={ c=0 d=1 e=2 } f=3 }"), - - new("a.*", - "a={ b={ c=0 d=1 e=2 } f=3 }"), - - new("a.b", - "a={ b={ c=0 d=1 e=2 } }"), - - new("a.[0].c", - "a={ b={ c=0 } }"), - - new("a.[1]", - "a={ f=3 }"), - - new("a.b.(c|d)", - "a={ b={ c=0 d=1 } }"), - - new("a.(b.e|f)", - "a={ b={ e=2 } f=3 }"), - ]; - - foreach (var test in testCases) - { - TestSearchExpression(saveStream, test); - } - -/* Console.InputEncoding = Encoding.UTF8; Console.OutputEncoding = Encoding.UTF8; Console.CursorVisible = false; @@ -148,7 +83,6 @@ public partial class Program { logger.LogError(ex.ToStringDemystified()); } -*/ } public static void PrepareLocalFiles() diff --git a/ParadoxSaveParser.sln b/ParadoxSaveParser.sln index f3bde14..9034b81 100644 --- a/ParadoxSaveParser.sln +++ b/ParadoxSaveParser.sln @@ -10,6 +10,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SolutionFolder", "SolutionF TODO.txt = TODO.txt EndProjectSection EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ParadoxSaveParser.Lib.Tests", "ParadoxSaveParser.Lib.Tests\ParadoxSaveParser.Lib.Tests.csproj", "{23F4BE1B-3043-4821-9F65-74FF5F57FA59}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -24,5 +26,9 @@ Global {53ED0135-9513-4DE2-9187-CF2899F179B3}.Debug|Any CPU.Build.0 = Debug|Any CPU {53ED0135-9513-4DE2-9187-CF2899F179B3}.Release|Any CPU.ActiveCfg = Release|Any CPU {53ED0135-9513-4DE2-9187-CF2899F179B3}.Release|Any CPU.Build.0 = Release|Any CPU + {23F4BE1B-3043-4821-9F65-74FF5F57FA59}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {23F4BE1B-3043-4821-9F65-74FF5F57FA59}.Debug|Any CPU.Build.0 = Debug|Any CPU + {23F4BE1B-3043-4821-9F65-74FF5F57FA59}.Release|Any CPU.ActiveCfg = Release|Any CPU + {23F4BE1B-3043-4821-9F65-74FF5F57FA59}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal