Skip to content
This repository has been archived by the owner on Jan 18, 2022. It is now read-only.

Commit

Permalink
The beginnings of a useful testing framework (#1305)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamie Brynes authored Mar 5, 2020
1 parent fb24f1c commit 76f22ce
Show file tree
Hide file tree
Showing 13 changed files with 596 additions and 260 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
- De-duplicated code for generated `ComponentDiffStorage` instances. [#1290](https://github.com/spatialos/gdk-for-unity/pull/1290)
- `Improbable.Gdk.Core.EntityId` is now a readonly struct. [#1290](https://github.com/spatialos/gdk-for-unity/pull/1290)
- The Playground project now uses QBI instead of CBI. [#1370](https://github.com/spatialos/gdk-for-unity/pull/1307)
- Added `MockWorld` and `MockBase` classes to the `Improbable.Gdk.TestUtils` package. These are designed as a framework for testing Core code. [#1305](https://github.com/spatialos/gdk-for-unity/pull/1305)

## `0.3.3` - 2020-02-14

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"Improbable.Gdk.TransformSynchronization",
"UnityEngine.TestRunner",
"UnityEditor.TestRunner",
"Improbable.Gdk.TestBases"
"Improbable.Gdk.TestBases",
"Improbable.Gdk.TestUtils"
],
"includePlatforms": [
"Editor"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using Improbable.Gdk.Core;
using Improbable.Gdk.Subscriptions;
using Improbable.Gdk.TestBases;
using Improbable.Gdk.TestUtils;
using Improbable.TestSchema;
using Improbable.Worker.CInterop;
using NUnit.Framework;
Expand All @@ -12,118 +13,161 @@ namespace Improbable.Gdk.EditmodeTests.Subscriptions
/// This tests the injection criteria for a reader or writer.
/// </summary>
[TestFixture]
public class ReaderWriterInjectionCriteriaTests : SubscriptionsTestBase
public class ReaderWriterInjectionCriteriaTests : MockBase
{
private const long EntityId = 100;

private GameObject createdGameObject;

[SetUp]
public override void Setup()
{
base.Setup();

var template = new EntityTemplate();
template.AddComponent(new Position.Snapshot(), "worker");
ConnectionHandler.CreateEntity(EntityId, template);
ReceiveSystem.Update();
}

[TearDown]
public override void TearDown()
{
base.TearDown();
Object.DestroyImmediate(createdGameObject);
}

[Test]
public void Reader_is_not_injected_when_entity_component_is_not_checked_out()
{
createdGameObject = CreateAndLinkGameObjectWithComponent<ExhaustiveSingularReaderBehaviour>(EntityId);
var readerBehaviour = createdGameObject.GetComponent<ExhaustiveSingularReaderBehaviour>();

Assert.IsFalse(readerBehaviour.enabled);
Assert.IsNull(readerBehaviour.Reader);
World
.Step(world => { world.Connection.CreateEntity(EntityId, BasicEntity()); })
.Step(world =>
{
var (_, readerBehaviour) = world.CreateGameObject<ExhaustiveSingularReaderBehaviour>(EntityId);
return readerBehaviour;
})
.Step((world, reader) =>
{
Assert.IsFalse(reader.enabled);
Assert.IsNull(reader.Reader);
});
}

[Test]
public void Reader_is_injected_when_entity_component_is_checked_out()
{
createdGameObject = CreateAndLinkGameObjectWithComponent<PositionReaderBehaviour>(EntityId);
var readerBehaviour = createdGameObject.GetComponent<PositionReaderBehaviour>();

Assert.IsTrue(readerBehaviour.enabled);
Assert.IsNotNull(readerBehaviour.Reader);
World
.Step(world => { world.Connection.CreateEntity(EntityId, BasicEntity()); })
.Step(world =>
{
var (_, readerBehaviour) = world.CreateGameObject<PositionReaderBehaviour>(EntityId);
return readerBehaviour;
})
.Step((world, readerBehaviour) =>
{
Assert.IsTrue(readerBehaviour.enabled);
Assert.IsNotNull(readerBehaviour.Reader);
});
}

[Test]
public void Writer_is_not_injected_when_entity_component_is_not_checked_out()
{
createdGameObject = CreateAndLinkGameObjectWithComponent<ExhaustiveSingularWriterBehaviour>(EntityId);
var writerBehaviour = createdGameObject.GetComponent<ExhaustiveSingularWriterBehaviour>();

Assert.IsFalse(writerBehaviour.enabled);
Assert.IsNull(writerBehaviour.Writer);
World
.Step(world => { world.Connection.CreateEntity(EntityId, BasicEntity()); })
.Step(world =>
{
var (_, writerBehaviour) = world.CreateGameObject<ExhaustiveSingularWriterBehaviour>(EntityId);
return writerBehaviour;
})
.Step((world, writerBehaviour) =>
{
Assert.IsFalse(writerBehaviour.enabled);
Assert.IsNull(writerBehaviour.Writer);
});
}

[Test]
public void Writer_is_not_injected_when_entity_component_is_checked_out_and_unauthoritative()
{
createdGameObject = CreateAndLinkGameObjectWithComponent<PositionWriterBehaviour>(EntityId);
var writerBehaviour = createdGameObject.GetComponent<PositionWriterBehaviour>();

Assert.IsFalse(writerBehaviour.enabled);
Assert.IsNull(writerBehaviour.Writer);
World
.Step(world => { world.Connection.CreateEntity(EntityId, BasicEntity()); })
.Step(world =>
{
var (_, writerBehaviour) = world.CreateGameObject<PositionWriterBehaviour>(EntityId);
return writerBehaviour;
})
.Step((world, writerBehaviour) =>
{
Assert.IsFalse(writerBehaviour.enabled);
Assert.IsNull(writerBehaviour.Writer);
});
}

[Test]
public void Writer_is_injected_when_entity_component_is_checked_out_and_authoritative()
{
ConnectionHandler.ChangeAuthority(EntityId, Position.ComponentId, Authority.Authoritative);
ReceiveSystem.Update();

createdGameObject = CreateAndLinkGameObjectWithComponent<PositionWriterBehaviour>(EntityId);
var writerBehaviour = createdGameObject.GetComponent<PositionWriterBehaviour>();

Assert.IsTrue(writerBehaviour.enabled);
Assert.IsNotNull(writerBehaviour.Writer);
World
.Step(world =>
{
world.Connection.CreateEntity(EntityId, BasicEntity());
world.Connection.ChangeAuthority(EntityId, Position.ComponentId, Authority.Authoritative);
})
.Step(world =>
{
var (_, writerBehaviour) = world.CreateGameObject<PositionWriterBehaviour>(EntityId);
return writerBehaviour;
})
.Step((world, writerBehaviour) =>
{
Assert.IsTrue(writerBehaviour.enabled);
Assert.IsNotNull(writerBehaviour.Writer);
});
}

[Test]
public void Injection_does_not_happen_if_not_all_constraints_are_satisfied()
{
createdGameObject = CreateAndLinkGameObjectWithComponent<CompositeBehaviour>(EntityId);
var compositeBehaviour = createdGameObject.GetComponent<CompositeBehaviour>();

Assert.IsFalse(compositeBehaviour.enabled);
Assert.IsNull(compositeBehaviour.Reader);
Assert.IsNull(compositeBehaviour.Writer);
World
.Step(world => { world.Connection.CreateEntity(EntityId, BasicEntity()); })
.Step(world =>
{
var (_, compositeBehaviour) = world.CreateGameObject<CompositeBehaviour>(EntityId);
return compositeBehaviour;
})
.Step((world, compositeBehaviour) =>
{
Assert.IsFalse(compositeBehaviour.enabled);
Assert.IsNull(compositeBehaviour.Reader);
Assert.IsNull(compositeBehaviour.Writer);
});
}

[Test]
public void Injection_happens_if_all_constraints_are_satisfied()
{
ConnectionHandler.ChangeAuthority(EntityId, Position.ComponentId, Authority.Authoritative);
ReceiveSystem.Update();

createdGameObject = CreateAndLinkGameObjectWithComponent<CompositeBehaviour>(EntityId);
var compositeBehaviour = createdGameObject.GetComponent<CompositeBehaviour>();

Assert.IsTrue(compositeBehaviour.enabled);
Assert.IsNotNull(compositeBehaviour.Reader);
Assert.IsNotNull(compositeBehaviour.Writer);
World
.Step(world =>
{
world.Connection.CreateEntity(EntityId, BasicEntity());
world.Connection.ChangeAuthority(EntityId, Position.ComponentId, Authority.Authoritative);
})
.Step(world =>
{
var (_, compositeBehaviour) = world.CreateGameObject<CompositeBehaviour>(EntityId);
return compositeBehaviour;
})
.Step((world, compositeBehaviour) =>
{
Assert.IsTrue(compositeBehaviour.enabled);
Assert.IsNotNull(compositeBehaviour.Reader);
Assert.IsNotNull(compositeBehaviour.Writer);
});
}

[Test]
public void Injection_happens_if_inherited_constraints_are_satisfied()
{
ReceiveSystem.Update();

createdGameObject = CreateAndLinkGameObjectWithComponent<InheritanceBehaviour>(EntityId);
var inheritanceBehaviour = createdGameObject.GetComponent<InheritanceBehaviour>();
World
.Step(world => { world.Connection.CreateEntity(EntityId, BasicEntity()); })
.Step(world =>
{
var (_, readerBehaviour) = world.CreateGameObject<InheritanceBehaviour>(EntityId);
return readerBehaviour;
})
.Step((world, readerBehaviour) =>
{
Assert.IsTrue(readerBehaviour.enabled);
Assert.IsNotNull(readerBehaviour.OwnReader);
});
}

Assert.IsTrue(inheritanceBehaviour.enabled);
Assert.IsNotNull(inheritanceBehaviour.OwnReader);
private static EntityTemplate BasicEntity()
{
var template = new EntityTemplate();
template.AddComponent(new Position.Snapshot(), "worker");
return template;
}

#pragma warning disable 649
Expand Down
Loading

0 comments on commit 76f22ce

Please sign in to comment.