Skip to content

Commit

Permalink
Merge branch 'Cleaning-up-Primitives'
Browse files Browse the repository at this point in the history
  • Loading branch information
dgmjr committed Oct 20, 2023
2 parents 6ae74aa + 09a820a commit 9498c26
Show file tree
Hide file tree
Showing 63 changed files with 4,682 additions and 3,112 deletions.
Empty file added BLACKBOX_README.md
Empty file.
3 changes: 2 additions & 1 deletion Tests/Dgmjr.Primitives.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
<PackageReference Include="Dgmjr.System.Extensions" />
<PackageReference Include="Dgmjr.Primitives" />
<PackageReference Include="Dgmjr.Testing.Logging" />
<!-- <ProjectReference Include="../src/Dgmjr.Primitives.csproj" /> -->
<PackageReference Include="Dgmjr.Testing.Tests" />
<ProjectReference Include="../src/Dgmjr.Primitives.csproj" />
</ItemGroup>


Expand Down
26 changes: 13 additions & 13 deletions Tests/Dgmjr.Primitives.Tests.sln
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
..\..\..\..\Packages\Versions\PowerShell.props = ..\..\..\..\Packages\Versions\PowerShell.props
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dgmjr.Primitives.Tests", "Dgmjr.Primitives.Tests.csproj", "{C83B5825-203F-43F6-BCD1-811BD10562F8}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dgmjr.Primitives.Tests", "Dgmjr.Primitives.Tests.csproj", "{89D6F98C-27F8-46DE-9F4A-4381BBEAD6B5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -63,18 +63,18 @@ Global
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C83B5825-203F-43F6-BCD1-811BD10562F8}.Local|Any CPU.ActiveCfg = Local|Any CPU
{C83B5825-203F-43F6-BCD1-811BD10562F8}.Local|Any CPU.Build.0 = Local|Any CPU
{C83B5825-203F-43F6-BCD1-811BD10562F8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C83B5825-203F-43F6-BCD1-811BD10562F8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C83B5825-203F-43F6-BCD1-811BD10562F8}.Testing|Any CPU.ActiveCfg = Testing|Any CPU
{C83B5825-203F-43F6-BCD1-811BD10562F8}.Testing|Any CPU.Build.0 = Testing|Any CPU
{C83B5825-203F-43F6-BCD1-811BD10562F8}.Staging|Any CPU.ActiveCfg = Staging|Any CPU
{C83B5825-203F-43F6-BCD1-811BD10562F8}.Staging|Any CPU.Build.0 = Staging|Any CPU
{C83B5825-203F-43F6-BCD1-811BD10562F8}.Production|Any CPU.ActiveCfg = Local|Any CPU
{C83B5825-203F-43F6-BCD1-811BD10562F8}.Production|Any CPU.Build.0 = Local|Any CPU
{C83B5825-203F-43F6-BCD1-811BD10562F8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C83B5825-203F-43F6-BCD1-811BD10562F8}.Release|Any CPU.Build.0 = Release|Any CPU
{89D6F98C-27F8-46DE-9F4A-4381BBEAD6B5}.Local|Any CPU.ActiveCfg = Local|Any CPU
{89D6F98C-27F8-46DE-9F4A-4381BBEAD6B5}.Local|Any CPU.Build.0 = Local|Any CPU
{89D6F98C-27F8-46DE-9F4A-4381BBEAD6B5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{89D6F98C-27F8-46DE-9F4A-4381BBEAD6B5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{89D6F98C-27F8-46DE-9F4A-4381BBEAD6B5}.Testing|Any CPU.ActiveCfg = Testing|Any CPU
{89D6F98C-27F8-46DE-9F4A-4381BBEAD6B5}.Testing|Any CPU.Build.0 = Testing|Any CPU
{89D6F98C-27F8-46DE-9F4A-4381BBEAD6B5}.Staging|Any CPU.ActiveCfg = Staging|Any CPU
{89D6F98C-27F8-46DE-9F4A-4381BBEAD6B5}.Staging|Any CPU.Build.0 = Staging|Any CPU
{89D6F98C-27F8-46DE-9F4A-4381BBEAD6B5}.Production|Any CPU.ActiveCfg = Local|Any CPU
{89D6F98C-27F8-46DE-9F4A-4381BBEAD6B5}.Production|Any CPU.Build.0 = Local|Any CPU
{89D6F98C-27F8-46DE-9F4A-4381BBEAD6B5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{89D6F98C-27F8-46DE-9F4A-4381BBEAD6B5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
12 changes: 6 additions & 6 deletions Tests/Dgmjr.Primitives.Tests.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

77 changes: 77 additions & 0 deletions Tests/DurationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
namespace Dgmjr.Primitives.Tests;

using Dgmjr.Primitives;

using Xunit;
using Xunit.Abstractions;

public class DurationTests : BaseTest
{
public DurationTests(IOutputHelper output)
: base(output) { }

[Theory]
[MemberData(nameof(DayTimeDurationTestData))]
public void Day_Time_Duration_Parses_Correctly(
string dayTimeDuration,
duration expectedDuration
)
{
var dtd = DayTimeDuration.Parse(dayTimeDuration);
((duration)dtd).Should().Be(expectedDuration);
}

[Theory]
[MemberData(nameof(YearMonthDurationTestData))]
public void Year_Month_Duration_Parses_Correctly(
string yearMonthDuration,
duration expectedDuration
)
{
var dtd = YearMonthDuration.Parse(yearMonthDuration);
((duration)dtd).Should().Be(expectedDuration);
}

public static IEnumerable<object[]> DayTimeDurationTestData =>
new[]
{
new object[]
{
"-P12DT13H27M12S",
duration.FromMilliseconds(new duration(12, 13, 27, 12).TotalMilliseconds * -1)
},
new object[] { "-PT525600M", duration.FromMinutes(-525600) },
new object[] { "PT525600M", duration.FromMinutes(525600) }
};

public static IEnumerable<object[]> YearMonthDurationTestData =>
new[]
{
new object[]
{
"-P6500Y1M",
duration.FromDays(
duration
.FromDays(
(6500 * YearMonthDuration.DaysPerYear) + YearMonthDuration.DaysPerMonth
)
.TotalDays * -1
)
},
new object[]
{
"P1Y3M",
duration.FromDays(
YearMonthDuration.DaysPerYear + (3 * YearMonthDuration.DaysPerMonth)
)
},
new object[]
{
"-P1Y3M",
duration.FromDays(
-(YearMonthDuration.DaysPerYear + (3 * YearMonthDuration.DaysPerMonth))
)
},
new object[] { "P3M", duration.FromDays(90) }
};
}
27 changes: 13 additions & 14 deletions Tests/EmailAddressTests.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
namespace Dgmjr.Primitives.Tests;

using System.Net.Mail;

public class EmailAddressTests : PrimitivesTests<EmailAddress, EmailAddressTests>
{
public EmailAddressTests(ITestOutputHelper output) : base(output) { }
public EmailAddressTests(ITestOutputHelper output)
: base(output) { }

protected override string[][] InvalidValuesStrings => new[]
{
new[] { "not an email" },
new[] { "missing@" },
new[] { "@missing-domain" }
};
protected override string[][] InvalidValuesStrings =>
new[] { new[] { "not an email" }, new[] { "missing@" }, new[] { "@missing-domain" } };

protected override string[][] ValidValuesStrings => new[]
{
new[] { "[email protected]" },
new[] { "[email protected]" },
new[] { "[email protected]" },
new[] { "[email protected]" }
};
protected override string[][] ValidValuesStrings =>
new[]
{
new[] { "[email protected]" },
new[] { "[email protected]" },
new[] { "[email protected]" },
new[] { "[email protected]" }
};
}
15 changes: 14 additions & 1 deletion Tests/HexStringTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* HexStringTests.cs
*
* Created: 2023-09-03-12:33:55
Expand All @@ -23,6 +23,7 @@ public static void Valid_Hex_String_Is_Valid(string validHexString)
var hexString = new HexString(validHexString);
hexString.Value.Should().Be(validHexString);
}

[Theory]
[InlineData("fcku!!!")]
[InlineData("go the fuck away!")]
Expand All @@ -32,4 +33,16 @@ public static void Invalid_Hex_String_Is_Invalid(string invalidHexString)
var hexStringCtor = () => new HexString(invalidHexString);
hexStringCtor.Should().Throw<ArgumentException>();
}

[Theory]
[InlineData("abcDEF09", "DEFab678")]
public static void Hex_String_Can_Be_Changed_To_Another_Valid_Hex_String(
string original,
string @new
)
{
var hexString = new HexString(original);
hexString = @new;
hexString.Value.Should().Be(@new);
}
}
7 changes: 6 additions & 1 deletion Tests/LoggerExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
namespace Dgmjr.Primitives.Tests;

using static Microsoft.Extensions.Logging.LogLevel;

public static partial class LoggerExtensions
{
[LoggerMessage(Information, "{PrimitiveType} original value: \"{OriginalValue}\"")]
public static partial void LogOriginalValue(this ILogger logger, Type primitiveType, string originalValue);
public static partial void LogOriginalValue(
this ILogger logger,
Type primitiveType,
string originalValue
);
}
27 changes: 13 additions & 14 deletions Tests/PhoneNumberTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* EmailAddressTests copy.cs
*
* Created: 2023-09-02-03:25:55
Expand All @@ -16,19 +16,18 @@ namespace Dgmjr.Primitives.Tests;

public class PhoneNumberTests : PrimitivesTests<PhoneNumber, PhoneNumberTests>
{
public PhoneNumberTests(ITestOutputHelper output) : base(output) { }
public PhoneNumberTests(ITestOutputHelper output)
: base(output) { }

protected override string[][] InvalidValuesStrings => new[]
{
new[] { "not-a-phone-number" },
new[] { "+1+9172125869" }
};
protected override string[][] InvalidValuesStrings =>
new[] { new[] { "not-a-phone-number" }, new[] { "+1+9172125869" } };

protected override string[][] ValidValuesStrings => new[]
{
new[] { "+1 202-503-4657" },
new[] { "+12025034657" },
new[] { "+1 202.503.4657" },
new[] { "+1 202 503 4657" }
};
protected override string[][] ValidValuesStrings =>
new[]
{
new[] { "+1 202-503-4657" },
new[] { "+12025034657" },
new[] { "+1 202.503.4657" },
new[] { "+1 202 503 4657" }
};
}
24 changes: 18 additions & 6 deletions Tests/PrimitivesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ public void Empty_Primitive_Is_Empty()
public void Empty_Primitive_Value_Is_Null_Or_Empty()
{
var emptyPrimitive = TPrimitive.Empty;
Logger.LogInformation($"Testing that empty primitive {emptyPrimitive} ToString is NullOrEmpty");
Logger.LogInformation(
$"Testing that empty primitive {emptyPrimitive} ToString is NullOrEmpty"
);
emptyPrimitive.ToString().Should().BeNullOrEmpty();
}

Expand All @@ -40,10 +42,16 @@ public void Empty_Primitive_Value_Is_Null_Or_Empty()
public virtual void Valid_Primitive_Is_Valid_And_Not_Empty(string validValue)
{
var primitive = TPrimitive.Parse(validValue);
Logger.LogInformation($"Testing that valid primitive {validValue} IsEmpty == false && contains {validValue}");
Logger.LogInformation(
$"Testing that valid primitive {validValue} IsEmpty == false && contains {validValue}"
);
Logger.LogOriginalValue(typeof(TPrimitive), primitive.OriginalString);
primitive.IsEmpty.Should().BeFalse();
primitive.Value.Trim().ToLowerInvariant().Should().Contain(validValue.Trim().ToLowerInvariant());
primitive.Value
.Trim()
.ToLowerInvariant()
.Should()
.Contain(validValue.Trim().ToLowerInvariant());
}

// [Theory]
Expand All @@ -60,13 +68,17 @@ public virtual void Valid_Primitive_Is_Valid_And_Not_Empty(string validValue)
public virtual void Invalid_Primitive_Throws_Exception_On_Parse(string invalidValue)
{
var parsePrimitive = () => TPrimitive.Parse(invalidValue);
Logger.LogInformation($"Testing that invalid primitive {invalidValue} throws exception on parse");
Logger.LogInformation(
$"Testing that invalid primitive {invalidValue} throws exception on parse"
);
parsePrimitive.Should().Throw<Exception>();
}

public static object[][] ValidValues => (Activator.CreateInstance(typeof(TSelf), _output) as TSelf).ValidValuesStrings;
public static object[][] ValidValues =>
(Activator.CreateInstance(typeof(TSelf), _output) as TSelf).ValidValuesStrings;
protected abstract string[][] ValidValuesStrings { get; }

public static object[][] InvalidValues => (Activator.CreateInstance(typeof(TSelf), _output) as TSelf).InvalidValuesStrings;
public static object[][] InvalidValues =>
(Activator.CreateInstance(typeof(TSelf), _output) as TSelf).InvalidValuesStrings;
protected abstract string[][] InvalidValuesStrings { get; }
}
46 changes: 23 additions & 23 deletions Tests/iriTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ namespace Dgmjr.Primitives.Tests;

