Skip to content
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

add NSubstitute and remove Moq #39

Merged
merged 3 commits into from
Aug 9, 2023
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
6 changes: 6 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,10 @@
<PublicKey>00240000048000009400000006020000002400005253413100040000010001003d5c022c088a46d41d5a5bf7591f3a3dcba30f76b0f43a312b6e45bb419d32283175cbd8bfd83134b123da6db83479e50596fb6bbe0e8c6cef50c01c64a0861c963daaf6905920f44ffe1ce44b3cfcb9c23779f34bc90c7b04e74e36a19bb58af3a69456d49b56993969dba9f8e9e935c2757844a11066d1091477f10cd923b7</PublicKey>
</PropertyGroup>

<!-- Block Projects with Privacy/Security Concerns -->
<Target Name="CheckBlockedPackages" AfterTargets="ResolvePackageDependenciesForBuild">
<Error Code="420" Text="Blocked package dependency detected: %(PackageDependencies.Identity)"
Condition="'%(PackageDependencies.Identity)' == 'Devlooped.SponsorLink'" />
</Target>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,22 @@
using MyCSharp.HttpUserAgentParser.DependencyInjection;
using Xunit;

namespace MyCSharp.HttpUserAgentParser.AspNetCore.UnitTests.DependencyInjection
namespace MyCSharp.HttpUserAgentParser.AspNetCore.UnitTests.DependencyInjection;

