From 0112bcfd629c534997017ad6bc6d9b6caf2b95ad Mon Sep 17 00:00:00 2001 From: ivarlovlie Date: Sat, 26 Feb 2022 18:36:39 +0100 Subject: feat: Add tests for StringHelpers; Various breaking changes --- src/IOL.Helpers.Tests/StringHelpersTests.cs | 76 +++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 src/IOL.Helpers.Tests/StringHelpersTests.cs (limited to 'src/IOL.Helpers.Tests/StringHelpersTests.cs') diff --git a/src/IOL.Helpers.Tests/StringHelpersTests.cs b/src/IOL.Helpers.Tests/StringHelpersTests.cs new file mode 100644 index 0000000..210a3ee --- /dev/null +++ b/src/IOL.Helpers.Tests/StringHelpersTests.cs @@ -0,0 +1,76 @@ +using System; +using System.Collections.Generic; +using Xunit; + +namespace IOL.Helpers.Tests; + +public class StringHelpersTests +{ + [Fact] + public void Base64_Encode_Decode_Behaves() { + const string input = "value"; + var encoded = input.AsBase64EncodedString(); + Assert.Equal(input, encoded.AsBase64DecodedString()); + } + + [Fact] + public void Unicorn_Format_Behaves() { + const string input = "Hello, {name}"; + var result = input.UnicornFormat(new Dictionary { + { + "name", "World" + } + }); + Assert.Equal("Hello, World", result); + } + + [Fact] + public void IsNullOrWhiteSpace_Behaves() { + const string stringWithValue = "value"; + const string emptyString = ""; + const string onlyWhitespace = " "; + Assert.False(stringWithValue.IsNullOrWhiteSpace()); + Assert.True(emptyString.IsNullOrWhiteSpace()); + Assert.True(onlyWhitespace.IsNullOrWhiteSpace()); + } + + [Fact] + public void AsSnakeCasedString_Behaves() { + Assert.Equal("snake_case", "SNAKE Case ".AsSnakeCasedString()); + } + + [Fact] + public void AsSlug_Behaves() { + Assert.Equal("tromso-at-night", "Tromsø @ night".AsSlug()); + } + + [Fact] + public void HasValue_Behaves() { + Assert.True("asdf".HasValue()); + Assert.False(" ".HasValue()); + } + + [Fact] + public void AsGuid_Behaves() { + Assert.Equal(new Guid("bb7e2fd8-2ded-4fe9-a770-efeba661539c"), "bb7e2fd8-2ded-4fe9-a770-efeba661539c".AsGuid()); + Assert.Throws(() => "not-a-guid".AsGuid()); + } + + [Fact] + public void AsGuidOrDefault_Behaves() { + Assert.Equal(new Guid("bb7e2fd8-2ded-4fe9-a770-efeba661539c"), "bb7e2fd8-2ded-4fe9-a770-efeba661539c".AsGuid()); + Assert.Equal(default(Guid), "not-a-guid".AsGuidOrDefault()); + } + + [Fact] + public void Capitalize_Behaves() { + Assert.Equal("The File", "the file".Capitalize()); + Assert.Equal("The file", "the file".Capitalize(true)); + } + + [Fact] + public void Obfuscate_Behaves() { + Assert.Equal("********", "asdf".Obfuscate()); + Assert.Equal("****1234", "asdfasdf1234".Obfuscate()); + } +} -- cgit v1.3