Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,80 +1,7 @@
using System;
using System.Linq;
using ConvertStringToBoolean;

namespace ConvertStringToBoolean
{
public class Program
{
public static void Main()
{
ToBooleanMethod();
StringToBoolean.ToBooleanMethod();

ParseMethod();
StringToBoolean.ParseMethod();

TryParseMethod();
}

public static void ToBooleanMethod()
{
string[] validString = { null, "true", "True", " true ", "false", "False", " false" };

string[] invalidString = { "", string.Empty, "t", " yes ", "-1", "0", "1" };

var values = validString.Concat(invalidString);

foreach (var value in values)
{
try
{
Console.WriteLine($"Converted '{value}' to {Convert.ToBoolean(value)}.\n");
}
catch (FormatException)
{
Console.WriteLine($"Unable to convert '{value}' to a Boolean.\n");
}
}
}

public static void ParseMethod()
{
string[] validString = { "true", "True", " true ", "false", "False", " false" };

string[] invalidString = {null, "", string.Empty, "t", " yes ", "-1", "0", "1" };

var values = validString.Concat(invalidString);

foreach (var value in values)
{
try
{
Console.WriteLine($"Converted '{value}' to {bool.Parse(value)}.\n");
}
catch (Exception)
{
Console.WriteLine($"Unable to convert '{value}' to a Boolean.\n");
}
}
}

public static void TryParseMethod()
{
string[] validString = { "true", "True", " true ", "false", "False", " false" };

string[] invalidString = { null, "", string.Empty, "t", " yes ", "-1", "0", "1" };

var values = validString.Concat(invalidString);

foreach (var value in values)
{
if (bool.TryParse(value, out bool booleanValue))
{
Console.WriteLine($"Conversion successful: '{value}' to {booleanValue}.\n");
}
else
{
Console.WriteLine($"Conversion Failed: '{value}' to {booleanValue}.\n");
}
}
}
}
}
StringToBoolean.TryParseMethod();
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
namespace ConvertStringToBoolean;

