diff --git a/CHANGELOG.md b/CHANGELOG.md
index f23f8f6..4a3c484 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -16,11 +16,11 @@
# v0.23.0
## What's Added
-* `IStackable` interface to represent stackable items with the properties
+* `IStackable` - interface to represent stackable items with the properties
* `Amount` - The current amount of the item in the stack.
* `MaxAmount` - The maximum amount of the item that can be in a single stack.
* `AvailableAmount` - The amount of items that can be added to the stack until it reaches its max amount.
-* `IContentState` interface to represent the state of the content in a slot with the properties
+* `IContentState` - interface to represent the state of the content in a slot with the properties
* `IsEmpty` - Indicates whether the slot is empty (i.e., contains no items).
* `IsFull` - Indicates whether the slot is full (i.e., contains the maximum number of items).
* `IContainer` - Non-generic Interface to represent a container that holds slots with the properties
diff --git a/src/Containers/Interfaces/ILazyStackContainer.cs b/src/Containers/Interfaces/ILazyStackContainer.cs
index ca15048..8cba888 100644
--- a/src/Containers/Interfaces/ILazyStackContainer.cs
+++ b/src/Containers/Interfaces/ILazyStackContainer.cs
@@ -1,4 +1,5 @@
-using TheChest.Core.Abstractions;
+using System.Collections.Generic;
+using TheChest.Core.Abstractions;
namespace TheChest.Core.Containers.Interfaces
{
@@ -6,5 +7,5 @@ namespace TheChest.Core.Containers.Interfaces
/// Defines a generic stack-like container that supports lazy evaluation and items of type .
///
/// The type of items that the container can hold.
- public interface ILazyStackContainer : IContainer { }
+ public interface ILazyStackContainer : IContainer, IEnumerable> { }
}
diff --git a/src/Containers/LazyStackContainer.cs b/src/Containers/LazyStackContainer.cs
index 50a06d8..9591a5e 100644
--- a/src/Containers/LazyStackContainer.cs
+++ b/src/Containers/LazyStackContainer.cs
@@ -1,4 +1,6 @@
using System;
+using System.Collections;
+using System.Collections.Generic;
using TheChest.Core.Containers.Interfaces;
using TheChest.Core.Extensions;
using TheChest.Core.Slots;
@@ -13,10 +15,14 @@ namespace TheChest.Core.Containers
/// The type of elements stored in the container.
public class LazyStackContainer : ILazyStackContainer
{
+ #region Fields
///
/// The collection of slots used to store elements of type .
///
protected ILazyStackSlot[] slots;
+ #endregion
+
+ #region Properties
///
public virtual int Size => this.slots.Length;
///
@@ -47,6 +53,9 @@ public virtual bool IsEmpty
return true;
}
}
+ #endregion
+
+ #region Constructors
///
/// Initializes a new instance of the class with default size of 20 and max stack size of 1.
///
@@ -103,6 +112,25 @@ public LazyStackContainer(ILazyStackSlot[] slots)
{
this.slots = slots ?? throw new ArgumentNullException(nameof(slots));
}
+ #endregion
+
+ #region IEnumerable Implementation
+ ///
+ public IEnumerator> GetEnumerator()
+ {
+ foreach (var slot in this.slots)
+ {
+ foreach (var items in slot)
+ {
+ yield return items;
+ }
+ }
+ }
+ IEnumerator IEnumerable.GetEnumerator()
+ {
+ return GetEnumerator();
+ }
+ #endregion
}
}
diff --git a/src/Slots/Interfaces/ILazyStackSlot.cs b/src/Slots/Interfaces/ILazyStackSlot.cs
index 6dd0742..66c16a5 100644
--- a/src/Slots/Interfaces/ILazyStackSlot.cs
+++ b/src/Slots/Interfaces/ILazyStackSlot.cs
@@ -1,13 +1,11 @@
-using TheChest.Core.Abstractions;
+using System.Collections.Generic;
+using TheChest.Core.Abstractions;
namespace TheChest.Core.Slots.Interfaces
{
///
/// Represents a slot that can lazily hold a stack of items of a specified type.
///
- ///
- /// extends
- ///
/// The type of item the slot can hold
- public interface ILazyStackSlot : IContentState, IStackable { }
+ public interface ILazyStackSlot : IContentState, IStackable, IEnumerable> { }
}
diff --git a/src/Slots/LazyStackSlot.cs b/src/Slots/LazyStackSlot.cs
index a87c0c4..461bfdd 100644
--- a/src/Slots/LazyStackSlot.cs
+++ b/src/Slots/LazyStackSlot.cs
@@ -1,4 +1,6 @@
using System;
+using System.Collections;
+using System.Collections.Generic;
using TheChest.Core.Slots.Interfaces;
using TheChest.Core.Validators;
@@ -14,13 +16,13 @@ namespace TheChest.Core.Slots
/// The type of the content stored in the slot.
public class LazyStackSlot : ILazyStackSlot
{
- ///
- /// The content inside the slot
- ///
+ #region Fields
private object content;
private int amount;
private int maxAmount;
+ #endregion
+ #region Properties
///
/// The content inside the slot
///
@@ -72,7 +74,9 @@ protected set
public virtual bool IsFull => !this.IsEmpty && this.amount == this.maxAmount;
///
public virtual bool IsEmpty => this.amount == 0 || this.content is null;
+ #endregion
+ #region Constructors
///
/// Initializes a new instance of the class with default values.
///
@@ -112,6 +116,23 @@ public LazyStackSlot(T currentItem, int amount, int maxAmount)
this.amount = this.content is null ? 0 : amount;
this.maxAmount = maxAmount;
}
+ #endregion
+
+ #region IEnumerable Implementation
+
+ ///
+ public IEnumerator> GetEnumerator()
+ {
+ for (int i = 0; i < this.amount; i++)
+ {
+ yield return new T[1] { this.Content };
+ }
+ }
+ IEnumerator IEnumerable.GetEnumerator()
+ {
+ return GetEnumerator();
+ }
+ #endregion
}
}
diff --git a/src/TheChest.Core.csproj b/src/TheChest.Core.csproj
index 0ce83ce..b2c481c 100644
--- a/src/TheChest.Core.csproj
+++ b/src/TheChest.Core.csproj
@@ -92,6 +92,7 @@
+
diff --git a/tests/Containers/LazyStackContainer/GetEnumeratorTests.cs b/tests/Containers/LazyStackContainer/GetEnumeratorTests.cs
new file mode 100644
index 0000000..2e58973
--- /dev/null
+++ b/tests/Containers/LazyStackContainer/GetEnumeratorTests.cs
@@ -0,0 +1,55 @@
+using System.Collections;
+using System.Collections.Generic;
+using System.Linq;
+using NUnit.Framework;
+
+namespace TheChest.Core.Tests.Containers.LazyStackContainer
+{
+ public class GetEnumeratorTests : LazyStackContainerTests
+ {
+ [Test]
+ [Description("GetEnumerator returns all empty slots for an empty lazy stack container.")]
+ public void GetEnumerator_EmptyLazyStackContainer_ReturnsEmptySlots()
+ {
+ var (size, stackSize) = this.GenerateRandomSizeAndStackSize();
+ var container = this.containerFactory.Empty(size, stackSize);
+ var enumeratedItems = container.ToArray();
+
+ Assert.That(enumeratedItems, Is.Empty);
+ }
+
+ [Test]
+ [Description("GetEnumerator returns every slot, including empty slots, with its items.")]
+ public void GetEnumerator_LazyStackContainerWithItems_ReturnsSlotsAndItems()
+ {
+ var (size, stackSize) = this.GenerateRandomSizeAndStackSize();
+ var item = this.itemFactory.CreateDefault();
+ var container = this.containerFactory.ShuffledItems(size, stackSize, item);
+ var enumeratedItems = container.ToArray();
+
+ Assert.Multiple(() =>
+ {
+ Assert.That(enumeratedItems, Has.Length.EqualTo(stackSize));
+ Assert.That(enumeratedItems.Select(items => items.Count()), Is.All.EqualTo(1));
+ });
+ CollectionAssert.AreEqual(Enumerable.Repeat(item, stackSize), enumeratedItems.SelectMany(items => items));
+ }
+
+ [Test]
+ [Description("The non-generic GetEnumerator returns each slot and its items.")]
+ public void GetEnumerator_NonGeneric_ReturnsSlotsAndItems()
+ {
+ var (size, stackSize) = this.GenerateRandomSizeAndStackSize();
+ var item = this.itemFactory.CreateDefault();
+ IEnumerable container = this.containerFactory.ShuffledItems(size, stackSize, item);
+ var enumeratedItems = container.Cast>().ToArray();
+
+ Assert.Multiple(() =>
+ {
+ Assert.That(enumeratedItems, Has.Length.EqualTo(stackSize));
+ Assert.That(enumeratedItems.Select(items => items.Count()), Is.All.EqualTo(1));
+ });
+ CollectionAssert.AreEqual(Enumerable.Repeat(item, stackSize), enumeratedItems.SelectMany(items => items));
+ }
+ }
+}
diff --git a/tests/Slots/LazyStackSlot/GetEnumeratorTests.cs b/tests/Slots/LazyStackSlot/GetEnumeratorTests.cs
new file mode 100644
index 0000000..9e6ac96
--- /dev/null
+++ b/tests/Slots/LazyStackSlot/GetEnumeratorTests.cs
@@ -0,0 +1,47 @@
+using System.Collections;
+using System.Collections.Generic;
+using System.Linq;
+using NUnit.Framework;
+
+namespace TheChest.Core.Tests.Slots.LazyStackSlot
+{
+ public class GetEnumeratorTests : LazyStackSlotTests
+ {
+ [Test]
+ [Description("GetEnumerator returns no items for an empty lazy stack slot.")]
+ public void GetEnumerator_EmptySlot_ReturnsNoItems()
+ {
+ var maxStackSize = this.GenerateStackSize();
+ IEnumerable> slot = this.slotFactory.Empty(0, maxStackSize);
+
+ CollectionAssert.IsEmpty(slot);
+ }
+
+ [Test]
+ [Description("GetEnumerator returns each stored item in its own sequence.")]
+ public void GetEnumerator_SlotWithItems_ReturnsSingleItemSequences()
+ {
+ var maxStackSize = this.GenerateStackSize();
+ var amount = this.random.Next(1, maxStackSize);
+ var item = this.itemFactory.CreateDefault();
+ IEnumerable> slot = this.slotFactory.WithItem(item, amount, maxStackSize);
+
+ CollectionAssert.AreEqual(Enumerable.Repeat(item, amount), slot.Select(items => items.Single()));
+ }
+
+ [Test]
+ [Description("The non-generic GetEnumerator returns each stored item.")]
+ public void GetEnumerator_NonGeneric_ReturnsItems()
+ {
+ var maxStackSize = this.GenerateStackSize();
+ var amount = this.random.Next(1, maxStackSize);
+ var item = this.itemFactory.CreateDefault();
+ IEnumerable slot = this.slotFactory.WithItem(item, amount, maxStackSize);
+
+ CollectionAssert.AreEqual(
+ Enumerable.Repeat(item, amount),
+ slot.Cast>().SelectMany(items => items)
+ );
+ }
+ }
+}
\ No newline at end of file