last hope
This commit is contained in:
@@ -23,10 +23,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Engine\Engine.csproj"/>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="src\"/>
|
||||
<ProjectReference Include="..\TestUtil\TestUtil.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -68,4 +68,220 @@ public class GameObjectTests
|
||||
|
||||
Assert.Throws<ArgumentException>(() => _gameObject.ProcessChanges());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AddComponent_ShouldSetGameObjectForAddedComponent()
|
||||
{
|
||||
var testComponent = new TestComponent();
|
||||
_gameObject.AddComponent(testComponent);
|
||||
_gameObject.ProcessChanges();
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(testComponent.GameObject, Is.EqualTo(_gameObject));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AddComponent_ShouldCallAwakeOnAddedComponentOnPreUpdate()
|
||||
{
|
||||
var testComponent = new TestComponent();
|
||||
var wasAwakeCalled = false;
|
||||
testComponent.OnAwake += () => wasAwakeCalled = true;
|
||||
|
||||
_gameObject.AddComponent(testComponent);
|
||||
_gameObject.ProcessChanges();
|
||||
_gameObject.PreUpdate(0);
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(wasAwakeCalled, Is.True);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AddComponent_ShouldCallStartOnAddedComponentOnPreUpdate()
|
||||
{
|
||||
var testComponent = new TestComponent();
|
||||
var wasStartCalled = false;
|
||||
testComponent.OnStart += () => wasStartCalled = true;
|
||||
|
||||
_gameObject.AddComponent(testComponent);
|
||||
_gameObject.ProcessChanges();
|
||||
_gameObject.PreUpdate(0);
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(wasStartCalled, Is.True);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AddComponent_ShouldCallPreUpdateOnAddedComponentOnPreUpdate()
|
||||
{
|
||||
var testComponent = new TestComponent();
|
||||
var wasPreUpdateCalled = false;
|
||||
testComponent.OnPreUpdate += _ => wasPreUpdateCalled = true;
|
||||
|
||||
_gameObject.AddComponent(testComponent);
|
||||
_gameObject.ProcessChanges();
|
||||
_gameObject.PreUpdate(0);
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(wasPreUpdateCalled, Is.True);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AddComponent_ShouldCallUpdateOnAddedComponentOnUpdate()
|
||||
{
|
||||
var testComponent = new TestComponent();
|
||||
var wasUpdateCalled = false;
|
||||
testComponent.OnUpdate += _ => wasUpdateCalled = true;
|
||||
|
||||
_gameObject.AddComponent(testComponent);
|
||||
_gameObject.ProcessChanges();
|
||||
_gameObject.Update(0);
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(wasUpdateCalled, Is.True);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AddComponent_ShouldCallPostUpdateOnAddedComponentOnPostUpdate()
|
||||
{
|
||||
var testComponent = new TestComponent();
|
||||
var wasPostUpdateCalled = false;
|
||||
testComponent.OnPostUpdate += _ => wasPostUpdateCalled = true;
|
||||
|
||||
_gameObject.AddComponent(testComponent);
|
||||
_gameObject.ProcessChanges();
|
||||
_gameObject.PostUpdate(0);
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(wasPostUpdateCalled, Is.True);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AddComponent_ShouldCallRenderOnAddedComponentOnRender()
|
||||
{
|
||||
var testComponent = new TestComponent();
|
||||
var wasRenderCalled = false;
|
||||
testComponent.OnRender += () => wasRenderCalled = true;
|
||||
|
||||
_gameObject.AddComponent(testComponent);
|
||||
_gameObject.ProcessChanges();
|
||||
_gameObject.Render();
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(wasRenderCalled, Is.True);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AddComponent_ShouldCallDestroyOnAddedComponentOnDestroy()
|
||||
{
|
||||
var testComponent = new TestComponent();
|
||||
var wasDestroyCalled = false;
|
||||
testComponent.OnDestroy += () => wasDestroyCalled = true;
|
||||
|
||||
_gameObject.AddComponent(testComponent);
|
||||
_gameObject.ProcessChanges();
|
||||
_gameObject.Destroy();
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(wasDestroyCalled, Is.True);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AddComponent_ShouldCallDisableOnAddedComponentOnDisable()
|
||||
{
|
||||
var testComponent = new TestComponent();
|
||||
var wasDisableCalled = false;
|
||||
testComponent.OnDisable += () => wasDisableCalled = true;
|
||||
|
||||
_gameObject.IsEnabled = false;
|
||||
|
||||
_gameObject.AddComponent(testComponent);
|
||||
_gameObject.ProcessChanges();
|
||||
_gameObject.Update(0);
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(wasDisableCalled, Is.True);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AddComponent_ShouldCallEnableOnAddedComponentOnEnable()
|
||||
{
|
||||
var testComponent = new TestComponent();
|
||||
var wasEnableCalled = false;
|
||||
testComponent.OnEnable += () => wasEnableCalled = true;
|
||||
|
||||
_gameObject.IsEnabled = false;
|
||||
|
||||
_gameObject.AddComponent(testComponent);
|
||||
_gameObject.ProcessChanges();
|
||||
_gameObject.Update(0);
|
||||
|
||||
_gameObject.IsEnabled = true;
|
||||
_gameObject.ProcessChanges();
|
||||
_gameObject.Update(0);
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(wasEnableCalled, Is.True);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void RemoveComponent_ShouldThrowIfComponentIsTransform()
|
||||
{
|
||||
Assert.Throws<ArgumentException>(() => _gameObject.RemoveComponent<Transform>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void RemoveComponent_ShouldThrowIfComponentDoesNotExist()
|
||||
{
|
||||
_gameObject.RemoveComponent<TestComponent>();
|
||||
|
||||
Assert.Throws<ArgumentException>(() => _gameObject.ProcessChanges());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetComponent_ShouldReturnComponentIfExists()
|
||||
{
|
||||
var testComponent = new TestComponent();
|
||||
_gameObject.AddComponent(testComponent);
|
||||
_gameObject.ProcessChanges();
|
||||
|
||||
var component = _gameObject.GetComponent<TestComponent>();
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(component, Is.EqualTo(testComponent));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetComponent_ShouldReturnNullIfComponentDoesNotExist()
|
||||
{
|
||||
_gameObject.ProcessChanges();
|
||||
|
||||
var component = _gameObject.GetComponent<TestComponent>();
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(component, Is.Null);
|
||||
});
|
||||
}
|
||||
}
|
||||
199
EngineTests/src/Scene/SceneTests.cs
Normal file
199
EngineTests/src/Scene/SceneTests.cs
Normal file
@@ -0,0 +1,199 @@
|
||||
using Engine.Scene;
|
||||
|
||||
namespace EngineTests.Scene;
|
||||
|
||||
public class SceneTests
|
||||
{
|
||||
private Engine.Scene.Scene _scene;
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
_scene = new Engine.Scene.Scene();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Enter_ShouldSetSceneToPlaying()
|
||||
{
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(_scene.IsPlaying, Is.False);
|
||||
});
|
||||
|
||||
_scene.Enter();
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(_scene.IsPlaying, Is.True);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Exit_ShouldUnsetSceneToPlaying()
|
||||
{
|
||||
_scene.Enter();
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(_scene.IsPlaying, Is.True);
|
||||
});
|
||||
|
||||
_scene.Exit();
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(_scene.IsPlaying, Is.False);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Update_ShouldRespectTimeScale()
|
||||
{
|
||||
const double INITIAL_DELTA_TIME = 1.0;
|
||||
const double TIME_SCALE = 2.0;
|
||||
|
||||
var (gameObject, testComponent) = CreateTestGameObject();
|
||||
var actualDeltaTime = 0.0;
|
||||
testComponent.OnUpdate += parDeltaTime => actualDeltaTime = parDeltaTime;
|
||||
|
||||
_scene.Add(gameObject);
|
||||
_scene.Enter();
|
||||
|
||||
_scene.TimeScale = TIME_SCALE;
|
||||
_scene.Update(INITIAL_DELTA_TIME);
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actualDeltaTime, Is.EqualTo(INITIAL_DELTA_TIME * TIME_SCALE));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Add_ShouldSetSceneForAddedGameObject()
|
||||
{
|
||||
var (gameObject, _) = CreateTestGameObject();
|
||||
_scene.Add(gameObject);
|
||||
_scene.ProcessChanges();
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(gameObject.Scene, Is.EqualTo(_scene));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Add_ShouldAddGameObjectToHierarchy()
|
||||
{
|
||||
var (gameObject, _) = CreateTestGameObject();
|
||||
_scene.Add(gameObject);
|
||||
_scene.ProcessChanges();
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(_scene.Hierarchy.Objects, Contains.Item(gameObject));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Remove_ShouldUnsetSceneForRemovedGameObject()
|
||||
{
|
||||
var (gameObject, _) = CreateTestGameObject();
|
||||
_scene.Add(gameObject);
|
||||
_scene.ProcessChanges();
|
||||
|
||||
_scene.Remove(gameObject);
|
||||
_scene.ProcessChanges();
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(gameObject.Scene, Is.Null);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Remove_ShouldRemoveGameObjectFromHierarchy()
|
||||
{
|
||||
var (gameObject, _) = CreateTestGameObject();
|
||||
_scene.Add(gameObject);
|
||||
_scene.ProcessChanges();
|
||||
|
||||
_scene.Remove(gameObject);
|
||||
_scene.ProcessChanges();
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(_scene.Hierarchy.Objects, Is.Not.Contains(gameObject));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void FindAllComponents_ShouldReturnAllComponentsCount1()
|
||||
{
|
||||
var (gameObject, testComponent) = CreateTestGameObject();
|
||||
_scene.Add(gameObject);
|
||||
_scene.ProcessChanges();
|
||||
|
||||
var components = _scene.FindAllComponents<TestComponent>();
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(components, Contains.Item(testComponent));
|
||||
Assert.That(components, Has.Count.EqualTo(1));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void FindAllComponents_ShouldReturnAllComponentsCount2()
|
||||
{
|
||||
var (gameObject1, testComponent1) = CreateTestGameObject();
|
||||
var (gameObject2, testComponent2) = CreateTestGameObject();
|
||||
_scene.Add(gameObject1);
|
||||
_scene.Add(gameObject2);
|
||||
_scene.ProcessChanges();
|
||||
|
||||
var components = _scene.FindAllComponents<TestComponent>();
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(components, Contains.Item(testComponent1));
|
||||
Assert.That(components, Contains.Item(testComponent2));
|
||||
Assert.That(components, Has.Count.EqualTo(2));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void FindFirstComponent_ShouldReturnFirstComponent()
|
||||
{
|
||||
var (gameObject, testComponent) = CreateTestGameObject();
|
||||
_scene.Add(gameObject);
|
||||
_scene.ProcessChanges();
|
||||
|
||||
var component = _scene.FindFirstComponent<TestComponent>();
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(component, Is.EqualTo(testComponent));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void FindFirstComponent_ShouldReturnNullIfComponentDoesNotExist()
|
||||
{
|
||||
_scene.ProcessChanges();
|
||||
|
||||
var component = _scene.FindFirstComponent<TestComponent>();
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(component, Is.Null);
|
||||
});
|
||||
}
|
||||
|
||||
private static (GameObject, TestComponent) CreateTestGameObject()
|
||||
{
|
||||
var gameObject = new GameObject();
|
||||
var testComponent = new TestComponent();
|
||||
|
||||
gameObject.AddComponent(testComponent);
|
||||
gameObject.ProcessChanges();
|
||||
|
||||
return (gameObject, testComponent);
|
||||
}
|
||||
}
|
||||
61
EngineTests/src/Scene/TestComponent.cs
Normal file
61
EngineTests/src/Scene/TestComponent.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
using Engine.Scene.Component;
|
||||
|
||||
namespace EngineTests.Scene;
|
||||
|
||||
public class TestComponent : Component
|
||||
{
|
||||
public event Action? OnAwake;
|
||||
public event Action? OnStart;
|
||||
public event Action<double>? OnPreUpdate;
|
||||
public event Action<double>? OnUpdate;
|
||||
public event Action<double>? OnPostUpdate;
|
||||
public event Action? OnRender;
|
||||
public event Action? OnDestroy;
|
||||
public event Action? OnEnable;
|
||||
public event Action? OnDisable;
|
||||
|
||||
public override void Awake()
|
||||
{
|
||||
OnAwake?.Invoke();
|
||||
}
|
||||
|
||||
public override void Start()
|
||||
{
|
||||
OnStart?.Invoke();
|
||||
}
|
||||
|
||||
public override void PreUpdate(double parDeltaTime)
|
||||
{
|
||||
OnPreUpdate?.Invoke(parDeltaTime);
|
||||
}
|
||||
|
||||
public override void Update(double parDeltaTime)
|
||||
{
|
||||
OnUpdate?.Invoke(parDeltaTime);
|
||||
}
|
||||
|
||||
public override void PostUpdate(double parDeltaTime)
|
||||
{
|
||||
OnPostUpdate?.Invoke(parDeltaTime);
|
||||
}
|
||||
|
||||
public override void Render()
|
||||
{
|
||||
OnRender?.Invoke();
|
||||
}
|
||||
|
||||
public override void Destroy()
|
||||
{
|
||||
OnDestroy?.Invoke();
|
||||
}
|
||||
|
||||
public override void Enable()
|
||||
{
|
||||
OnEnable?.Invoke();
|
||||
}
|
||||
|
||||
public override void Disable()
|
||||
{
|
||||
OnDisable?.Invoke();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user