Test cleanup

This commit is contained in:
Alex
2025-07-20 20:03:32 +01:00
parent e05ae25650
commit 9584d171f0
5 changed files with 158 additions and 58 deletions
+16 -8
View File
@@ -1,4 +1,7 @@
using Microsoft.Extensions.DependencyInjection;
using SPTarkov.DI;
using SPTarkov.Server.Core.DI;
using SPTarkov.Server.Core.Models.Spt.Mod;
using SPTarkov.Server.Core.Models.Utils;
using SPTarkov.Server.Core.Utils;
using SPTarkov.Server.Core.Utils.Cloners;
@@ -21,17 +24,22 @@ public class DI
}
var services = new ServiceCollection();
var jsonUtil = new JsonUtil([new SptJsonConverterRegistrator()]);
var mathUtil = new MathUtil();
services.AddSingleton<JsonUtil>(jsonUtil);
services.AddSingleton<MathUtil>(mathUtil);
services.AddSingleton<ICloner, JsonCloner>();
services.AddSingleton<ISptLogger<RandomUtil>, MockLogger<RandomUtil>>();
services.AddSingleton<RandomUtil>();
services.AddSingleton<HashUtil>();
var diHandler = new DependencyInjectionHandler(services);
diHandler.AddInjectableTypesFromTypeAssembly(typeof(App));
diHandler.AddInjectableTypesFromTypeList([typeof(MockLogger<>)/* TODO: this needs to be enabled but the randomizer needs to NOT be random, typeof(MockRandomUtil)*/]);
diHandler.InjectAll();
services.AddSingleton<IReadOnlyList<SptMod>>(_ => []);
_serviceProvider = services.BuildServiceProvider();
foreach (var onLoad in _serviceProvider.GetServices<IOnLoad>())
{
onLoad.OnLoad().Wait();
}
}
public static T GetService<T>()
+1 -1
View File
@@ -16,7 +16,7 @@ public class MockLogger<T> : ISptLogger<T>
Exception? ex = null
)
{
throw new NotImplementedException();
Console.WriteLine(data);
}
public void Success(string data, Exception? ex = null)
+121
View File
@@ -0,0 +1,121 @@
using SPTarkov.DI.Annotations;
using SPTarkov.Server.Core.Models.Utils;
using SPTarkov.Server.Core.Utils;
using SPTarkov.Server.Core.Utils.Cloners;
namespace UnitTests.Mock;
[Injectable(TypeOverride = typeof(RandomUtil))]
public class MockRandomUtil(ISptLogger<RandomUtil> _logger, ICloner _cloner) : RandomUtil(_logger, _cloner)
{
public override int GetInt(int min, int max = Int32.MaxValue, bool exclusive = false)
{
return min;
}
public override double GetDouble(double min, double max)
{
return min;
}
public override bool GetBool()
{
return true;
}
public override void NextBytes(Span<byte> bytes)
{
// TODO: No idea what this does
base.NextBytes(bytes);
}
public override double GetPercentOfValue(double percent, double number, int toFixed = 2)
{
// TODO: No idea what this does
return base.GetPercentOfValue(percent, number, toFixed);
}
public override double ReduceValueByPercent(double number, double percentage)
{
// TODO: No idea what this does
return base.ReduceValueByPercent(number, percentage);
}
public override bool GetChance100(double? chancePercent)
{
return true;
}
public override T GetCollectionValue<T>(IEnumerable<T> collection)
{
if (!collection.Any())
{
throw new InvalidOperationException("Sequence contains no elements.");
}
return collection.First();
}
public override TKey GetKey<TKey, TVal>(Dictionary<TKey, TVal> dictionary)
{
return GetCollectionValue(dictionary.Keys);
}
public override TVal GetVal<TKey, TVal>(Dictionary<TKey, TVal> dictionary)
{
return GetCollectionValue(dictionary.Values);
}
public override double GetNormallyDistributedRandomNumber(double mean, double sigma, int attempt = 0)
{
// TODO: No idea what to do with this
return base.GetNormallyDistributedRandomNumber(mean, sigma, attempt);
}
public override int RandInt(int low, int? high = null)
{
return low;
}
public override double RandNum(double val1, double val2 = 0, int precision = 6)
{
return val1;
}
public override List<T> DrawRandomFromList<T>(List<T> originalList, int count = 1, bool replacement = true)
{
return originalList.Slice(0, count);
}
public override List<TKey> DrawRandomFromDict<TKey, TVal>(Dictionary<TKey, TVal> dict, int count = 1, bool replacement = true)
{
// TODO: derandomize
return base.DrawRandomFromDict(dict, count, replacement);
}
public override double GetBiasedRandomNumber(double min, double max, double shift, double n)
{
return min;
}
public override List<T> Shuffle<T>(List<T> originalList)
{
return originalList;
}
public override int GetNumberPrecision(double num)
{
// TODO: derandomize
return base.GetNumberPrecision(num);
}
public override T? GetArrayValue<T>(IEnumerable<T> list) where T : default
{
return GetCollectionValue(list);
}
public override bool RollChance(double chance, double scale = 1)
{
return true;
}
}
-29
View File
@@ -1,29 +0,0 @@
using SPTarkov.Server.Core.Models.Spt.Templates;
using SPTarkov.Server.Core.Utils;
using UnitTests.Mock;
namespace UnitTests.Tests;
[TestClass]
public class Test
{
private Templates? _templates;
[TestInitialize]
public async Task Setup()
{
var importer = new ImporterUtil(
new MockLogger<ImporterUtil>(),
new FileUtil(),
DI.GetService<JsonUtil>()
);
_templates = await importer.LoadRecursiveAsync<Templates>("./TestAssets/");
}
[TestMethod]
public void TestMethod1()
{
var result = DI.GetService<JsonUtil>().Serialize(_templates);
Console.WriteLine(result);
}
}