mirror of
https://github.com/space-wizards/RobustToolbox.git
synced 2026-02-14 19:29:36 +01:00
Add API for creating mock configuration managers in unit tests.
This commit is contained in:
@@ -39,7 +39,7 @@ END TEMPLATE-->
|
||||
|
||||
### New features
|
||||
|
||||
*None yet*
|
||||
* Added `MockInterfaces.MakeConfigurationManager` for creating functional configuration managers for unit test mocking.
|
||||
|
||||
### Bugfixes
|
||||
|
||||
|
||||
@@ -460,10 +460,17 @@ namespace Robust.Shared.Configuration
|
||||
|
||||
public void LoadCVarsFromAssembly(Assembly assembly)
|
||||
{
|
||||
foreach (var defField in assembly
|
||||
foreach (var type in assembly
|
||||
.GetTypes()
|
||||
.Where(p => Attribute.IsDefined(p, typeof(CVarDefsAttribute)))
|
||||
.SelectMany(p => p.GetFields(BindingFlags.Public | BindingFlags.Static)))
|
||||
.Where(p => Attribute.IsDefined(p, typeof(CVarDefsAttribute))))
|
||||
{
|
||||
LoadCVarsFromType(type);
|
||||
}
|
||||
}
|
||||
|
||||
public void LoadCVarsFromType(Type containingType)
|
||||
{
|
||||
foreach (var defField in containingType.GetFields(BindingFlags.Public | BindingFlags.Static))
|
||||
{
|
||||
var fieldType = defField.FieldType;
|
||||
if (!fieldType.IsGenericType || fieldType.GetGenericTypeDefinition() != typeof(CVarDef<>))
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
|
||||
@@ -7,6 +8,7 @@ namespace Robust.Shared.Configuration
|
||||
{
|
||||
void OverrideConVars(IEnumerable<(string key, string value)> cVars);
|
||||
void LoadCVarsFromAssembly(Assembly assembly);
|
||||
void LoadCVarsFromType(Type containingType);
|
||||
|
||||
void Initialize(bool isServer);
|
||||
|
||||
|
||||
53
Robust.UnitTesting/MockInterfaces.cs
Normal file
53
Robust.UnitTesting/MockInterfaces.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Robust.Shared.Configuration;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Log;
|
||||
using Robust.Shared.Timing;
|
||||
|
||||
namespace Robust.UnitTesting;
|
||||
|
||||
/// <summary>
|
||||
/// Creates implementations of common engine interfaces for use in unit tests.
|
||||
/// </summary>
|
||||
public static class MockInterfaces
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates an instance of <see cref="IConfigurationManager"/>.
|
||||
/// </summary>
|
||||
/// <param name="gameTiming">The timing service used.</param>
|
||||
/// <param name="logManager">The log service used.</param>
|
||||
/// <param name="isServer">
|
||||
/// Whether this configuration manager will treat itself as server or client.
|
||||
/// This is only relevant for CVars that are defined as <see cref="CVar.CLIENTONLY"/> or <see cref="CVar.SERVERONLY"/>.
|
||||
/// </param>
|
||||
/// <param name="loadCvarsFromTypes">
|
||||
/// A list of additional types to pull CVar definitions from.
|
||||
/// These normally have <see cref="CVarDefsAttribute"/>.
|
||||
/// </param>
|
||||
public static IConfigurationManager MakeConfigurationManager(
|
||||
IGameTiming gameTiming,
|
||||
ILogManager logManager,
|
||||
bool isServer = true,
|
||||
IEnumerable<Type>? loadCvarsFromTypes = null)
|
||||
{
|
||||
var deps = new DependencyCollection();
|
||||
deps.RegisterInstance<IGameTiming>(gameTiming);
|
||||
deps.RegisterInstance<ILogManager>(logManager);
|
||||
deps.Register<ConfigurationManager>();
|
||||
deps.BuildGraph();
|
||||
|
||||
var cfg = deps.Resolve<ConfigurationManager>();
|
||||
cfg.Initialize(isServer);
|
||||
|
||||
if (loadCvarsFromTypes != null)
|
||||
{
|
||||
foreach (var loadFromType in loadCvarsFromTypes)
|
||||
{
|
||||
cfg.LoadCVarsFromType(loadFromType);
|
||||
}
|
||||
}
|
||||
|
||||
return cfg;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user