using System; using System.Collections.Generic; using System.Linq; using System.Utility; using FluentAssertions; using NUnit.Framework; namespace Chernobyl.Collections.Generic { [TestFixture, Description("Tests for the List type.")] public class ListTests { #region List.RemoveFirst(IList, Predicate) [Test, Description("Ensures RemoveFirst removes the correct item and returns the correct value.")] public void RemoveFirstRemovesAndReturnsCorrectValue() { var list = new List { 0, 1, 1, 3, 4 }; list.RemoveFirst(v => v == 1).Should().BeTrue(); list.Should().Equal(new List { 0, 1, 3, 4 }); } [Test, Description("Ensures RemoveFirst does not remove any items and returns the correct value.")] public void RemoveFirstDoesNotRemoveItemAndReturnsCorrectValue() { var list = new List { 0, 1, 1, 3, 4 }; list.RemoveFirst(v => v == 5).Should().BeFalse(); list.Should().Equal(new List { 0, 1, 1, 3, 4 }); } #endregion #region List.Pull(IList, int) [Test, Description("Ensures Pull removes and returns the correct item.")] public void PullRemovesAndReturnsCorrectValue() { var list = new List { 0, 1, 2, 3, 4 }; list.Pull(3).IsEqualTo(3, "List.Pull"); list.IsEqualTo(new List { 0, 1, 2, 4 }, "List.Pull"); } [Test, Description("Ensures Pull throws exception when provided index is less than zero.")] public void PullThrowsExceptionWhenIndexIsZero() { var list = new List { 0, 1, 2, 3, 4 }; Assert.Throws(() => list.Pull(-1)); } [Test, Description("Ensures Pull throws exception when provided index is outside list's bounds.")] public void PullThrowsExceptionWhenIndexIsOutsideListsBounds() { var list = new List { 0, 1, 2, 3, 4 }; Assert.Throws(() => list.Pull(list.Count)); } #endregion #region List.Pull(Random, IEnumerable, int) readonly Random _rand = new Random(); // ReSharper disable ReturnValueOfPureMethodIsNotUsed [Test, Description("Ensures Pull throws exception when count is less than zero.")] public void PullThrowsExceptionWhenCountIsLessThanZero() { var list = new List { 0, 1, 2 }; Assert.Throws(() => list.Pull(_rand , -1).ToArray()); } [Test, Description("Ensures Pull throws exception when count is greater than list count.")] public void PullThrowsExceptionWhenCountIsGreaterThanListCount() { var list = new List { 0, 1, 2 }; var count = list.Count + 1; Assert.Throws(() => list.Pull(_rand, count).ToArray()); } // ReSharper restore ReturnValueOfPureMethodIsNotUsed [Test, Description("Ensures Pull removes and returns the correct number of values from the list.")] public void PullRemovesAndReturnsCorrectValues() { var list = new List { 0, 1, 2 }; var originalCount = list.Count; var pullCount = 2; var items = list.Pull(_rand, pullCount).ToArray(); items.Length.IsEqualTo(pullCount, "pulled length"); list.Count.IsEqualTo(originalCount - pullCount, "original list length"); } #endregion #region List.AtOrDefault(IList, int) [Test, Description("Ensures AtOrDefault returns correct item when index is in bounds.")] public void AtOrDefaultReturnsItemWhenIndexIsInBounds() { IList list = new List { "X", "Y", "Z" }; list.AtOrDefault(1).Should().Be("Y"); } [Test, Description("Ensures AtOrDefault returns a default value when index is out of bounds.")] public void AtOrDefaultReturnsDefaultWhenIndexIsOutOfBounds() { IList list = new List { "X", "Y", "Z" }; list.AtOrDefault(3).Should().Be(default); } #endregion } }