public class HttpUserAgentParserDependencyInjectionOptionsExtensionsTests
{
public class HttpUserAgentParserDependencyInjectionOptionsExtensionsTests
[Fact]
public void AddHttpUserAgentParserAccessor()
{
[Fact]
public void AddHttpUserAgentParserAccessor()
{
ServiceCollection services = new();
HttpUserAgentParserDependencyInjectionOptions options = new(services);
ServiceCollection services = new();
HttpUserAgentParserDependencyInjectionOptions options = new(services);

options.AddHttpUserAgentParserAccessor();
options.AddHttpUserAgentParserAccessor();

services.Count.Should().Be(1);
services.Count.Should().Be(1);

services[0].ServiceType.Should().Be<IHttpUserAgentParserAccessor>();
services[0].ImplementationType.Should().Be<HttpUserAgentParserAccessor>();
services[0].Lifetime.Should().Be(ServiceLifetime.Singleton);
}
services[0].ServiceType.Should().Be<IHttpUserAgentParserAccessor>();
services[0].ImplementationType.Should().Be<HttpUserAgentParserAccessor>();
services[0].Lifetime.Should().Be(ServiceLifetime.Singleton);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,36 @@

using FluentAssertions;
using Microsoft.AspNetCore.Http;
using Moq;
using MyCSharp.HttpUserAgentParser.Providers;
using MyCSharp.HttpUserAgentParser.TestHelpers;
using NSubstitute;
using Xunit;

namespace MyCSharp.HttpUserAgentParser.AspNetCore.UnitTests
namespace MyCSharp.HttpUserAgentParser.AspNetCore.UnitTests;

public class HttpUserAgentParserAccessorTests
{
public class HttpUserAgentParserAccessorTests
private readonly IHttpUserAgentParserProvider _parserMock = Substitute.For<IHttpUserAgentParserProvider>();

[Theory]
[InlineData("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36 Edg/90.0.818.62")]
public void Get(string userAgent)
{
[Theory]
[InlineData("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36 Edg/90.0.818.62")]
public void Get(string userAgent)
{
HttpUserAgentInformation userAgentInformation = HttpUserAgentInformation.Parse(userAgent);

Mock<IHttpUserAgentParserProvider> parserMock = new();
{
parserMock.Setup(x => x.Parse(userAgent)).Returns(userAgentInformation);
}

HttpContext httpContext = HttpContextTestHelpers.GetHttpContext(userAgent);

HttpUserAgentParserAccessor accessor = new HttpUserAgentParserAccessor(parserMock.Object);
HttpUserAgentInformation? info = accessor.Get(httpContext);

info.Should().NotBeNull();
info.Should().Be(userAgentInformation);
parserMock.Verify(x => x.Parse(userAgent), Times.Once);
}
// arrange
HttpUserAgentInformation userAgentInformation = HttpUserAgentInformation.Parse(userAgent);
_parserMock.Parse(userAgent).Returns(userAgentInformation);

// act
HttpContext httpContext = HttpContextTestHelpers.GetHttpContext(userAgent);

HttpUserAgentParserAccessor accessor = new(_parserMock);
HttpUserAgentInformation? info = accessor.Get(httpContext);

// assert
info.Should().NotBeNull();
info.Should().Be(userAgentInformation);

// verify
_parserMock.Received(1).Parse(userAgent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<PackageReference Include="FluentAssertions" Version="6.7.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.0" />
<PackageReference Include="Moq" Version="4.18.2" />
<PackageReference Include="NSubstitute" Version="5.0.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.console" Version="2.4.2">
<PrivateAssets>all</PrivateAssets>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,24 @@
using MyCSharp.HttpUserAgentParser.Providers;
using Xunit;

namespace MyCSharp.HttpUserAgentParser.MemoryCache.UnitTests.DependencyInjection
namespace MyCSharp.HttpUserAgentParser.MemoryCache.UnitTests.DependencyInjection;

public class HttpUserAgentParserMemoryCacheServiceCollectionExtensionssTests
{
public class HttpUserAgentParserMemoryCacheServiceCollectionExtensionssTests
[Fact]
public void AddHttpUserAgentMemoryCachedParser()
{
[Fact]
public void AddHttpUserAgentMemoryCachedParser()
{
ServiceCollection services = new();
ServiceCollection services = new();

services.AddHttpUserAgentMemoryCachedParser();
services.AddHttpUserAgentMemoryCachedParser();

services.Count.Should().Be(2);
services.Count.Should().Be(2);

services[0].ImplementationInstance.Should().BeOfType<HttpUserAgentParserMemoryCachedProviderOptions>();
services[0].Lifetime.Should().Be(ServiceLifetime.Singleton);
services[0].ImplementationInstance.Should().BeOfType<HttpUserAgentParserMemoryCachedProviderOptions>();
services[0].Lifetime.Should().Be(ServiceLifetime.Singleton);

services[1].ServiceType.Should().Be<IHttpUserAgentParserProvider>();
services[1].ImplementationType.Should().Be<HttpUserAgentParserMemoryCachedProvider>();
services[1].Lifetime.Should().Be(ServiceLifetime.Singleton);
}
services[1].ServiceType.Should().Be<IHttpUserAgentParserProvider>();
services[1].ImplementationType.Should().Be<HttpUserAgentParserMemoryCachedProvider>();
services[1].Lifetime.Should().Be(ServiceLifetime.Singleton);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,50 @@
using Microsoft.Extensions.Caching.Memory;
using Xunit;

namespace MyCSharp.HttpUserAgentParser.MemoryCache.UnitTests
namespace MyCSharp.HttpUserAgentParser.MemoryCache.UnitTests;

public class HttpUserAgentParserMemoryCachedProviderOptionsTests
{
public class HttpUserAgentParserMemoryCachedProviderOptionsTests
[Fact]
public void Ctor()
{
[Fact]
public void Ctor()
{
MemoryCacheOptions cacheOptions = new();
MemoryCacheEntryOptions cacheEntryOptions = new();
MemoryCacheOptions cacheOptions = new();
MemoryCacheEntryOptions cacheEntryOptions = new();

HttpUserAgentParserMemoryCachedProviderOptions options = new(cacheOptions, cacheEntryOptions);
HttpUserAgentParserMemoryCachedProviderOptions options = new(cacheOptions, cacheEntryOptions);

options.CacheOptions.Should().Be(cacheOptions);
options.CacheEntryOptions.Should().Be(cacheEntryOptions);
}
options.CacheOptions.Should().Be(cacheOptions);
options.CacheEntryOptions.Should().Be(cacheEntryOptions);
}

[Fact]
public void Ctor_MemoryCacheOptions()
{
MemoryCacheOptions cacheOptions = new();
[Fact]
public void Ctor_MemoryCacheOptions()
{
MemoryCacheOptions cacheOptions = new();

HttpUserAgentParserMemoryCachedProviderOptions options = new(cacheOptions);
HttpUserAgentParserMemoryCachedProviderOptions options = new(cacheOptions);

options.CacheOptions.Should().Be(cacheOptions);
options.CacheEntryOptions.Should().NotBeNull();
}
options.CacheOptions.Should().Be(cacheOptions);
options.CacheEntryOptions.Should().NotBeNull();
}

[Fact]
public void Ctor_MemoryCacheEntryOptions()
{
MemoryCacheEntryOptions cacheEntryOptions = new();
[Fact]
public void Ctor_MemoryCacheEntryOptions()
{
MemoryCacheEntryOptions cacheEntryOptions = new();

HttpUserAgentParserMemoryCachedProviderOptions options = new(cacheEntryOptions);
HttpUserAgentParserMemoryCachedProviderOptions options = new(cacheEntryOptions);

options.CacheOptions.Should().NotBeNull();
options.CacheEntryOptions.Should().Be(cacheEntryOptions);
}
options.CacheOptions.Should().NotBeNull();
options.CacheEntryOptions.Should().Be(cacheEntryOptions);
}

[Fact]
public void Ctor_Empty()
{
HttpUserAgentParserMemoryCachedProviderOptions options = new();
[Fact]
public void Ctor_Empty()
{
HttpUserAgentParserMemoryCachedProviderOptions options = new();

options.CacheOptions.Should().NotBeNull();
options.CacheEntryOptions.Should().NotBeNull();
}
options.CacheOptions.Should().NotBeNull();
options.CacheEntryOptions.Should().NotBeNull();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,39 @@
using FluentAssertions;
using Xunit;

namespace MyCSharp.HttpUserAgentParser.MemoryCache.UnitTests
namespace MyCSharp.HttpUserAgentParser.MemoryCache.UnitTests;

public class HttpUserAgentParserMemoryCachedProviderTests
{
public class HttpUserAgentParserMemoryCachedProviderTests
[Fact]
public void Parse()
{
[Fact]
public void Parse()
{
HttpUserAgentParserMemoryCachedProviderOptions cachedProviderOptions = new();
HttpUserAgentParserMemoryCachedProvider provider = new(cachedProviderOptions);
HttpUserAgentParserMemoryCachedProviderOptions cachedProviderOptions = new();
HttpUserAgentParserMemoryCachedProvider provider = new(cachedProviderOptions);

// create first
string userAgentOne =
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36 Edg/90.0.818.62";
// create first
string userAgentOne =
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36 Edg/90.0.818.62";

HttpUserAgentInformation infoOne = provider.Parse(userAgentOne);
HttpUserAgentInformation infoOne = provider.Parse(userAgentOne);

infoOne.Name.Should().Be("Edge");
infoOne.Version.Should().Be("90.0.818.62");
infoOne.Name.Should().Be("Edge");
infoOne.Version.Should().Be("90.0.818.62");

// check duplicate
// check duplicate

HttpUserAgentInformation infoDuplicate = provider.Parse(userAgentOne);
HttpUserAgentInformation infoDuplicate = provider.Parse(userAgentOne);

infoDuplicate.Name.Should().Be("Edge");
infoDuplicate.Version.Should().Be("90.0.818.62");
infoDuplicate.Name.Should().Be("Edge");
infoDuplicate.Version.Should().Be("90.0.818.62");

// create second
// create second

string userAgentTwo = "Mozilla/5.0 (Android 4.4; Tablet; rv:41.0) Gecko/41.0 Firefox/41.0";
string userAgentTwo = "Mozilla/5.0 (Android 4.4; Tablet; rv:41.0) Gecko/41.0 Firefox/41.0";

HttpUserAgentInformation infoTwo = provider.Parse(userAgentTwo);
HttpUserAgentInformation infoTwo = provider.Parse(userAgentTwo);

infoTwo.Name.Should().Be("Firefox");
infoTwo.Version.Should().Be("41.0");
}
infoTwo.Name.Should().Be("Firefox");
infoTwo.Version.Should().Be("41.0");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.0" />
<PackageReference Include="Moq" Version="4.18.2" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.console" Version="2.4.2">
<PrivateAssets>all</PrivateAssets>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public static class HttpContextTestHelpers
{
public static HttpContext GetHttpContext(string userAgent)
{
DefaultHttpContext context = new DefaultHttpContext();
DefaultHttpContext context = new();
context.Request.Headers["User-Agent"] = userAgent;

return context;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,21 @@

using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using MyCSharp.HttpUserAgentParser.DependencyInjection;
using NSubstitute;
using Xunit;

namespace MyCSharp.HttpUserAgentParser.UnitTests.DependencyInjection
namespace MyCSharp.HttpUserAgentParser.UnitTests.DependencyInjection;

public class UserAgentParserDependencyInjectionOptionsTests
{
public class UserAgentParserDependencyInjectionOptionsTests
{
[Fact]
public void Ctor_Should_Set_Property()
{
Mock<IServiceCollection> scMock = new();
private readonly IServiceCollection scMock = Substitute.For<IServiceCollection>();

HttpUserAgentParserDependencyInjectionOptions options = new(scMock.Object);
[Fact]
public void Ctor_Should_Set_Property()
{
HttpUserAgentParserDependencyInjectionOptions options = new(scMock);

options.Services.Should().BeEquivalentTo(scMock.Object);
}
options.Services.Should().BeEquivalentTo(scMock);
}
}
Loading
Loading