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
61 changes: 61 additions & 0 deletions TestUtils.Tests/HttpTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using NUnit.Framework;
using StoneAge.TestUtils;
using System.Net.Http;
using System.Threading.Tasks;

namespace TestUtils.Tests;

public class HttpTests
{
[Test]
public void TestHttpClientBuilder_Create_ReturnsClientAndHandler()
{
// Arrange
var builder = new TestHttpClientBuilder();

// Act
var (client, handler) = builder.Create();

// Assert
Assert.That(client, Is.Not.Null);
Assert.That(handler, Is.Not.Null);
Assert.That(client.GetType(), Is.EqualTo(typeof(HttpClient)));
Assert.That(handler.GetType(), Is.EqualTo(typeof(TestHttpMessageHandler)));
}

[Test]
public async Task TestHttpClientBuilder_WithPayload_SetsPayload()
{
// Arrange
var expectedPayload = "{\"name\":\"test\"}";
var builder = new TestHttpClientBuilder();

// Act
var (client, handler) = builder.With_Payload(expectedPayload).Create();
var response = await client.GetAsync("https://test.com");
var actualPayload = await response.Content.ReadAsStringAsync();

// Assert
Assert.That(client, Is.Not.Null);
Assert.That(actualPayload, Is.EqualTo(expectedPayload));
}

[Test]
public async Task TestHttpMessageHandler_SendAsync_ReturnsResponseWithPayload()
{
// Arrange
var expectedPayload = "{\"name\":\"test\"}";
var builder = new TestHttpClientBuilder().With_Payload(expectedPayload);
var (client, handler) = builder.Create();

// Act
var response = await client.GetAsync("https://example.com");
var content = await response.Content.ReadAsStringAsync();

// Assert
Assert.That(response.IsSuccessStatusCode, Is.True);
Assert.That(content, Is.EqualTo(expectedPayload));
Assert.That(handler.RequestMessage, Is.Not.Null);
Assert.That(handler.RequestMessage.RequestUri?.Host, Is.EqualTo("example.com"));
}
}
56 changes: 56 additions & 0 deletions TestUtils.Tests/MockLoggerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using Microsoft.Extensions.Logging;
using NUnit.Framework;
using StoneAge.TestUtils;
using System.Collections.Generic;

namespace TestUtils.Tests;

public class MockLoggerTests
{
private MockLogger<MockLoggerTests> _logger;

[SetUp]
public void Setup()
{
_logger = new MockLogger<MockLoggerTests>();
}

[Test]
public void Log_WhenCalled_StoresLogEntry()
{
// Arrange
var logLevel = LogLevel.Information;
var message = "Test log message";

// Act
_logger.Log(logLevel, 0, message, null, (state, ex) => state.ToString());

// Assert
Assert.That(_logger.LogEntries.ContainsKey(logLevel), Is.True);
Assert.That(_logger.LogEntries[logLevel], Contains.Item(message));
}

[Test]
public void Fetch_Entries_For_WhenCalled_ReturnsEntriesForLevel()
{
// Arrange
var logLevel = LogLevel.Warning;
var message = "Warning message";
_logger.Log(logLevel, 0, message, null, (state, ex) => state.ToString());

// Act
List<string> entries = _logger.Fetch_Entries_For(logLevel);

// Assert
Assert.That(entries, Contains.Item(message));
}

[Test]
public void IsEnabled_Always_ReturnsTrue()
{
// Act & Assert
Assert.That(_logger.IsEnabled(LogLevel.Information), Is.True);
Assert.That(_logger.IsEnabled(LogLevel.Error), Is.True);
Assert.That(_logger.IsEnabled(LogLevel.Debug), Is.True);
}
}
65 changes: 65 additions & 0 deletions TestUtils.Tests/MockOptionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using NUnit.Framework;
using StoneAge.TestUtils;

namespace TestUtils.Tests;

public class MockOptionsTests
{
private class TestOptions
{
public string? Name { get; set; }
public int Value { get; set; }
}

[Test]
public void Value_WhenAccessed_ReturnsProvidedValue()
{
// Arrange
var expectedOptions = new TestOptions { Name = "Test", Value = 42 };
var mockOptions = new MockOptions<TestOptions>(expectedOptions);

// Act
var result = mockOptions.Value;

// Assert
Assert.That(result, Is.EqualTo(expectedOptions));
Assert.That(result.Name, Is.EqualTo("Test"));
Assert.That(result.Value, Is.EqualTo(42));
}
}

