aboutsummaryrefslogtreecommitdiffstats
path: root/src/IOL.Helpers.Tests
diff options
context:
space:
mode:
authorivarlovlie <git@ivarlovlie.no>2022-02-26 18:36:39 +0100
committerivarlovlie <git@ivarlovlie.no>2022-02-26 18:36:39 +0100
commit0112bcfd629c534997017ad6bc6d9b6caf2b95ad (patch)
tree2945c10d4f8ea86aebfb643bf468ebaed2ce256a /src/IOL.Helpers.Tests
parentfeb989c750fe3bad630b8950af7a54b51a0c46a1 (diff)
downloaddotnet-helpers-0112bcfd629c534997017ad6bc6d9b6caf2b95ad.tar.xz
dotnet-helpers-0112bcfd629c534997017ad6bc6d9b6caf2b95ad.zip
feat: Add tests for StringHelpers; Various breaking changes
Diffstat (limited to 'src/IOL.Helpers.Tests')
-rw-r--r--src/IOL.Helpers.Tests/IOL.Helpers.Tests.csproj27
-rw-r--r--src/IOL.Helpers.Tests/StringHelpersTests.cs76
2 files changed, 103 insertions, 0 deletions
diff --git a/src/IOL.Helpers.Tests/IOL.Helpers.Tests.csproj b/src/IOL.Helpers.Tests/IOL.Helpers.Tests.csproj
new file mode 100644
index 0000000..aeb661a
--- /dev/null
+++ b/src/IOL.Helpers.Tests/IOL.Helpers.Tests.csproj
@@ -0,0 +1,27 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+ <PropertyGroup>
+ <TargetFramework>net6.0</TargetFramework>
+ <Nullable>enable</Nullable>
+
+ <IsPackable>false</IsPackable>
+ </PropertyGroup>
+
+ <ItemGroup>
+ <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
+ <PackageReference Include="xunit" Version="2.4.1" />
+ <PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
+ <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
+ <PrivateAssets>all</PrivateAssets>
+ </PackageReference>
+ <PackageReference Include="coverlet.collector" Version="3.1.0">
+ <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
+ <PrivateAssets>all</PrivateAssets>
+ </PackageReference>
+ </ItemGroup>
+
+ <ItemGroup>
+ <ProjectReference Include="..\IOL.Helpers\IOL.Helpers.csproj" />
+ </ItemGroup>
+
+</Project>
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<string, string> {
+ {
+ "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<FormatException>(() => "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());
+ }
+}