diff --git a/Aplib.Core.Tests/BdiAgentTests.cs b/Aplib.Core.Tests/BdiAgentTests.cs
index 3745714a..dd4cc9eb 100644
--- a/Aplib.Core.Tests/BdiAgentTests.cs
+++ b/Aplib.Core.Tests/BdiAgentTests.cs
@@ -1,3 +1,7 @@
+// This program has been developed by students from the bachelor Computer Science at Utrecht
+// University within the Software Project course.
+// Copyright Utrecht University (Department of Information and Computing Sciences)
+
using Aplib.Core.Agents;
using Aplib.Core.Belief.BeliefSets;
using Aplib.Core.Desire.DesireSets;
diff --git a/Aplib.Core.Tests/Belief/BeliefSetTests.cs b/Aplib.Core.Tests/Belief/BeliefSetTests.cs
index 6a0b2a4f..0200b6ca 100644
--- a/Aplib.Core.Tests/Belief/BeliefSetTests.cs
+++ b/Aplib.Core.Tests/Belief/BeliefSetTests.cs
@@ -1,4 +1,8 @@
-using Aplib.Core.Belief.Beliefs;
+// This program has been developed by students from the bachelor Computer Science at Utrecht
+// University within the Software Project course.
+// Copyright Utrecht University (Department of Information and Computing Sciences)
+
+using Aplib.Core.Belief.Beliefs;
using Aplib.Core.Belief.BeliefSets;
namespace Aplib.Core.Tests.Belief;
@@ -9,79 +13,45 @@ namespace Aplib.Core.Tests.Belief;
public class BeliefSetTests
{
///
- /// Given a BeliefSet instance with multiple public field beliefs,
- /// When UpdateBeliefs is called,
- /// Then all beliefs are updated.
+ /// A simple belief that can be used to test whether has been called.
///
- [Fact]
- public void UpdateBeliefs_PublicBeliefFields_UpdatesAllBeliefs()
+ private class SimpleBelief : IBelief
{
- // Arrange
- TestBeliefSetPublic beliefSet = new();
-
- // Act
- // UpdateBeliefs should set Updated to true for all beliefs.
- beliefSet.UpdateBeliefs();
+ ///
+ /// Stores whether has been called.
+ ///
+ public bool Updated { get; private set; } = false;
- // Assert
- Assert.True(beliefSet.Belief1.Updated);
- Assert.True(beliefSet.Belief2.Updated);
+ ///
+ /// Sets to true.
+ ///
+ public void UpdateBelief()
+ {
+ Updated = true;
+ }
}
- ///
- /// Given a BeliefSet instance with multiple public property beliefs,
- /// When UpdateBeliefs is called,
- /// Then no beliefs are updated.
- ///
- [Fact]
- public void UpdateBeliefs_PublicBeliefProperties_DoesNotUpdateAnyBeliefs()
- {
- // Arrange
- TestBeliefSetProperties beliefSet = new();
-
- // Act
- // UpdateBeliefs should *not* set Updated to true for any belief.
- beliefSet.UpdateBeliefs();
-
- // Assert
- Assert.False(beliefSet.Belief1.Updated);
- Assert.False(beliefSet.Belief2.Updated);
- }
///
- /// Given a BeliefSet instance with multiple private field beliefs,
- /// When UpdateBeliefs is called,
- /// Then no beliefs are updated.
+ /// A test belief set that contains two private simple beliefs.
///
- [Fact]
- public void UpdateBeliefs_PrivateBeliefFields_DoesNotUpdateAnyBeliefs()
+ private class TestBeliefSetPrivate : BeliefSet
{
- // Arrange
- TestBeliefSetPrivate beliefSet = new();
-
- // Act
- // UpdateBeliefs should *not* set Updated to true for any belief.
- beliefSet.UpdateBeliefs();
+ ///
+ public SimpleBelief Belief1 => _belief1;
- // Assert
- Assert.False(beliefSet.Belief1.Updated);
- Assert.False(beliefSet.Belief2.Updated);
- }
+ ///
+ public SimpleBelief Belief2 => _belief2;
- ///
- /// A test belief set that contains two public simple beliefs.
- ///
- private class TestBeliefSetPublic : BeliefSet
- {
///
/// Belief that sets Updated to true when UpdateBelief is called.
///
- public SimpleBelief Belief1 = new();
+ private SimpleBelief _belief1 = new();
///
/// Belief that sets Updated to true when UpdateBelief is called.
///
- public SimpleBelief Belief2 = new();
+ private SimpleBelief _belief2 = new();
}
@@ -101,45 +71,79 @@ private class TestBeliefSetProperties : BeliefSet
public SimpleBelief Belief2 { get; } = new();
}
-
///
- /// A test belief set that contains two private simple beliefs.
+ /// A test belief set that contains two public simple beliefs.
///
- private class TestBeliefSetPrivate : BeliefSet
+ private class TestBeliefSetPublic : BeliefSet
{
///
/// Belief that sets Updated to true when UpdateBelief is called.
///
- private SimpleBelief _belief1 = new();
+ public SimpleBelief Belief1 = new();
///
/// Belief that sets Updated to true when UpdateBelief is called.
///
- private SimpleBelief _belief2 = new();
+ public SimpleBelief Belief2 = new();
+ }
- ///
- public SimpleBelief Belief1 => _belief1;
+ ///
+ /// Given a BeliefSet instance with multiple private field beliefs,
+ /// When UpdateBeliefs is called,
+ /// Then no beliefs are updated.
+ ///
+ [Fact]
+ public void UpdateBeliefs_PrivateBeliefFields_DoesNotUpdateAnyBeliefs()
+ {
+ // Arrange
+ TestBeliefSetPrivate beliefSet = new();
- ///
- public SimpleBelief Belief2 => _belief2;
+ // Act
+ // UpdateBeliefs should *not* set Updated to true for any belief.
+ beliefSet.UpdateBeliefs();
+
+ // Assert
+ Assert.False(beliefSet.Belief1.Updated);
+ Assert.False(beliefSet.Belief2.Updated);
}
///
- /// A simple belief that can be used to test whether has been called.
+ /// Given a BeliefSet instance with multiple public field beliefs,
+ /// When UpdateBeliefs is called,
+ /// Then all beliefs are updated.
///
- private class SimpleBelief : IBelief
+ [Fact]
+ public void UpdateBeliefs_PublicBeliefFields_UpdatesAllBeliefs()
{
- ///
- /// Stores whether has been called.
- ///
- public bool Updated { get; private set; } = false;
+ // Arrange
+ TestBeliefSetPublic beliefSet = new();
- ///
- /// Sets to true.
- ///
- public void UpdateBelief()
- {
- Updated = true;
- }
+ // Act
+ // UpdateBeliefs should set Updated to true for all beliefs.
+ beliefSet.UpdateBeliefs();
+
+ // Assert
+ Assert.True(beliefSet.Belief1.Updated);
+ Assert.True(beliefSet.Belief2.Updated);
+ }
+
+ ///
+ /// Given a BeliefSet instance with multiple public property beliefs,
+ /// When UpdateBeliefs is called,
+ /// Then no beliefs are updated.
+ ///
+ [Fact]
+ public void UpdateBeliefs_PublicBeliefProperties_DoesNotUpdateAnyBeliefs()
+ {
+ // Arrange
+ TestBeliefSetProperties beliefSet = new();
+
+ // Act
+ // UpdateBeliefs should *not* set Updated to true for any belief.
+ beliefSet.UpdateBeliefs();
+
+ // Assert
+ Assert.False(beliefSet.Belief1.Updated);
+ Assert.False(beliefSet.Belief2.Updated);
}
}
diff --git a/Aplib.Core.Tests/Belief/BeliefTests.cs b/Aplib.Core.Tests/Belief/BeliefTests.cs
index 342c2b0d..b286c3c9 100644
--- a/Aplib.Core.Tests/Belief/BeliefTests.cs
+++ b/Aplib.Core.Tests/Belief/BeliefTests.cs
@@ -1,4 +1,8 @@
-using Aplib.Core.Belief.Beliefs;
+// This program has been developed by students from the bachelor Computer Science at Utrecht
+// University within the Software Project course.
+// Copyright Utrecht University (Department of Information and Computing Sciences)
+
+using Aplib.Core.Belief.Beliefs;
using FluentAssertions;
using Moq;
using System.Collections;
@@ -34,9 +38,8 @@ public IEnumerator GetEnumerator()
public class TestBelief : Belief
{
- public object Reference => _reference;
-
public System.Func GetObservationFromReference => _getObservationFromReference;
+ public object Reference => _reference;
public System.Predicate ShouldUpdate => _shouldUpdate;
@@ -68,6 +71,67 @@ public TestBelief(object reference, System.Func getObservationFr
}
}
+ ///
+ /// Given a Belief instance,
+ /// When it is assigned to a variable of its observation type,
+ /// Then it is implicitly converted to its observation type.
+ ///
+ [Fact]
+ public void Belief_AssignedToObservationType_IsCorrectlyImplicitlyConvertedToObservationType()
+ {
+ // Arrange
+ // ReSharper disable once ConvertToConstant.Local
+ string def = "def";
+
+ // Observation: Get the first letter.
+ Belief belief = new(def, reference => reference[0]);
+
+ // Act, Assert
+ Assert.Equal(belief.Observation, belief);
+ }
+
+ ///
+ /// Given a reference that is actually a value type, hidden behind an interface,
+ /// When a new Belief is constructed from this reference,
+ /// The constructor throws an ArgumentException.
+ ///
+ [Fact]
+ public void Belief_ConstructedWithAValueTypeViaAnInterface_IsRejected()
+ {
+ // Arrange
+ MyEnumerable value = new(1);
+ const string paramName = "reference";
+
+ // ReSharper disable once ConvertToLocalFunction
+ System.Action construction = () =>
+ {
+ // The bug is the fact that we can get around the constraint that `TReference` should be a reference type.
+ _ = new Belief, List>(value, values => values.ToList());
+ };
+
+ // Act, Assert
+ Assert.Throws(paramName, construction);
+ }
+
+ ///
+ /// Given a reference,
+ /// When a new Belief is constructed,
+ /// Then the observation is also initialized.
+ ///
+ [Fact]
+ public void Belief_DuringConstruction_UpdatesTheObservation()
+ {
+ // Arrange
+ // ReSharper disable once ConvertToConstant.Local
+ string def = "def";
+
+ // Act
+ Belief belief = new(def, str => str);
+
+ // Assert
+ Assert.Equal(def, belief.Observation);
+ }
+
[Fact]
public void Belief_WhenConstructed_HasExpectedData()
{
@@ -107,24 +171,6 @@ public void Belief_WithoutMetadata_HasExpectedData()
belief.ShouldUpdate.Should().Be(shouldUpdate);
}
- [Fact]
- public void Belief_WithoutShouldUpdate_HasExpectedData()
- {
- // Arrange
- Metadata metadata = It.IsAny();
- object reference = new Mock().Object;
- System.Func getObservationFromReference = new Mock>().Object;
-
- // Act
- TestBelief belief = new(metadata, reference, getObservationFromReference);
-
- // Assert
- belief.Metadata.Should().Be(metadata);
- belief.Reference.Should().Be(reference);
- belief.GetObservationFromReference.Should().Be(getObservationFromReference);
- belief.ShouldUpdate(reference).Should().BeTrue();
- }
-
[Fact]
public void Belief_WithoutMetadataWithoutShouldUpdate_HasExpectedData()
{
@@ -144,65 +190,22 @@ public void Belief_WithoutMetadataWithoutShouldUpdate_HasExpectedData()
belief.ShouldUpdate(reference).Should().BeTrue();
}
- ///
- /// Given a Belief instance,
- /// When it is assigned to a variable of its observation type,
- /// Then it is implicitly converted to its observation type.
- ///
- [Fact]
- public void Belief_AssignedToObservationType_IsCorrectlyImplicitlyConvertedToObservationType()
- {
- // Arrange
- // ReSharper disable once ConvertToConstant.Local
- string def = "def";
-
- // Observation: Get the first letter.
- Belief belief = new(def, reference => reference[0]);
-
- // Act, Assert
- Assert.Equal(belief.Observation, belief);
- }
-
- ///
- /// Given a reference that is actually a value type, hidden behind an interface,
- /// When a new Belief is constructed from this reference,
- /// The constructor throws an ArgumentException.
- ///
- [Fact]
- public void Belief_ConstructedWithAValueTypeViaAnInterface_IsRejected()
- {
- // Arrange
- MyEnumerable value = new(1);
- const string paramName = "reference";
-
- // ReSharper disable once ConvertToLocalFunction
- System.Action construction = () =>
- {
- // The bug is the fact that we can get around the constraint that `TReference` should be a reference type.
- _ = new Belief, List>(value, values => values.ToList());
- };
-
- // Act, Assert
- Assert.Throws(paramName, construction);
- }
-
- ///
- /// Given a reference,
- /// When a new Belief is constructed,
- /// Then the observation is also initialized.
- ///
[Fact]
- public void Belief_DuringConstruction_UpdatesTheObservation()
+ public void Belief_WithoutShouldUpdate_HasExpectedData()
{
// Arrange
- // ReSharper disable once ConvertToConstant.Local
- string def = "def";
+ Metadata metadata = It.IsAny();
+ object reference = new Mock().Object;
+ System.Func getObservationFromReference = new Mock>().Object;
// Act
- Belief belief = new(def, str => str);
+ TestBelief belief = new(metadata, reference, getObservationFromReference);
// Assert
- Assert.Equal(def, belief.Observation);
+ belief.Metadata.Should().Be(metadata);
+ belief.Reference.Should().Be(reference);
+ belief.GetObservationFromReference.Should().Be(getObservationFromReference);
+ belief.ShouldUpdate(reference).Should().BeTrue();
}
///
diff --git a/Aplib.Core.Tests/Belief/ListBeliefTests.cs b/Aplib.Core.Tests/Belief/ListBeliefTests.cs
index 5d9e042f..49c16de4 100644
--- a/Aplib.Core.Tests/Belief/ListBeliefTests.cs
+++ b/Aplib.Core.Tests/Belief/ListBeliefTests.cs
@@ -1,3 +1,7 @@
+// This program has been developed by students from the bachelor Computer Science at Utrecht
+// University within the Software Project course.
+// Copyright Utrecht University (Department of Information and Computing Sciences)
+
using Aplib.Core.Belief.Beliefs;
using System.Collections.Generic;
@@ -9,26 +13,22 @@ namespace Aplib.Core.Tests.Belief;
public class ListBeliefTests
{
///
- /// Given a ListBelief without an explicit shouldUpdate parameter,
- /// When UpdateBelief is called,
- /// Then the observation is updated.
+ /// Given an empty collection,
+ /// When a ListBelief is created from that collection,
+ /// Then the observation is also empty
///
[Fact]
- public void ListBelief_WithoutShouldUpdate_UpdatesObservation()
+ public void ListBelief_FromEmptyEnumerable_HasEmptyObservationList()
{
// Arrange
- int[] numbers = [1, 1, 2, 3, 5, 8];
-
- // Observation: Is the number even?
- ListBelief belief = new(numbers, i => i % 2 == 0);
+ // ReSharper disable once CollectionNeverUpdated.Local
+ Stack stack = new();
// Act
- numbers[0] = 0;
- belief.UpdateBelief();
+ ListBelief belief = new(stack, b => b);
// Assert
- List expected = [true, false, true, false, false, true];
- Assert.Equal(expected, belief);
+ Assert.Empty(belief.Observation);
}
///
@@ -55,21 +55,25 @@ public void ListBelief_ShouldUpdateConditionIsNotSatisfied_DoesNotUpdateObservat
}
///
- /// Given an empty collection,
- /// When a ListBelief is created from that collection,
- /// Then the observation is also empty
+ /// Given a ListBelief without an explicit shouldUpdate parameter,
+ /// When UpdateBelief is called,
+ /// Then the observation is updated.
///
[Fact]
- public void ListBelief_FromEmptyEnumerable_HasEmptyObservationList()
+ public void ListBelief_WithoutShouldUpdate_UpdatesObservation()
{
// Arrange
- // ReSharper disable once CollectionNeverUpdated.Local
- Stack stack = new();
+ int[] numbers = [1, 1, 2, 3, 5, 8];
+
+ // Observation: Is the number even?
+ ListBelief belief = new(numbers, i => i % 2 == 0);
// Act
- ListBelief belief = new(stack, b => b);
+ numbers[0] = 0;
+ belief.UpdateBelief();
// Assert
- Assert.Empty(belief.Observation);
+ List expected = [true, false, true, false, false, true];
+ Assert.Equal(expected, belief);
}
}
diff --git a/Aplib.Core.Tests/Belief/MemoryBeliefTests.cs b/Aplib.Core.Tests/Belief/MemoryBeliefTests.cs
index 71d3b303..f43f7a7b 100644
--- a/Aplib.Core.Tests/Belief/MemoryBeliefTests.cs
+++ b/Aplib.Core.Tests/Belief/MemoryBeliefTests.cs
@@ -1,3 +1,7 @@
+// This program has been developed by students from the bachelor Computer Science at Utrecht
+// University within the Software Project course.
+// Copyright Utrecht University (Department of Information and Computing Sciences)
+
using Aplib.Core.Belief.Beliefs;
using Aplib.Core.Collections;
using FluentAssertions;
@@ -13,13 +17,12 @@ public class MemoryBeliefTests
{
public class TestMemoryBelief : MemoryBelief
{
- public object Reference => _reference;
-
public System.Func GetObservationFromReference => _getObservationFromReference;
- public System.Predicate ShouldUpdate => _shouldUpdate;
-
public ExposedQueue MemorizedObservations => _memorizedObservations;
+ public object Reference => _reference;
+
+ public System.Predicate ShouldUpdate => _shouldUpdate;
public TestMemoryBelief
(
@@ -62,88 +65,68 @@ public TestMemoryBelief
}
}
+ ///
+ /// Given a MemoryBelief instance with an observation,
+ /// When the observation is updated and GetAllMemories is called,
+ /// Then all the currently saved observations are returned.
+ ///
[Fact]
- public void MemoryBelief_WhenConstructed_HasExpectedData()
- {
- // Arrange
- Metadata metadata = It.IsAny();
- object reference = new Mock().Object;
- System.Func getObservationFromReference = new Mock>().Object;
- const int framesToRemember = 0;
- System.Predicate shouldUpdate = It.IsAny>();
-
- // Act
- TestMemoryBelief belief = new(metadata, reference, getObservationFromReference, framesToRemember, shouldUpdate);
-
- // Assert
- belief.Metadata.Should().Be(metadata);
- belief.Reference.Should().Be(reference);
- belief.GetObservationFromReference.Should().Be(getObservationFromReference);
- belief.MemorizedObservations.MaxCount.Should().Be(framesToRemember);
- belief.ShouldUpdate.Should().Be(shouldUpdate);
- }
-
- [Fact]
- public void MemoryBelief_WithoutMetadata_HasExpectedData()
+ public void GetAllMemories_ReturnsAllMemories()
{
// Arrange
- object reference = new Mock().Object;
- System.Func getObservationFromReference = new Mock>().Object;
- const int framesToRemember = 1;
- System.Predicate shouldUpdate = It.IsAny>();
+ List list = [1, 2, 3];
+ MemoryBelief, int> belief = new(list, reference => reference.Count, 3);
// Act
- TestMemoryBelief belief = new(reference, getObservationFromReference, framesToRemember, shouldUpdate);
+ list.Add(4);
+ belief.UpdateBelief();
// Assert
- belief.Metadata.Id.Should().NotBeEmpty();
- belief.Metadata.Name.Should().BeNull();
- belief.Metadata.Description.Should().BeNull();
- belief.Reference.Should().Be(reference);
- belief.GetObservationFromReference.Should().Be(getObservationFromReference);
- belief.MemorizedObservations.MaxCount.Should().Be(framesToRemember);
- belief.ShouldUpdate.Should().Be(shouldUpdate);
+ Assert.Equal([3], belief.GetAllMemories());
}
+ ///
+ /// Given a MemoryBelief instance with an observation,
+ /// When asking for an index that is out of bounds,
+ /// Then an exception should be thrown.
+ ///
[Fact]
- public void MemoryBelief_WithoutShouldUpdate_HasExpectedData()
+ public void GetMemoryAt_IndexOutOfBounds_ShouldThrowException()
{
// Arrange
- Metadata metadata = It.IsAny();
- object reference = new Mock().Object;
- System.Func getObservationFromReference = new Mock>().Object;
- const int framesToRemember = 2;
+ List list = [1, 2, 3];
+ MemoryBelief, int> belief = new(list, reference => reference.Count, 3);
// Act
- TestMemoryBelief belief = new(metadata, reference, getObservationFromReference, framesToRemember);
+ void GetMemoryAtNegativeIndex() => belief.GetMemoryAt(-1);
+ void GetMemoryAtIndexGreaterThanCount() => belief.GetMemoryAt(3);
// Assert
- belief.Metadata.Should().Be(metadata);
- belief.Reference.Should().Be(reference);
- belief.GetObservationFromReference.Should().Be(getObservationFromReference);
- belief.MemorizedObservations.MaxCount.Should().Be(framesToRemember);
- belief.ShouldUpdate(reference).Should().BeTrue();
+ Assert.Throws(GetMemoryAtNegativeIndex);
+ Assert.Throws(GetMemoryAtIndexGreaterThanCount);
}
+ ///
+ /// Given a MemoryBelief instance with an observation,
+ /// When the observation is updated and GetMemoryAt is called with an index,
+ /// Then the observation at the specified index is returned.
+ ///
[Fact]
- public void MemoryBelief_WithoutMetadataWithoutShouldUpdate_HasExpectedData()
+ public void GetMemoryAt_WhenObservationIsUpdated_ShouldReturnObservationAtSpecifiedIndex()
{
// Arrange
- object reference = new Mock().Object;
- System.Func getObservationFromReference = new Mock>().Object;
- const int framesToRemember = 3;
+ List list = [1, 2, 3];
+ MemoryBelief, int> belief = new(list, reference => reference.Count, 3);
// Act
- TestMemoryBelief belief = new(reference, getObservationFromReference, framesToRemember);
+ list.Add(4);
+ belief.UpdateBelief();
+ list.Add(5);
+ belief.UpdateBelief();
// Assert
- belief.Metadata.Id.Should().NotBeEmpty();
- belief.Metadata.Name.Should().BeNull();
- belief.Metadata.Description.Should().BeNull();
- belief.Reference.Should().Be(reference);
- belief.GetObservationFromReference.Should().Be(getObservationFromReference);
- belief.MemorizedObservations.MaxCount.Should().Be(framesToRemember);
- belief.ShouldUpdate(reference).Should().BeTrue();
+ Assert.Equal(4, belief.GetMemoryAt(0));
+ Assert.Equal(3, belief.GetMemoryAt(1));
}
///
@@ -166,67 +149,87 @@ public void GetMostRecentMemory_WhenObservationIsUpdated_ShouldReturnLastObserva
Assert.Equal(1, belief.GetMostRecentMemory());
}
- ///
- /// Given a MemoryBelief instance with an observation,
- /// When the observation is updated and GetMemoryAt is called with an index,
- /// Then the observation at the specified index is returned.
- ///
[Fact]
- public void GetMemoryAt_WhenObservationIsUpdated_ShouldReturnObservationAtSpecifiedIndex()
+ public void MemoryBelief_WhenConstructed_HasExpectedData()
{
// Arrange
- List list = [1, 2, 3];
- MemoryBelief, int> belief = new(list, reference => reference.Count, 3);
+ Metadata metadata = It.IsAny();
+ object reference = new Mock().Object;
+ System.Func getObservationFromReference = new Mock>().Object;
+ const int framesToRemember = 0;
+ System.Predicate shouldUpdate = It.IsAny>();
// Act
- list.Add(4);
- belief.UpdateBelief();
- list.Add(5);
- belief.UpdateBelief();
+ TestMemoryBelief belief = new(metadata, reference, getObservationFromReference, framesToRemember, shouldUpdate);
// Assert
- Assert.Equal(4, belief.GetMemoryAt(0));
- Assert.Equal(3, belief.GetMemoryAt(1));
+ belief.Metadata.Should().Be(metadata);
+ belief.Reference.Should().Be(reference);
+ belief.GetObservationFromReference.Should().Be(getObservationFromReference);
+ belief.MemorizedObservations.MaxCount.Should().Be(framesToRemember);
+ belief.ShouldUpdate.Should().Be(shouldUpdate);
}
- ///
- /// Given a MemoryBelief instance with an observation,
- /// When asking for an index that is out of bounds,
- /// Then an exception should be thrown.
- ///
[Fact]
- public void GetMemoryAt_IndexOutOfBounds_ShouldThrowException()
+ public void MemoryBelief_WithoutMetadata_HasExpectedData()
{
// Arrange
- List list = [1, 2, 3];
- MemoryBelief, int> belief = new(list, reference => reference.Count, 3);
+ object reference = new Mock().Object;
+ System.Func getObservationFromReference = new Mock>().Object;
+ const int framesToRemember = 1;
+ System.Predicate shouldUpdate = It.IsAny>();
// Act
- void GetMemoryAtNegativeIndex() => belief.GetMemoryAt(-1);
- void GetMemoryAtIndexGreaterThanCount() => belief.GetMemoryAt(3);
+ TestMemoryBelief belief = new(reference, getObservationFromReference, framesToRemember, shouldUpdate);
// Assert
- Assert.Throws(GetMemoryAtNegativeIndex);
- Assert.Throws(GetMemoryAtIndexGreaterThanCount);
+ belief.Metadata.Id.Should().NotBeEmpty();
+ belief.Metadata.Name.Should().BeNull();
+ belief.Metadata.Description.Should().BeNull();
+ belief.Reference.Should().Be(reference);
+ belief.GetObservationFromReference.Should().Be(getObservationFromReference);
+ belief.MemorizedObservations.MaxCount.Should().Be(framesToRemember);
+ belief.ShouldUpdate.Should().Be(shouldUpdate);
}
- ///
- /// Given a MemoryBelief instance with an observation,
- /// When the observation is updated and GetAllMemories is called,
- /// Then all the currently saved observations are returned.
- ///
[Fact]
- public void GetAllMemories_ReturnsAllMemories()
+ public void MemoryBelief_WithoutMetadataWithoutShouldUpdate_HasExpectedData()
{
// Arrange
- List list = [1, 2, 3];
- MemoryBelief, int> belief = new(list, reference => reference.Count, 3);
+ object reference = new Mock().Object;
+ System.Func getObservationFromReference = new Mock>().Object;
+ const int framesToRemember = 3;
// Act
- list.Add(4);
- belief.UpdateBelief();
+ TestMemoryBelief belief = new(reference, getObservationFromReference, framesToRemember);
// Assert
- Assert.Equal([3], belief.GetAllMemories());
+ belief.Metadata.Id.Should().NotBeEmpty();
+ belief.Metadata.Name.Should().BeNull();
+ belief.Metadata.Description.Should().BeNull();
+ belief.Reference.Should().Be(reference);
+ belief.GetObservationFromReference.Should().Be(getObservationFromReference);
+ belief.MemorizedObservations.MaxCount.Should().Be(framesToRemember);
+ belief.ShouldUpdate(reference).Should().BeTrue();
+ }
+
+ [Fact]
+ public void MemoryBelief_WithoutShouldUpdate_HasExpectedData()
+ {
+ // Arrange
+ Metadata metadata = It.IsAny();
+ object reference = new Mock().Object;
+ System.Func getObservationFromReference = new Mock>().Object;
+ const int framesToRemember = 2;
+
+ // Act
+ TestMemoryBelief belief = new(metadata, reference, getObservationFromReference, framesToRemember);
+
+ // Assert
+ belief.Metadata.Should().Be(metadata);
+ belief.Reference.Should().Be(reference);
+ belief.GetObservationFromReference.Should().Be(getObservationFromReference);
+ belief.MemorizedObservations.MaxCount.Should().Be(framesToRemember);
+ belief.ShouldUpdate(reference).Should().BeTrue();
}
}
diff --git a/Aplib.Core.Tests/Belief/SampledMemoryBeliefTests.cs b/Aplib.Core.Tests/Belief/SampledMemoryBeliefTests.cs
index 6f0cf03c..679c843f 100644
--- a/Aplib.Core.Tests/Belief/SampledMemoryBeliefTests.cs
+++ b/Aplib.Core.Tests/Belief/SampledMemoryBeliefTests.cs
@@ -1,3 +1,7 @@
+// This program has been developed by students from the bachelor Computer Science at Utrecht
+// University within the Software Project course.
+// Copyright Utrecht University (Department of Information and Computing Sciences)
+
using Aplib.Core.Belief.Beliefs;
using System.Collections.Generic;
using static Aplib.Core.Belief.Beliefs.UpdateMode;
@@ -86,28 +90,28 @@ SampledMemoryBelief, int> belief
}
///
- /// Given a SampledMemoryBelief instance with update mode 'AlwaysUpdate',
- /// When the belief is not in a sampleInterval-th cycle,
- /// Then Observation should be up-to-date.
+ /// Given a SampledMemoryBelief instance with a shouldUpdate method,
+ /// When shouldUpdate returns false,
+ /// Then Observation should not be updated regardless of the update mode.
///
[Fact]
- public void Observation_WhenUpdateModeIsAlwaysUpdate_ShouldBeUpToDate()
+ public void Observation_WhenShouldUpdateReturnsFalse_ShouldNotBeUpdated()
{
// Arrange
List list = [];
int sampleInterval = 2,
framesToRemember = 3;
SampledMemoryBelief, int> belief
- = new(list, reference => reference.Count, sampleInterval, AlwaysUpdate, framesToRemember);
+ = new(list, reference => reference.Count, sampleInterval, AlwaysUpdate, framesToRemember, _ => false);
// Act
// Expected values:
// -----------------------------------------
// | list.Count | Observation
// -----------------------------------------
- // Initial | 0 | 0 (observation is updated)
- // Iteration 0 | 1 | 1 (observation is updated)
- // Iteration 1 | 2 | 2 (observation is updated)
+ // Initial | 0 | 0 (set initial observation)
+ // Iteration 0 | 1 | 0
+ // Iteration 1 | 2 | 0
// -----------------------------------------
for (int i = 0; i < 2; i++)
{
@@ -116,43 +120,41 @@ SampledMemoryBelief, int> belief
}
// Assert
- Assert.Equal(list.Count, belief.Observation);
+ Assert.Equal(0, belief.Observation);
}
///
- /// Given a SampledMemoryBelief instance with update mode 'UpdateWhenSampled',
+ /// Given a SampledMemoryBelief instance with update mode 'AlwaysUpdate',
/// When the belief is not in a sampleInterval-th cycle,
- /// Then Observation should be outdated.
+ /// Then Observation should be up-to-date.
///
[Fact]
- public void Observation_WhenUpdateModeIsUpdateWhenSampled_ShouldBeOutdated()
+ public void Observation_WhenUpdateModeIsAlwaysUpdate_ShouldBeUpToDate()
{
// Arrange
List list = [];
int sampleInterval = 2,
framesToRemember = 3;
SampledMemoryBelief, int> belief
- = new(list, reference => reference.Count, sampleInterval, UpdateWhenSampled, framesToRemember);
+ = new(list, reference => reference.Count, sampleInterval, AlwaysUpdate, framesToRemember);
// Act
// Expected values:
// -----------------------------------------
// | list.Count | Observation
// -----------------------------------------
- // Initial | 0 | 0
+ // Initial | 0 | 0 (observation is updated)
// Iteration 0 | 1 | 1 (observation is updated)
- // Iteration 1 | 2 | 1
- // Iteration 2 | 3 | 3 (observation is updated)
- // Iteration 3 | 4 | 3
+ // Iteration 1 | 2 | 2 (observation is updated)
// -----------------------------------------
- for (int i = 0; i < 4; i++)
+ for (int i = 0; i < 2; i++)
{
list.Add(0);
belief.UpdateBelief();
}
// Assert
- Assert.NotEqual(list.Count, belief.Observation);
+ Assert.Equal(list.Count, belief.Observation);
}
///
@@ -200,36 +202,38 @@ SampledMemoryBelief, int> belief
}
///
- /// Given a SampledMemoryBelief instance with a shouldUpdate method,
- /// When shouldUpdate returns false,
- /// Then Observation should not be updated regardless of the update mode.
+ /// Given a SampledMemoryBelief instance with update mode 'UpdateWhenSampled',
+ /// When the belief is not in a sampleInterval-th cycle,
+ /// Then Observation should be outdated.
///
[Fact]
- public void Observation_WhenShouldUpdateReturnsFalse_ShouldNotBeUpdated()
+ public void Observation_WhenUpdateModeIsUpdateWhenSampled_ShouldBeOutdated()
{
// Arrange
List list = [];
int sampleInterval = 2,
framesToRemember = 3;
SampledMemoryBelief, int> belief
- = new(list, reference => reference.Count, sampleInterval, AlwaysUpdate, framesToRemember, _ => false);
+ = new(list, reference => reference.Count, sampleInterval, UpdateWhenSampled, framesToRemember);
// Act
// Expected values:
// -----------------------------------------
// | list.Count | Observation
// -----------------------------------------
- // Initial | 0 | 0 (set initial observation)
- // Iteration 0 | 1 | 0
- // Iteration 1 | 2 | 0
+ // Initial | 0 | 0
+ // Iteration 0 | 1 | 1 (observation is updated)
+ // Iteration 1 | 2 | 1
+ // Iteration 2 | 3 | 3 (observation is updated)
+ // Iteration 3 | 4 | 3
// -----------------------------------------
- for (int i = 0; i < 2; i++)
+ for (int i = 0; i < 4; i++)
{
list.Add(0);
belief.UpdateBelief();
}
// Assert
- Assert.Equal(0, belief.Observation);
+ Assert.NotEqual(list.Count, belief.Observation);
}
}
diff --git a/Aplib.Core.Tests/Collections/CircularArrayTests.cs b/Aplib.Core.Tests/Collections/CircularArrayTests.cs
index 34cda7ed..30b5bf03 100644
--- a/Aplib.Core.Tests/Collections/CircularArrayTests.cs
+++ b/Aplib.Core.Tests/Collections/CircularArrayTests.cs
@@ -1,3 +1,7 @@
+// This program has been developed by students from the bachelor Computer Science at Utrecht
+// University within the Software Project course.
+// Copyright Utrecht University (Department of Information and Computing Sciences)
+
using Aplib.Core.Collections;
namespace Aplib.Core.Tests.Collections;
@@ -6,93 +10,93 @@ public class CircularArrayTests
{
///
/// Given a CircularArray instance,
- /// When an element is put into the array,
- /// The array should wrap around when it reaches its end.
- /// (i.e., the first element should become the previous second element,
- /// and the last element should be the new element)
+ /// When the head is updated,
+ /// GetLast should return the last element.
///
[Fact]
- public void Put_ArrayIsFull_WrapsAround()
+ public void GetFirst_HeadIsUpdated_ReturnsFirstElement()
{
// Arrange
- CircularArray circularArray = new([1, 2, 3, 4, 5]);
+ CircularArray circularArray = new([1, 2, 3]);
+ int prevFirst = circularArray.GetFirst();
// Act
- circularArray.Put(0);
+ circularArray.Put(4);
+ int firstElement = circularArray.GetFirst();
// Assert
- Assert.Equal(0, circularArray[0]);
- Assert.Equal(1, circularArray[1]);
- Assert.Equal(2, circularArray[2]);
+ Assert.NotEqual(prevFirst, firstElement);
+ Assert.Equal(4, firstElement);
}
///
/// Given a CircularArray instance,
/// When the head is updated,
- /// Putting an element should set the correct index
- /// even if the head is not at the start of the array.
+ /// GetHead should return the correct head.
///
[Fact]
- public void Put_HeadIsUpdated_SetsCorrectIndex()
+ public void GetHead_HeadIsUpdated_ReturnsLastElement()
{
// Arrange
- CircularArray circularArray = new(3);
+ CircularArray circularArray = new([1, 2, 3]);
+ int prevHead = circularArray.GetHead();
// Act
- // circularArray.ToArray() == [0, 0, 0]
- circularArray[1] = 6;
- // circularArray.ToArray() == [0, 6, 0]
- circularArray.Put(4);
- // circularArray.ToArray() == [4, 0, 6]
- circularArray[1] = 5;
- // circularArray.ToArray() == [4, 5, 6]
+ circularArray.Put(0);
+ int head = circularArray.GetHead();
// Assert
- Assert.Equal(4, circularArray[0]);
- Assert.Equal(5, circularArray[1]);
- Assert.Equal(6, circularArray[2]);
+ Assert.NotEqual(prevHead, head);
+ Assert.Equal(2, head);
}
///
/// Given a CircularArray instance,
- /// When the head is updated,
- /// GetHead should return the correct head.
+ /// When an element is put into the array,
+ /// The array should wrap around when it reaches its end.
+ /// (i.e., the first element should become the previous second element,
+ /// and the last element should be the new element)
///
[Fact]
- public void GetHead_HeadIsUpdated_ReturnsLastElement()
+ public void Put_ArrayIsFull_WrapsAround()
{
// Arrange
- CircularArray circularArray = new([1, 2, 3]);
- int prevHead = circularArray.GetHead();
+ CircularArray circularArray = new([1, 2, 3, 4, 5]);
// Act
circularArray.Put(0);
- int head = circularArray.GetHead();
// Assert
- Assert.NotEqual(prevHead, head);
- Assert.Equal(2, head);
+ Assert.Equal(0, circularArray[0]);
+ Assert.Equal(1, circularArray[1]);
+ Assert.Equal(2, circularArray[2]);
}
///
/// Given a CircularArray instance,
/// When the head is updated,
- /// GetLast should return the last element.
+ /// Putting an element should set the correct index
+ /// even if the head is not at the start of the array.
///
[Fact]
- public void GetFirst_HeadIsUpdated_ReturnsFirstElement()
+ public void Put_HeadIsUpdated_SetsCorrectIndex()
{
// Arrange
- CircularArray circularArray = new([1, 2, 3]);
- int prevFirst = circularArray.GetFirst();
+ CircularArray circularArray = new(3);
// Act
+ // circularArray.ToArray() == [0, 0, 0]
+ circularArray[1] = 6;
+ // circularArray.ToArray() == [0, 6, 0]
circularArray.Put(4);
- int firstElement = circularArray.GetFirst();
+ // circularArray.ToArray() == [4, 0, 6]
+ circularArray[1] = 5;
+ // circularArray.ToArray() == [4, 5, 6]
// Assert
- Assert.NotEqual(prevFirst, firstElement);
- Assert.Equal(4, firstElement);
+ Assert.Equal(4, circularArray[0]);
+ Assert.Equal(5, circularArray[1]);
+ Assert.Equal(6, circularArray[2]);
}
///
diff --git a/Aplib.Core.Tests/Collections/ExposedQueueTests.cs b/Aplib.Core.Tests/Collections/ExposedQueueTests.cs
index 81319fff..bb4f2444 100644
--- a/Aplib.Core.Tests/Collections/ExposedQueueTests.cs
+++ b/Aplib.Core.Tests/Collections/ExposedQueueTests.cs
@@ -1,3 +1,7 @@
+// This program has been developed by students from the bachelor Computer Science at Utrecht
+// University within the Software Project course.
+// Copyright Utrecht University (Department of Information and Computing Sciences)
+
using Aplib.Core.Collections;
using System.Collections.Generic;
diff --git a/Aplib.Core.Tests/Collections/OptimizedActivationStackTests.cs b/Aplib.Core.Tests/Collections/OptimizedActivationStackTests.cs
index 76f0b144..6f48500f 100644
--- a/Aplib.Core.Tests/Collections/OptimizedActivationStackTests.cs
+++ b/Aplib.Core.Tests/Collections/OptimizedActivationStackTests.cs
@@ -1,4 +1,8 @@
-using Aplib.Core.Collections;
+// This program has been developed by students from the bachelor Computer Science at Utrecht
+// University within the Software Project course.
+// Copyright Utrecht University (Department of Information and Computing Sciences)
+
+using Aplib.Core.Collections;
using FluentAssertions;
using System.Collections.Generic;
using System.Linq;
@@ -28,6 +32,30 @@ public void ActivatableStackItems_WhenActivatablesAreGiven_ShouldContainEncapsul
items.Should().BeEquivalentTo(activatables);
}
+ ///
+ /// Given an optimized activation stack,
+ /// When an item from a different optimized activation stack is activated on this stack,
+ /// Then an argument exception should be thrown.
+ ///
+ [Fact]
+ public void Activate_WhenActivatedItemIsFromDifferentStack_ShouldThrowArgumentException()
+ {
+ // Arrange
+ int[] activatables = [1, 2, 3];
+ OptimizedActivationStack activationStack = new(activatables);
+
+ int[] otherActivatables = [4, 5, 6];
+ OptimizedActivationStack otherActivationStack = new(otherActivatables);
+ OptimizedActivationStack.StackItem stackItemToActivate
+ = otherActivationStack.ActivatableStackItems.First();
+
+ // Act
+ System.Action activateItem = () => activationStack.Activate(stackItemToActivate);
+
+ // Assert
+ activateItem.Should().Throw();
+ }
+
///
/// Given an optimized activation stack with items on the stack,
/// When an item is activated that was not activated yet,
@@ -87,27 +115,28 @@ OptimizedActivationStack.StackItem stackItemToActivate
}
///
- /// Given an optimized activation stack,
- /// When an item from a different optimized activation stack is activated on this stack,
- /// Then an argument exception should be thrown.
+ /// Given an optimized activation stack with activated items on the stack,
+ /// When an item is peeked,
+ /// Then the count of the optimized activation stack should stay the same.
///
[Fact]
- public void Activate_WhenActivatedItemIsFromDifferentStack_ShouldThrowArgumentException()
+ public void Peek_WhenCalled_CountShouldStayTheSame()
{
// Arrange
int[] activatables = [1, 2, 3];
OptimizedActivationStack activationStack = new(activatables);
- int[] otherActivatables = [4, 5, 6];
- OptimizedActivationStack otherActivationStack = new(otherActivatables);
- OptimizedActivationStack.StackItem stackItemToActivate
- = otherActivationStack.ActivatableStackItems.First();
+ // Activate all stack items.
+ foreach (OptimizedActivationStack.StackItem stackItem in activationStack.ActivatableStackItems)
+ activationStack.Activate(stackItem);
// Act
- System.Action activateItem = () => activationStack.Activate(stackItemToActivate);
+ int countBeforePeek = activationStack.Count;
+ _ = activationStack.Peek();
+ int countAfterPeek = activationStack.Count;
// Assert
- activateItem.Should().Throw();
+ countBeforePeek.Should().Be(countAfterPeek);
}
///
@@ -155,11 +184,11 @@ public void Peek_WhenStackIsEmpty_ShouldThrowInvalidOperationException()
///
/// Given an optimized activation stack with activated items on the stack,
- /// When an item is peeked,
- /// Then the count of the optimized activation stack should stay the same.
+ /// When an item is popped,
+ /// Then the count of the optimized activation stack should be decreased by one.
///
[Fact]
- public void Peek_WhenCalled_CountShouldStayTheSame()
+ public void Pop_WhenCalled_CountShouldDecrement()
{
// Arrange
int[] activatables = [1, 2, 3];
@@ -170,12 +199,12 @@ public void Peek_WhenCalled_CountShouldStayTheSame()
activationStack.Activate(stackItem);
// Act
- int countBeforePeek = activationStack.Count;
- _ = activationStack.Peek();
- int countAfterPeek = activationStack.Count;
+ int countBeforePop = activationStack.Count;
+ _ = activationStack.Pop();
+ int countAfterPop = activationStack.Count;
// Assert
- countBeforePeek.Should().Be(countAfterPeek);
+ countAfterPop.Should().Be(countBeforePop - 1);
}
///
@@ -220,29 +249,4 @@ public void Pop_WhenStackIsEmpty_ShouldThrowInvalidOperationException()
// Assert
popItem.Should().Throw();
}
-
- ///
- /// Given an optimized activation stack with activated items on the stack,
- /// When an item is popped,
- /// Then the count of the optimized activation stack should be decreased by one.
- ///
- [Fact]
- public void Pop_WhenCalled_CountShouldDecrement()
- {
- // Arrange
- int[] activatables = [1, 2, 3];
- OptimizedActivationStack activationStack = new(activatables);
-
- // Activate all stack items.
- foreach (OptimizedActivationStack.StackItem stackItem in activationStack.ActivatableStackItems)
- activationStack.Activate(stackItem);
-
- // Act
- int countBeforePop = activationStack.Count;
- _ = activationStack.Pop();
- int countAfterPop = activationStack.Count;
-
- // Assert
- countAfterPop.Should().Be(countBeforePop - 1);
- }
}
diff --git a/Aplib.Core.Tests/CombinatorTests.cs b/Aplib.Core.Tests/CombinatorTests.cs
index 675e635c..0686a746 100644
--- a/Aplib.Core.Tests/CombinatorTests.cs
+++ b/Aplib.Core.Tests/CombinatorTests.cs
@@ -1,3 +1,7 @@
+// This program has been developed by students from the bachelor Computer Science at Utrecht
+// University within the Software Project course.
+// Copyright Utrecht University (Department of Information and Computing Sciences)
+
using Aplib.Core.Belief.BeliefSets;
using Aplib.Core.Desire.Goals;
using Aplib.Core.Desire.GoalStructures;
@@ -19,161 +23,91 @@ [new Metadata(System.Guid.Empty, description: "my description"), null, "my descr
[new Metadata(System.Guid.Empty, "a name", "a description"), "a name", "a description"]
];
- private static void CheckMetadata(string? expectedName, string? expectedDescription, IMetadata actual)
- {
- actual.Id.Should().BeEmpty();
- actual.Name.Should().Be(expectedName);
- actual.Description.Should().Be(expectedDescription);
- }
-
- private static void CheckDefaultMetadata(IMetadata actual)
- {
- actual.Id.Should().NotBeEmpty();
- actual.Name.Should().BeNull();
- actual.Description.Should().BeNull();
- }
-
- #region GoalStructure combinator tests
-
[Theory]
[MemberData(nameof(Metadatas))]
- public void FirstOfGoalStructureCombinator_WhenCalled_GivesExpectedGoalStructure
+ public void AnyOfTacticCombinator_WhenCalled_GivesExpectedTactic
(Metadata metadata, string? expectedName, string? expectedDescription)
{
// Arrange
- Mock> goalStructure1 = new();
- goalStructure1.SetupGet(g => g.Status).Returns(CompletionStatus.Success);
-
- Mock> goalStructure2 = new();
- goalStructure2.SetupGet(g => g.Status).Returns(CompletionStatus.Success);
-
- IBeliefSet beliefSet = Mock.Of();
-
- // Act
- FirstOfGoalStructure firstOfGoalStructure =
- FirstOf(metadata, goalStructure1.Object, goalStructure2.Object);
-
- firstOfGoalStructure.UpdateStatus(beliefSet);
- firstOfGoalStructure.UpdateStatus(beliefSet);
-
- // Assert
- CheckMetadata(expectedName, expectedDescription, firstOfGoalStructure.Metadata);
- firstOfGoalStructure.Status.Should().Be(CompletionStatus.Success);
- goalStructure1.Verify(x => x.UpdateStatus(It.IsAny()), Times.Once);
- goalStructure2.Verify(x => x.UpdateStatus(It.IsAny()), Times.Never);
- }
-
- [Fact]
- public void FirstOfGoalStructureCombinator_WithoutMetadata_GivesExpectedGoalStructure()
- {
- // Arrange
- Mock> goalStructure1 = new();
- goalStructure1.SetupGet(g => g.Status).Returns(CompletionStatus.Success);
-
- Mock> goalStructure2 = new();
- goalStructure2.SetupGet(g => g.Status).Returns(CompletionStatus.Success);
-
- IBeliefSet beliefSet = Mock.Of();
+ Action action1 = new(_ => { });
+ Action action2 = new(_ => { });
+ // ReSharper disable once ConvertToLocalFunction
+ System.Predicate guard = _ => false;
// Act
- FirstOfGoalStructure firstOfGoalStructure =
- FirstOf(goalStructure1.Object, goalStructure2.Object);
+ RandomTactic anyOfTactic =
+ Random(metadata, guard, Primitive(action1, _ => true), Primitive(action2, _ => false));
- firstOfGoalStructure.UpdateStatus(beliefSet);
- firstOfGoalStructure.UpdateStatus(beliefSet);
+ IAction? selectedAction = anyOfTactic.GetAction(It.IsAny());
// Assert
- CheckDefaultMetadata(firstOfGoalStructure.Metadata);
- firstOfGoalStructure.Status.Should().Be(CompletionStatus.Success);
- goalStructure1.Verify(x => x.UpdateStatus(It.IsAny()), Times.Once);
- goalStructure2.Verify(x => x.UpdateStatus(It.IsAny()), Times.Never);
+ CheckMetadata(expectedName, expectedDescription, anyOfTactic.Metadata);
+ selectedAction.Should().BeNull();
}
[Theory]
[MemberData(nameof(Metadatas))]
- public void PrimitiveGoalStructureCombinator_WhenCalled_GivesExpectedGoalStructure
+ public void AnyOfTacticCombinator_WithoutGuard_GivesExpectedTactic
(Metadata metadata, string? expectedName, string? expectedDescription)
{
// Arrange
- Mock> goal = new();
- goal.Setup(g => g.Status).Returns(CompletionStatus.Success);
- IBeliefSet beliefSet = Mock.Of();
+ Action action1 = new(_ => { });
+ Action action2 = new(_ => { });
// Act
- PrimitiveGoalStructure primitiveGoalStructure = Primitive(metadata, goal.Object);
+ RandomTactic anyOfTactic =
+ Random(metadata, Primitive(action1, _ => true), Primitive(action2, _ => false));
- primitiveGoalStructure.UpdateStatus(beliefSet);
+ IAction? selectedAction = anyOfTactic.GetAction(It.IsAny());
// Assert
- CheckMetadata(expectedName, expectedDescription, primitiveGoalStructure.Metadata);
- primitiveGoalStructure.Status.Should().Be(CompletionStatus.Success);
+ CheckMetadata(expectedName, expectedDescription, anyOfTactic.Metadata);
+ selectedAction.Should().Be(action1);
}
[Fact]
- public void PrimitiveGoalStructureCombinator_WithoutMetadata_GivesExpectedGoalStructure()
- {
- // Arrange
- Mock> goal = new();
- goal.Setup(g => g.Status).Returns(CompletionStatus.Success);
- IBeliefSet beliefSet = Mock.Of();
-
- // Act
- PrimitiveGoalStructure primitiveGoalStructure = Primitive(goal.Object);
-
- primitiveGoalStructure.UpdateStatus(beliefSet);
-
- // Assert
- CheckDefaultMetadata(primitiveGoalStructure.Metadata);
- primitiveGoalStructure.Status.Should().Be(CompletionStatus.Success);
- }
-
- [Theory]
- [MemberData(nameof(Metadatas))]
- public void RepeatGoalStructureCombinator_WhenCalled_GivesExpectedGoalStructure
- (Metadata metadata, string? expectedName, string? expectedDescription)
+ public void AnyOfTacticCombinator_WithoutMetadata_GivesExpectedTactic()
{
// Arrange
- Mock> goal = new();
- goal.Setup(g => g.Status).Returns(CompletionStatus.Failure);
- IBeliefSet beliefSet = Mock.Of();
+ Action action1 = new(_ => { });
+ Action action2 = new(_ => { });
+ // ReSharper disable once ConvertToLocalFunction
+ System.Predicate guard = _ => false;
// Act
- RepeatGoalStructure repeatGoalStructure = Repeat(metadata, Primitive(goal.Object));
+ RandomTactic anyOfTactic =
+ Random(guard, Primitive(action1, _ => true), Primitive(action2, _ => false));
- repeatGoalStructure.UpdateStatus(beliefSet);
- IGoal currentGoal = repeatGoalStructure.GetCurrentGoal(beliefSet);
+ IAction? selectedAction = anyOfTactic.GetAction(It.IsAny());
// Assert
- CheckMetadata(expectedName, expectedDescription, repeatGoalStructure.Metadata);
- repeatGoalStructure.Status.Should().Be(CompletionStatus.Unfinished);
- currentGoal.Should().Be(goal.Object);
+ CheckDefaultMetadata(anyOfTactic.Metadata);
+ selectedAction.Should().BeNull();
}
[Fact]
- public void RepeatGoalStructureCombinator_WithoutMetadata_GivesExpectedGoalStructure()
+ public void AnyOfTacticCombinator_WithoutMetadataWithoutGuard_GivesExpectedTactic()
{
// Arrange
- Mock> goal = new();
- goal.Setup(g => g.Status).Returns(CompletionStatus.Failure);
- IBeliefSet beliefSet = Mock.Of();
+ Action action1 = new(_ => { });
+ Action action2 = new(_ => { });
// Act
- RepeatGoalStructure repeatGoalStructure = Repeat(Primitive(goal.Object));
+ RandomTactic anyOfTactic = Random(Primitive(action1, _ => true), Primitive(action2, _ => false));
- repeatGoalStructure.UpdateStatus(beliefSet);
- IGoal currentGoal = repeatGoalStructure.GetCurrentGoal(beliefSet);
+ IAction? selectedAction = anyOfTactic.GetAction(It.IsAny());
// Assert
- CheckDefaultMetadata(repeatGoalStructure.Metadata);
- repeatGoalStructure.Status.Should().Be(CompletionStatus.Unfinished);
- currentGoal.Should().Be(goal.Object);
+ CheckDefaultMetadata(anyOfTactic.Metadata);
+ selectedAction.Should().Be(action1);
}
[Theory]
[MemberData(nameof(Metadatas))]
- public void SequentialGoalStructureCombinator_WhenCalled_GivesExpectedGoalStructure
+ public void FirstOfGoalStructureCombinator_WhenCalled_GivesExpectedGoalStructure
(Metadata metadata, string? expectedName, string? expectedDescription)
{
+ // Arrange
Mock> goalStructure1 = new();
goalStructure1.SetupGet(g => g.Status).Returns(CompletionStatus.Success);
@@ -183,21 +117,23 @@ public void SequentialGoalStructureCombinator_WhenCalled_GivesExpectedGoalStruct
IBeliefSet beliefSet = Mock.Of();
// Act
- SequentialGoalStructure sequentialGoalStructure =
- Seq(metadata, goalStructure1.Object, goalStructure2.Object);
+ FirstOfGoalStructure firstOfGoalStructure =
+ FirstOf(metadata, goalStructure1.Object, goalStructure2.Object);
- sequentialGoalStructure.UpdateStatus(beliefSet);
- sequentialGoalStructure.UpdateStatus(beliefSet);
+ firstOfGoalStructure.UpdateStatus(beliefSet);
+ firstOfGoalStructure.UpdateStatus(beliefSet);
// Assert
- CheckMetadata(expectedName, expectedDescription, sequentialGoalStructure.Metadata);
- sequentialGoalStructure.Status.Should().Be(CompletionStatus.Success);
+ CheckMetadata(expectedName, expectedDescription, firstOfGoalStructure.Metadata);
+ firstOfGoalStructure.Status.Should().Be(CompletionStatus.Success);
goalStructure1.Verify(x => x.UpdateStatus(It.IsAny()), Times.Once);
+ goalStructure2.Verify(x => x.UpdateStatus(It.IsAny()), Times.Never);
}
[Fact]
- public void SequentialGoalStructureCombinator_WithoutMetadata_GivesExpectedGoalStructure()
+ public void FirstOfGoalStructureCombinator_WithoutMetadata_GivesExpectedGoalStructure()
{
+ // Arrange
Mock> goalStructure1 = new();
goalStructure1.SetupGet(g => g.Status).Returns(CompletionStatus.Success);
@@ -207,24 +143,22 @@ public void SequentialGoalStructureCombinator_WithoutMetadata_GivesExpectedGoalS
IBeliefSet beliefSet = Mock.Of();
// Act
- SequentialGoalStructure sequentialGoalStructure = Seq(goalStructure1.Object, goalStructure2.Object);
+ FirstOfGoalStructure firstOfGoalStructure =
+ FirstOf(goalStructure1.Object, goalStructure2.Object);
- sequentialGoalStructure.UpdateStatus(beliefSet);
- sequentialGoalStructure.UpdateStatus(beliefSet);
+ firstOfGoalStructure.UpdateStatus(beliefSet);
+ firstOfGoalStructure.UpdateStatus(beliefSet);
// Assert
- CheckDefaultMetadata(sequentialGoalStructure.Metadata);
- sequentialGoalStructure.Status.Should().Be(CompletionStatus.Success);
+ CheckDefaultMetadata(firstOfGoalStructure.Metadata);
+ firstOfGoalStructure.Status.Should().Be(CompletionStatus.Success);
goalStructure1.Verify(x => x.UpdateStatus(It.IsAny()), Times.Once);
+ goalStructure2.Verify(x => x.UpdateStatus(It.IsAny()), Times.Never);
}
- #endregion
-
- #region Tactic combinator tests
-
[Theory]
[MemberData(nameof(Metadatas))]
- public void AnyOfTacticCombinator_WhenCalled_GivesExpectedTactic
+ public void FirstOfTacticCombinator_WhenCalled_GivesExpectedTactic
(Metadata metadata, string? expectedName, string? expectedDescription)
{
// Arrange
@@ -234,177 +168,204 @@ public void AnyOfTacticCombinator_WhenCalled_GivesExpectedTactic
System.Predicate guard = _ => false;
// Act
- RandomTactic anyOfTactic =
- Random(metadata, guard, Primitive(action1, _ => true), Primitive(action2, _ => false));
+ FirstOfTactic firstOfTactic =
+ FirstOf(metadata, guard, Primitive(action1, _ => false), Primitive(action2, _ => true));
- IAction? selectedAction = anyOfTactic.GetAction(It.IsAny());
+ IAction? selectedAction = firstOfTactic.GetAction(It.IsAny());
// Assert
- CheckMetadata(expectedName, expectedDescription, anyOfTactic.Metadata);
+ CheckMetadata(expectedName, expectedDescription, firstOfTactic.Metadata);
selectedAction.Should().BeNull();
}
- [Fact]
- public void AnyOfTacticCombinator_WithoutMetadata_GivesExpectedTactic()
+ [Theory]
+ [MemberData(nameof(Metadatas))]
+ public void FirstOfTacticCombinator_WithoutGuard_GivesExpectedTactic
+ (Metadata metadata, string? expectedName, string? expectedDescription)
{
// Arrange
Action action1 = new(_ => { });
Action action2 = new(_ => { });
- // ReSharper disable once ConvertToLocalFunction
- System.Predicate guard = _ => false;
// Act
- RandomTactic anyOfTactic =
- Random(guard, Primitive(action1, _ => true), Primitive(action2, _ => false));
+ FirstOfTactic firstOfTactic =
+ FirstOf(metadata, Primitive(action1, _ => false), Primitive(action2, _ => true));
- IAction? selectedAction = anyOfTactic.GetAction(It.IsAny());
+ IAction? selectedAction = firstOfTactic.GetAction(It.IsAny());
// Assert
- CheckDefaultMetadata(anyOfTactic.Metadata);
- selectedAction.Should().BeNull();
+ CheckMetadata(expectedName, expectedDescription, firstOfTactic.Metadata);
+ selectedAction.Should().Be(action2);
}
- [Theory]
- [MemberData(nameof(Metadatas))]
- public void AnyOfTacticCombinator_WithoutGuard_GivesExpectedTactic
- (Metadata metadata, string? expectedName, string? expectedDescription)
+ [Fact]
+ public void FirstOfTacticCombinator_WithoutMetadata_GivesExpectedTactic()
{
// Arrange
Action action1 = new(_ => { });
Action action2 = new(_ => { });
+ // ReSharper disable once ConvertToLocalFunction
+ System.Predicate guard = _ => false;
// Act
- RandomTactic anyOfTactic =
- Random(metadata, Primitive(action1, _ => true), Primitive(action2, _ => false));
+ FirstOfTactic firstOfTactic =
+ FirstOf(guard, Primitive(action1, _ => false), Primitive(action2, _ => true));
- IAction? selectedAction = anyOfTactic.GetAction(It.IsAny());
+ IAction? selectedAction = firstOfTactic.GetAction(It.IsAny());
// Assert
- CheckMetadata(expectedName, expectedDescription, anyOfTactic.Metadata);
- selectedAction.Should().Be(action1);
+ CheckDefaultMetadata(firstOfTactic.Metadata);
+ selectedAction.Should().BeNull();
}
[Fact]
- public void AnyOfTacticCombinator_WithoutMetadataWithoutGuard_GivesExpectedTactic()
+ public void FirstOfTacticCombinator_WithoutMetadataWithoutGuard_GivesExpectedTactic()
{
// Arrange
Action action1 = new(_ => { });
Action action2 = new(_ => { });
// Act
- RandomTactic anyOfTactic = Random(Primitive(action1, _ => true), Primitive(action2, _ => false));
+ FirstOfTactic firstOfTactic =
+ FirstOf(Primitive(action1, _ => false), Primitive(action2, _ => true));
- IAction? selectedAction = anyOfTactic.GetAction(It.IsAny());
+ IAction? selectedAction = firstOfTactic.GetAction(It.IsAny());
// Assert
- CheckDefaultMetadata(anyOfTactic.Metadata);
- selectedAction.Should().Be(action1);
+ CheckDefaultMetadata(firstOfTactic.Metadata);
+ selectedAction.Should().Be(action2);
}
[Theory]
[MemberData(nameof(Metadatas))]
- public void FirstOfTacticCombinator_WhenCalled_GivesExpectedTactic
+ public void PrimitiveGoalStructureCombinator_WhenCalled_GivesExpectedGoalStructure
(Metadata metadata, string? expectedName, string? expectedDescription)
{
// Arrange
- Action action1 = new(_ => { });
- Action action2 = new(_ => { });
- // ReSharper disable once ConvertToLocalFunction
- System.Predicate guard = _ => false;
+ Mock> goal = new();
+ goal.Setup(g => g.Status).Returns(CompletionStatus.Success);
+ IBeliefSet beliefSet = Mock.Of();
// Act
- FirstOfTactic firstOfTactic =
- FirstOf(metadata, guard, Primitive(action1, _ => false), Primitive(action2, _ => true));
+ PrimitiveGoalStructure primitiveGoalStructure = Primitive(metadata, goal.Object);
- IAction? selectedAction = firstOfTactic.GetAction(It.IsAny());
+ primitiveGoalStructure.UpdateStatus(beliefSet);
// Assert
- CheckMetadata(expectedName, expectedDescription, firstOfTactic.Metadata);
- selectedAction.Should().BeNull();
+ CheckMetadata(expectedName, expectedDescription, primitiveGoalStructure.Metadata);
+ primitiveGoalStructure.Status.Should().Be(CompletionStatus.Success);
}
[Fact]
- public void FirstOfTacticCombinator_WithoutMetadata_GivesExpectedTactic()
+ public void PrimitiveGoalStructureCombinator_WithoutMetadata_GivesExpectedGoalStructure()
{
// Arrange
- Action action1 = new(_ => { });
- Action action2 = new(_ => { });
- // ReSharper disable once ConvertToLocalFunction
- System.Predicate guard = _ => false;
+ Mock> goal = new();
+ goal.Setup(g => g.Status).Returns(CompletionStatus.Success);
+ IBeliefSet beliefSet = Mock.Of();
// Act
- FirstOfTactic firstOfTactic =
- FirstOf(guard, Primitive(action1, _ => false), Primitive(action2, _ => true));
+ PrimitiveGoalStructure primitiveGoalStructure = Primitive(goal.Object);
- IAction? selectedAction = firstOfTactic.GetAction(It.IsAny());
+ primitiveGoalStructure.UpdateStatus(beliefSet);
// Assert
- CheckDefaultMetadata(firstOfTactic.Metadata);
- selectedAction.Should().BeNull();
+ CheckDefaultMetadata(primitiveGoalStructure.Metadata);
+ primitiveGoalStructure.Status.Should().Be(CompletionStatus.Success);
}
[Theory]
[MemberData(nameof(Metadatas))]
- public void FirstOfTacticCombinator_WithoutGuard_GivesExpectedTactic
+ public void PrimitiveTacticCombinator_FromQueryable_GivesExpectedTactic
(Metadata metadata, string? expectedName, string? expectedDescription)
{
// Arrange
- Action action1 = new(_ => { });
- Action action2 = new(_ => { });
+ Mock> query = new();
+ query.SetupSequence(f => f(It.IsAny())).Returns(1).Returns(2);
+ QueryAction queryAction = new((_, _) => { }, query.Object);
+ Mock> guard = new();
+ guard.SetupSequence(f => f(It.IsAny())).Returns(false).Returns(true);
// Act
- FirstOfTactic firstOfTactic =
- FirstOf(metadata, Primitive(action1, _ => false), Primitive(action2, _ => true));
+ PrimitiveTactic primitiveTactic = Primitive(metadata, queryAction, guard.Object);
- IAction? selectedAction = firstOfTactic.GetAction(It.IsAny());
+ IAction? actionFromFirstCall = primitiveTactic.GetAction(It.IsAny());
+ IAction? actionFromSecondCall = primitiveTactic.GetAction(It.IsAny());
// Assert
- CheckMetadata(expectedName, expectedDescription, firstOfTactic.Metadata);
- selectedAction.Should().Be(action2);
+ CheckMetadata(expectedName, expectedDescription, primitiveTactic.Metadata);
+ actionFromFirstCall.Should().BeNull();
+ actionFromSecondCall.Should().Be(queryAction);
}
- [Fact]
- public void FirstOfTacticCombinator_WithoutMetadataWithoutGuard_GivesExpectedTactic()
+ [Theory]
+ [MemberData(nameof(Metadatas))]
+ public void PrimitiveTacticCombinator_FromQueryableWithoutGuard_GivesExpectedTactic
+ (Metadata metadata, string? expectedName, string? expectedDescription)
{
// Arrange
- Action action1 = new(_ => { });
- Action action2 = new(_ => { });
+ Mock