Skip to content

Commit

Permalink
move SubjectFixture to common lib and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Confusingboat committed Sep 28, 2024
1 parent c3c1886 commit 4bee4de
Show file tree
Hide file tree
Showing 11 changed files with 129 additions and 6 deletions.
7 changes: 7 additions & 0 deletions Ephemerally.sln
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".github", ".github", "{EBD6
.github\workflows\tag.yml = .github\workflows\tag.yml
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ephemerally.Xunit", "src\Ephemerally.Xunit\Ephemerally.Xunit.csproj", "{BF2CF962-181D-4A63-B61E-32F3130B84DF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -74,6 +76,10 @@ Global
{5A30E9EA-AB80-4E08-A81F-99CE329B34B8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5A30E9EA-AB80-4E08-A81F-99CE329B34B8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5A30E9EA-AB80-4E08-A81F-99CE329B34B8}.Release|Any CPU.Build.0 = Release|Any CPU
{BF2CF962-181D-4A63-B61E-32F3130B84DF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BF2CF962-181D-4A63-B61E-32F3130B84DF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BF2CF962-181D-4A63-B61E-32F3130B84DF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BF2CF962-181D-4A63-B61E-32F3130B84DF}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -88,6 +94,7 @@ Global
{7E763132-0532-4689-A738-7CB71635AC63} = {F9102409-3748-4DA6-9D7B-784339508244}
{E186436A-12C9-4140-A279-19227CFF8533} = {AF30F58C-5E9B-4406-9E13-114C46F4E410}
{5A30E9EA-AB80-4E08-A81F-99CE329B34B8} = {F9102409-3748-4DA6-9D7B-784339508244}
{BF2CF962-181D-4A63-B61E-32F3130B84DF} = {F9102409-3748-4DA6-9D7B-784339508244}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {5690E595-5302-46D7-AA86-89D55117B068}
Expand Down
2 changes: 2 additions & 0 deletions src/Ephemerally.Azure.Cosmos.Xunit/ConsumingSubjectFixture.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Ephemerally.Xunit;

namespace Ephemerally.Azure.Cosmos.Xunit;

public abstract class ConsumingSubjectFixture<TSubject> : SubjectFixture<TSubject>
Expand Down
1 change: 1 addition & 0 deletions src/Ephemerally.Azure.Cosmos.Xunit/CosmosClientFixture.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.Azure.Cosmos;
using System.Diagnostics.CodeAnalysis;
using Ephemerally.Xunit;
using Xunit;

namespace Ephemerally.Azure.Cosmos.Xunit;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics.CodeAnalysis;
using Ephemerally.Xunit;
using Microsoft.Azure.Cosmos;

namespace Ephemerally.Azure.Cosmos.Xunit;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics.CodeAnalysis;
using Ephemerally.Xunit;
using Microsoft.Azure.Cosmos;

namespace Ephemerally.Azure.Cosmos.Xunit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

<ItemGroup>
<ProjectReference Include="..\Ephemerally.Azure.Cosmos\Ephemerally.Azure.Cosmos.csproj" />
<ProjectReference Include="..\Ephemerally.Xunit\Ephemerally.Xunit.csproj" />
</ItemGroup>

</Project>
16 changes: 16 additions & 0 deletions src/Ephemerally.Xunit/Ephemerally.Xunit.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>
<LangVersion>latest</LangVersion>
<IsTestProject>false</IsTestProject>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Xunit" Version="2.4.2" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System.Diagnostics.CodeAnalysis;
using Xunit;

namespace Ephemerally.Azure.Cosmos.Xunit;
namespace Ephemerally.Xunit;

public interface ISubjectFixture;
public interface ISubjectFixture : IAsyncLifetime;

public interface ISubjectFixture<TSubject> : ISubjectFixture
{
Expand All @@ -13,8 +13,7 @@ public interface ISubjectFixture<TSubject> : ISubjectFixture
[SuppressMessage("ReSharper", "UseConfigureAwaitFalse")]
public abstract class SubjectFixture<TSubject> :
ISubjectFixture<TSubject>,
IAsyncDisposable,
IAsyncLifetime
IAsyncDisposable
{
private readonly Lazy<Task<TSubject>> _subject;

Expand All @@ -28,11 +27,11 @@ public abstract class SubjectFixture<TSubject> :

protected abstract Task<TSubject> CreateSubjectAsync();

protected abstract Task DisposeSubjectAsync();
protected virtual Task DisposeSubjectAsync() => Task.CompletedTask;

public Task InitializeAsync() => _subject.Value;

public virtual async Task DisposeAsync()
public async Task DisposeAsync()
{
if (!_subject.IsValueCreated) return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<PackageReference Include="NUnit3TestAdapter" Version="4.4.2" />
<PackageReference Include="NUnit.Analyzers" Version="3.6.1" />
<PackageReference Include="coverlet.collector" Version="3.2.0" />
<PackageReference Include="Shouldly" Version="4.2.1" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 2 additions & 0 deletions tests/Ephemerally.Tests/Ephemerally.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
<PackageReference Include="NUnit3TestAdapter" Version="4.4.2" />
<PackageReference Include="NUnit.Analyzers" Version="3.6.1" />
<PackageReference Include="coverlet.collector" Version="3.2.0" />
<PackageReference Include="Shouldly" Version="4.2.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Ephemerally.Redis.Xunit\Ephemerally.Redis.Xunit.csproj" />
<ProjectReference Include="..\..\src\Ephemerally.Xunit\Ephemerally.Xunit.csproj" />
<ProjectReference Include="..\..\src\Ephemerally\Ephemerally.csproj" />
</ItemGroup>

Expand Down
92 changes: 92 additions & 0 deletions tests/Ephemerally.Tests/Fixtures/SubjectFixtureTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using Ephemerally.Xunit;
using Shouldly;

namespace Ephemerally.Tests.Fixtures;

public class SubjectFixtureTests
{
[Test]
public void Fixture_should_be_constructed_as_uninitialized()
{
var sut = new TestSubjectFixture();
sut.State.Value.ShouldBe(State.Uninitialized);
}

[Test]
public async Task InitializeAsync_should_initialize_fixture()
{
var sut = new TestSubjectFixture();

await sut.InitializeAsync();

sut.State.Value.ShouldBe(State.Initialized);
}

[Test]
public async Task DisposeAsync_on_initialized_fixture_should_dispose_fixture()
{
var sut = new TestSubjectFixture();
await sut.InitializeAsync();

await sut.DisposeAsync();

sut.State.Value.ShouldBe(State.Disposed);
}

[Test]
public async Task DisposeAsync_on_uninitialized_fixture_should_be_uninitialized()
{
var sut = new TestSubjectFixture();

await sut.DisposeAsync();

sut.State.Value.ShouldBe(State.Uninitialized);
}

[Test]
public async Task GetOrCreateSubjectAsync_should_always_return_same_instance()
{
var sut = new ObjectSubjectFixture();
await sut.InitializeAsync();

var gettingRef1 = sut.GetOrCreateSubjectAsync();
var gettingRef2 = sut.GetOrCreateSubjectAsync();
var refs = await Task.WhenAll(gettingRef1, gettingRef2);

refs[1].ShouldBeSameAs(refs[0]);
}
}

file class ObjectSubjectFixture : SubjectFixture<object>
{
protected override Task<object> CreateSubjectAsync() => Task.FromResult(new object());
}

file class TestSubjectFixture : SubjectFixture<StateWrapper>
{
public StateWrapper State { get; } = new();

protected override Task<StateWrapper> CreateSubjectAsync()
{
State.Value = Fixtures.State.Initialized;
return Task.FromResult(State);
}

protected override Task DisposeSubjectAsync()
{
State.Value = Fixtures.State.Disposed;
return Task.CompletedTask;
}
}

internal class StateWrapper
{
public State Value { get; set; } = State.Uninitialized;
}

internal enum State
{
Uninitialized = 0,
Initialized = 1,
Disposed = 2
}

0 comments on commit 4bee4de

Please sign in to comment.