Add more unit tests and improve existing ones

This commit is contained in:
Stone_Red
2024-04-05 18:23:24 +02:00
parent fde63845f5
commit f0f727117a
8 changed files with 259 additions and 7 deletions
@@ -0,0 +1,117 @@
using CuteUtils.Misc;
namespace CuteUtils.Tests;
[TestClass]
public class BoolExtentionsTests
{
[TestMethod]
public void OneWayTrue_ShouldSetValueToTrue_WhenInputIsTrue()
{
// Arrange
bool value = false;
bool input = true;
// Act
value.OneWayTrue(input);
// Assert
Assert.IsTrue(value);
}
[TestMethod]
public void OneWayTrue_ShouldNotChangeValue_WhenInputIsFalse()
{
// Arrange
bool value = true;
bool input = false;
// Act
value.OneWayTrue(input);
// Assert
Assert.IsTrue(value);
}
[TestMethod]
public void OneWayFalse_ShouldSetValueToFalse_WhenInputIsFalse()
{
// Arrange
bool value = true;
bool input = false;
// Act
value.OneWayFalse(input);
// Assert
Assert.IsFalse(value);
}
[TestMethod]
public void OneWayFalse_ShouldNotChangeValue_WhenInputIsTrue()
{
// Arrange
bool value = false;
bool input = true;
// Act
value.OneWayFalse(input);
// Assert
Assert.IsFalse(value);
}
[TestMethod]
public void ToInt_ShouldReturn1_WhenInputIsTrue()
{
// Arrange
bool input = true;
// Act
int result = input.ToInt();
// Assert
Assert.AreEqual(1, result);
}
[TestMethod]
public void ToInt_ShouldReturn0_WhenInputIsFalse()
{
// Arrange
bool input = false;
// Act
int result = input.ToInt();
// Assert
Assert.AreEqual(0, result);
}
[TestMethod]
public void FromInt_ShouldSetValueToTrue_WhenInputIs1()
{
// Arrange
bool value = false;
int input = 1;
// Act
value.FromInt(input);
// Assert
Assert.IsTrue(value);
}
[TestMethod]
public void FromInt_ShouldSetValueToFalse_WhenInputIs0()
{
// Arrange
bool value = true;
int input = 0;
// Act
value.FromInt(input);
// Assert
Assert.IsFalse(value);
}
}
@@ -0,0 +1,133 @@
using CuteUtils.Misc;
using System.Text;
namespace CuteUtils.Tests.Misc;
[TestClass]
public class CollectionExtentionsTests
{
private StringBuilder consoleOutput = null!;
[TestInitialize]
public void Initialize()
{
consoleOutput = new StringBuilder();
Console.SetOut(new StringWriter(consoleOutput));
}
[TestMethod]
public void Print_ShouldPrintCollectionElements()
{
// Arrange
List<int> collection = [1, 2, 3, 4, 5];
string expectedOutput = "1, 2, 3, 4, 5";
// Act
collection.Print();
string actualOutput = consoleOutput.ToString();
// Assert
Assert.AreEqual(expectedOutput, actualOutput);
}
[TestMethod]
public void Print_ShouldPrintCollectionElementsWithCustomDelimiter()
{
// Arrange
List<string> collection = ["apple", "banana", "cherry"];
string expectedOutput = "apple, banana, cherry";
// Act
collection.Print(',');
string actualOutput = consoleOutput.ToString();
// Assert
Assert.AreEqual(expectedOutput, actualOutput);
}
[TestMethod]
public void Print_ShouldPrintCollectionElementsToDebugConsole()
{
Assert.Inconclusive("Need to find a way to test this.");
}
[TestMethod]
public void PrintTable_ShouldPrint2DArrayInTableFormat()
{
// Arrange
int[,] array = new int[,]
{
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
string expectedOutput = Environment.NewLine +
$"-------------{Environment.NewLine}" +
$"| 1 | 2 | 3 |{Environment.NewLine}" +
$"-------------{Environment.NewLine}" +
$"| 4 | 5 | 6 |{Environment.NewLine}" +
$"-------------{Environment.NewLine}" +
$"| 7 | 8 | 9 |{Environment.NewLine}" +
$"-------------{Environment.NewLine}";
// Act
array.PrintTable();
string actualOutput = consoleOutput.ToString();
// Assert
Assert.AreEqual(expectedOutput, actualOutput);
}
[TestMethod]
public void PrintTable_ShouldPrint2DArrayInTableFormatWithAlternativeStyle()
{
// Arrange
int[,] array = new int[,]
{
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
string expectedOutput = Environment.NewLine +
$"+---+---+---+{Environment.NewLine}" +
$"| 1 | 2 | 3 |{Environment.NewLine}" +
$"+---+---+---+{Environment.NewLine}" +
$"| 4 | 5 | 6 |{Environment.NewLine}" +
$"+---+---+---+{Environment.NewLine}" +
$"| 7 | 8 | 9 |{Environment.NewLine}" +
$"+---+---+---+{Environment.NewLine}";
// Act
array.PrintTable(TableStyle.Alternative);
string actualOutput = consoleOutput.ToString();
// Assert
Assert.AreEqual(expectedOutput, actualOutput);
}
[TestMethod]
public void PrintTable_ShouldPrint2DArrayInTableFormatWithListStyle()
{
// Arrange
int[,] array = new int[,]
{
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
string expectedOutput = Environment.NewLine +
$"+---+---+---+{Environment.NewLine}" +
$"| 1 | 2 | 3 |{Environment.NewLine}" +
$"+---+---+---+{Environment.NewLine}" +
$"| 4 | 5 | 6 |{Environment.NewLine}" +
$"| 7 | 8 | 9 |{Environment.NewLine}";
// Act
array.PrintTable(TableStyle.List);
string actualOutput = consoleOutput.ToString();
// Assert
Assert.AreEqual(expectedOutput, actualOutput);
}
}