This repository has been archived on 2023-02-02. You can view files and clone it, but cannot push or open issues or pull requests.
mfgames-gallium-cil/tests/MfGames.Gallium.Tests/EntityTests.cs
D. Moonfire fb0a03e963
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
ci/woodpecker/manual/woodpecker Pipeline was successful
feat(entity): added SetAll() which takes a params list
- added unit test to verify
- minor code clean up to get the tests running

closes #1
2023-01-14 17:13:01 -06:00

252 lines
6.6 KiB
C#

using System;
using MfGames.Gallium;
using Xunit;
using Xunit.Abstractions;
namespace MfGames.Gallium.Tests;
public class EntityTests : GalliumTestsBase
{
public EntityTests(ITestOutputHelper output)
: base(output)
{
}
[Fact]
public void AddingComponentOnceWorks()
{
var component1 = new TestComponent1();
Entity entity1 = new Entity().Add(component1);
Assert.Equal(1, entity1.Count);
Assert.True(entity1.Has<TestComponent1>());
Assert.Equal(component1, entity1.Get<TestComponent1>());
}
[Fact]
public void AddingComponentViaInterfaceWork()
{
var component1 = new TestComponent3a();
Entity entity1 = new Entity().Add<ITestComponent3>(component1);
Assert.Equal(1, entity1.Count);
Assert.False(entity1.Has<TestComponent3a>());
Assert.True(entity1.Has<ITestComponent3>());
Assert.Equal(component1, entity1.Get<ITestComponent3>());
}
[Fact]
public void AddingTwiceThrowsException()
{
var component1 = new TestComponent1();
Exception exception = Assert.Throws<ArgumentException>(
() => new Entity()
.Add(component1)
.Add(component1));
Assert.Equal(
"An element with the same type "
+ "(MfGames.Gallium.Tests.TestComponent1)"
+ " already exists. (Parameter 'component')",
exception.Message);
}
[Fact]
public void CopyEntityAlsoCopiesComponents()
{
var component1 = new TestComponent1();
Entity entity1 = new Entity().Add(component1);
Entity entity2 = entity1.Copy();
Assert.Equal(1, entity2.Count);
Assert.True(entity2.Has<TestComponent1>());
Assert.Equal(component1, entity2.Get<TestComponent1>());
}
[Fact]
public void ExactCopyEntityAlsoCopiesComponents()
{
var component1 = new TestComponent1();
Entity entity1 = new Entity().Add(component1);
Entity entity2 = entity1.ExactCopy();
Assert.Equal(1, entity2.Count);
Assert.True(entity2.Has<TestComponent1>());
Assert.Equal(component1, entity2.Get<TestComponent1>());
}
[Fact]
public void GetOptionalWorks()
{
var component1 = new TestComponent1();
Entity entity1 = new Entity().Add(component1);
Assert.Equal(component1, entity1.GetOptional<TestComponent1>());
Assert.Null(entity1.GetOptional<TestComponent2>());
}
[Fact]
public void HasReturnsFalseIfNotRegistered()
{
var entity1 = new Entity();
Assert.False(entity1.Has<TestComponent1>());
}
[Fact]
public void NewEntityHasNoComponents()
{
var entity1 = new Entity();
Assert.Equal(0, entity1.Count);
}
[Fact]
public void RemoveAlreadyMissingComponentWorks()
{
var entity1 = new Entity();
Assert.Equal(0, entity1.Count);
Entity entity2 = entity1.Remove<TestComponent1>();
Assert.Equal(0, entity1.Count);
Assert.Equal(0, entity2.Count);
}
[Fact]
public void RemoveComponentDoesNotRemoveFromCopies()
{
var component1 = new TestComponent1();
Entity entity1 = new Entity().Add(component1);
Entity entity2 = entity1.ExactCopy();
Entity entity3 = entity1.Copy();
Assert.Equal(1, entity1.Count);
Assert.Equal(1, entity2.Count);
Assert.Equal(1, entity3.Count);
Entity entity1A = entity1.Remove<TestComponent1>();
Assert.Equal(1, entity1.Count);
Assert.Equal(0, entity1A.Count);
Assert.Equal(1, entity2.Count);
Assert.Equal(1, entity3.Count);
}
[Fact]
public void RemoveComponentWorks()
{
var component1 = new TestComponent1();
Entity entity1 = new Entity().Add(component1);
Assert.Equal(1, entity1.Count);
Entity entity2 = entity1.Remove<TestComponent1>();
Assert.Equal(1, entity1.Count);
Assert.Equal(0, entity2.Count);
}
[Fact]
public void SetAllSkipsNull()
{
var component1 = new TestComponent1();
var component2 = new TestComponent2();
Entity entity1 = new Entity().SetAll(component1, null);
Assert.Equal(1, entity1.Count);
Assert.True(entity1.Has<TestComponent1>());
Assert.False(entity1.Has<TestComponent2>());
}
[Fact]
public void SetAllWithNothingWorks()
{
Entity entity1 = new Entity().SetAll();
Assert.Equal(0, entity1.Count);
Assert.False(entity1.Has<TestComponent1>());
Assert.False(entity1.Has<TestComponent2>());
}
[Fact]
public void SetAllWorks()
{
var component1 = new TestComponent1();
var component2 = new TestComponent2();
Entity entity1 = new Entity().SetAll(component1, component2);
Assert.Equal(2, entity1.Count);
Assert.True(entity1.Has<TestComponent1>());
Assert.True(entity1.Has<TestComponent2>());
Assert.False(entity1.Has<TestComponent3a>());
}
[Fact]
public void SettingComponentsWorks()
{
var component1 = new TestComponent3a();
var component2 = new TestComponent3b();
Entity entity1 = new Entity()
.Set<ITestComponent3>(component1)
.Set<ITestComponent3>(component1)
.Set<ITestComponent3>(component2);
Assert.Equal(1, entity1.Count);
Assert.Equal(component2, entity1.Get<ITestComponent3>());
}
[Fact]
public void TryGetWorks()
{
var component1 = new TestComponent1();
Entity entity1 = new Entity().Add(component1);
bool result1 = entity1.TryGet(out TestComponent1 value1);
Assert.True(result1);
Assert.Equal(component1, value1);
bool result2 = entity1.TryGet(out TestComponent2 _);
Assert.False(result2);
}
[Fact]
public void TwoCopiesHaveDifferentIds()
{
var entity1 = new Entity();
Entity entity2 = entity1.Copy();
Assert.NotEqual(entity1.Id, entity2.Id);
Assert.NotEqual(entity1, entity2);
Assert.False(entity1 == entity2);
}
[Fact]
public void TwoEntitiesHaveDifferentIds()
{
var entity1 = new Entity();
var entity2 = new Entity();
Assert.NotEqual(entity1.Id, entity2.Id);
Assert.NotEqual(entity1, entity2);
Assert.False(entity1 == entity2);
}
[Fact]
public void TwoExactCopiesHaveDifferentIds()
{
var entity1 = new Entity();
Entity entity2 = entity1.ExactCopy();
Assert.Equal(entity1.Id, entity2.Id);
Assert.Equal(entity1, entity2);
Assert.True(entity1 == entity2);
}
}