mirror of
https://github.com/Stone-Red-Code/CuteUtils.git
synced 2026-09-04 00:46:11 +02:00
Add more unit tests and improve existing ones
This commit is contained in:
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,6 @@
|
|||||||
using System.Diagnostics;
|
using CuteUtils.Misc;
|
||||||
|
|
||||||
|
using System.Diagnostics;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
|
|
||||||
namespace CuteUtils.Logging;
|
namespace CuteUtils.Logging;
|
||||||
@@ -258,7 +260,7 @@ public class Logger
|
|||||||
File.Create(outputConfig.FilePath).Close();
|
File.Create(outputConfig.FilePath).Close();
|
||||||
}
|
}
|
||||||
|
|
||||||
File.AppendAllLines(outputConfig.FilePath, new[] { fileOutput });
|
File.AppendAllLines(outputConfig.FilePath, [fileOutput]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -274,4 +276,4 @@ public class Logger
|
|||||||
_ => Config.DebugConfig
|
_ => Config.DebugConfig
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
namespace CuteUtils;
|
namespace CuteUtils.Misc;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// <see cref="bool"/> Extensions
|
/// <see cref="bool"/> Extensions
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
|
||||||
namespace CuteUtils;
|
namespace CuteUtils.Misc;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Table Style
|
/// Table Style
|
||||||
@@ -3,7 +3,7 @@ using System.Diagnostics.CodeAnalysis;
|
|||||||
|
|
||||||
#pragma warning disable S3998 // Threads should not lock on objects with weak identity
|
#pragma warning disable S3998 // Threads should not lock on objects with weak identity
|
||||||
|
|
||||||
namespace CuteUtils;
|
namespace CuteUtils.Misc;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// <see cref="Console"/> Extensions
|
/// <see cref="Console"/> Extensions
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
namespace CuteUtils;
|
namespace CuteUtils.Misc;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// <see cref="Random"/> Extensions
|
/// <see cref="Random"/> Extensions
|
||||||
Reference in New Issue
Block a user