Skip to content

Commit

Permalink
Merge pull request #5 from anno-mods/devel/v0.4-fixes
Browse files Browse the repository at this point in the history
v0.4 fixes
  • Loading branch information
jakobharder authored Jun 19, 2022
2 parents a5fe869 + d49afc9 commit 0a28992
Show file tree
Hide file tree
Showing 23 changed files with 1,494 additions and 246 deletions.
16 changes: 11 additions & 5 deletions AnnoMapEditor.Tests/AnnoMapEditor.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,25 @@
</ItemGroup>

<ItemGroup>
<None Update="TestMaps\campaign_chapter03_colony01.xml">
<None Update="TestData\assets.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="TestMaps\colony02_01.xml">
<None Update="TestData\campaign_chapter03_colony01.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="TestMaps\moderate_c_01.xml">
<None Update="TestData\colony02_01.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="TestMaps\moderate_islandarc_ss_01.xml">
<None Update="TestData\moderate_c_01.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="TestMaps\scenario_02_colony_01.xml">
<None Update="TestData\moderate_islandarc_ss_01.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="TestData\scenario_02_colony_01.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Utils\xmltest.exe">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
Expand Down
96 changes: 96 additions & 0 deletions AnnoMapEditor.Tests/Assets.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using AnnoMapEditor.MapTemplates;
using AnnoMapEditor.MapTemplates.Serializing;
using AnnoMapEditor.Mods;
using AnnoMapEditor.Tests.Utils;
using System.Text;
using System.Xml.Linq;

namespace AnnoMapEditor.Tests
{
static class CollectionExtensions
{
public static bool HasNoDuplicates<TSource, TResult>(this IEnumerable<TSource> that, Func<TSource, TResult> selector)
{
var asList = that.Select(selector);
var asSet = new HashSet<TResult>(asList);
return asList.ToArray().SequenceEqual(asSet.ToArray());
}
}

public class PatchedAssetsFixture : IDisposable
{
public readonly Dictionary<MapType, XDocument> Data;

public PatchedAssetsFixture()
{
using Stream assetsXml = File.OpenRead("./TestData/assets.xml");
Data = new(MapType.GetAllTypes().Select(x =>
{
Stream patch = new MemoryStream(Encoding.Unicode.GetBytes(Mods.Mod.CreateAssetsModOps(MapType.Archipelago, "mods/[Map] test/test.a7t")));
return new KeyValuePair<MapType, XDocument>(x, XDocument.Load(XmlTest.Patch(assetsXml, patch)!));
}));
}

public void Dispose()
{
}
}

public class MapTypeData : IEnumerable<object[]>
{
public IEnumerator<object[]> GetEnumerator()
{
var types = MapType.GetAllTypes();
foreach (var type in types)
yield return new object[] { type };
}

System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => GetEnumerator();
}

public class Assets : IClassFixture<PatchedAssetsFixture>
{
PatchedAssetsFixture assetsFixture;

public Assets(PatchedAssetsFixture fixture)
{
assetsFixture = fixture;
}

[Theory]
[ClassData(typeof(MapTypeData))]
public void NotEmpty(MapType mapType)
{
var xml = assetsFixture.Data[mapType];
var assets = xml.Descendants("Asset");
Assert.NotEmpty(assets);
}

[Theory]
[ClassData(typeof(MapTypeData))]
public void IsPatched(MapType mapType)
{
var xml = assetsFixture.Data[mapType];
var assets = xml.Descendants("Asset");
Assert.NotEmpty(assets.Where(x => x.GetValueFromPath("Values/MapTemplate/TemplateFilename")?.StartsWith("mods/[Map]") ?? false));
}

[Theory]
[ClassData(typeof(MapTypeData))]
public void NoDuplicateName(MapType mapType)
{
var xml = assetsFixture.Data[mapType];
var assets = xml.Descendants("Asset");
Assert.True(assets.HasNoDuplicates(x => x.GetValueFromPath("Values/Standard/Name") ?? ""));
}

[Theory]
[ClassData(typeof(MapTypeData))]
public void NoDuplicateTemplateFilename(MapType mapType)
{
var xml = assetsFixture.Data[mapType];
var assets = xml.Descendants("Asset");
Assert.True(assets.HasNoDuplicates(x => x.GetValueFromPath("Values/MapTemplate/TemplateFilename") ?? ""));
}
}
}
10 changes: 5 additions & 5 deletions AnnoMapEditor.Tests/RoundTrip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ namespace AnnoMapEditor.Tests
public class RoundTrip
{
[Theory]
[InlineData("./TestMaps/moderate_c_01.xml")]
[InlineData("./TestMaps/campaign_chapter03_colony01.xml")]
[InlineData("./TestMaps/moderate_islandarc_ss_01.xml")]
[InlineData("./TestMaps/colony02_01.xml")]
[InlineData("./TestMaps/scenario_02_colony_01.xml")]
[InlineData("./TestData/moderate_c_01.xml")]
[InlineData("./TestData/campaign_chapter03_colony01.xml")]
[InlineData("./TestData/moderate_islandarc_ss_01.xml")]
[InlineData("./TestData/colony02_01.xml")]
[InlineData("./TestData/scenario_02_colony_01.xml")]
public async Task XmlToA7tinfoToXml(string filePath)
{
using Stream inputXml = File.OpenRead(filePath);
Expand Down
Loading

0 comments on commit 0a28992

Please sign in to comment.