ParadoxSaveParser/ParadoxSaveParser.Lib.Tests/SearchExpressionTests.cs

49 lines
1.7 KiB
C#

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));
}
}