Skip to content

Migrate from FluentAssertions to Shouldly #666

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="FluentAssertions" Version="7.0.0" />
<PackageReference Include="Shouldly" Version="4.3.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="ObjectLayoutInspector" Version="0.1.4" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Xunit.SkippableFact" Version="1.4.13" />
<PackageReference Include="Xunit.SkippableFact" Version="1.5.23" />
</ItemGroup>

<ItemGroup>
Expand Down
16 changes: 7 additions & 9 deletions BitFaster.Caching.UnitTests.Std/DurationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
using System.Diagnostics;
using System.Runtime.InteropServices;
using BitFaster.Caching.Lru;
using FluentAssertions;
using Xunit;
using Xunit.Abstractions;
using Shouldly;

namespace BitFaster.Caching.UnitTests.Std
{
Expand All @@ -25,13 +25,11 @@ public void SinceEpoch()
// On .NET Standard, only windows uses TickCount64
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Duration.SinceEpoch().raw.Should().BeCloseTo(Duration.GetTickCount64(), 15);
Duration.SinceEpoch().raw.ShouldBe(Duration.GetTickCount64());
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per shouldly/shouldly#274, looks like you can give the tolerance like this:

Duration.SinceEpoch().raw.ShouldBe(Duration.GetTickCount64(), 15);

Not sure if that applies to all data types or just DateTime.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, it only applies to DateTime and TimeSpan. Although there is ShouldBeInRange. Another solution could be to use TimeSpan.FromTicks

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A little bloated sadly but you can do this:

TimeSpan.FromTicks(Duration.SinceEpoch().raw).ShouldBe(TimeSpan.FromTicks(Stopwatch.GetTimestamp()), TimeSpan.FromTicks(15));

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I fixed it.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like your current commit fails to build.

The convoluted version converting to TimeSpan is a bit of a mess - will need to think on how that might be cleaned up.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extension method maybe? I thought you wouldn't want to bloat the project with extension methods.

}
else
{
// eps is 1/200 of a second
ulong eps = (ulong)(Stopwatch.Frequency / 200);
Duration.SinceEpoch().raw.Should().BeCloseTo(Stopwatch.GetTimestamp(), eps);
Duration.SinceEpoch().raw.ShouldBe(Stopwatch.GetTimestamp());
}
}

Expand All @@ -41,12 +39,12 @@ public void ToTimeSpan()
// On .NET Standard, only windows uses TickCount64
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
new Duration(1000).ToTimeSpan().Should().BeCloseTo(TimeSpan.FromMilliseconds(1000), TimeSpan.FromMilliseconds(10));
new Duration(1000).ToTimeSpan().ShouldBe(TimeSpan.FromMilliseconds(1000), TimeSpan.FromMilliseconds(10));
}
else
{
// for Stopwatch.GetTimestamp() this is number of ticks
new Duration(1 * Stopwatch.Frequency).ToTimeSpan().Should().BeCloseTo(TimeSpan.FromSeconds(1), TimeSpan.FromMilliseconds(10));
new Duration(1 * Stopwatch.Frequency).ToTimeSpan().ShouldBe(TimeSpan.FromSeconds(1), TimeSpan.FromMilliseconds(10));
}
}

Expand All @@ -57,12 +55,12 @@ public void FromTimeSpan()
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Duration.FromTimeSpan(TimeSpan.FromSeconds(1)).raw
.Should().Be((long)TimeSpan.FromSeconds(1).TotalMilliseconds);
.ShouldBe((long)TimeSpan.FromSeconds(1).TotalMilliseconds);
}
else
{
Duration.FromTimeSpan(TimeSpan.FromSeconds(1)).raw
.Should().Be(Stopwatch.Frequency);
.ShouldBe(Stopwatch.Frequency);
}
}

Expand Down
48 changes: 24 additions & 24 deletions BitFaster.Caching.UnitTests/Atomic/AsyncAtomicFactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Threading;
using System.Threading.Tasks;
using BitFaster.Caching.Atomic;
using FluentAssertions;
using Shouldly;
using Xunit;

namespace BitFaster.Caching.UnitTests.Atomic
Expand All @@ -14,53 +14,53 @@ public void DefaultCtorValueIsNotCreated()
{
var a = new AsyncAtomicFactory<int, int>();

a.IsValueCreated.Should().BeFalse();
a.ValueIfCreated.Should().Be(0);
a.IsValueCreated.ShouldBeFalse();
a.ValueIfCreated.ShouldBe(0);
}

[Fact]
public void WhenValuePassedToCtorValueIsStored()
{
var a = new AsyncAtomicFactory<int, int>(1);

a.ValueIfCreated.Should().Be(1);
a.IsValueCreated.Should().BeTrue();
a.ValueIfCreated.ShouldBe(1);
a.IsValueCreated.ShouldBeTrue();
}