public class MockOptionsSnapshotTests
{
private class TestOptions
{
public string? Name { get; set; }
public int Value { get; set; }
}

[Test]
public void Value_WhenAccessed_ReturnsProvidedValue()
{
// Arrange
var expectedOptions = new TestOptions { Name = "Test", Value = 42 };
var mockOptionsSnapshot = new MockOptionsSnapshot<TestOptions>(expectedOptions);

// Act
var result = mockOptionsSnapshot.Value;

// Assert
Assert.That(result, Is.EqualTo(expectedOptions));
Assert.That(result.Name, Is.EqualTo("Test"));
Assert.That(result.Value, Is.EqualTo(42));
}

[Test]
public void Get_WhenCalled_ThrowsNotImplementedException()
{
// Arrange
var options = new TestOptions();
var mockOptionsSnapshot = new MockOptionsSnapshot<TestOptions>(options);

// Act & Assert
Assert.Throws<System.NotImplementedException>(() => mockOptionsSnapshot.Get("name"));
}
}
27 changes: 27 additions & 0 deletions TestUtils.Tests/TestUtils.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="NUnit" Version="4.2.2" />
<PackageReference Include="NUnit.Analyzers" Version="4.4.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
</ItemGroup>

<ItemGroup>
<Using Include="NUnit.Framework" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\TestUtils\StoneAge.TestUtils.csproj" />
</ItemGroup>

</Project>
76 changes: 51 additions & 25 deletions TestUtils.sln
Original file line number Diff line number Diff line change
@@ -1,25 +1,51 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29009.5
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StoneAge.TestUtils", "TestUtils\StoneAge.TestUtils.csproj", "{5F82CAB0-84CF-43F2-8572-27A74D56DF20}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{5F82CAB0-84CF-43F2-8572-27A74D56DF20}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5F82CAB0-84CF-43F2-8572-27A74D56DF20}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5F82CAB0-84CF-43F2-8572-27A74D56DF20}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5F82CAB0-84CF-43F2-8572-27A74D56DF20}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {D0B5268A-049B-4892-96B6-A6F45F80032A}
EndGlobalSection
EndGlobal

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29009.5
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StoneAge.TestUtils", "TestUtils\StoneAge.TestUtils.csproj", "{5F82CAB0-84CF-43F2-8572-27A74D56DF20}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestUtils.Tests", "TestUtils.Tests\TestUtils.Tests.csproj", "{99F70BC2-53DC-46CE-A496-545EFF81D144}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{5F82CAB0-84CF-43F2-8572-27A74D56DF20}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5F82CAB0-84CF-43F2-8572-27A74D56DF20}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5F82CAB0-84CF-43F2-8572-27A74D56DF20}.Debug|x64.ActiveCfg = Debug|Any CPU
{5F82CAB0-84CF-43F2-8572-27A74D56DF20}.Debug|x64.Build.0 = Debug|Any CPU
{5F82CAB0-84CF-43F2-8572-27A74D56DF20}.Debug|x86.ActiveCfg = Debug|Any CPU
{5F82CAB0-84CF-43F2-8572-27A74D56DF20}.Debug|x86.Build.0 = Debug|Any CPU
{5F82CAB0-84CF-43F2-8572-27A74D56DF20}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5F82CAB0-84CF-43F2-8572-27A74D56DF20}.Release|Any CPU.Build.0 = Release|Any CPU
{5F82CAB0-84CF-43F2-8572-27A74D56DF20}.Release|x64.ActiveCfg = Release|Any CPU
{5F82CAB0-84CF-43F2-8572-27A74D56DF20}.Release|x64.Build.0 = Release|Any CPU
{5F82CAB0-84CF-43F2-8572-27A74D56DF20}.Release|x86.ActiveCfg = Release|Any CPU
{5F82CAB0-84CF-43F2-8572-27A74D56DF20}.Release|x86.Build.0 = Release|Any CPU
{99F70BC2-53DC-46CE-A496-545EFF81D144}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{99F70BC2-53DC-46CE-A496-545EFF81D144}.Debug|Any CPU.Build.0 = Debug|Any CPU
{99F70BC2-53DC-46CE-A496-545EFF81D144}.Debug|x64.ActiveCfg = Debug|Any CPU
{99F70BC2-53DC-46CE-A496-545EFF81D144}.Debug|x64.Build.0 = Debug|Any CPU
{99F70BC2-53DC-46CE-A496-545EFF81D144}.Debug|x86.ActiveCfg = Debug|Any CPU
{99F70BC2-53DC-46CE-A496-545EFF81D144}.Debug|x86.Build.0 = Debug|Any CPU
{99F70BC2-53DC-46CE-A496-545EFF81D144}.Release|Any CPU.ActiveCfg = Release|Any CPU
{99F70BC2-53DC-46CE-A496-545EFF81D144}.Release|Any CPU.Build.0 = Release|Any CPU
{99F70BC2-53DC-46CE-A496-545EFF81D144}.Release|x64.ActiveCfg = Release|Any CPU
{99F70BC2-53DC-46CE-A496-545EFF81D144}.Release|x64.Build.0 = Release|Any CPU
{99F70BC2-53DC-46CE-A496-545EFF81D144}.Release|x86.ActiveCfg = Release|Any CPU
{99F70BC2-53DC-46CE-A496-545EFF81D144}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {D0B5268A-049B-4892-96B6-A6F45F80032A}
EndGlobalSection
EndGlobal