Refactored unit tests to use NUnit and added DI usage

This commit is contained in:
Alex
2025-07-22 19:41:34 +01:00
parent c852debf2b
commit 5bbf671acd
15 changed files with 988 additions and 979 deletions
+20 -5
View File
@@ -1,19 +1,31 @@
using Microsoft.Extensions.DependencyInjection;
using NUnit.Framework;
using SPTarkov.DI;
using SPTarkov.Server.Core.DI;
using SPTarkov.Server.Core.Models.Spt.Mod;
using SPTarkov.Server.Core.Utils;
using SPTarkov.Server.Core.Utils.Logger.Handlers;
using UnitTests.Mock;
namespace UnitTests;
[TestClass]
[TestFixture]
public class DI
{
private static IServiceProvider _serviceProvider;
[AssemblyInitialize]
public static void ConfigureServices(TestContext context)
private static DI? _instance;
private DI()
{
ConfigureServices();
}
public static DI GetInstance()
{
return _instance ??= new DI();
}
private void ConfigureServices()
{
if (_serviceProvider != null)
{
@@ -39,12 +51,15 @@ public class DI
foreach (var onLoad in _serviceProvider.GetServices<IOnLoad>())
{
if (onLoad is FileLogHandler)
{
continue;
}
onLoad.OnLoad().Wait();
}
}
public static T GetService<T>()
where T : notnull
public T GetService<T>() where T : notnull
{
return _serviceProvider.GetRequiredService<T>();
}
-1
View File
@@ -1 +0,0 @@
[assembly: Parallelize(Scope = ExecutionScope.MethodLevel)]
@@ -1,14 +1,15 @@
using SPTarkov.Server.Core.Extensions;
using NUnit.Framework;
using SPTarkov.Server.Core.Extensions;
namespace UnitTests.Tests.Extensions;
[TestClass]
[TestFixture]
public partial class ContainerExtensionsTests
{
[TestInitialize]
[SetUp]
public void Initialize() { }
[TestMethod]
[Test]
public void CanItemBePlacedInContainerAtPosition_1x1_Item_Fits_1x2_Container_At_0x0()
{
var container = new int[1, 2];
@@ -27,7 +28,7 @@ public partial class ContainerExtensionsTests
Assert.IsTrue(result);
}
[TestMethod]
[Test]
public void CanItemBePlacedInContainerAtPosition_1x1_Item_Fails_1x2_Container_At_0x0_With_Item_At_0x0()
{
var container = new int[1, 2];
@@ -47,7 +48,7 @@ public partial class ContainerExtensionsTests
Assert.IsFalse(result);
}
[TestMethod]
[Test]
public void CanItemBePlacedInContainerAtPosition_1x2_Item_Fits_1x2_Container_At_0x0()
{
var container = new int[2, 1];
@@ -66,7 +67,7 @@ public partial class ContainerExtensionsTests
Assert.IsTrue(result);
}
[TestMethod]
[Test]
public void CanItemBePlacedInContainerAtPosition_1x2_Item_Fails_1x2_Container_At_0x0_With_Item_At_0x0()
{
var container = new int[1, 2];
@@ -86,7 +87,7 @@ public partial class ContainerExtensionsTests
Assert.IsFalse(result);
}
[TestMethod]
[Test]
public void CanItemBePlacedInContainerAtPosition_2x2_Item_Fits_2x2_Container_At_0x0()
{
var container = new int[2, 2];
@@ -105,7 +106,7 @@ public partial class ContainerExtensionsTests
Assert.IsTrue(result);
}
[TestMethod]
[Test]
public void CanItemBePlacedInContainerAtPosition_1x2_Item_Fits_2x2_Container_At_0x1()
{
var container = new int[2, 2];
@@ -127,7 +128,7 @@ public partial class ContainerExtensionsTests
public partial class ContainerExtensionsTests
{
[TestMethod]
[Test]
public void FindSlotForItem_1x1_item_fits_1x1_container_no_rotation()
{
var container = new int[1, 1];
@@ -142,7 +143,7 @@ public partial class ContainerExtensionsTests
Assert.AreEqual(result.Y, 0);
}
[TestMethod]
[Test]
public void FindSlotForItem_1x2_item_fits_3x3_container_rotated_with_items()
{
// |1|1|1|
@@ -167,7 +168,7 @@ public partial class ContainerExtensionsTests
Assert.AreEqual(result.Y, 1);
}
[TestMethod]
[Test]
public void FindSlotForItem_1x1_item_fails_1x1_container_no_space()
{
var container = new int[1, 1];
@@ -180,7 +181,7 @@ public partial class ContainerExtensionsTests
Assert.IsFalse(result.Success);
}
[TestMethod]
[Test]
public void FindSlotForItem_1x2_item_fits_1x2_container_no_rotation()
{
var container = new int[2, 1];
@@ -195,7 +196,7 @@ public partial class ContainerExtensionsTests
Assert.AreEqual(result.Y, 0);
}
[TestMethod]
[Test]
public void FindSlotForItem_1x2_item_fails_1x2_container_no_space()
{
var container = new int[1, 1];
@@ -209,7 +210,7 @@ public partial class ContainerExtensionsTests
Assert.IsFalse(result.Success);
}
[TestMethod]
[Test]
public void FindSlotForItem_2x2_item_fits_2x2_container_no_rotation()
{
var container = new int[2, 2];
@@ -224,7 +225,7 @@ public partial class ContainerExtensionsTests
Assert.AreEqual(result.Y, 0);
}
[TestMethod]
[Test]
public void FindSlotForItem_1x2_item_fits_2x2_container_no_rotation_with_item_at_0x0()
{
var container = new int[2, 2];
@@ -243,7 +244,7 @@ public partial class ContainerExtensionsTests
public partial class ContainerExtensionsTests
{
[TestMethod]
[Test]
public void FillContainerMapWithItem_1x1_at_0x0_in_1x1_no_rotation()
{
var container = new int[1, 1];
@@ -266,7 +267,7 @@ public partial class ContainerExtensionsTests
Assert.AreEqual(container[0, 0], 1);
}
[TestMethod]
[Test]
public void FillContainerMapWithItem_1x2_at_0x0_in_1x2_no_rotation()
{
var container = new int[2, 1];
@@ -290,7 +291,7 @@ public partial class ContainerExtensionsTests
Assert.AreEqual(container[1, 0], 1);
}
[TestMethod]
[Test]
public void FillContainerMapWithItem_2x2_at_0x0_in_2x2_no_rotation()
{
var container = new int[2, 2];
@@ -314,7 +315,7 @@ public partial class ContainerExtensionsTests
Assert.AreEqual(container[1, 1], 1);
}
[TestMethod]
[Test]
public void FillContainerMapWithItem_1x2_at_0x0_in_2x2_with_rotation()
{
var container = new int[2, 2];
@@ -338,7 +339,7 @@ public partial class ContainerExtensionsTests
Assert.AreEqual(container[0, 1], 1);
}
[TestMethod]
[Test]
public void FillContainerMapWithItem_1x2_at_1x0_in_2x2_with_rotation_with_existing_item()
{
var container = new int[2, 2];
+145 -145
View File
@@ -1,162 +1,162 @@
using SPTarkov.Server.Core.Extensions;
using NUnit.Framework;
using SPTarkov.Server.Core.Extensions;
using SPTarkov.Server.Core.Models.Common;
using SPTarkov.Server.Core.Models.Eft.Common.Tables;
namespace UnitTests.Tests.Extensions
namespace UnitTests.Tests.Extensions;
[TestFixture]
public class ItemTests
{
[TestClass]
public class ItemTests
[SetUp]
public void Initialize() { }
[Test]
public void FindAndReturnChildrenAsItems_one_child_mods_only()
{
[TestInitialize]
public void Initialize() { }
[TestMethod]
public void FindAndReturnChildrenAsItems_one_child_mods_only()
var testData = new List<Item>();
var rootItem = new Item
{
var testData = new List<Item>();
var rootItem = new Item
{
Id = new MongoId(),
Template = ItemTpl.AMMOBOX_127X33_COPPER_20RND,
};
var childItem = new Item
{
Id = new MongoId(),
Template = ItemTpl.AMMO_127X33_COPPER,
ParentId = rootItem.Id,
};
testData.Add(rootItem);
testData.Add(childItem);
var result = testData.FindAndReturnChildrenAsItems(rootItem.Id, true);
Assert.AreEqual(result[1].Id, childItem.Id);
}
[TestMethod]
public void FindAndReturnChildrenAsItems_mods_only_one_inventory_item()
Id = new MongoId(),
Template = ItemTpl.AMMOBOX_127X33_COPPER_20RND,
};
var childItem = new Item
{
var testData = new List<Item>();
var rootItem = new Item
{
Id = new MongoId(),
Template = ItemTpl.AMMOBOX_127X33_COPPER_20RND,
};
var childItem = new Item
{
Id = new MongoId(),
Template = ItemTpl.AMMO_127X33_COPPER,
ParentId = rootItem.Id,
Location = 1,
};
var childItem2 = new Item
{
Id = new MongoId(),
Template = ItemTpl.AMMO_26X75_GREEN,
ParentId = rootItem.Id,
};
testData.Add(rootItem);
testData.Add(childItem);
testData.Add(childItem2);
Id = new MongoId(),
Template = ItemTpl.AMMO_127X33_COPPER,
ParentId = rootItem.Id,
};
testData.Add(rootItem);
testData.Add(childItem);
var result = testData.FindAndReturnChildrenAsItems(rootItem.Id, true);
var result = testData.FindAndReturnChildrenAsItems(rootItem.Id, true);
Assert.AreEqual(result[1].Id, childItem2.Id);
Assert.AreEqual(result.Count, 2);
}
Assert.AreEqual(result[1].Id, childItem.Id);
}
[TestMethod]
public void FindAndReturnChildrenAsItems_mods_and_inventory_item()
[Test]
public void FindAndReturnChildrenAsItems_mods_only_one_inventory_item()
{
var testData = new List<Item>();
var rootItem = new Item
{
var testData = new List<Item>();
var rootItem = new Item
{
Id = new MongoId(),
Template = ItemTpl.AMMOBOX_127X33_COPPER_20RND,
};
var childItem = new Item
{
Id = new MongoId(),
Template = ItemTpl.AMMO_127X33_COPPER,
ParentId = rootItem.Id,
Location = 1,
};
var childItem2 = new Item
{
Id = new MongoId(),
Template = ItemTpl.AMMO_26X75_GREEN,
ParentId = rootItem.Id,
};
testData.Add(rootItem);
testData.Add(childItem);
testData.Add(childItem2);
var result = testData.FindAndReturnChildrenAsItems(rootItem.Id, false);
Assert.AreEqual(result[1].Id, childItem.Id);
Assert.AreEqual(result.Count, 3);
}
[TestMethod]
public void FindAndReturnChildrenAsItems_mod_with_child()
Id = new MongoId(),
Template = ItemTpl.AMMOBOX_127X33_COPPER_20RND,
};
var childItem = new Item
{
var testData = new List<Item>();
var rootItem = new Item
{
Id = new MongoId(),
Template = ItemTpl.AMMOBOX_127X33_COPPER_20RND,
};
var childItem = new Item
{
Id = new MongoId(),
Template = ItemTpl.AMMO_127X33_COPPER,
ParentId = rootItem.Id,
};
var childOfChild = new Item
{
Id = new MongoId(),
Template = ItemTpl.AMMO_26X75_GREEN,
ParentId = childItem.Id,
};
testData.Add(rootItem);
testData.Add(childItem);
testData.Add(childOfChild);
var result = testData.FindAndReturnChildrenAsItems(rootItem.Id, true);
Assert.AreEqual(result[1].Id, childItem.Id);
Assert.AreEqual(result.Count, 3);
}
[TestMethod]
public void FindAndReturnChildrenAsItems_no_matching_children()
Id = new MongoId(),
Template = ItemTpl.AMMO_127X33_COPPER,
ParentId = rootItem.Id,
Location = 1,
};
var childItem2 = new Item
{
var testData = new List<Item>();
var rootItem = new Item
{
Id = new MongoId(),
Template = ItemTpl.AMMOBOX_127X33_COPPER_20RND,
};
var childItem = new Item
{
Id = new MongoId(),
Template = ItemTpl.AMMO_127X33_COPPER,
ParentId = new MongoId(),
};
var childOfChild = new Item
{
Id = new MongoId(),
Template = ItemTpl.AMMO_26X75_GREEN,
ParentId = childItem.Id,
};
testData.Add(rootItem);
testData.Add(childItem);
testData.Add(childOfChild);
Id = new MongoId(),
Template = ItemTpl.AMMO_26X75_GREEN,
ParentId = rootItem.Id,
};
testData.Add(rootItem);
testData.Add(childItem);
testData.Add(childItem2);
var result = testData.FindAndReturnChildrenAsItems(rootItem.Id, true);
var result = testData.FindAndReturnChildrenAsItems(rootItem.Id, true);
Assert.AreEqual(result[0].Id, rootItem.Id);
Assert.AreEqual(result.Count, 1);
}
Assert.AreEqual(result[1].Id, childItem2.Id);
Assert.AreEqual(result.Count, 2);
}
[Test]
public void FindAndReturnChildrenAsItems_mods_and_inventory_item()
{
var testData = new List<Item>();
var rootItem = new Item
{
Id = new MongoId(),
Template = ItemTpl.AMMOBOX_127X33_COPPER_20RND,
};
var childItem = new Item
{
Id = new MongoId(),
Template = ItemTpl.AMMO_127X33_COPPER,
ParentId = rootItem.Id,
Location = 1,
};
var childItem2 = new Item
{
Id = new MongoId(),
Template = ItemTpl.AMMO_26X75_GREEN,
ParentId = rootItem.Id,
};
testData.Add(rootItem);
testData.Add(childItem);
testData.Add(childItem2);
var result = testData.FindAndReturnChildrenAsItems(rootItem.Id, false);
Assert.AreEqual(result[1].Id, childItem.Id);
Assert.AreEqual(result.Count, 3);
}
[Test]
public void FindAndReturnChildrenAsItems_mod_with_child()
{
var testData = new List<Item>();
var rootItem = new Item
{
Id = new MongoId(),
Template = ItemTpl.AMMOBOX_127X33_COPPER_20RND,
};
var childItem = new Item
{
Id = new MongoId(),
Template = ItemTpl.AMMO_127X33_COPPER,
ParentId = rootItem.Id,
};
var childOfChild = new Item
{
Id = new MongoId(),
Template = ItemTpl.AMMO_26X75_GREEN,
ParentId = childItem.Id,
};
testData.Add(rootItem);
testData.Add(childItem);
testData.Add(childOfChild);
var result = testData.FindAndReturnChildrenAsItems(rootItem.Id, true);
Assert.AreEqual(result[1].Id, childItem.Id);
Assert.AreEqual(result.Count, 3);
}
[Test]
public void FindAndReturnChildrenAsItems_no_matching_children()
{
var testData = new List<Item>();
var rootItem = new Item
{
Id = new MongoId(),
Template = ItemTpl.AMMOBOX_127X33_COPPER_20RND,
};
var childItem = new Item
{
Id = new MongoId(),
Template = ItemTpl.AMMO_127X33_COPPER,
ParentId = new MongoId(),
};
var childOfChild = new Item
{
Id = new MongoId(),
Template = ItemTpl.AMMO_26X75_GREEN,
ParentId = childItem.Id,
};
testData.Add(rootItem);
testData.Add(childItem);
testData.Add(childOfChild);
var result = testData.FindAndReturnChildrenAsItems(rootItem.Id, true);
Assert.AreEqual(result[0].Id, rootItem.Id);
Assert.AreEqual(result.Count, 1);
}
}
@@ -1,75 +1,69 @@
using SPTarkov.Server.Core.Generators;
using NUnit.Framework;
using SPTarkov.Server.Core.Generators;
using SPTarkov.Server.Core.Helpers;
using SPTarkov.Server.Core.Models.Common;
using SPTarkov.Server.Core.Services;
using SPTarkov.Server.Core.Utils;
namespace UnitTests.Tests.Generators
namespace UnitTests.Tests.Generators;
[TestFixture]
public class BotWeaponGeneratorTests
{
[TestClass]
public class BotWeaponGeneratorTests
{
private BotWeaponGenerator _botWeaponGenerator;
private DatabaseService _databaseService;
private InventoryHelper _inventoryHelper;
private BotWeaponGenerator _botWeaponGenerator;
private DatabaseService _databaseService;
private InventoryHelper _inventoryHelper;
[TestInitialize]
public void Initialize()
[OneTimeSetUp]
public void Initialize()
{
_botWeaponGenerator = DI.GetInstance().GetService<BotWeaponGenerator>();
_databaseService = DI.GetInstance().GetService<DatabaseService>();
_inventoryHelper = DI.GetInstance().GetService<InventoryHelper>();
}
[Test]
public void GenerateWeaponByTpl_generate_m4_pmc()
{
var usecTemplate = _databaseService.GetBots().Types["usec"];
var botTemplateInventory = usecTemplate.BotInventory;
var sessionId = new MongoId();
var weaponTpl = ItemTpl.ASSAULTRIFLE_COLT_M4A1_556X45_ASSAULT_RIFLE;
const string slotName = "FirstPrimaryWeapon";
var weaponModChances = usecTemplate.BotChances.WeaponModsChances;
foreach (var (key, _) in weaponModChances)
{
_botWeaponGenerator = DI.GetService<BotWeaponGenerator>();
var databaseImporter = DI.GetService<DatabaseImporter>();
_databaseService = DI.GetService<DatabaseService>();
_inventoryHelper = DI.GetService<InventoryHelper>();
Task.Factory.StartNew(() =>
{
databaseImporter.OnLoad();
});
// Set all mods to 100%
weaponModChances[key] = 100d;
}
[TestMethod]
public void GenerateWeaponByTpl_generate_m4_pmc()
var weaponParentId = new MongoId();
for (var i = 0; i < 100; i++)
{
var usecTemplate = _databaseService.GetBots().Types["usec"];
var botTemplateInventory = usecTemplate.BotInventory;
var result = _botWeaponGenerator.GenerateWeaponByTpl(
sessionId,
weaponTpl,
slotName,
botTemplateInventory,
weaponParentId,
weaponModChances,
"pmcUSEC",
true,
69
);
var sessionId = new MongoId();
var weaponTpl = ItemTpl.ASSAULTRIFLE_COLT_M4A1_556X45_ASSAULT_RIFLE;
const string slotName = "FirstPrimaryWeapon";
var weaponModChances = usecTemplate.BotChances.WeaponModsChances;
foreach (var (key, _) in weaponModChances)
{
// Set all mods to 100%
weaponModChances[key] = 100d;
}
var itemSize = _inventoryHelper.GetItemSize(
weaponTpl,
result.Weapon[0].Id,
result.Weapon
);
var weaponParentId = new MongoId();
Assert.AreEqual(weaponTpl, result.WeaponTemplate.Id);
for (var i = 0; i < 100; i++)
{
var result = _botWeaponGenerator.GenerateWeaponByTpl(
sessionId,
weaponTpl,
slotName,
botTemplateInventory,
weaponParentId,
weaponModChances,
"pmcUSEC",
true,
69
);
var itemSize = _inventoryHelper.GetItemSize(
weaponTpl,
result.Weapon[0].Id,
result.Weapon
);
Assert.AreEqual(weaponTpl, result.WeaponTemplate.Id);
// Ensure it's bigger than just weapon lower
Assert.AreNotEqual(2, itemSize.Item1);
Assert.AreNotEqual(1, itemSize.Item2);
}
// Ensure it's bigger than just weapon lower
Assert.AreNotEqual(2, itemSize.Item1);
Assert.AreNotEqual(1, itemSize.Item2);
}
}
}
+432 -434
View File
@@ -1,159 +1,403 @@
using SPTarkov.Server.Core.Helpers;
using NUnit.Framework;
using SPTarkov.Server.Core.Generators;
using SPTarkov.Server.Core.Helpers;
using SPTarkov.Server.Core.Models.Common;
using SPTarkov.Server.Core.Models.Eft.Common;
using SPTarkov.Server.Core.Models.Eft.Common.Tables;
using SPTarkov.Server.Core.Models.Enums;
using SPTarkov.Server.Core.Utils;
namespace UnitTests.Tests.Helpers
namespace UnitTests.Tests.Helpers;
[TestFixture]
public class BotGeneratorHelperTests
{
[TestClass]
public class BotGeneratorHelperTests
private BotGeneratorHelper _botGeneratorHelper;
private BotLootGenerator _botLootGenerator;
[OneTimeSetUp]
public void Initialize()
{
private BotGeneratorHelper _botGeneratorHelper;
_botGeneratorHelper = DI.GetInstance().GetService<BotGeneratorHelper>();
_botLootGenerator = DI.GetInstance().GetService<BotLootGenerator>();
}
[TestInitialize]
public void Initialize()
#region AddItemWithChildrenToEquipmentSlot
[Test]
public void AddItemWithChildrenToEquipmentSlot_fit_vertical()
{
var stashId = new MongoId();
var equipmentId = new MongoId();
var botInventory = new BotBaseInventory
{
_botGeneratorHelper = DI.GetService<BotGeneratorHelper>();
var databaseImporter = DI.GetService<DatabaseImporter>();
Task.Factory.StartNew(() =>
{
databaseImporter.OnLoad();
});
;
}
Items = [],
Stash = stashId,
Equipment = equipmentId,
};
#region AddItemWithChildrenToEquipmentSlot
[TestMethod]
public void AddItemWithChildrenToEquipmentSlot_fit_vertical()
// Create backpack on player
var backpack = new Item
{
var stashId = new MongoId();
var equipmentId = new MongoId();
var botInventory = new BotBaseInventory
{
Items = [],
Stash = stashId,
Equipment = equipmentId,
};
Id = new MongoId(),
// Has a 3grids, first is a 3hx5v grid
Template = ItemTpl.BACKPACK_EBERLESTOCK_G2_GUNSLINGER_II_BACKPACK_DRY_EARTH,
ParentId = equipmentId,
SlotId = "Backpack",
};
botInventory.Items.Add(backpack);
// Create backpack on player
var backpack = new Item
var rootWeaponId = new MongoId();
var weaponWithChildren = CreateMp18(rootWeaponId);
var result = _botGeneratorHelper.AddItemWithChildrenToEquipmentSlot(
[EquipmentSlots.Backpack],
rootWeaponId,
ItemTpl.SHOTGUN_MP18_762X54R_SINGLESHOT_RIFLE,
weaponWithChildren,
botInventory
);
Assert.AreEqual(ItemAddedResult.SUCCESS, result);
var weaponRoot = weaponWithChildren.FirstOrDefault(item => item.Id == rootWeaponId);
Assert.AreEqual((weaponRoot.Location as ItemLocation).X, 0);
Assert.AreEqual((weaponRoot.Location as ItemLocation).Y, 0);
Assert.AreEqual((weaponRoot.Location as ItemLocation).R, ItemRotation.Vertical);
}
private static List<Item> CreateMp18(MongoId rootWeaponId)
{
var weaponWithChildren = new List<Item>();
var weaponRoot = new Item
{
Id = rootWeaponId,
Template = ItemTpl.SHOTGUN_MP18_762X54R_SINGLESHOT_RIFLE,
};
weaponWithChildren.Add(weaponRoot);
var weaponStock = new Item
{
Id = new MongoId(),
Template = ItemTpl.STOCK_MP18_WOODEN,
ParentId = weaponRoot.Id,
SlotId = "mod_stock",
};
weaponWithChildren.Add(weaponStock);
var weaponBarrel = new Item
{
Id = new MongoId(),
Template = ItemTpl.BARREL_MP18_762X54R_600MM,
ParentId = weaponRoot.Id,
SlotId = "mod_barrel",
};
weaponWithChildren.Add(weaponBarrel);
return weaponWithChildren;
}
[Test]
public void AddItemWithChildrenToEquipmentSlot_fit_horizontal()
{
var stashId = new MongoId();
var equipmentId = new MongoId();
var botInventory = new BotBaseInventory
{
Items = [],
Stash = stashId,
Equipment = equipmentId,
};
// Create backpack on player
var backpack = new Item
{
Id = new MongoId(),
Template = ItemTpl.BACKPACK_ANA_TACTICAL_BETA_2_BATTLE_BACKPACK_OLIVE_DRAB,
ParentId = equipmentId,
SlotId = "Backpack",
};
botInventory.Items.Add(backpack);
var rootWeaponId = new MongoId();
var weaponWithChildren = CreateMp18(rootWeaponId);
var result = _botGeneratorHelper.AddItemWithChildrenToEquipmentSlot(
[EquipmentSlots.Backpack],
rootWeaponId,
ItemTpl.SHOTGUN_MP18_762X54R_SINGLESHOT_RIFLE,
weaponWithChildren,
botInventory
);
_botLootGenerator.AddLootFromPool(new Dictionary<MongoId, double>{{ItemTpl.BARTER_MALBORO_CIGARETTES, 1}, {ItemTpl.FOREGRIP_SAKO_TRG_M10_GRIP_PAD, 1}, {ItemTpl.BARTER_GOLD_SKULL_RING, 1}, {ItemTpl.BARTER_PACK_OF_NAILS, 1}}, [EquipmentSlots.Backpack], 4, botInventory, "assault", null);
Assert.AreEqual(ItemAddedResult.SUCCESS, result);
var weaponRoot = weaponWithChildren.FirstOrDefault(item => item.Id == rootWeaponId);
Assert.AreEqual((weaponRoot.Location as ItemLocation).X, 0);
Assert.AreEqual((weaponRoot.Location as ItemLocation).Y, 0);
Assert.AreEqual((weaponRoot.Location as ItemLocation).R, ItemRotation.Horizontal);
}
/// <summary>
/// Backpack with one bullet in top row, blocking gun from being placed at 0,0
/// </summary>
[Test]
public void AddItemWithChildrenToEquipmentSlot_fit_vertical_with_items_in_backpack()
{
var botInventory = new BotBaseInventory { Items = [] };
var backpack = new Item
{
Id = new MongoId(),
// Has a 3hx5v grid first
Template = ItemTpl.BACKPACK_EBERLESTOCK_G2_GUNSLINGER_II_BACKPACK_DRY_EARTH,
SlotId = "Backpack",
};
botInventory.Items.Add(backpack);
botInventory.Items.Add(
new Item
{
Id = new MongoId(),
// Has a 3grids, first is a 3hx5v grid
Template = ItemTpl.BACKPACK_EBERLESTOCK_G2_GUNSLINGER_II_BACKPACK_DRY_EARTH,
ParentId = equipmentId,
SlotId = "Backpack",
};
botInventory.Items.Add(backpack);
Template = ItemTpl.AMMO_762X25TT_AKBS,
ParentId = backpack.Id,
SlotId = "main",
Location = new ItemLocation
{
X = 0,
Y = 0,
R = ItemRotation.Horizontal,
},
Upd = new Upd { StackObjectsCount = 1 },
}
);
var rootWeaponId = new MongoId();
var weaponWithChildren = CreateMp18(rootWeaponId);
var rootWeaponId = new MongoId();
var weaponWithChildren = CreateMp18(rootWeaponId);
var result = _botGeneratorHelper.AddItemWithChildrenToEquipmentSlot(
[EquipmentSlots.Backpack],
rootWeaponId,
ItemTpl.SHOTGUN_MP18_762X54R_SINGLESHOT_RIFLE,
weaponWithChildren,
botInventory
);
var result = _botGeneratorHelper.AddItemWithChildrenToEquipmentSlot(
[EquipmentSlots.Backpack],
rootWeaponId,
ItemTpl.SHOTGUN_MP18_762X54R_SINGLESHOT_RIFLE,
weaponWithChildren,
botInventory
);
Assert.AreEqual(ItemAddedResult.SUCCESS, result);
Assert.AreEqual(ItemAddedResult.SUCCESS, result);
var weaponRoot = weaponWithChildren.FirstOrDefault(item => item.Id == rootWeaponId);
Assert.AreEqual((weaponRoot.Location as ItemLocation).X, 0);
Assert.AreEqual((weaponRoot.Location as ItemLocation).Y, 0);
Assert.AreEqual((weaponRoot.Location as ItemLocation).R, ItemRotation.Vertical);
}
var weaponRoot = weaponWithChildren.FirstOrDefault(item => item.Id == rootWeaponId);
Assert.AreEqual((weaponRoot.Location as ItemLocation).X, 1);
Assert.AreEqual((weaponRoot.Location as ItemLocation).Y, 0);
Assert.AreEqual((weaponRoot.Location as ItemLocation).R, ItemRotation.Vertical);
}
private static List<Item> CreateMp18(MongoId rootWeaponId)
/// <summary>
/// No space for gun
/// </summary>
[Test]
public void AddItemWithChildrenToEquipmentSlot_no_space_in_first_grid_choose_second_grid()
{
var botInventory = new BotBaseInventory { Items = [] };
var backpack = new Item
{
var weaponWithChildren = new List<Item>();
var weaponRoot = new Item
{
Id = rootWeaponId,
Template = ItemTpl.SHOTGUN_MP18_762X54R_SINGLESHOT_RIFLE,
};
weaponWithChildren.Add(weaponRoot);
var weaponStock = new Item
Id = new MongoId(),
// Has a 3hx5v grid first
Template = ItemTpl.BACKPACK_EBERLESTOCK_G2_GUNSLINGER_II_BACKPACK_DRY_EARTH,
SlotId = "Backpack",
};
botInventory.Items.Add(backpack);
botInventory.Items.AddRange(
new Item
{
Id = new MongoId(),
Template = ItemTpl.STOCK_MP18_WOODEN,
ParentId = weaponRoot.Id,
SlotId = "mod_stock",
};
weaponWithChildren.Add(weaponStock);
var weaponBarrel = new Item
Template = ItemTpl.AMMO_762X25TT_AKBS,
ParentId = backpack.Id,
SlotId = "main",
Location = new ItemLocation
{
X = 0,
Y = 0,
R = ItemRotation.Horizontal,
},
Upd = new Upd { StackObjectsCount = 1 },
},
new Item
{
Id = new MongoId(),
Template = ItemTpl.BARREL_MP18_762X54R_600MM,
ParentId = weaponRoot.Id,
SlotId = "mod_barrel",
};
weaponWithChildren.Add(weaponBarrel);
Template = ItemTpl.AMMO_762X25TT_AKBS,
ParentId = backpack.Id,
SlotId = "main",
Location = new ItemLocation
{
X = 1,
Y = 0,
R = ItemRotation.Horizontal,
},
Upd = new Upd { StackObjectsCount = 1 },
},
new Item
{
Id = new MongoId(),
Template = ItemTpl.AMMO_762X25TT_AKBS,
ParentId = backpack.Id,
SlotId = "main",
Location = new ItemLocation
{
X = 2,
Y = 0,
R = ItemRotation.Horizontal,
},
Upd = new Upd { StackObjectsCount = 1 },
}
);
return weaponWithChildren;
}
var rootWeaponId = new MongoId();
var weaponWithChildren = CreateMp18(rootWeaponId);
[TestMethod]
public void AddItemWithChildrenToEquipmentSlot_fit_horizontal()
var result = _botGeneratorHelper.AddItemWithChildrenToEquipmentSlot(
[EquipmentSlots.Backpack],
rootWeaponId,
ItemTpl.SHOTGUN_MP18_762X54R_SINGLESHOT_RIFLE,
weaponWithChildren,
botInventory
);
Assert.AreEqual(ItemAddedResult.SUCCESS, result);
var weaponRoot = weaponWithChildren.FirstOrDefault(item => item.Id == rootWeaponId);
Assert.AreEqual("1", weaponRoot.SlotId);
Assert.AreEqual((weaponRoot.Location as ItemLocation).X, 0);
Assert.AreEqual((weaponRoot.Location as ItemLocation).Y, 0);
Assert.AreEqual((weaponRoot.Location as ItemLocation).R, ItemRotation.Vertical);
}
/// <summary>
/// No space for gun
/// </summary>
[Test]
public void AddItemWithChildrenToEquipmentSlot_no_space()
{
var botInventory = new BotBaseInventory { Items = [] };
var backpack = new Item
{
var stashId = new MongoId();
var equipmentId = new MongoId();
var botInventory = new BotBaseInventory
{
Items = [],
Stash = stashId,
Equipment = equipmentId,
};
Id = new MongoId(),
// Has a 4hx5v grid first
Template = ItemTpl.BACKPACK_WARTECH_BERKUT_BB102_BACKPACK_ATACS_FG,
SlotId = "Backpack",
};
botInventory.Items.Add(backpack);
// Create backpack on player
var backpack = new Item
botInventory.Items.AddRange(
new Item
{
Id = new MongoId(),
Template = ItemTpl.BACKPACK_ANA_TACTICAL_BETA_2_BATTLE_BACKPACK_OLIVE_DRAB,
ParentId = equipmentId,
SlotId = "Backpack",
};
botInventory.Items.Add(backpack);
Template = ItemTpl.AMMO_762X25TT_AKBS,
ParentId = backpack.Id,
SlotId = "main",
Location = new ItemLocation
{
X = 0,
Y = 0,
R = ItemRotation.Horizontal,
},
Upd = new Upd { StackObjectsCount = 1 },
},
new Item
{
Id = new MongoId(),
Template = ItemTpl.AMMO_762X25TT_AKBS,
ParentId = backpack.Id,
SlotId = "main",
Location = new ItemLocation
{
X = 1,
Y = 0,
R = ItemRotation.Horizontal,
},
Upd = new Upd { StackObjectsCount = 1 },
},
new Item
{
Id = new MongoId(),
Template = ItemTpl.AMMO_762X25TT_AKBS,
ParentId = backpack.Id,
SlotId = "main",
Location = new ItemLocation
{
X = 2,
Y = 0,
R = ItemRotation.Horizontal,
},
Upd = new Upd { StackObjectsCount = 1 },
},
new Item
{
Id = new MongoId(),
Template = ItemTpl.AMMO_762X25TT_AKBS,
ParentId = backpack.Id,
SlotId = "main",
Location = new ItemLocation
{
X = 3,
Y = 0,
R = ItemRotation.Horizontal,
},
Upd = new Upd { StackObjectsCount = 1 },
}
);
var rootWeaponId = new MongoId();
var weaponWithChildren = CreateMp18(rootWeaponId);
var rootWeaponId = new MongoId();
var weaponWithChildren = CreateMp18(rootWeaponId);
var result = _botGeneratorHelper.AddItemWithChildrenToEquipmentSlot(
[EquipmentSlots.Backpack],
rootWeaponId,
ItemTpl.SHOTGUN_MP18_762X54R_SINGLESHOT_RIFLE,
weaponWithChildren,
botInventory
);
var result = _botGeneratorHelper.AddItemWithChildrenToEquipmentSlot(
[EquipmentSlots.Backpack],
rootWeaponId,
ItemTpl.SHOTGUN_MP18_762X54R_SINGLESHOT_RIFLE,
weaponWithChildren,
botInventory
);
Assert.AreEqual(ItemAddedResult.SUCCESS, result);
Assert.AreEqual(ItemAddedResult.NO_SPACE, result);
}
var weaponRoot = weaponWithChildren.FirstOrDefault(item => item.Id == rootWeaponId);
Assert.AreEqual((weaponRoot.Location as ItemLocation).X, 0);
Assert.AreEqual((weaponRoot.Location as ItemLocation).Y, 0);
Assert.AreEqual((weaponRoot.Location as ItemLocation).R, ItemRotation.Horizontal);
}
/// <summary>
/// Backpack with one bullet in top row, blocking gun from being placed at 0,0
/// </summary>
[TestMethod]
public void AddItemWithChildrenToEquipmentSlot_fit_vertical_with_items_in_backpack()
/// <summary>
/// Fill all slots except for a 2x6 rectangle, with the top right corner filled, result should be no space
/// </summary>
[Test]
public void AddItemWithChildrenToEquipmentSlot_custom_gun_no_space()
{
var botInventory = new BotBaseInventory { Items = [] };
var backpack = new Item
{
var botInventory = new BotBaseInventory { Items = [] };
var backpack = new Item
{
Id = new MongoId(),
// Has a 3hx5v grid first
Template = ItemTpl.BACKPACK_EBERLESTOCK_G2_GUNSLINGER_II_BACKPACK_DRY_EARTH,
SlotId = "Backpack",
};
botInventory.Items.Add(backpack);
Id = new MongoId(),
// Has a 4hx5v grid first
Template = ItemTpl.BACKPACK_GRUPPA_99_T30_BACKPACK_BLACK,
SlotId = "Backpack",
};
botInventory.Items.Add(backpack);
var takenSlots = new List<XY>
{
new() { X = 1, Y = 0 },
new() { X = 2, Y = 0 },
new() { X = 3, Y = 0 },
new() { X = 4, Y = 0 },
new() { X = 2, Y = 1 },
new() { X = 3, Y = 1 },
new() { X = 4, Y = 1 },
new() { X = 2, Y = 2 },
new() { X = 3, Y = 2 },
new() { X = 4, Y = 2 },
new() { X = 2, Y = 3 },
new() { X = 3, Y = 3 },
new() { X = 4, Y = 3 },
new() { X = 2, Y = 4 },
new() { X = 3, Y = 4 },
new() { X = 4, Y = 4 },
new() { X = 2, Y = 5 },
new() { X = 3, Y = 5 },
new() { X = 4, Y = 5 },
};
foreach (var takenSlot in takenSlots)
{
botInventory.Items.Add(
new Item
{
@@ -163,335 +407,89 @@ namespace UnitTests.Tests.Helpers
SlotId = "main",
Location = new ItemLocation
{
X = 0,
Y = 0,
X = (int)takenSlot.X.Value,
Y = (int)takenSlot.Y.Value,
R = ItemRotation.Horizontal,
},
Upd = new Upd { StackObjectsCount = 1 },
}
);
var rootWeaponId = new MongoId();
var weaponWithChildren = CreateMp18(rootWeaponId);
var result = _botGeneratorHelper.AddItemWithChildrenToEquipmentSlot(
[EquipmentSlots.Backpack],
rootWeaponId,
ItemTpl.SHOTGUN_MP18_762X54R_SINGLESHOT_RIFLE,
weaponWithChildren,
botInventory
);
Assert.AreEqual(ItemAddedResult.SUCCESS, result);
var weaponRoot = weaponWithChildren.FirstOrDefault(item => item.Id == rootWeaponId);
Assert.AreEqual((weaponRoot.Location as ItemLocation).X, 1);
Assert.AreEqual((weaponRoot.Location as ItemLocation).Y, 0);
Assert.AreEqual((weaponRoot.Location as ItemLocation).R, ItemRotation.Vertical);
}
/// <summary>
/// No space for gun
/// </summary>
[TestMethod]
public void AddItemWithChildrenToEquipmentSlot_no_space_in_first_grid_choose_second_grid()
var rootWeaponId = new MongoId();
var weaponWithChildren = new List<Item>();
var root = new Item
{
var botInventory = new BotBaseInventory { Items = [] };
var backpack = new Item
{
Id = new MongoId(),
// Has a 3hx5v grid first
Template = ItemTpl.BACKPACK_EBERLESTOCK_G2_GUNSLINGER_II_BACKPACK_DRY_EARTH,
SlotId = "Backpack",
};
botInventory.Items.Add(backpack);
Id = rootWeaponId,
Template = ItemTpl.ASSAULTRIFLE_MOLOT_ARMS_VPO136_VEPRKM_762X39_CARBINE,
};
weaponWithChildren.Add(root);
botInventory.Items.AddRange(
new Item
{
Id = new MongoId(),
Template = ItemTpl.AMMO_762X25TT_AKBS,
ParentId = backpack.Id,
SlotId = "main",
Location = new ItemLocation
{
X = 0,
Y = 0,
R = ItemRotation.Horizontal,
},
Upd = new Upd { StackObjectsCount = 1 },
},
new Item
{
Id = new MongoId(),
Template = ItemTpl.AMMO_762X25TT_AKBS,
ParentId = backpack.Id,
SlotId = "main",
Location = new ItemLocation
{
X = 1,
Y = 0,
R = ItemRotation.Horizontal,
},
Upd = new Upd { StackObjectsCount = 1 },
},
new Item
{
Id = new MongoId(),
Template = ItemTpl.AMMO_762X25TT_AKBS,
ParentId = backpack.Id,
SlotId = "main",
Location = new ItemLocation
{
X = 2,
Y = 0,
R = ItemRotation.Horizontal,
},
Upd = new Upd { StackObjectsCount = 1 },
}
);
var rootWeaponId = new MongoId();
var weaponWithChildren = CreateMp18(rootWeaponId);
var result = _botGeneratorHelper.AddItemWithChildrenToEquipmentSlot(
[EquipmentSlots.Backpack],
rootWeaponId,
ItemTpl.SHOTGUN_MP18_762X54R_SINGLESHOT_RIFLE,
weaponWithChildren,
botInventory
);
Assert.AreEqual(ItemAddedResult.SUCCESS, result);
var weaponRoot = weaponWithChildren.FirstOrDefault(item => item.Id == rootWeaponId);
Assert.AreEqual("1", weaponRoot.SlotId);
Assert.AreEqual((weaponRoot.Location as ItemLocation).X, 0);
Assert.AreEqual((weaponRoot.Location as ItemLocation).Y, 0);
Assert.AreEqual((weaponRoot.Location as ItemLocation).R, ItemRotation.Vertical);
}
/// <summary>
/// No space for gun
/// </summary>
[TestMethod]
public void AddItemWithChildrenToEquipmentSlot_no_space()
var stock = new Item
{
var botInventory = new BotBaseInventory { Items = [] };
var backpack = new Item
{
Id = new MongoId(),
// Has a 4hx5v grid first
Template = ItemTpl.BACKPACK_WARTECH_BERKUT_BB102_BACKPACK_ATACS_FG,
SlotId = "Backpack",
};
botInventory.Items.Add(backpack);
Id = new MongoId(),
Template = ItemTpl.STOCK_VPO136_VEPRKM_WOODEN,
ParentId = root.Id,
SlotId = "mod_stock",
};
weaponWithChildren.Add(stock);
botInventory.Items.AddRange(
new Item
{
Id = new MongoId(),
Template = ItemTpl.AMMO_762X25TT_AKBS,
ParentId = backpack.Id,
SlotId = "main",
Location = new ItemLocation
{
X = 0,
Y = 0,
R = ItemRotation.Horizontal,
},
Upd = new Upd { StackObjectsCount = 1 },
},
new Item
{
Id = new MongoId(),
Template = ItemTpl.AMMO_762X25TT_AKBS,
ParentId = backpack.Id,
SlotId = "main",
Location = new ItemLocation
{
X = 1,
Y = 0,
R = ItemRotation.Horizontal,
},
Upd = new Upd { StackObjectsCount = 1 },
},
new Item
{
Id = new MongoId(),
Template = ItemTpl.AMMO_762X25TT_AKBS,
ParentId = backpack.Id,
SlotId = "main",
Location = new ItemLocation
{
X = 2,
Y = 0,
R = ItemRotation.Horizontal,
},
Upd = new Upd { StackObjectsCount = 1 },
},
new Item
{
Id = new MongoId(),
Template = ItemTpl.AMMO_762X25TT_AKBS,
ParentId = backpack.Id,
SlotId = "main",
Location = new ItemLocation
{
X = 3,
Y = 0,
R = ItemRotation.Horizontal,
},
Upd = new Upd { StackObjectsCount = 1 },
}
);
var rootWeaponId = new MongoId();
var weaponWithChildren = CreateMp18(rootWeaponId);
var result = _botGeneratorHelper.AddItemWithChildrenToEquipmentSlot(
[EquipmentSlots.Backpack],
rootWeaponId,
ItemTpl.SHOTGUN_MP18_762X54R_SINGLESHOT_RIFLE,
weaponWithChildren,
botInventory
);
Assert.AreEqual(ItemAddedResult.NO_SPACE, result);
}
/// <summary>
/// Fill all slots except for a 2x6 rectangle, with the top right corner filled, result should be no space
/// </summary>
[TestMethod]
public void AddItemWithChildrenToEquipmentSlot_custom_gun_no_space()
var magazine = new Item
{
var botInventory = new BotBaseInventory { Items = [] };
var backpack = new Item
{
Id = new MongoId(),
// Has a 4hx5v grid first
Template = ItemTpl.BACKPACK_GRUPPA_99_T30_BACKPACK_BLACK,
SlotId = "Backpack",
};
botInventory.Items.Add(backpack);
Id = new MongoId(),
Template = ItemTpl.MAGAZINE_366TKM_AK_AL_10RND,
ParentId = root.Id,
SlotId = "mod_magazine",
};
weaponWithChildren.Add(magazine);
var takenSlots = new List<XY>
{
new() { X = 1, Y = 0 },
new() { X = 2, Y = 0 },
new() { X = 3, Y = 0 },
new() { X = 4, Y = 0 },
new() { X = 2, Y = 1 },
new() { X = 3, Y = 1 },
new() { X = 4, Y = 1 },
new() { X = 2, Y = 2 },
new() { X = 3, Y = 2 },
new() { X = 4, Y = 2 },
new() { X = 2, Y = 3 },
new() { X = 3, Y = 3 },
new() { X = 4, Y = 3 },
new() { X = 2, Y = 4 },
new() { X = 3, Y = 4 },
new() { X = 4, Y = 4 },
new() { X = 2, Y = 5 },
new() { X = 3, Y = 5 },
new() { X = 4, Y = 5 },
};
foreach (var takenSlot in takenSlots)
{
botInventory.Items.Add(
new Item
{
Id = new MongoId(),
Template = ItemTpl.AMMO_762X25TT_AKBS,
ParentId = backpack.Id,
SlotId = "main",
Location = new ItemLocation
{
X = (int)takenSlot.X.Value,
Y = (int)takenSlot.Y.Value,
R = ItemRotation.Horizontal,
},
Upd = new Upd { StackObjectsCount = 1 },
}
);
}
var rootWeaponId = new MongoId();
var weaponWithChildren = new List<Item>();
var root = new Item
{
Id = rootWeaponId,
Template = ItemTpl.ASSAULTRIFLE_MOLOT_ARMS_VPO136_VEPRKM_762X39_CARBINE,
};
weaponWithChildren.Add(root);
var stock = new Item
{
Id = new MongoId(),
Template = ItemTpl.STOCK_VPO136_VEPRKM_WOODEN,
ParentId = root.Id,
SlotId = "mod_stock",
};
weaponWithChildren.Add(stock);
var magazine = new Item
{
Id = new MongoId(),
Template = ItemTpl.MAGAZINE_366TKM_AK_AL_10RND,
ParentId = root.Id,
SlotId = "mod_magazine",
};
weaponWithChildren.Add(magazine);
var muzzle = new Item
{
Id = new MongoId(),
Template = ItemTpl.SILENCER_AKM_HEXAGON_762X39_SOUND_SUPPRESSOR,
ParentId = root.Id,
SlotId = "mod_muzzle",
};
weaponWithChildren.Add(muzzle);
var result = _botGeneratorHelper.AddItemWithChildrenToEquipmentSlot(
[EquipmentSlots.Backpack],
rootWeaponId,
root.Template,
weaponWithChildren,
botInventory
);
Assert.AreEqual(ItemAddedResult.NO_SPACE, result);
}
#endregion
#region GetBotEquipmentRole
[TestMethod]
public void GetBotEquipmentRole_assault()
var muzzle = new Item
{
var result = _botGeneratorHelper.GetBotEquipmentRole("assault");
Id = new MongoId(),
Template = ItemTpl.SILENCER_AKM_HEXAGON_762X39_SOUND_SUPPRESSOR,
ParentId = root.Id,
SlotId = "mod_muzzle",
};
weaponWithChildren.Add(muzzle);
Assert.AreEqual("assault", result);
}
var result = _botGeneratorHelper.AddItemWithChildrenToEquipmentSlot(
[EquipmentSlots.Backpack],
rootWeaponId,
root.Template,
weaponWithChildren,
botInventory
);
[TestMethod]
public void GetBotEquipmentRole_pmcBEAR()
{
var result = _botGeneratorHelper.GetBotEquipmentRole("pmcBEAR");
Assert.AreEqual("pmc", result);
}
[TestMethod]
public void GetBotEquipmentRole_pmcBEAR_lowercase()
{
var result = _botGeneratorHelper.GetBotEquipmentRole("pmcbear");
Assert.AreEqual("pmc", result);
}
#endregion
Assert.AreEqual(ItemAddedResult.NO_SPACE, result);
}
#endregion
#region GetBotEquipmentRole
[Test]
public void GetBotEquipmentRole_assault()
{
var result = _botGeneratorHelper.GetBotEquipmentRole("assault");
Assert.AreEqual("assault", result);
}
[Test]
public void GetBotEquipmentRole_pmcBEAR()
{
var result = _botGeneratorHelper.GetBotEquipmentRole("pmcBEAR");
Assert.AreEqual("pmc", result);
}
[Test]
public void GetBotEquipmentRole_pmcBEAR_lowercase()
{
var result = _botGeneratorHelper.GetBotEquipmentRole("pmcbear");
Assert.AreEqual("pmc", result);
}
#endregion
}
+176 -176
View File
@@ -1,204 +1,204 @@
using SPTarkov.Server.Core.Helpers;
using NUnit.Framework;
using SPTarkov.Server.Core.Helpers;
using SPTarkov.Server.Core.Models.Common;
using SPTarkov.Server.Core.Models.Eft.Common.Tables;
using SPTarkov.Server.Core.Utils;
namespace UnitTests.Tests.Helpers
namespace UnitTests.Tests.Helpers;
[TestFixture]
public class InventoryHelperTests
{
[TestClass]
public class InventoryHelperTests
private InventoryHelper _helper;
private PresetHelper _presetHelper;
[OneTimeSetUp]
public void Initialize()
{
private InventoryHelper _helper;
private PresetHelper _presetHelper;
_helper = DI.GetInstance().GetService<InventoryHelper>();
_presetHelper = DI.GetInstance().GetService<PresetHelper>();
}
[TestInitialize]
public void Initialize()
[Test]
public void GetItemSize_vss_val()
{
var vssValPreset = _presetHelper.GetDefaultPreset(
ItemTpl.MARKSMANRIFLE_VSS_VINTOREZ_9X39_SPECIAL_SNIPER_RIFLE
);
var result = _helper.GetItemSize(
ItemTpl.MARKSMANRIFLE_VSS_VINTOREZ_9X39_SPECIAL_SNIPER_RIFLE,
vssValPreset.Parent,
vssValPreset.Items
);
Assert.AreEqual(5, result.Item1);
Assert.AreEqual(2, result.Item2);
}
[Test]
public void GetItemSize_m4a1()
{
var vssValPreset = _presetHelper.GetDefaultPreset(
ItemTpl.ASSAULTRIFLE_COLT_M4A1_556X45_ASSAULT_RIFLE
);
var result = _helper.GetItemSize(
ItemTpl.ASSAULTRIFLE_COLT_M4A1_556X45_ASSAULT_RIFLE,
vssValPreset.Parent,
vssValPreset.Items
);
Assert.AreEqual(5, result.Item1);
Assert.AreEqual(2, result.Item2);
}
[Test]
public void GetItemSize_glock_17()
{
var vssValPreset = _presetHelper.GetDefaultPreset(ItemTpl.PISTOL_GLOCK_17_9X19);
var result = _helper.GetItemSize(
ItemTpl.PISTOL_GLOCK_17_9X19,
vssValPreset.Parent,
vssValPreset.Items
);
Assert.AreEqual(2, result.Item1);
Assert.AreEqual(1, result.Item2);
}
[Test]
public void GetItemSize_custom_vpo_136_6x2()
{
var rootWeaponId = new MongoId();
var weaponWithChildren = new List<Item>();
var root = new Item
{
_helper = DI.GetService<InventoryHelper>();
var databaseImporter = DI.GetService<DatabaseImporter>();
_presetHelper = DI.GetService<PresetHelper>();
Task.Factory.StartNew(() =>
{
databaseImporter.OnLoad();
});
}
Id = rootWeaponId,
Template = ItemTpl.ASSAULTRIFLE_MOLOT_ARMS_VPO136_VEPRKM_762X39_CARBINE,
};
weaponWithChildren.Add(root);
[TestMethod]
public void GetItemSize_vss_val()
var stock = new Item
{
var vssValPreset = _presetHelper.GetDefaultPreset(
ItemTpl.MARKSMANRIFLE_VSS_VINTOREZ_9X39_SPECIAL_SNIPER_RIFLE
);
Id = new MongoId(),
Template = ItemTpl.STOCK_VPO136_VEPRKM_WOODEN,
ParentId = root.Id,
SlotId = "mod_stock",
};
weaponWithChildren.Add(stock);
var result = _helper.GetItemSize(
ItemTpl.MARKSMANRIFLE_VSS_VINTOREZ_9X39_SPECIAL_SNIPER_RIFLE,
vssValPreset.Parent,
vssValPreset.Items
);
Assert.AreEqual(5, result.Item1);
Assert.AreEqual(2, result.Item2);
}
[TestMethod]
public void GetItemSize_m4a1()
var magazine = new Item
{
var vssValPreset = _presetHelper.GetDefaultPreset(
ItemTpl.ASSAULTRIFLE_COLT_M4A1_556X45_ASSAULT_RIFLE
);
Id = new MongoId(),
Template = ItemTpl.MAGAZINE_366TKM_AK_AL_10RND,
ParentId = root.Id,
SlotId = "mod_magazine",
};
weaponWithChildren.Add(magazine);
var result = _helper.GetItemSize(
ItemTpl.ASSAULTRIFLE_COLT_M4A1_556X45_ASSAULT_RIFLE,
vssValPreset.Parent,
vssValPreset.Items
);
Assert.AreEqual(5, result.Item1);
Assert.AreEqual(2, result.Item2);
}
[TestMethod]
public void GetItemSize_glock_17()
var muzzle = new Item
{
var vssValPreset = _presetHelper.GetDefaultPreset(ItemTpl.PISTOL_GLOCK_17_9X19);
Id = new MongoId(),
Template = ItemTpl.SILENCER_AKM_HEXAGON_762X39_SOUND_SUPPRESSOR,
ParentId = root.Id,
SlotId = "mod_muzzle",
};
weaponWithChildren.Add(muzzle);
var result = _helper.GetItemSize(
ItemTpl.PISTOL_GLOCK_17_9X19,
vssValPreset.Parent,
vssValPreset.Items
);
var result = _helper.GetItemSize(root.Template, rootWeaponId, weaponWithChildren);
Assert.AreEqual(2, result.Item1);
Assert.AreEqual(1, result.Item2);
}
Assert.AreEqual(6, result.Item1);
Assert.AreEqual(2, result.Item2);
}
[TestMethod]
public void GetItemSize_custom_vpo_136_6x2()
[Test]
public void GetItemSize_uzi_unfolded()
{
var rootWeaponId = new MongoId();
var weaponWithChildren = new List<Item>();
var root = new Item
{
var rootWeaponId = new MongoId();
Id = rootWeaponId,
Template = ItemTpl.SMG_IWI_UZI_9X19_SUBMACHINE_GUN,
};
weaponWithChildren.Add(root);
var weaponWithChildren = new List<Item>();
var root = new Item
{
Id = rootWeaponId,
Template = ItemTpl.ASSAULTRIFLE_MOLOT_ARMS_VPO136_VEPRKM_762X39_CARBINE,
};
weaponWithChildren.Add(root);
var stock = new Item
{
Id = new MongoId(),
Template = ItemTpl.STOCK_VPO136_VEPRKM_WOODEN,
ParentId = root.Id,
SlotId = "mod_stock",
};
weaponWithChildren.Add(stock);
var magazine = new Item
{
Id = new MongoId(),
Template = ItemTpl.MAGAZINE_366TKM_AK_AL_10RND,
ParentId = root.Id,
SlotId = "mod_magazine",
};
weaponWithChildren.Add(magazine);
var muzzle = new Item
{
Id = new MongoId(),
Template = ItemTpl.SILENCER_AKM_HEXAGON_762X39_SOUND_SUPPRESSOR,
ParentId = root.Id,
SlotId = "mod_muzzle",
};
weaponWithChildren.Add(muzzle);
var result = _helper.GetItemSize(root.Template, rootWeaponId, weaponWithChildren);
Assert.AreEqual(6, result.Item1);
Assert.AreEqual(2, result.Item2);
}
[TestMethod]
public void GetItemSize_uzi_unfolded()
var stock = new Item
{
var rootWeaponId = new MongoId();
Id = new MongoId(),
Template = "6699249f3c4fda6471005cba",
ParentId = root.Id,
SlotId = "mod_stock",
};
weaponWithChildren.Add(stock);
var weaponWithChildren = new List<Item>();
var root = new Item
{
Id = rootWeaponId,
Template = ItemTpl.SMG_IWI_UZI_9X19_SUBMACHINE_GUN,
};
weaponWithChildren.Add(root);
var stock = new Item
{
Id = new MongoId(),
Template = "6699249f3c4fda6471005cba",
ParentId = root.Id,
SlotId = "mod_stock",
};
weaponWithChildren.Add(stock);
var magazine = new Item
{
Id = new MongoId(),
Template = "669927203c4fda6471005cbe",
ParentId = root.Id,
SlotId = "mod_magazine",
};
weaponWithChildren.Add(magazine);
var result = _helper.GetItemSize(
ItemTpl.SMG_IWI_UZI_9X19_SUBMACHINE_GUN,
rootWeaponId,
weaponWithChildren
);
Assert.AreEqual(3, result.Item1);
Assert.AreEqual(2, result.Item2);
}
[TestMethod]
public void GetItemSize_uzi_folded()
var magazine = new Item
{
var rootWeaponId = new MongoId();
Id = new MongoId(),
Template = "669927203c4fda6471005cbe",
ParentId = root.Id,
SlotId = "mod_magazine",
};
weaponWithChildren.Add(magazine);
var weaponWithChildren = new List<Item>();
var root = new Item
var result = _helper.GetItemSize(
ItemTpl.SMG_IWI_UZI_9X19_SUBMACHINE_GUN,
rootWeaponId,
weaponWithChildren
);
Assert.AreEqual(3, result.Item1);
Assert.AreEqual(2, result.Item2);
}
[Test]
public void GetItemSize_uzi_folded()
{
var rootWeaponId = new MongoId();
var weaponWithChildren = new List<Item>();
var root = new Item
{
Id = rootWeaponId,
Template = ItemTpl.SMG_IWI_UZI_9X19_SUBMACHINE_GUN,
};
weaponWithChildren.Add(root);
var stock = new Item
{
Id = new MongoId(),
Template = "6699249f3c4fda6471005cba",
ParentId = root.Id,
SlotId = "mod_stock",
Upd = new Upd
{
Id = rootWeaponId,
Template = ItemTpl.SMG_IWI_UZI_9X19_SUBMACHINE_GUN,
};
weaponWithChildren.Add(root);
Foldable = new UpdFoldable
{
Folded = true
}
},
};
weaponWithChildren.Add(stock);
var stock = new Item
{
Id = new MongoId(),
Template = "6699249f3c4fda6471005cba",
ParentId = root.Id,
SlotId = "mod_stock",
Upd = new Upd { Foldable = new UpdFoldable { Folded = true } },
};
weaponWithChildren.Add(stock);
var magazine = new Item
{
Id = new MongoId(),
Template = "669927203c4fda6471005cbe",
ParentId = root.Id,
SlotId = "mod_magazine",
};
weaponWithChildren.Add(magazine);
var magazine = new Item
{
Id = new MongoId(),
Template = "669927203c4fda6471005cbe",
ParentId = root.Id,
SlotId = "mod_magazine",
};
weaponWithChildren.Add(magazine);
var result = _helper.GetItemSize(
ItemTpl.SMG_IWI_UZI_9X19_SUBMACHINE_GUN,
rootWeaponId,
weaponWithChildren
);
var result = _helper.GetItemSize(
ItemTpl.SMG_IWI_UZI_9X19_SUBMACHINE_GUN,
rootWeaponId,
weaponWithChildren
);
Assert.AreEqual(2, result.Item1);
Assert.AreEqual(2, result.Item2);
}
Assert.AreEqual(2, result.Item1);
Assert.AreEqual(2, result.Item2);
}
}
+68 -69
View File
@@ -1,82 +1,81 @@
using System.Collections.Concurrent;
using System.Diagnostics;
using NUnit.Framework;
using SPTarkov.Server.Core.Extensions;
using SPTarkov.Server.Core.Models.Common;
namespace UnitTests.Tests
namespace UnitTests.Tests;
[TestFixture]
public class MongoIDTests
{
[TestClass]
public class MongoIDTests
[OneTimeSetUp]
public void Initialize() { }
[Test]
public void GenerateTest()
{
[TestInitialize]
public void Initialize() { }
[TestMethod]
public void GenerateTest()
// Generate 100 MongoId's
for (var i = 0; i < 100; i++)
{
// Generate 100 MongoId's
for (var i = 0; i < 100; i++)
{
// Invalid mongoId character
var result = new MongoId();
// Invalid mongoId character
var result = new MongoId();
// Invalid mongoId length
var test = result.IsValidMongoId();
// Invalid mongoId length
var test = result.IsValidMongoId();
Assert.IsTrue(test, $"IsValidMongoId(): `{result}` is not a valid MongoId.");
}
}
[TestMethod]
[DataRow(
"677ddb67406e9918a0264bbz",
false,
"677ddb67406e9918a0264bbz contains invalid char `z`, but result was true"
)]
[DataRow(
"677ddb67406e9918a0264bbcc",
false,
"677ddb67406e9918a0264bbcc is 25 characters, but result was true"
)]
[DataRow(
"677ddb67406e9918a0264bbc",
true,
"IsValidMongoId() `677ddb67406e9918a0264bbc` is a valid mongoId, but result was false"
)]
public void IsValidMongoIdTest(string mongoId, bool passes, string failMessage)
{
var result = new MongoId(mongoId);
Assert.AreEqual(passes, passes, result, failMessage);
}
[TestMethod]
public void MultiThreadedMongoIDGenerationTest()
{
var concurrentBag = new ConcurrentBag<string>();
var random = new Random();
var stopwatch = new Stopwatch();
stopwatch.Start();
Parallel.For(
0,
1000,
i =>
{
Thread.Sleep(random.Next(0, 10));
var mongoId = new MongoId();
concurrentBag.Add(mongoId);
}
);
stopwatch.Stop();
Console.WriteLine($"Elapsed time: {stopwatch.ElapsedMilliseconds} ms");
var uniqueCount = concurrentBag.Distinct().Count();
var totalCount = concurrentBag.Count;
Assert.AreEqual(
totalCount,
uniqueCount,
$"Expected all generated MongoId's to be unique, but found: {totalCount - uniqueCount} duplicates."
);
Assert.IsTrue(test, $"IsValidMongoId(): `{result}` is not a valid MongoId.");
}
}
[TestCase(
"677ddb67406e9918a0264bbz",
false,
"677ddb67406e9918a0264bbz contains invalid char `z`, but result was true"
)]
[TestCase(
"677ddb67406e9918a0264bbcc",
false,
"677ddb67406e9918a0264bbcc is 25 characters, but result was true"
)]
[TestCase(
"677ddb67406e9918a0264bbc",
true,
"IsValidMongoId() `677ddb67406e9918a0264bbc` is a valid mongoId, but result was false"
)]
public void IsValidMongoIdTest(string mongoId, bool passes, string failMessage)
{
var result = new MongoId(mongoId);
Assert.AreEqual(passes, passes, result, failMessage);
}
[Test]
public void MultiThreadedMongoIDGenerationTest()
{
var concurrentBag = new ConcurrentBag<string>();
var random = new Random();
var stopwatch = new Stopwatch();
stopwatch.Start();
Parallel.For(
0,
1000,
i =>
{
Thread.Sleep(random.Next(0, 10));
var mongoId = new MongoId();
concurrentBag.Add(mongoId);
}
);
stopwatch.Stop();
Console.WriteLine($"Elapsed time: {stopwatch.ElapsedMilliseconds} ms");
var uniqueCount = concurrentBag.Distinct().Count();
var totalCount = concurrentBag.Count;
Assert.AreEqual(
totalCount,
uniqueCount,
$"Expected all generated MongoId's to be unique, but found: {totalCount - uniqueCount} duplicates."
);
}
}
+5 -4
View File
@@ -1,20 +1,21 @@
using NUnit.Framework;
using SPTarkov.Server.Core.Models.Enums;
using SPTarkov.Server.Core.Utils;
namespace UnitTests.Tests.Utils;
[TestClass]
[TestFixture]
public class JsonUtilTests
{
private JsonUtil _jsonUtil;
[TestInitialize]
[OneTimeSetUp]
public void Initialize()
{
_jsonUtil = DI.GetService<JsonUtil>();
_jsonUtil = DI.GetInstance().GetService<JsonUtil>();
}
[TestMethod]
[Test]
public void SerializeAndDeserialize_WithDictionaryOfETFEnum_ExpectCorrectParsing()
{
var value = new Dictionary<QuestStatusEnum, int>
+14 -14
View File
@@ -1,20 +1,21 @@
using NUnit.Framework;
using SPTarkov.Server.Core.Extensions;
using SPTarkov.Server.Core.Utils;
namespace UnitTests.Tests.Utils;
[TestClass]
[TestFixture]
public class MathUtilTests
{
private MathUtil _mathUtil;
[TestInitialize]
[OneTimeSetUp]
public void Initialize()
{
_mathUtil = DI.GetService<MathUtil>();
_mathUtil = DI.GetInstance().GetService<MathUtil>();
}
[TestMethod]
[Test]
public void ListSumTest()
{
var test = new List<float> { 1.1f, 2.1f, 3.3f };
@@ -25,7 +26,7 @@ public class MathUtilTests
Assert.AreEqual(expected, actual, $"ListSum() Expected: {expected}, Actual: {actual}");
}
[TestMethod]
[Test]
public void ListCumSumTest()
{
var test = new List<double> { 1f, 2f, 3f, 4f };
@@ -44,7 +45,7 @@ public class MathUtilTests
}
}
[TestMethod]
[Test]
public void ListProductTest()
{
var test = new List<double> { 1f, 2f, 3f, 4f };
@@ -63,7 +64,7 @@ public class MathUtilTests
}
}
[TestMethod]
[Test]
public void ListAddTest()
{
var test = new List<double> { 1f, 2f, 3f, 4f };
@@ -82,7 +83,7 @@ public class MathUtilTests
}
}
[TestMethod]
[Test]
public void MapToRangeTest()
{
const double expected = 2;
@@ -92,22 +93,21 @@ public class MathUtilTests
Assert.AreEqual(expected, actual, $"MapToRange() Expected: {expected}, Actual: {actual}");
}
[TestMethod]
[DataRow(
[TestCase(
15d,
new double[] { 1, 10, 20, 30, 40, 50, 60 },
new double[] { 11000, 20000, 32000, 45000, 58000, 70000, 82000 },
26000d
)]
[DataRow(5d, new double[] { 1, 10 }, new double[] { 0, 1000 }, 444.44444444444446d)]
[DataRow(
[TestCase(5d, new double[] { 1, 10 }, new double[] { 0, 1000 }, 444.44444444444446d)]
[TestCase(
12d,
new double[] { 1, 10, 500, 510 },
new double[] { 0, 10, 20, 30 },
10.040816326530612d
)]
[DataRow(1d, new double[] { 1, 10, 500, 510 }, new double[] { 2, 10, 20, 30 }, 2d)]
[DataRow(11d, new double[] { 1, 10 }, new double[] { 2, 10 }, 10d)]
[TestCase(1d, new double[] { 1, 10, 500, 510 }, new double[] { 2, 10, 20, 30 }, 2d)]
[TestCase(11d, new double[] { 1, 10 }, new double[] { 2, 10 }, 10d)]
public void InterpTest(double input, double[] x, double[] y, double expected)
{
var actual = _mathUtil.Interp1(input, x.ToList(), y.ToList());
+26 -26
View File
@@ -1,39 +1,39 @@
using SPTarkov.Server.Core.Extensions;
using NUnit.Framework;
using SPTarkov.Server.Core.Extensions;
using SPTarkov.Server.Core.Models.Common;
namespace UnitTests.Tests.Utils
namespace UnitTests.Tests.Utils;
/// <summary>
/// Unit tests for the <see cref="MongoId"/> struct.
/// </summary>
[TestFixture]
public class MongoIdTests
{
/// <summary>
/// Unit tests for the <see cref="MongoId"/> struct.
/// Test that generates 1000 <see cref="MongoId"/> and ensures they are all valid. <br/>
/// Validity is checked by ensuring the ID is non-empty, exactly 24 characters, and matches the expected format.
/// </summary>
[TestClass]
public class MongoIdTests
[Test]
public void Generate_ShouldProduceValidMongoIds()
{
/// <summary>
/// Test that generates 1000 <see cref="MongoId"/> and ensures they are all valid. <br/>
/// Validity is checked by ensuring the ID is non-empty, exactly 24 characters, and matches the expected format.
/// </summary>
[TestMethod]
public void Generate_ShouldProduceValidMongoIds()
var invalidIds = new List<string>();
for (var i = 0; i < 1000; i++)
{
var invalidIds = new List<string>();
var id = new MongoId();
var idStr = id.ToString();
for (var i = 0; i < 1000; i++)
if (string.IsNullOrWhiteSpace(idStr) || idStr.Length != 24 || !id.IsValidMongoId())
{
var id = new MongoId();
var idStr = id.ToString();
if (string.IsNullOrWhiteSpace(idStr) || idStr.Length != 24 || !id.IsValidMongoId())
{
invalidIds.Add(idStr);
}
invalidIds.Add(idStr);
}
Assert.AreEqual(
0,
invalidIds.Count,
$"Invalid MongoIds found: {string.Join(", ", invalidIds)}"
);
}
Assert.AreEqual(
0,
invalidIds.Count,
$"Invalid MongoIds found: {string.Join(", ", invalidIds)}"
);
}
}
+20 -20
View File
@@ -1,19 +1,21 @@
using NUnit.Framework;
using SPTarkov.Server.Core.Utils;
namespace UnitTests.Tests.Utils;
[TestClass]
[TestFixture]
[TestFixture]
public sealed class RandomUtilTests
{
private RandomUtil _randomUtil;
[TestInitialize]
[OneTimeSetUp]
public void Initialize()
{
_randomUtil = DI.GetService<RandomUtil>();
_randomUtil = DI.GetInstance().GetService<RandomUtil>();
}
[TestMethod]
[Test]
public void GetIntTest()
{
// Run 10000 test cases
@@ -30,7 +32,7 @@ public sealed class RandomUtilTests
}
}
[TestMethod]
[Test]
public void GetIntExTest()
{
// Run 10000 test cases
@@ -45,7 +47,7 @@ public sealed class RandomUtilTests
}
}
[TestMethod]
[Test]
public void GetDoubleTest()
{
// Run 10000 test cases
@@ -62,7 +64,7 @@ public sealed class RandomUtilTests
}
}
[TestMethod]
[Test]
public void GetPercentOfValueTest()
{
const float expected = 45.5f;
@@ -76,7 +78,7 @@ public sealed class RandomUtilTests
);
}
[TestMethod]
[Test]
public void ReduceValueByPercentTest()
{
const float expected = 54.5f;
@@ -90,7 +92,7 @@ public sealed class RandomUtilTests
);
}
[TestMethod]
[Test]
public void GetChance100Test()
{
for (var i = 0; i < 100; i++)
@@ -120,7 +122,7 @@ public sealed class RandomUtilTests
// TODO: Missing methods between these two
[TestMethod]
[Test]
public void RandIntTest()
{
for (var i = 0; i < 100; i++)
@@ -148,7 +150,7 @@ public sealed class RandomUtilTests
}
}
[TestMethod]
[Test]
public void RandNumTest()
{
for (var i = 0; i < 10000; i++)
@@ -190,7 +192,7 @@ public sealed class RandomUtilTests
}
}
[TestMethod]
[Test]
public void ShuffleTest()
{
var testList = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
@@ -205,19 +207,17 @@ public sealed class RandomUtilTests
);
}
[TestMethod]
[DataRow(0.1, 1)]
[DataRow(0.0001, 4)]
[DataRow(0, 0)]
[DataRow(10000000, 0)]
[DataRow(0.000_000_000_000_000_000_000_000_1D, 25)]
[TestCase(0.1, 1)]
[TestCase(0.0001, 4)]
[TestCase(0, 0)]
[TestCase(10000000, 0)]
[TestCase(0.000_000_000_000_000_000_000_000_1D, 25)]
public void GetNumberPrecision_WithDoubles_ReturnsDecimalPoints(double value, int decimalPoints)
{
Assert.AreEqual(decimalPoints, _randomUtil.GetNumberPrecision(value));
}
[TestMethod]
[DataRow(new[] { "test" }, "test", "Expected first array value")]
[TestCase(new[] { "test" }, "test", "Expected first array value")]
public void GetArrayValueTest(string[] input, string expectedOutput, string failMessage)
{
var result = _randomUtil.GetArrayValue(input);
+2 -4
View File
@@ -6,10 +6,8 @@
<ItemGroup>
<PackageReference Include="FastCloner" Version="3.3.8" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.0" />
<PackageReference Include="MSTest" Version="3.9.0" />
</ItemGroup>
<ItemGroup>
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.3.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Libraries\SPTarkov.Server.Assets\SPTarkov.Server.Assets.csproj" />