From f0f727117abcfa82aca7ce53a96921655e1d01b2 Mon Sep 17 00:00:00 2001 From: Stone_Red <56473591+Stone-Red-Code@users.noreply.github.com> Date: Fri, 5 Apr 2024 18:23:24 +0200 Subject: [PATCH] Add more unit tests and improve existing ones --- .../{ => DynDto}/DynDtoTests.cs | 0 .../Misc/BoolExtentionsTests.cs | 117 +++++++++++++++ .../Misc/CollectionExtentionsTests.cs | 133 ++++++++++++++++++ src/CuteUtils/Logging/Logger.cs | 8 +- src/CuteUtils/{ => Misc}/BoolExtentions.cs | 2 +- .../{ => Misc}/CollectionExtentions.cs | 2 +- src/CuteUtils/{ => Misc}/ConsoleExtentions.cs | 2 +- src/CuteUtils/{ => Misc}/RandomExtentions.cs | 2 +- 8 files changed, 259 insertions(+), 7 deletions(-) rename src/CuteUtils.Tests/{ => DynDto}/DynDtoTests.cs (100%) create mode 100644 src/CuteUtils.Tests/Misc/BoolExtentionsTests.cs create mode 100644 src/CuteUtils.Tests/Misc/CollectionExtentionsTests.cs rename src/CuteUtils/{ => Misc}/BoolExtentions.cs (97%) rename src/CuteUtils/{ => Misc}/CollectionExtentions.cs (99%) rename src/CuteUtils/{ => Misc}/ConsoleExtentions.cs (99%) rename src/CuteUtils/{ => Misc}/RandomExtentions.cs (98%) diff --git a/src/CuteUtils.Tests/DynDtoTests.cs b/src/CuteUtils.Tests/DynDto/DynDtoTests.cs similarity index 100% rename from src/CuteUtils.Tests/DynDtoTests.cs rename to src/CuteUtils.Tests/DynDto/DynDtoTests.cs diff --git a/src/CuteUtils.Tests/Misc/BoolExtentionsTests.cs b/src/CuteUtils.Tests/Misc/BoolExtentionsTests.cs new file mode 100644 index 0000000..f2e0302 --- /dev/null +++ b/src/CuteUtils.Tests/Misc/BoolExtentionsTests.cs @@ -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); + } +} \ No newline at end of file diff --git a/src/CuteUtils.Tests/Misc/CollectionExtentionsTests.cs b/src/CuteUtils.Tests/Misc/CollectionExtentionsTests.cs new file mode 100644 index 0000000..9a3aff8 --- /dev/null +++ b/src/CuteUtils.Tests/Misc/CollectionExtentionsTests.cs @@ -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 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 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); + } +} \ No newline at end of file diff --git a/src/CuteUtils/Logging/Logger.cs b/src/CuteUtils/Logging/Logger.cs index ce43d68..37da229 100644 --- a/src/CuteUtils/Logging/Logger.cs +++ b/src/CuteUtils/Logging/Logger.cs @@ -1,4 +1,6 @@ -using System.Diagnostics; +using CuteUtils.Misc; + +using System.Diagnostics; using System.Runtime.CompilerServices; namespace CuteUtils.Logging; @@ -258,7 +260,7 @@ public class Logger 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 }; } -} +} \ No newline at end of file diff --git a/src/CuteUtils/BoolExtentions.cs b/src/CuteUtils/Misc/BoolExtentions.cs similarity index 97% rename from src/CuteUtils/BoolExtentions.cs rename to src/CuteUtils/Misc/BoolExtentions.cs index 71e8819..cc9f3a9 100644 --- a/src/CuteUtils/BoolExtentions.cs +++ b/src/CuteUtils/Misc/BoolExtentions.cs @@ -1,4 +1,4 @@ -namespace CuteUtils; +namespace CuteUtils.Misc; /// /// Extensions diff --git a/src/CuteUtils/CollectionExtentions.cs b/src/CuteUtils/Misc/CollectionExtentions.cs similarity index 99% rename from src/CuteUtils/CollectionExtentions.cs rename to src/CuteUtils/Misc/CollectionExtentions.cs index a9cb784..b418f12 100644 --- a/src/CuteUtils/CollectionExtentions.cs +++ b/src/CuteUtils/Misc/CollectionExtentions.cs @@ -1,6 +1,6 @@ using System.Diagnostics; -namespace CuteUtils; +namespace CuteUtils.Misc; /// /// Table Style diff --git a/src/CuteUtils/ConsoleExtentions.cs b/src/CuteUtils/Misc/ConsoleExtentions.cs similarity index 99% rename from src/CuteUtils/ConsoleExtentions.cs rename to src/CuteUtils/Misc/ConsoleExtentions.cs index 5ca80d1..683b5bc 100644 --- a/src/CuteUtils/ConsoleExtentions.cs +++ b/src/CuteUtils/Misc/ConsoleExtentions.cs @@ -3,7 +3,7 @@ using System.Diagnostics.CodeAnalysis; #pragma warning disable S3998 // Threads should not lock on objects with weak identity -namespace CuteUtils; +namespace CuteUtils.Misc; /// /// Extensions diff --git a/src/CuteUtils/RandomExtentions.cs b/src/CuteUtils/Misc/RandomExtentions.cs similarity index 98% rename from src/CuteUtils/RandomExtentions.cs rename to src/CuteUtils/Misc/RandomExtentions.cs index 36fc0d8..22337f9 100644 --- a/src/CuteUtils/RandomExtentions.cs +++ b/src/CuteUtils/Misc/RandomExtentions.cs @@ -1,4 +1,4 @@ -namespace CuteUtils; +namespace CuteUtils.Misc; /// /// Extensions