-
Notifications
You must be signed in to change notification settings - Fork 447
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a method for copying components (#5654)
Co-authored-by: metalgearsloth <comedian_vs_clown@hotmail.com>
- Loading branch information
1 parent
7104a4f
commit 8a04a4f
Showing
5 changed files
with
313 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
Robust.UnitTesting/Shared/GameObjects/EntityManagerCopyTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
using System.Numerics; | ||
using NUnit.Framework; | ||
using Robust.Shared.GameObjects; | ||
using Robust.Shared.Map; | ||
using Robust.Shared.Serialization.Manager.Attributes; | ||
using Robust.UnitTesting.Server; | ||
|
||
namespace Robust.UnitTesting.Shared.GameObjects; | ||
|
||
[TestFixture] | ||
public sealed partial class EntityManagerCopyTests | ||
{ | ||
[Test] | ||
public void CopyComponentGeneric() | ||
{ | ||
var instant = RobustServerSimulation.NewSimulation(); | ||
instant.RegisterComponents(fac => | ||
{ | ||
fac.RegisterClass<AComponent>(); | ||
}); | ||
|
||
var sim = instant.InitializeInstance(); | ||
var entManager = sim.Resolve<IEntityManager>(); | ||
var mapSystem = entManager.System<SharedMapSystem>(); | ||
|
||
mapSystem.CreateMap(out var mapId); | ||
|
||
var original = entManager.Spawn(null, new MapCoordinates(Vector2.Zero, mapId)); | ||
var comp = entManager.AddComponent<AComponent>(original); | ||
|
||
Assert.That(comp.Value, Is.EqualTo(false)); | ||
comp.Value = true; | ||
|
||
var target = entManager.Spawn(null, new MapCoordinates(Vector2.Zero, mapId)); | ||
Assert.That(!entManager.HasComponent<AComponent>(target)); | ||
|
||
var targetComp = entManager.CopyComponent(original, target, comp); | ||
|
||
Assert.That(targetComp!.Owner == target); | ||
Assert.That(targetComp.Value, Is.EqualTo(comp.Value)); | ||
Assert.That(!ReferenceEquals(comp, targetComp)); | ||
} | ||
|
||
[Test] | ||
public void CopyComponentNonGeneric() | ||
{ | ||
var instant = RobustServerSimulation.NewSimulation(); | ||
instant.RegisterComponents(fac => | ||
{ | ||
fac.RegisterClass<AComponent>(); | ||
}); | ||
|
||
var sim = instant.InitializeInstance(); | ||
var entManager = sim.Resolve<IEntityManager>(); | ||
var mapSystem = entManager.System<SharedMapSystem>(); | ||
|
||
mapSystem.CreateMap(out var mapId); | ||
|
||
var original = entManager.Spawn(null, new MapCoordinates(Vector2.Zero, mapId)); | ||
var comp = entManager.AddComponent<AComponent>(original); | ||
|
||
Assert.That(comp.Value, Is.EqualTo(false)); | ||
comp.Value = true; | ||
|
||
var target = entManager.Spawn(null, new MapCoordinates(Vector2.Zero, mapId)); | ||
Assert.That(!entManager.HasComponent<AComponent>(target)); | ||
|
||
var targetComp = entManager.CopyComponent(original, target, (IComponent) comp); | ||
|
||
Assert.That(targetComp!.Owner == target); | ||
Assert.That(((AComponent) targetComp).Value, Is.EqualTo(comp.Value)); | ||
Assert.That(!ReferenceEquals(comp, targetComp)); | ||
} | ||
|
||
[Test] | ||
public void CopyComponentMultiple() | ||
{ | ||
var instant = RobustServerSimulation.NewSimulation(); | ||
instant.RegisterComponents(fac => | ||
{ | ||
fac.RegisterClass<AComponent>(); | ||
fac.RegisterClass<BComponent>(); | ||
}); | ||
|
||
var sim = instant.InitializeInstance(); | ||
var entManager = sim.Resolve<IEntityManager>(); | ||
var mapSystem = entManager.System<SharedMapSystem>(); | ||
|
||
mapSystem.CreateMap(out var mapId); | ||
|
||
var original = entManager.Spawn(null, new MapCoordinates(Vector2.Zero, mapId)); | ||
var comp = entManager.AddComponent<AComponent>(original); | ||
var comp2 = entManager.AddComponent<BComponent>(original); | ||
|
||
Assert.That(comp.Value, Is.EqualTo(false)); | ||
comp.Value = true; | ||
|
||
var target = entManager.Spawn(null, new MapCoordinates(Vector2.Zero, mapId)); | ||
Assert.That(!entManager.HasComponent<AComponent>(target)); | ||
|
||
entManager.CopyComponents(original, target, null, comp, comp2); | ||
var targetComp = entManager.GetComponent<AComponent>(target); | ||
var targetComp2 = entManager.GetComponent<BComponent>(target); | ||
|
||
Assert.That(targetComp!.Owner == target); | ||
Assert.That(targetComp.Value, Is.EqualTo(comp.Value)); | ||
|
||
Assert.That(targetComp2!.Owner == target); | ||
Assert.That(targetComp2.Value, Is.EqualTo(comp2.Value)); | ||
|
||
Assert.That(!ReferenceEquals(comp, targetComp)); | ||
Assert.That(!ReferenceEquals(comp2, targetComp2)); | ||
} | ||
|
||
[DataDefinition] | ||
private sealed partial class AComponent : Component | ||
{ | ||
[DataField] | ||
public bool Value = false; | ||
} | ||
|
||
[DataDefinition] | ||
private sealed partial class BComponent : Component | ||
{ | ||
[DataField] | ||
public bool Value = false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters