Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 13 additions & 29 deletions src/SampSharp.MapObjects.Tests/MapObjectServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
public class MapObjectServiceTests
{
private FakeMapContext _fakeMapContext;
private Dictionary<string, MapDefinition> _maps;

[SetUp]
public void Init()
Expand All @@ -13,15 +12,13 @@ public void Init()
Substitute.For<IOmpEntityProvider>(),
NullLogger<MapContext>.Instance
);

_maps = [];
}

[Test]
public void Load_WhenMapNameIsNull_ShouldThrowArgumentNullException()
{
// Arrange
MapObjectService mapObjects = new(_fakeMapContext, _maps);
MapObjectService mapObjects = new(_fakeMapContext, CreateMaps());

// Act
Action act = () => mapObjects.Load(null!);
Expand All @@ -34,7 +31,7 @@ public void Load_WhenMapNameIsNull_ShouldThrowArgumentNullException()
public void Load_WhenMapNameIsEmpty_ShouldThrowArgumentException()
{
// Arrange
MapObjectService mapObjects = new(_fakeMapContext, _maps);
MapObjectService mapObjects = new(_fakeMapContext, CreateMaps());

// Act
Action act = () => mapObjects.Load(string.Empty);
Expand All @@ -47,7 +44,7 @@ public void Load_WhenMapNameIsEmpty_ShouldThrowArgumentException()
public void Load_WhenMapIsNotRegistered_ShouldDoNothing()
{
// Arrange
MapObjectService mapObjects = new(_fakeMapContext, _maps);
MapObjectService mapObjects = new(_fakeMapContext, CreateMaps());

// Act
Action act = () => mapObjects.Load("Unknown");
Expand All @@ -61,9 +58,7 @@ public void Load_WhenMapIsRegistered_ShouldLoadMap()
{
// Arrange
TestMapDefinition map = new("Map");
_maps.Add(map.Name, map);

MapObjectService mapObjects = new(_fakeMapContext, _maps);
MapObjectService mapObjects = new(_fakeMapContext, CreateMaps(map));

// Act
mapObjects.Load(map.Name);
Expand All @@ -78,11 +73,7 @@ public void Load_WhenAnotherMapIsAlreadyLoaded_ShouldThrowInvalidOperationExcept
// Arrange
TestMapDefinition map1 = new("Map1");
TestMapDefinition map2 = new("Map2");

_maps.Add(map1.Name, map1);
_maps.Add(map2.Name, map2);

MapObjectService mapObjects = new(_fakeMapContext, _maps);
MapObjectService mapObjects = new(_fakeMapContext, CreateMaps(map1, map2));
mapObjects.Load(map1.Name);

// Act
Expand All @@ -96,7 +87,7 @@ public void Load_WhenAnotherMapIsAlreadyLoaded_ShouldThrowInvalidOperationExcept
public void Unload_WhenNoMapIsLoaded_ShouldThrowInvalidOperationException()
{
// Arrange
MapObjectService mapObjects = new(_fakeMapContext, _maps);
MapObjectService mapObjects = new(_fakeMapContext, CreateMaps());

// Act
Action act = mapObjects.Unload;
Expand All @@ -110,9 +101,7 @@ public void Unload_WhenMapIsLoaded_ShouldUnloadMap()
{
// Arrange
TestMapDefinition map = new("Map");
_maps.Add(map.Name, map);

MapObjectService mapObjects = new(_fakeMapContext, _maps);
MapObjectService mapObjects = new(_fakeMapContext, CreateMaps(map));
mapObjects.Load(map.Name);

// Act
Expand All @@ -129,11 +118,7 @@ public void Unload_WhenMapWasUnloaded_ShouldAllowLoadingAnotherMap()
// Arrange
TestMapDefinition map1 = new("Map1");
TestMapDefinition map2 = new("Map2");

_maps.Add(map1.Name, map1);
_maps.Add(map2.Name, map2);

MapObjectService mapObjects = new(_fakeMapContext, _maps);
MapObjectService mapObjects = new(_fakeMapContext, CreateMaps(map1, map2));
mapObjects.Load(map1.Name);
mapObjects.Unload();

Expand All @@ -153,13 +138,8 @@ public void Unload_WhenOnUnloadThrows_ShouldAllowLoadingAnotherMap()
{
ThrowOnUnload = true
};

TestMapDefinition map2 = new("Map2");

_maps.Add(map1.Name, map1);
_maps.Add(map2.Name, map2);

MapObjectService mapObjects = new(_fakeMapContext, _maps);
MapObjectService mapObjects = new(_fakeMapContext, CreateMaps(map1, map2));
mapObjects.Load(map1.Name);

// Act
Expand All @@ -172,4 +152,8 @@ public void Unload_WhenOnUnloadThrows_ShouldAllowLoadingAnotherMap()
load.Should().NotThrow();
map2.WasLoaded.Should().BeTrue();
}

private static FrozenDictionary<string, MapDefinition> CreateMaps(
params MapDefinition[] maps)
=> maps.ToFrozenDictionary(m => m.Name);
}
1 change: 1 addition & 0 deletions src/SampSharp.MapObjects.Tests/Usings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
global using System.Numerics;
global using System.Collections.Frozen;
global using NUnit.Framework;
global using FluentAssertions;
global using NSubstitute;
Expand Down
2 changes: 1 addition & 1 deletion src/SampSharp.MapObjects/MapObjectService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

internal sealed class MapObjectService(
MapContext mapContext,
Dictionary<string, MapDefinition> maps) : IMapObjectService
FrozenDictionary<string, MapDefinition> maps) : IMapObjectService
{
private MapDefinition _loadedMap;

Expand Down
2 changes: 1 addition & 1 deletion src/SampSharp.MapObjects/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace SampSharp.MapObjects;

public static class ServiceCollectionExtensions

Check warning on line 3 in src/SampSharp.MapObjects/ServiceCollectionExtensions.cs

View workflow job for this annotation

GitHub Actions / unit_testing

Missing XML comment for publicly visible type or member 'ServiceCollectionExtensions'
{
/// <summary>
/// Registers the services required to manage object maps, including all built-in map definitions.
Expand Down Expand Up @@ -59,7 +59,7 @@
.AddSingleton(sp =>
{
IEnumerable<MapDefinition> maps = sp.GetRequiredService<IEnumerable<MapDefinition>>();
return maps.ToDictionary(map => map.Name);
return maps.ToFrozenDictionary(map => map.Name);
})
.AddSingleton<IMapObjectService, MapObjectService>()
.AddSingleton<MapContext>()
Expand Down
1 change: 1 addition & 0 deletions src/SampSharp.MapObjects/Usings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
global using System.Numerics;
global using System.Collections.Frozen;
global using Microsoft.Extensions.Logging;
global using Microsoft.Extensions.DependencyInjection;
global using SampSharp.Entities;
Expand Down
Loading