Skip to content

Commit

Permalink
1. xUnit Tests added for AltaSoft.DomainPrimitives assembly
Browse files Browse the repository at this point in the history
2. xUnit Tests added for AltaSoft.DomainPrimitives.Abstractions assembly
  • Loading branch information
GregoryNikolaishvili committed Jan 9, 2024
1 parent 6b6b81f commit facd0fc
Show file tree
Hide file tree
Showing 18 changed files with 677 additions and 1 deletion.
6 changes: 6 additions & 0 deletions AltaSoft.DomainPrimitives.sln
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
README.md = README.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AltaSoft.DomainPrimitives.Tests", "tests\AltaSoft.DomainPrimitives.Tests\AltaSoft.DomainPrimitives.Tests.csproj", "{A3D3536F-993E-4606-B9DF-A4C4B3E1AF69}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -33,6 +35,10 @@ Global
{01537E81-E36F-41A0-B843-D1089E0D76AB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{01537E81-E36F-41A0-B843-D1089E0D76AB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{01537E81-E36F-41A0-B843-D1089E0D76AB}.Release|Any CPU.Build.0 = Release|Any CPU
{A3D3536F-993E-4606-B9DF-A4C4B3E1AF69}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A3D3536F-993E-4606-B9DF-A4C4B3E1AF69}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A3D3536F-993E-4606-B9DF-A4C4B3E1AF69}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A3D3536F-993E-4606-B9DF-A4C4B3E1AF69}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("AltaSoft.DomainPrimitives.Tests")]
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public static class JsonInternalConverters

private static JsonConverter<ulong>? s_uint64Converter;

