Skip to content

Commit

Permalink
Fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Aragas committed Jan 7, 2024
1 parent c1750bd commit 810e539
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ private static bool TryGetContent<T>(MemberInfo memberInfo,
var value = GetFunction(typeof(T), instance, memberInfo)();
if (value is null)
{
Type memberType = memberInfo is PropertyInfo propertyInfo ? propertyInfo.PropertyType : ((MethodInfo) memberInfo).ReturnType;
var memberType = memberInfo is PropertyInfo propertyInfo ? propertyInfo.PropertyType : ((MethodInfo) memberInfo).ReturnType;
errorMessage += $"is of type: {memberType.Name}. A Member flagged with a Content attribute must be " +
$"of one of the types listed in {nameof(PrefabExtensionInsertPatch.PrefabExtensionContentAttribute)}";
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,12 @@

<ItemGroup Condition="!Exists($(GameFolder))">
<PackageReference Include="Bannerlord.ReferenceAssemblies.Core" Version="$(GameVersion).*" PrivateAssets="All" />
<PackageReference Include="Bannerlord.ReferenceAssemblies.Native" Version="$(GameVersion).*" PrivateAssets="All" />
<PackageReference Include="Bannerlord.ReferenceAssemblies.StoryMode" Version="$(GameVersion).*" PrivateAssets="All" />
<PackageReference Include="Bannerlord.ReferenceAssemblies.SandBox" Version="$(GameVersion).*" PrivateAssets="All" />
</ItemGroup>

<ItemGroup Condition="Exists($(GameFolder))">
<Reference Include="$(GameFolder)\bin\Win64_Shipping_Client\TaleWorlds.*.dll" Exclude="$(GameFolder)\bin\Win64_Shipping_Client\TaleWorlds.Native.dll">
<HintPath>%(Identity)</HintPath>
</Reference>
<Reference Include="$(GameFolder)\Modules\Native\bin\Win64_Shipping_Client\*.dll">
<HintPath>%(Identity)</HintPath>
</Reference>
<Reference Include="$(GameFolder)\Modules\SandBox\bin\Win64_Shipping_Client\*.dll">
<HintPath>%(Identity)</HintPath>
</Reference>
<Reference Include="$(GameFolder)\Modules\StoryMode\bin\Win64_Shipping_Client\*.dll">
<HintPath>%(Identity)</HintPath>
</Reference>
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 1 addition & 3 deletions tests/Bannerlord.UIExtenderEx.Tests/BaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

namespace Bannerlord.UIExtenderEx.Tests;

public class BaseTests
public class BaseTests : SharedTests
{
protected class MockWidgetFactory : WidgetFactory
{
Expand Down Expand Up @@ -119,8 +119,6 @@ public void OneTimeSetup()
{
_ = Assembly.Load("TaleWorlds.Engine.GauntletUI");

System.Diagnostics.Trace.Listeners.Clear();

var property = AccessTools2.DeclaredProperty("TaleWorlds.Engine.GauntletUI.UIResourceManager:WidgetFactory");
property!.SetValue(null, new MockWidgetFactory());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,32 @@ public class PrefabsTests : BaseTests
{
private const int Elements = 6;

private UIExtender? _uiExtender;

[SetUp]
public void Setup()
{
_uiExtender = UIExtender.Create("TestModule");
_uiExtender.Register(new[]
{
typeof(TestPrefabExtensionInsertAsSiblingAppendPatch),
typeof(TestPrefabExtensionInsertAsSiblingPrependPatch),
typeof(TestPrefabExtensionInsertPatch),
typeof(TestPrefabExtensionReplacePatch),
typeof(TestPrefabExtensionSetAttributePatch),
});
_uiExtender.Enable();
}

[TearDown]
public void Finalization()
{
_uiExtender?.Deregister();
}

[Test]
public void PrefabsInsertTest()
{
var uiExtender = UIExtender.Create("TestModule");
uiExtender.Register(typeof(PrefabsTests).Assembly);
uiExtender.Enable();

var widgetTemplateInsert = UIResourceManager.WidgetFactory.GetCustomType("Insert").RootTemplate;
var childrenInsert1 = GetChildren(widgetTemplateInsert);
var childrenInsert2 = GetChildren(childrenInsert1[0]);
Expand Down Expand Up @@ -53,17 +72,12 @@ public void PrefabsInsertTest()
var childrenSetAttribute3 = GetChildren(childrenSetAttribute2[0]);
Assert.True(childrenSetAttribute3.Count == Elements);
Assert.True(childrenSetAttribute3[3].AllAttributes.Any(a => a.Key == "CustomAttribute" && a.Value == "Value"));

uiExtender.Deregister();
}

[Test]
public void PrefabsInsertTestDisabled()
{
var uiExtender = UIExtender.Create("TestModule");
uiExtender.Register(typeof(PrefabsTests).Assembly);
uiExtender.Enable();
uiExtender.Disable(typeof(TestPrefabExtensionInsertAsSiblingAppendPatch));
_uiExtender.Disable(typeof(TestPrefabExtensionInsertAsSiblingAppendPatch));

Check warning on line 80 in tests/Bannerlord.UIExtenderEx.Tests/Prefabs/IntegrationTests/PrefabsTests.cs

View workflow job for this annotation

GitHub Actions / Test

Dereference of a possibly null reference.

Check warning on line 80 in tests/Bannerlord.UIExtenderEx.Tests/Prefabs/IntegrationTests/PrefabsTests.cs

View workflow job for this annotation

GitHub Actions / Test

Dereference of a possibly null reference.

Check warning on line 80 in tests/Bannerlord.UIExtenderEx.Tests/Prefabs/IntegrationTests/PrefabsTests.cs

View workflow job for this annotation

GitHub Actions / Test

Dereference of a possibly null reference.

Check warning on line 80 in tests/Bannerlord.UIExtenderEx.Tests/Prefabs/IntegrationTests/PrefabsTests.cs

View workflow job for this annotation

GitHub Actions / Test

Dereference of a possibly null reference.

var widgetTemplateInsert = UIResourceManager.WidgetFactory.GetCustomType("Insert").RootTemplate;
var childrenInsert1 = GetChildren(widgetTemplateInsert);
Expand Down Expand Up @@ -99,7 +113,5 @@ public void PrefabsInsertTestDisabled()
var childrenSetAttribute3 = GetChildren(childrenSetAttribute2[0]);
Assert.True(childrenSetAttribute3.Count == Elements);
Assert.True(childrenSetAttribute3[3].AllAttributes.Any(a => a.Key == "CustomAttribute" && a.Value == "Value"));

uiExtender.Deregister();
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
using Bannerlord.UIExtenderEx.Tests.Prefabs.IntegrationTests;
using NUnit.Framework;

using NUnit.Framework;

using System.Collections.Generic;
using System.Linq;

using TaleWorlds.Engine.GauntletUI;
using TaleWorlds.GauntletUI.PrefabSystem;

namespace Bannerlord.UIExtenderEx.Tests.Prefabs2;

Expand All @@ -18,8 +14,21 @@ public class Prefabs2Tests : BaseTests
[SetUp]
public void Setup()
{
_uiExtender = UIExtender.Create("TestModule2");
_uiExtender.Register(typeof(PrefabsTests).Assembly);
_uiExtender = UIExtender.Create("TestModule");
_uiExtender.Register(new[]
{
typeof(TestPrefabExtensionInsertFileNamePatch),
typeof(TestPrefabExtensionInsertFileNamePatchRemoveRootNode),
typeof(TestPrefabExtensionInsertTextPatch),
typeof(TestPrefabExtensionInsertTextPatchRemoveRootNode),
typeof(TestPrefabExtensionInsertXmlDocumentPatch),
typeof(TestPrefabExtensionInsertXmlDocumentPatchRemoveRootNode),
typeof(TestPrefabExtensionInsertXmlNodePatch),
typeof(TestPrefabExtensionInsertXmlNodePatchRemoveRootNode),
typeof(TestPrefabExtensionInsertXmlNodesPatch),
typeof(TestPrefabExtensionRemovePatch),
typeof(TestPrefabExtensionSetAttributePatch),
});
_uiExtender.Enable();
}

Expand All @@ -33,9 +42,9 @@ public void Finalization()
public void Prefabs2_Insert()
{
var widgetTemplateInsert = UIResourceManager.WidgetFactory.GetCustomType("Insert2").RootTemplate;
List<WidgetTemplate>? optionsScreenWidget = GetChildren(widgetTemplateInsert);
List<WidgetTemplate>? standardTopPanel = GetChildren(optionsScreenWidget[0]);
List<WidgetTemplate>? listPanel = GetChildren(standardTopPanel[0]);
var optionsScreenWidget = GetChildren(widgetTemplateInsert);
var standardTopPanel = GetChildren(optionsScreenWidget[0]);
var listPanel = GetChildren(standardTopPanel[0]);
Assert.AreEqual(OptionsTabToggleCount + 1, listPanel.Count, $"Children were: {string.Join(", ", listPanel.Select(x => $"Type: {x.Type} | Id: {x.Id}\n"))}");
Assert.AreEqual("Insert", listPanel[3].Id);
}
Expand All @@ -44,9 +53,9 @@ public void Prefabs2_Insert()
public void Prefabs2_SetAttribute()
{
var widgetTemplateSetAttribute = UIResourceManager.WidgetFactory.GetCustomType("SetAttribute2").RootTemplate;
List<WidgetTemplate>? optionsScreenWidget = GetChildren(widgetTemplateSetAttribute);
List<WidgetTemplate>? standardTopPanel = GetChildren(optionsScreenWidget[0]);
List<WidgetTemplate>? listPanel = GetChildren(standardTopPanel[0]);
var optionsScreenWidget = GetChildren(widgetTemplateSetAttribute);
var standardTopPanel = GetChildren(optionsScreenWidget[0]);
var listPanel = GetChildren(standardTopPanel[0]);
Assert.AreEqual(OptionsTabToggleCount, listPanel.Count, $"Children were: {string.Join(", ", listPanel.Select(x => $"Type: {x.Type} | Id: {x.Id}\n"))}");
Assert.IsTrue(listPanel[3].AllAttributes.Any(a => a.Key == "CustomAttribute" && a.Value == "Value"));
Assert.IsTrue(listPanel[3].AllAttributes.Any(a => a.Key == "CustomAttribute2" && a.Value == "Value2"));
Expand All @@ -56,8 +65,8 @@ public void Prefabs2_SetAttribute()
public void Prefabs2_Append()
{
var widgetTemplateAppend = UIResourceManager.WidgetFactory.GetCustomType("Append2").RootTemplate;
List<WidgetTemplate>? optionsScreenWidget = GetChildren(widgetTemplateAppend);
List<WidgetTemplate>? standardTopPanel = GetChildren(optionsScreenWidget[0]);
var optionsScreenWidget = GetChildren(widgetTemplateAppend);
var standardTopPanel = GetChildren(optionsScreenWidget[0]);
Assert.AreEqual(2, standardTopPanel.Count);
Assert.AreEqual("Append", standardTopPanel[1].Id);
}
Expand All @@ -66,31 +75,33 @@ public void Prefabs2_Append()
public void Prefabs2_Prepend()
{
var widgetTemplateAppend = UIResourceManager.WidgetFactory.GetCustomType("Prepend2").RootTemplate;
List<WidgetTemplate>? optionsScreenWidget = GetChildren(widgetTemplateAppend);
List<WidgetTemplate>? standardTopPanel = GetChildren(optionsScreenWidget[0]);
var optionsScreenWidget = GetChildren(widgetTemplateAppend);
var standardTopPanel = GetChildren(optionsScreenWidget[0]);
Assert.AreEqual(2, standardTopPanel.Count);
Assert.AreEqual("Prepend", standardTopPanel[0].Id);
}

/*
[Test]
public void Prefabs2_ReplaceKeepChildren()
{
var widgetTemplateAppend = UIResourceManager.WidgetFactory.GetCustomType("ReplaceKeepChildren2").RootTemplate;
List<WidgetTemplate>? optionsScreenWidget = GetChildren(widgetTemplateAppend);
List<WidgetTemplate>? standardTopPanel = GetChildren(optionsScreenWidget[0]);
List<WidgetTemplate>? listPanel = GetChildren(standardTopPanel[0]);
var optionsScreenWidget = UIResourceManager.WidgetFactory.GetCustomType("ReplaceKeepChildren2").RootTemplate;
var standardTopPanel = GetChildren(optionsScreenWidget);
var validRoot = GetChildren(standardTopPanel[0]);
var listPanel = GetChildren(validRoot[0]);
Assert.AreEqual(1, standardTopPanel.Count);
Assert.AreEqual("ReplaceKeepChildren", standardTopPanel[0].Id);
Assert.AreEqual(OptionsTabToggleCount, listPanel.Count, "ReplaceKeepChildren did not keep the original child count. " +
$"Remaining Children: {string.Join(", ", listPanel.Select(x => $"Type: {x.Type} | Id: {x.Id}\n"))}");
}
*/

[Test]
public void Prefabs2_AppendRemoveRootNode()
{
var widgetTemplateAppend = UIResourceManager.WidgetFactory.GetCustomType("AppendRemoveRootNode").RootTemplate;
List<WidgetTemplate>? optionsScreenWidget = GetChildren(widgetTemplateAppend);
List<WidgetTemplate>? standardTopPanel = GetChildren(optionsScreenWidget[0]);
var optionsScreenWidget = GetChildren(widgetTemplateAppend);
var standardTopPanel = GetChildren(optionsScreenWidget[0]);
Assert.AreEqual(3, standardTopPanel.Count);
Assert.AreEqual("Append1", standardTopPanel[1].Id);
Assert.AreEqual("Append2", standardTopPanel[2].Id);
Expand All @@ -100,20 +111,21 @@ public void Prefabs2_AppendRemoveRootNode()
public void Prefabs2_PrependRemoveRootNode()
{
var widgetTemplateAppend = UIResourceManager.WidgetFactory.GetCustomType("PrependRemoveRootNode").RootTemplate;
List<WidgetTemplate>? optionsScreenWidget = GetChildren(widgetTemplateAppend);
List<WidgetTemplate>? standardTopPanel = GetChildren(optionsScreenWidget[0]);
var optionsScreenWidget = GetChildren(widgetTemplateAppend);
var standardTopPanel = GetChildren(optionsScreenWidget[0]);
Assert.AreEqual(3, standardTopPanel.Count);
Assert.AreEqual("Prepend1", standardTopPanel[0].Id);
Assert.AreEqual("Prepend2", standardTopPanel[1].Id);
}

/*
[Test]
public void Prefabs2_ReplaceKeepChildrenRemoveRootNode()
{
var widgetTemplateAppend = UIResourceManager.WidgetFactory.GetCustomType("ReplaceKeepChildrenRemoveRootNode").RootTemplate;
List<WidgetTemplate>? optionsScreenWidget = GetChildren(widgetTemplateAppend);
List<WidgetTemplate>? standardTopPanel = GetChildren(optionsScreenWidget[0]);
List<WidgetTemplate>? customListPanel3 = GetChildren(standardTopPanel[2]);
var optionsScreenWidget = GetChildren(widgetTemplateAppend);
var standardTopPanel = GetChildren(optionsScreenWidget[0]);
var customListPanel3 = GetChildren(standardTopPanel[2]);
Assert.AreEqual(4, standardTopPanel.Count);
Assert.AreEqual("ReplaceKeepChildren1", standardTopPanel[0].Id);
Assert.AreEqual("ReplaceKeepChildren2", standardTopPanel[1].Id);
Expand All @@ -122,4 +134,5 @@ public void Prefabs2_ReplaceKeepChildrenRemoveRootNode()
Assert.AreEqual(OptionsTabToggleCount, customListPanel3.Count, "ReplaceKeepChildren did not insert the children into correct new node. " +
$"Should have been {standardTopPanel[2].Id}. Was: {standardTopPanel.FirstOrDefault(x => GetChildren(x).Count > 0)?.Id ?? "None"}");
}
*/
}
Loading

0 comments on commit 810e539

Please sign in to comment.