public partial class iriTests : PrimitivesTests<iri, iriTests>
{
public iriTests(ITestOutputHelper output) : base(output) { }
public iriTests(ITestOutputHelper output)
: base(output) { }

[StringSyntax(StringSyntaxAttribute.Regex)]
private const string RegexString =
@"^(?<Scheme>
@"^(?<Scheme>
[a-z][a-z0-9+\-.]*
)
:
Expand Down Expand Up @@ -50,28 +51,27 @@ public iriTests(ITestOutputHelper output) : base(output) { }
(?:%[0-9a-f]{2}|[-._~!$&'()*+,;=:@\/?]|(?:[a-z0-9]|%[0-9a-f]{2})*)*
)?
)?$";

[GeneratedRegex(RegexString, Compiled | Singleline | IgnoreCase | IgnorePatternWhitespace)]
private static partial REx Regex();
private static partial Regex Regex();

protected override string[][] InvalidValuesStrings => new[]
{
new[] { "fcku!!!!!!" }
};
protected override string[][] InvalidValuesStrings => new[] { new[] { "fcku!!!!!!" } };

protected override string[][] ValidValuesStrings => new[]
{
new[] { uri.ExampleStringValue },
new[] { xri.ExampleStringValue },
new[] { url.ExampleStringValue },
new[] { urn.ExampleStringValue },
new[] { iri.ExampleStringValue },
new[] { "urn:uuid:1ffaa033-f06c-495c-a9f5-0f252dd740d8" },
new[] { "urn:uuid:1ffaa033f06c495ca9f50f252dd740d8" },
new[] { "https://dgmjr.io" },
new[] { "ftp://dgmjr.io" },
new[] { "telnet://dgmjr.io" },
new[] { "tg://user/?id=10245674" },
new[] { "dgmjr:foo" },
new[] { "https://dgmjr.🤯" },
};
protected override string[][] ValidValuesStrings =>
new[]
{
new[] { uri.ExampleStringValue },
new[] { xri.ExampleStringValue },
new[] { url.ExampleStringValue },
new[] { urn.ExampleStringValue },
new[] { iri.ExampleStringValue },
new[] { "urn:uuid:1ffaa033-f06c-495c-a9f5-0f252dd740d8" },
new[] { "urn:uuid:1ffaa033f06c495ca9f50f252dd740d8" },
new[] { "https://dgmjr.io" },
new[] { "ftp://dgmjr.io" },
new[] { "telnet://dgmjr.io" },
new[] { "tg://user/?id=10245674" },
new[] { "dgmjr:foo" },
new[] { "https://dgmjr.🤯" },
};
}
Loading

0 comments on commit 9498c26

Please sign in to comment.