private static JsonConverter<T> GetConverter<T>()
internal static JsonConverter<T> GetConverter<T>()
{
//todo keep track and remove later.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\AltaSoft.DomainPrimitives.Abstractions\AltaSoft.DomainPrimitives.Abstractions.csproj" />
<ProjectReference Include="..\..\src\AltaSoft.DomainPrimitives\AltaSoft.DomainPrimitives.csproj" />
</ItemGroup>

</Project>
40 changes: 40 additions & 0 deletions tests/AltaSoft.DomainPrimitives.Tests/AsciiStringTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using AltaSoft.DomainPrimitives.Abstractions;

namespace AltaSoft.DomainPrimitives.Tests;

/// <summary>
/// Contains unit tests for the AsciiString class.
/// </summary>
public class AsciiStringTests
{
[Fact]
public void Validate_ValidAsciiString_DoesNotThrowException()
{
// Arrange
const string validString = "Hello";

// Act & Assert
var exception = Record.Exception(() => AsciiString.Validate(validString));
Assert.Null(exception);
}

[Fact]
public void Validate_InvalidAsciiString_ThrowsException()
{
// Arrange
const string invalidString = "Hello😊";

// Act & Assert
Assert.Throws<InvalidDomainValueException>(() => AsciiString.Validate(invalidString));
}

[Fact]
public void Default_ReturnsEmptyString()
{
// Act
var defaultValue = AsciiString.Default;

// Assert
Assert.Equal(string.Empty, defaultValue);
}
}
47 changes: 47 additions & 0 deletions tests/AltaSoft.DomainPrimitives.Tests/GDayTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
namespace AltaSoft.DomainPrimitives.Tests;

/// <summary>
/// Contains unit tests for the GDay class.
/// </summary>
public class GDayTests
{
[Theory]
[InlineData(1)]
[InlineData(15)]
[InlineData(31)]
public void Validate_ShouldNotThrowException_WhenValidDateOnly(int day)
{
// Arrange
var validDateOnly = new DateOnly(2000, 1, day);

// Act & Assert
var exception = Record.Exception(() => GDay.Validate(validDateOnly));
Assert.Null(exception);
}

[Fact]
public void Default_ShouldReturnDefaultDateOnly()
{
// Act
var defaultDateOnly = GDay.Default;

// Assert
Assert.Equal(default, defaultDateOnly);
}

[Theory]
[InlineData(1, "01")]
[InlineData(15, "15")]
[InlineData(31, "31")]
public void ToString_ShouldReturnFormattedString(int day, string expectedString)
{
// Arrange
var dateOnly = new DateOnly(2000, 1, day);

// Act
var result = GDay.ToString(dateOnly);

// Assert
Assert.Equal(expectedString, result);
}
}
49 changes: 49 additions & 0 deletions tests/AltaSoft.DomainPrimitives.Tests/GMonthDayTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
namespace AltaSoft.DomainPrimitives.Tests;

/// <summary>
/// This class contains unit tests for the GMonthDay class.
/// </summary>
public class GMonthDayTests
{
[Theory]
[InlineData(1, 1)]
[InlineData(12, 31)]
[InlineData(2, 28)]
[InlineData(3, 15)]
public void Validate_ShouldNotThrowException_WhenValidDateOnly(int month, int day)
{
// Arrange
var validDateOnly = new DateOnly(2000, month, day);

// Act & Assert
var exception = Record.Exception(() => GMonthDay.Validate(validDateOnly));
Assert.Null(exception);
}

[Fact]
public void Default_ShouldReturnDefaultDateOnly()
{
// Act
var defaultDateOnly = GMonthDay.Default;

// Assert
Assert.Equal(default, defaultDateOnly);
}

[Theory]
[InlineData(1, 1, "01-01")]
[InlineData(12, 31, "12-31")]
[InlineData(2, 28, "02-28")]
[InlineData(3, 15, "03-15")]
public void ToString_ShouldReturnFormattedString(int month, int day, string expectedString)
{
// Arrange
var dateOnly = new DateOnly(2000, month, day);

// Act
var result = GMonthDay.ToString(dateOnly);

// Assert
Assert.Equal(expectedString, result);
}
}
47 changes: 47 additions & 0 deletions tests/AltaSoft.DomainPrimitives.Tests/GMonthTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
namespace AltaSoft.DomainPrimitives.Tests;

/// <summary>
/// Contains unit tests for the GMonth class.
/// </summary>
public class GMonthTests
{
[Theory]
[InlineData(1)]
[InlineData(6)]
[InlineData(12)]
public void Validate_ShouldNotThrowException_WhenValidDateOnly(int month)
{
// Arrange
var validDateOnly = new DateOnly(2000, month, 1);

// Act & Assert
var exception = Record.Exception(() => GMonth.Validate(validDateOnly));
Assert.Null(exception);
}

[Fact]
public void Default_ShouldReturnDefaultDateOnly()
{
// Act
var defaultDateOnly = GMonth.Default;

// Assert
Assert.Equal(default, defaultDateOnly);
}

[Theory]
[InlineData(1, "01")]
[InlineData(6, "06")]
[InlineData(12, "12")]
public void ToString_ShouldReturnFormattedString(int month, string expectedString)
{
// Arrange
var dateOnly = new DateOnly(2000, month, 1);

// Act
var result = GMonth.ToString(dateOnly);

// Assert
Assert.Equal(expectedString, result);
}
}
49 changes: 49 additions & 0 deletions tests/AltaSoft.DomainPrimitives.Tests/GYearMonthTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
namespace AltaSoft.DomainPrimitives.Tests;

/// <summary>
/// Contains unit tests for the GYearMonth class.
/// </summary>
public class GYearMonthTests
{
[Theory]
[InlineData(2022, 1)]
[InlineData(2024, 12)]
[InlineData(2024, 2)]
[InlineData(2023, 3)]
public void Validate_ShouldNotThrowException_WhenValidDateOnly(int year, int month)
{
// Arrange
var validDateOnly = new DateOnly(year, month, 1);

// Act & Assert
var exception = Record.Exception(() => GYearMonth.Validate(validDateOnly));
Assert.Null(exception);
}

[Fact]
public void Default_ShouldReturnDefaultDateOnly()
{
// Act
var defaultDateOnly = GYearMonth.Default;

// Assert
Assert.Equal(default, defaultDateOnly);
}

[Theory]
[InlineData(2022, 1, "2022-01")]
[InlineData(2024, 12, "2024-12")]
[InlineData(2024, 2, "2024-02")]
[InlineData(2023, 3, "2023-03")]
public void ToString_ShouldReturnFormattedString(int year, int month, string expectedString)
{
// Arrange
var dateOnly = new DateOnly(year, month, 1);

// Act
var result = GYearMonth.ToString(dateOnly);

// Assert
Assert.Equal(expectedString, result);
}
}
48 changes: 48 additions & 0 deletions tests/AltaSoft.DomainPrimitives.Tests/GYearTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
namespace AltaSoft.DomainPrimitives.Tests;

/// <summary>
/// Contains unit tests for the GYear class.
/// </summary>
public class GYearTests
{
[Theory]
[InlineData(2022)]
[InlineData(2024)]
[InlineData(2023)]
public void Validate_ShouldNotThrowException_WhenValidDateOnly(int year)
{
// Arrange
var validDateOnly = new DateOnly(year, 1, 1);

// Act & Assert
var exception = Record.Exception(() => GYear.Validate(validDateOnly));
Assert.Null(exception);
}

[Fact]
public void Default_ShouldReturnDefaultDateOnly()
{
// Act
var defaultDateOnly = GYear.Default;

// Assert
Assert.Equal(default, defaultDateOnly);
}

[Theory]
[InlineData(2022, "2022")]
[InlineData(2024, "2024")]
[InlineData(2024, "2024")]

Check warning on line 35 in tests/AltaSoft.DomainPrimitives.Tests/GYearTests.cs

View workflow job for this annotation

GitHub Actions / build

Theory method 'ToString_ShouldReturnFormattedString' on test class 'GYearTests' has InlineData duplicate(s). Remove redundant attribute(s) from the theory method. (https://xunit.net/xunit.analyzers/rules/xUnit1025)

Check warning on line 35 in tests/AltaSoft.DomainPrimitives.Tests/GYearTests.cs

View workflow job for this annotation

GitHub Actions / build

Theory method 'ToString_ShouldReturnFormattedString' on test class 'GYearTests' has InlineData duplicate(s). Remove redundant attribute(s) from the theory method. (https://xunit.net/xunit.analyzers/rules/xUnit1025)
[InlineData(2023, "2023")]
public void ToString_ShouldReturnFormattedString(int year, string expectedString)
{
// Arrange
var dateOnly = new DateOnly(year, 1, 1);

// Act
var result = GYear.ToString(dateOnly);

// Assert
Assert.Equal(expectedString, result);
}
}
1 change: 1 addition & 0 deletions tests/AltaSoft.DomainPrimitives.Tests/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Xunit;
Loading

0 comments on commit facd0fc

Please sign in to comment.