[Fact]
public async Task WhenValueCreatedValueReturned()
{
var a = new AsyncAtomicFactory<int, int>();
(await a.GetValueAsync(1, k => Task.FromResult(2))).Should().Be(2);
(await a.GetValueAsync(1, k => Task.FromResult(2))).ShouldBe(2);

a.ValueIfCreated.Should().Be(2);
a.IsValueCreated.Should().BeTrue();
a.ValueIfCreated.ShouldBe(2);
a.IsValueCreated.ShouldBeTrue();
}

[Fact]
public async Task WhenValueCreatedWithArgValueReturned()
{
var a = new AsyncAtomicFactory<int, int>();
(await a.GetValueAsync(1, (k, a) => Task.FromResult(k + a), 7)).Should().Be(8);
(await a.GetValueAsync(1, (k, a) => Task.FromResult(k + a), 7)).ShouldBe(8);

a.ValueIfCreated.Should().Be(8);
a.IsValueCreated.Should().BeTrue();
a.ValueIfCreated.ShouldBe(8);
a.IsValueCreated.ShouldBeTrue();
}

[Fact]
public async Task WhenValueCreatedGetValueReturnsOriginalValue()
{
var a = new AsyncAtomicFactory<int, int>();
await a.GetValueAsync(1, k => Task.FromResult(2));
(await a.GetValueAsync(1, k => Task.FromResult(3))).Should().Be(2);
(await a.GetValueAsync(1, k => Task.FromResult(3))).ShouldBe(2);
}

[Fact]
public async Task WhenValueCreatedArgGetValueReturnsOriginalValue()
{
var a = new AsyncAtomicFactory<int, int>();
await a.GetValueAsync(1, (k, a) => Task.FromResult(k + a), 7);
(await a.GetValueAsync(1, (k, a) => Task.FromResult(k + a), 9)).Should().Be(8);
(await a.GetValueAsync(1, (k, a) => Task.FromResult(k + a), 9)).ShouldBe(8);
}

[Fact]
Expand All @@ -70,9 +70,9 @@ public async Task WhenValueCreateThrowsValueIsNotStored()

Func<Task> getOrAdd = async () => { await a.GetValueAsync(1, k => throw new ArithmeticException()); };

await getOrAdd.Should().ThrowAsync<ArithmeticException>();
var ex = await getOrAdd.ShouldThrowAsync<ArithmeticException>();;

(await a.GetValueAsync(1, k => Task.FromResult(3))).Should().Be(3);
(await a.GetValueAsync(1, k => Task.FromResult(3))).ShouldBe(3);
}

[Fact]
Expand Down Expand Up @@ -108,26 +108,26 @@ public async Task WhenCallersRunConcurrentlyResultIsFromWinner()
await enter.Task;
resume.SetResult(true);

(await first).Should().Be(result);
(await second).Should().Be(result);
(await first).ShouldBe(result);
(await second).ShouldBe(result);

winnerCount.Should().Be(1);
winnerCount.ShouldBe(1);
}

[Fact]
public void WhenValueNotCreatedHashCodeIsZero()
{
new AsyncAtomicFactory<int, int>()
.GetHashCode()
.Should().Be(0);
.ShouldBe(0);
}

[Fact]
public void WhenValueCreatedHashCodeIsValueHashCode()
{
new AsyncAtomicFactory<int, int>(1)
.GetHashCode()
.Should().Be(1);
.ShouldBe(1);
}

[Fact]
Expand All @@ -136,7 +136,7 @@ public void WhenValueNotCreatedEqualsFalse()
var a = new AsyncAtomicFactory<int, int>();
var b = new AsyncAtomicFactory<int, int>();

a.Equals(b).Should().BeFalse();
a.Equals(b).ShouldBeFalse();
}

[Fact]
Expand All @@ -145,15 +145,15 @@ public void WhenOtherValueNotCreatedEqualsFalse()
var a = new AsyncAtomicFactory<int, int>(1);
var b = new AsyncAtomicFactory<int, int>();

a.Equals(b).Should().BeFalse();
a.Equals(b).ShouldBeFalse();
}

[Fact]
public void WhenArgNullEqualsFalse()
{
new AsyncAtomicFactory<int, int>(1)
.Equals(null)
.Should().BeFalse();
.ShouldBeFalse();
}

[Fact]
Expand All @@ -163,7 +163,7 @@ public void WhenArgObjectValuesAreSameEqualsTrue()

new AsyncAtomicFactory<int, int>(1)
.Equals(other)
.Should().BeTrue();
.ShouldBeTrue();
}
}
}
Loading
Loading