Fix the damn tests

This commit is contained in:
hulkhan22
2025-05-31 01:40:47 +02:00
parent a2d2a44a2f
commit 18bd0957fd
7 changed files with 79 additions and 16 deletions
@@ -11,6 +11,7 @@ public class JsonUtil
{
private static JsonSerializerOptions? jsonSerializerOptionsNoIndent;
private static JsonSerializerOptions? jsonSerializerOptionsIndented;
private static readonly Lock _lock = new();
public JsonUtil(
IEnumerable<IJsonConverterRegistrator> registrators
@@ -28,14 +29,20 @@ public class JsonUtil
{
foreach (var converter in registrator.GetJsonConverters())
{
jsonSerializerOptionsNoIndent.Converters.Add(converter);
lock (_lock)
{
jsonSerializerOptionsNoIndent.Converters.Add(converter);
}
}
}
jsonSerializerOptionsIndented = new JsonSerializerOptions(jsonSerializerOptionsNoIndent)
lock (_lock)
{
WriteIndented = true
};
jsonSerializerOptionsIndented = new JsonSerializerOptions(jsonSerializerOptionsNoIndent)
{
WriteIndented = true
};
}
}
/// <summary>
@@ -400,7 +400,7 @@ public class RandomUtil(ISptLogger<RandomUtil> _logger, ICloner _cloner)
while (currentIndex != 0)
{
var randomIndex = GetInt(0, currentIndex);
var randomIndex = GetInt(0, currentIndex, true);
currentIndex--;
// Swap it with the current element.
+39
View File
@@ -0,0 +1,39 @@
using Microsoft.Extensions.DependencyInjection;
using SPTarkov.Server.Core.Models.Utils;
using SPTarkov.Server.Core.Utils;
using SPTarkov.Server.Core.Utils.Cloners;
using SPTarkov.Server.Core.Utils.Json.Converters;
using UnitTests.Mock;
namespace UnitTests;
public class DI
{
private static IServiceProvider _serviceProvider;
private static IServiceProvider ConfigureServices()
{
if (_serviceProvider != null)
{
return _serviceProvider;
}
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>();
return _serviceProvider = services.BuildServiceProvider();
}
public static T GetService<T>() where T : notnull
{
return ConfigureServices().GetRequiredService<T>();
}
}
+7 -4
View File
@@ -1,16 +1,19 @@
using System.Collections.Concurrent;
using System.Diagnostics;
using SPTarkov.Server.Core.Utils;
using SPTarkov.Server.Core.Utils.Cloners;
using SPTarkov.Server.Core.Utils.Json.Converters;
using UnitTests.Mock;
namespace UnitTests.Tests.Utils;
[TestClass]
public class HashUtilTests
{
protected HashUtil _hashUtil = new(new RandomUtil(new MockLogger<RandomUtil>(), new JsonCloner(new JsonUtil([ new SptJsonConverterRegistrator() ]))));
private HashUtil _hashUtil;
[TestInitialize]
public void Initialize()
{
_hashUtil = DI.GetService<HashUtil>();
}
[TestMethod]
public void GenerateTest()
+7 -2
View File
@@ -1,13 +1,18 @@
using SPTarkov.Server.Core.Models.Enums;
using SPTarkov.Server.Core.Utils;
using SPTarkov.Server.Core.Utils.Json.Converters;
namespace UnitTests.Tests.Utils;
[TestClass]
public class JsonUtilTests
{
protected JsonUtil _jsonUtil = new([ new SptJsonConverterRegistrator() ]);
private JsonUtil _jsonUtil;
[TestInitialize]
public void Initialize()
{
_jsonUtil = DI.GetService<JsonUtil>();
}
[TestMethod]
public void SerializeAndDeserialize_WithDictionaryOfETFEnum_ExpectCorrectParsing()
+7 -1
View File
@@ -5,7 +5,13 @@ namespace UnitTests.Tests.Utils;
[TestClass]
public class MathUtilTests
{
protected MathUtil _mathUtil = new();
private MathUtil _mathUtil;
[TestInitialize]
public void Initialize()
{
_mathUtil = DI.GetService<MathUtil>();
}
[TestMethod]
public void ListSumTest()
+7 -4
View File
@@ -1,14 +1,17 @@
using SPTarkov.Server.Core.Utils;
using SPTarkov.Server.Core.Utils.Cloners;
using SPTarkov.Server.Core.Utils.Json.Converters;
using UnitTests.Mock;
namespace UnitTests.Tests.Utils;
[TestClass]
public sealed class RandomUtilTests
{
private readonly RandomUtil _randomUtil = new(new MockLogger<RandomUtil>(), new JsonCloner(new JsonUtil([ new SptJsonConverterRegistrator() ])));
private RandomUtil _randomUtil;
[TestInitialize]
public void Initialize()
{
_randomUtil = DI.GetService<RandomUtil>();
}
[TestMethod]
public void GetIntTest()