public static class StringToBoolean
{
public static void ToBooleanMethod()
{
string?[] validString = { null, "true", "True", " true ", "false", "False", " false" };

string[] invalidString = { "", string.Empty, "t", " yes ", "-1", "0", "1" };

var values = validString.Concat(invalidString);

foreach (var value in values)
{
try
{
Console.WriteLine($"Converted '{value}' to {Convert.ToBoolean(value)}.\n");
}
catch (FormatException)
{
Console.WriteLine($"Unable to convert '{value}' to a Boolean.\n");
}
}
}

public static void ParseMethod()
{
string[] validString = { "true", "True", " true ", "false", "False", " false" };

string?[] invalidString = { null, "", string.Empty, "t", " yes ", "-1", "0", "1" };

var values = validString.Concat(invalidString);

foreach (var value in values)
{
try
{
Console.WriteLine($"Converted '{value}' to {bool.Parse(value!)}.\n");
}
catch (ArgumentNullException)
{
Console.WriteLine("Unable to convert null to a Boolean.\n");
}
catch (FormatException)
{
Console.WriteLine($"Unable to convert '{value}' to a Boolean.\n");
}
}
}

public static void TryParseMethod()
{
string[] validString = { "true", "True", " true ", "false", "False", " false" };

string?[] invalidString = { null, "", string.Empty, "t", " yes ", "-1", "0", "1" };

var values = validString.Concat(invalidString);

foreach (var value in values)
{
if (bool.TryParse(value, out bool booleanValue))
{
Console.WriteLine($"Conversion successful: '{value}' to {booleanValue}.\n");
}
else
{
Console.WriteLine($"Conversion Failed: '{value}' to {booleanValue}.\n");
}
}
}

public static bool? ToBoolOrNull(string? value) => value?.Trim().ToLowerInvariant() switch
{
"true" or "yes" or "y" or "1" or "on" => true,
"false" or "no" or "n" or "0" or "off" => false,
_ => null
};
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using ConvertStringToBoolean;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;

Expand All @@ -13,6 +14,14 @@ public void GivenConvertToBoolean_WhenValid_ThenConversionSuccess()

Assert.AreEqual(true, Convert.ToBoolean("True"));

Assert.AreEqual(true, Convert.ToBoolean("TRUE"));

Assert.AreEqual(true, Convert.ToBoolean(" true "));

Assert.AreEqual(false, Convert.ToBoolean("false"));

Assert.AreEqual(false, Convert.ToBoolean("False"));

Assert.AreEqual(false, Convert.ToBoolean(" false "));

Assert.AreEqual(false, Convert.ToBoolean("False "));
Expand All @@ -23,19 +32,23 @@ public void GivenConvertToBoolean_WhenValid_ThenConversionSuccess()
[TestMethod]
public void GivenConvertToBoolean_WhenInvalid_ThenConversionThrowsException()
{
Assert.ThrowsException<FormatException>(() => Convert.ToBoolean(""));
Assert.ThrowsExactly<FormatException>(() => Convert.ToBoolean(""));

Assert.ThrowsExactly<FormatException>(() => Convert.ToBoolean(string.Empty));

Assert.ThrowsException<FormatException>(() => Convert.ToBoolean(string.Empty));
Assert.ThrowsExactly<FormatException>(() => Convert.ToBoolean("t"));

Assert.ThrowsException<FormatException>(() => Convert.ToBoolean("t"));
Assert.ThrowsExactly<FormatException>(() => Convert.ToBoolean("y"));

Assert.ThrowsException<FormatException>(() => Convert.ToBoolean(" yes "));
Assert.ThrowsExactly<FormatException>(() => Convert.ToBoolean("yes"));

Assert.ThrowsException<FormatException>(() => Convert.ToBoolean("-1"));
Assert.ThrowsExactly<FormatException>(() => Convert.ToBoolean(" yes "));

Assert.ThrowsException<FormatException>(() => Convert.ToBoolean("0"));
Assert.ThrowsExactly<FormatException>(() => Convert.ToBoolean("-1"));

Assert.ThrowsException<FormatException>(() => Convert.ToBoolean("1"));
Assert.ThrowsExactly<FormatException>(() => Convert.ToBoolean("0"));

Assert.ThrowsExactly<FormatException>(() => Convert.ToBoolean("1"));
}

[TestMethod]
Expand All @@ -45,6 +58,14 @@ public void GivenboolParse_WhenValid_ThenConversionSuccess()

Assert.AreEqual(true, bool.Parse("True"));

Assert.AreEqual(true, bool.Parse("TRUE"));

Assert.AreEqual(true, bool.Parse(" true "));

Assert.AreEqual(false, bool.Parse("false"));

Assert.AreEqual(false, bool.Parse("False"));

Assert.AreEqual(false, bool.Parse(" false "));

Assert.AreEqual(false, bool.Parse("False "));
Expand All @@ -53,21 +74,25 @@ public void GivenboolParse_WhenValid_ThenConversionSuccess()
[TestMethod]
public void GivenboolParse_WhenInvalid_ThenConversionThrowsException()
{
Assert.ThrowsException<ArgumentNullException>(() => bool.Parse(null));
Assert.ThrowsExactly<ArgumentNullException>(() => bool.Parse(null));

Assert.ThrowsExactly<FormatException>(() => bool.Parse(""));

Assert.ThrowsExactly<FormatException>(() => bool.Parse(string.Empty));

Assert.ThrowsException<FormatException>(() => bool.Parse(""));
Assert.ThrowsExactly<FormatException>(() => bool.Parse("t"));

Assert.ThrowsException<FormatException>(() => bool.Parse(string.Empty));
Assert.ThrowsExactly<FormatException>(() => bool.Parse("y"));

Assert.ThrowsException<FormatException>(() => bool.Parse("t"));
Assert.ThrowsExactly<FormatException>(() => bool.Parse("yes"));

Assert.ThrowsException<FormatException>(() => bool.Parse(" yes "));
Assert.ThrowsExactly<FormatException>(() => bool.Parse(" yes "));

Assert.ThrowsException<FormatException>(() => bool.Parse("-1"));
Assert.ThrowsExactly<FormatException>(() => bool.Parse("-1"));

Assert.ThrowsException<FormatException>(() => bool.Parse("0"));
Assert.ThrowsExactly<FormatException>(() => bool.Parse("0"));

Assert.ThrowsException<FormatException>(() => bool.Parse("1"));
Assert.ThrowsExactly<FormatException>(() => bool.Parse("1"));
}

[TestMethod]
Expand All @@ -77,17 +102,25 @@ public void GivenboolTryParse_WhenValid_ThenConversionSuccess()

Assert.AreEqual(true, result1);

Assert.IsTrue(bool.TryParse("True", out bool result2));
Assert.IsTrue(bool.TryParse("TRUE", out bool result2));

Assert.AreEqual(true, result2);

Assert.IsTrue(bool.TryParse(" false ", out bool result3));
Assert.IsTrue(bool.TryParse(" true ", out bool result3));

Assert.AreEqual(false, result3);
Assert.AreEqual(true, result3);

Assert.IsTrue(bool.TryParse(" False ", out bool result4));
Assert.IsTrue(bool.TryParse("false", out bool result4));

Assert.AreEqual(false, result4);

Assert.IsTrue(bool.TryParse("False", out bool result5));

Assert.AreEqual(false, result5);

Assert.IsTrue(bool.TryParse(" false ", out bool result6));

Assert.AreEqual(false, result6);
}

