using System.IO; using System.Text.Encodings.Web; using System.Text.Json; using System.Text.Json.Serialization; using DTLib.Extensions; namespace ParadoxSaveParser.Lib.Tests; [TestFixture] [TestOf(typeof(ISearchExpression))] public class SearchExpressionTests { [SetUp] public void Setup() { _smallSaveData = "EU4txt a={ b={ c=0 d=1 e=2 } f=3 }".ToBytes(); } private byte[] _smallSaveData; private static readonly JsonSerializerOptions _smallSaveSerializerOptions = new() { WriteIndented = false, Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, MaxDepth = 1024, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, }; 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)); } }