[TestMethod]
Expand All @@ -105,21 +138,55 @@ public void GivenboolTryParse_WhenInvalid_ThenNoExceptionReturnFalse()

Assert.IsFalse(result3);

Assert.IsFalse(bool.TryParse(" yes ", out bool result4));
Assert.IsFalse(bool.TryParse("y", out bool result4));

Assert.IsFalse(result4);

Assert.IsFalse(bool.TryParse("-1", out bool result5));
Assert.IsFalse(bool.TryParse("yes", out bool result5));

Assert.IsFalse(result5);

Assert.IsFalse(bool.TryParse("0", out bool result6));
Assert.IsFalse(bool.TryParse("-1", out bool result6));

Assert.IsFalse(result6);

Assert.IsFalse(bool.TryParse("1", out bool result7));
Assert.IsFalse(bool.TryParse("0", out bool result7));

Assert.IsFalse(result7);

Assert.IsFalse(bool.TryParse("1", out bool result8));

Assert.IsFalse(result8);
}

[TestMethod]
public void GivenToBoolOrNull_WhenRecognisedTrueText_ThenReturnsTrue()
{
Assert.AreEqual(true, StringToBoolean.ToBoolOrNull("1"));

Assert.AreEqual(true, StringToBoolean.ToBoolOrNull("yes"));

Assert.AreEqual(true, StringToBoolean.ToBoolOrNull("Y"));

Assert.AreEqual(true, StringToBoolean.ToBoolOrNull(" ON "));
}

[TestMethod]
public void GivenToBoolOrNull_WhenRecognisedFalseText_ThenReturnsFalse()
{
Assert.AreEqual(false, StringToBoolean.ToBoolOrNull("0"));

Assert.AreEqual(false, StringToBoolean.ToBoolOrNull("no"));

Assert.AreEqual(false, StringToBoolean.ToBoolOrNull("off"));
}

[TestMethod]
public void GivenToBoolOrNull_WhenUnrecognisedText_ThenReturnsNull()
{
Assert.IsNull(StringToBoolean.ToBoolOrNull(null));

Assert.IsNull(StringToBoolean.ToBoolOrNull("maybe"));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.3" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.3" />
<PackageReference Include="coverlet.collector" Version="3.0.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.10.1" />
<PackageReference Include="MSTest.TestAdapter" Version="4.4.0" />
<PackageReference Include="MSTest.TestFramework" Version="4.4.0" />
<PackageReference Include="coverlet.collector" Version="10.0.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ConvertStringToBoolean\ConvertStringToBoolean.csproj" />
</ItemGroup>

</Project>
Loading