Skip to content
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 @@ -6,27 +6,24 @@
<Import Project="$(SolutionDir)\solution.targets"/>

<PropertyGroup>
<!-- xUnit does not support .NETStandardad, cf.: https://xunit.github.io/docs/why-no-netstandard -->
<!--<TargetFrameworks>net48;netstandard2.1;netcoreapp2.1;netcoreapp3.1</TargetFrameworks>-->
<!-- skip other targets for testing (since all tests same) -->
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<IsPackable>false</IsPackable>
<RootNamespace>FluentAssertions.Autofac</RootNamespace>

<CollectCoverage>true</CollectCoverage>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Autofac" Version="6.3.0"/>
<PackageReference Include="FluentAssertions" Version="6.5.0"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0"/>
<PackageReference Include="NSubstitute" Version="4.3.0"/>
<PackageReference Include="xunit" Version="2.4.1"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PackageReference Include="Autofac" Version="7.1.0"/>
<PackageReference Include="FluentAssertions" Version="8.10.0"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0"/>
<PackageReference Include="NSubstitute" Version="5.3.0"/>
<PackageReference Include="xunit" Version="2.9.3"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<PackageReference Include="coverlet.collector" Version="6.0.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
9 changes: 3 additions & 6 deletions FluentAssertions.Autofac/AutofacAssertionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Autofac;
using FluentAssertions.Execution;

namespace FluentAssertions.Autofac;

Expand All @@ -15,16 +16,12 @@ public static class AutofacAssertionExtensions
/// <see cref="IComponentContext" /> (e.g. <see cref="IContainer" /> or <see cref="ILifetimeScope" />).
/// </summary>
public static ContainerAssertions Should(this IComponentContext container)
{
return new ContainerAssertions(container);
}
=> new(container, AssertionChain.GetOrCreate());

/// <summary>
/// Returns an <see cref="BuilderAssertions" /> object that can be used to assert the current
/// <see cref="ContainerBuilder" />.
/// </summary>
public static BuilderAssertions Should(this ContainerBuilder builder)
{
return new BuilderAssertions(builder);
}
=> new(builder, AssertionChain.GetOrCreate());
}
11 changes: 5 additions & 6 deletions FluentAssertions.Autofac/BuilderAssertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ public class BuilderAssertions : ReferenceTypeAssertions<ContainerBuilder, Build
/// Initializes a new instance of the <see cref="BuilderAssertions" /> class.
/// </summary>
/// <param name="subject"></param>
/// <exception cref="ArgumentNullException"></exception>
public BuilderAssertions(ContainerBuilder subject) : base(subject)
/// <param name="assertionChain">The current <see cref="AssertionChain"/></param>
public BuilderAssertions(ContainerBuilder subject, AssertionChain assertionChain)
: base(subject, assertionChain)
{
}

Expand All @@ -44,9 +45,7 @@ public BuilderAssertions(ContainerBuilder subject) : base(subject)
/// </summary>
/// <typeparam name="TModule">The module type</typeparam>
public void RegisterModule<TModule>() where TModule : Module, new()
{
RegisterModule(typeof(TModule));
}
=> RegisterModule(typeof(TModule));

/// <summary>
/// Asserts that the specified module type has been registered on the current <see cref="ContainerBuilder" />.
Expand All @@ -56,7 +55,7 @@ public void RegisterModule(Type moduleType)
{
EnsureVisited();
var module = _modules.FirstOrDefault(m => m.GetType() == moduleType);
Execute.Assertion
CurrentAssertionChain
.ForCondition(module != null)
.FailWith($"Module '{moduleType}' should be registered but it was not.");
}
Expand Down
31 changes: 11 additions & 20 deletions FluentAssertions.Autofac/ContainerAssertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Reflection;
using Autofac;
using Autofac.Util;
using FluentAssertions.Execution;
using FluentAssertions.Primitives;

namespace FluentAssertions.Autofac;
Expand All @@ -29,7 +30,9 @@ public class ContainerAssertions : ReferenceTypeAssertions<IComponentContext, Co
/// Initializes a new instance of the <see cref="ContainerAssertions" /> class.
/// </summary>
/// <param name="container">The subject</param>
public ContainerAssertions(IComponentContext container) : base(container)
/// <param name="assertionChain">The current <see cref="AssertionChain"/></param>
public ContainerAssertions(IComponentContext container, AssertionChain assertionChain)
: base(container, assertionChain)
{
}

Expand All @@ -38,45 +41,35 @@ public ContainerAssertions(IComponentContext container) : base(container)
/// <see cref="IComponentContext" />.
/// </summary>
public ContainerRegistrationAssertions Have()
{
return new ContainerRegistrationAssertions(Subject);
}
=> new(Subject, CurrentAssertionChain);

/// <summary>
/// Returns an <see cref="ResolveAssertions" /> object that can be used to assert the current
/// <see paramref="serviceType" />.
/// </summary>
public ResolveAssertions Resolve(Type serviceType)
{
return new ResolveAssertions(Subject, serviceType);
}
=> new(Subject, serviceType, CurrentAssertionChain);

/// <summary>
/// Returns an <see cref="ResolveAssertions" /> object that can be used to assert the current
/// <see typeparamref="TService" />.
/// </summary>
public ResolveAssertions Resolve<TService>()
{
return Resolve(typeof(TService));
}
=> Resolve(typeof(TService));

/// <summary>
/// Asserts the specified type has been registered with auto activation on the current <see cref="IComponentContext" />
/// .
/// </summary>
public void AutoActivate(Type type)
{
Subject.AssertAutoActivates(type);
}
=> Subject.AssertAutoActivates(type);

/// <summary>
/// Asserts the specified type has been registered with auto activation on the current <see cref="IComponentContext" />
/// .
/// </summary>
public void AutoActivate<TService>()
{
AutoActivate(typeof(TService));
}
=> AutoActivate(typeof(TService));

/// <summary>
/// Returns an <see cref="TypeScanningAssertions" /> object that can be used to assert registered types on the current
Expand All @@ -86,7 +79,7 @@ public void AutoActivate<TService>()
public TypeScanningAssertions RegisterAssemblyTypes(params Assembly[] assemblies)
{
var types = assemblies.SelectMany(assembly => assembly.GetLoadableTypes());
return new TypeScanningAssertions(Subject, types);
return new TypeScanningAssertions(Subject, types, CurrentAssertionChain);
}

/// <summary>
Expand All @@ -95,7 +88,5 @@ public TypeScanningAssertions RegisterAssemblyTypes(params Assembly[] assemblies
/// </summary>
/// <param name="types"></param>
public TypeScanningAssertions RegisterTypes(IEnumerable<Type> types)
{
return new TypeScanningAssertions(Subject, types);
}
=> new(Subject, types, CurrentAssertionChain);
}
63 changes: 20 additions & 43 deletions FluentAssertions.Autofac/ContainerRegistrationAssertions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Diagnostics.CodeAnalysis;
using Autofac;
using FluentAssertions.Execution;
using FluentAssertions.Primitives;

namespace FluentAssertions.Autofac;
Expand All @@ -13,8 +14,8 @@ namespace FluentAssertions.Autofac;
#if !DEBUG
[System.Diagnostics.DebuggerNonUserCode]
#endif
public class
ContainerRegistrationAssertions : ReferenceTypeAssertions<IComponentContext, ContainerRegistrationAssertions>
public class ContainerRegistrationAssertions
: ReferenceTypeAssertions<IComponentContext, ContainerRegistrationAssertions>
{
/// <inheritdoc />
/// <summary>
Expand All @@ -27,7 +28,9 @@ public class
/// Initializes a new instance of the <see cref="ContainerRegistrationAssertions" /> class.
/// </summary>
/// <param name="subject">The subject</param>
public ContainerRegistrationAssertions(IComponentContext subject) : base(subject)
/// <param name="assertionChain">The current <see cref="AssertionChain"/></param>
public ContainerRegistrationAssertions(IComponentContext subject, AssertionChain assertionChain)
: base(subject, assertionChain)
{
}

Expand All @@ -36,105 +39,79 @@ public ContainerRegistrationAssertions(IComponentContext subject) : base(subject
/// <see cref="IComponentContext" /> and <see typeparamref="TService" />.
/// </summary>
public RegisterAssertions Registered<TService>()
{
return new RegisterAssertions(Subject, typeof(TService));
}
=> new(Subject, typeof(TService), CurrentAssertionChain);

/// <summary>
/// Returns an <see cref="RegisterAssertions" /> object that can be used to assert the current
/// <see cref="IComponentContext" /> and the specified type.
/// </summary>
public RegisterAssertions Registered(Type type)
{
return new RegisterAssertions(Subject, type);
}
=> new(Subject, type, CurrentAssertionChain);

/// <summary>
/// Returns an <see cref="RegisterAssertions" /> object that can be used to assert the current
/// <see cref="IComponentContext" /> and the specified instance.
/// </summary>
public RegisterAssertions Registered(object instance)
{
if (instance == null)
throw new ArgumentNullException(nameof(instance));

return new RegisterAssertions(Subject, instance.GetType());
ArgumentNullException.ThrowIfNull(instance);
return new RegisterAssertions(Subject, instance.GetType(), CurrentAssertionChain);
}

/// <summary>
/// Asserts that the specified <see typeparamref="TService" /> has not been registered on the current
/// <see cref="IComponentContext" />.
/// </summary>
/// <typeparam name="TService">The service type</typeparam>
public void NotRegistered<TService>()
{
NotRegistered(typeof(TService));
}
public void NotRegistered<TService>() => NotRegistered(typeof(TService));

/// <summary>
/// Asserts that the specified service type has not been registered on the current <see cref="IComponentContext" />.
/// </summary>
/// <param name="type">The service type</param>
public void NotRegistered(Type type)
{
Subject.IsRegistered(type).Should().BeFalse($"Type '{type}' should not be registered");
}
=> Subject.IsRegistered(type).Should().BeFalse($"Type '{type}' should not be registered");

/// <summary>
/// Asserts that the specified <see typeparamref="TService" /> has not been registered on the current
/// <see cref="IComponentContext" /> with the specified name.
/// </summary>
/// <param name="serviceName">The service name</param>
/// <typeparam name="TService">The service type</typeparam>
public void NotRegistered<TService>(string serviceName)
{
NotRegistered(serviceName, typeof(TService));
}
public void NotRegistered<TService>(string serviceName) => NotRegistered(serviceName, typeof(TService));

/// <summary>
/// Asserts that the specified service type has not been registered on the current <see cref="IComponentContext" />
/// with the
/// specified name.
/// with the specified name.
/// </summary>
/// <param name="serviceName">The service name</param>
/// <param name="type">The service type</param>
public void NotRegistered(string serviceName, Type type)
{
Subject.IsRegisteredWithName(serviceName, type).Should()
=> Subject.IsRegisteredWithName(serviceName, type).Should()
.BeFalse($"Type '{type}' should not be registered with name '{serviceName}'");
}

/// <summary>
/// Asserts that the specified service type has not been registered on the current <see cref="IComponentContext" />
/// with the
/// specified key.
/// with the specified key.
/// </summary>
/// <param name="serviceKey">The service key</param>
/// <typeparam name="TService">The service type</typeparam>
public void NotRegistered<TService>(object serviceKey)
{
NotRegistered(serviceKey, typeof(TService));
}
public void NotRegistered<TService>(object serviceKey) => NotRegistered(serviceKey, typeof(TService));

/// <summary>
/// Asserts that the specified service type has not been registered on the current <see cref="IComponentContext" />
/// with the
/// specified key.
/// with the specified key.
/// </summary>
/// <param name="serviceKey">The service key</param>
/// <param name="type">The service type</param>
public void NotRegistered(object serviceKey, Type type)
{
Subject.IsRegisteredWithKey(serviceKey, type).Should()
=> Subject.IsRegisteredWithKey(serviceKey, type).Should()
.BeFalse($"Type '{type}' should not be registered with key '{serviceKey}'");
}

/// <summary>
/// Returns an <see cref="RegisterGenericSourceAssertions" /> object that can be used to assert the current
/// <see cref="IComponentContext" /> and the specified generic type.
/// </summary>
public RegisterGenericSourceAssertions RegisteredGeneric(Type genericComponentTypeDefinition)
{
return new RegisterGenericSourceAssertions(Subject, genericComponentTypeDefinition);
}
=> new(Subject, genericComponentTypeDefinition, CurrentAssertionChain);
}
12 changes: 4 additions & 8 deletions FluentAssertions.Autofac/FluentAssertions.Autofac.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,15 @@
<Import Project="$(SolutionDir)\solution.targets"/>

<PropertyGroup>
<!-- https://docs.microsoft.com/en-us/dotnet/standard/frameworks#latest-target-framework-versions -->
<TargetFrameworks>net6.0;net5.0;netcoreapp3.1;net48;net472;netstandard2.0;netstandard2.1</TargetFrameworks>
<!-- Match FluentAssertions 8.x supported target frameworks -->
<TargetFrameworks>net47;net6.0;netstandard2.0;netstandard2.1</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<!-- Mark the project as being a test project -->
<SonarQubeTestProject>false</SonarQubeTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Autofac" Version="6.3.0"/>
<PackageReference Include="FluentAssertions" Version="6.5.0"/>
<PackageReference Include="GitVersion.MsBuild" Version="5.8.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Autofac" Version="7.1.0"/>
<PackageReference Include="FluentAssertions" Version="8.10.0"/>
</ItemGroup>
</Project>
14 changes: 6 additions & 8 deletions FluentAssertions.Autofac/RegisterAssertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Reflection;
using Autofac;
using FluentAssertions.Execution;

namespace FluentAssertions.Autofac;

Expand All @@ -19,7 +20,9 @@ public class RegisterAssertions : RegistrationAssertions
/// </summary>
/// <param name="subject">The container</param>
/// <param name="type">The type that should be registered on the container</param>
public RegisterAssertions(IComponentContext subject, Type type) : base(subject, type)
/// <param name="assertionChain">The current <see cref="AssertionChain"/></param>
public RegisterAssertions(IComponentContext subject, Type type, AssertionChain assertionChain)
: base(subject, type, assertionChain)
{
}

Expand Down Expand Up @@ -50,10 +53,7 @@ public RegisterAssertions As(Type type, params Type[] types)
/// <summary>
/// Asserts that the registered service type can be resolved from the current <see cref="IComponentContext" />.
/// </summary>
public RegisterAssertions AsSelf()
{
return As(Type);
}
public RegisterAssertions AsSelf() => As(Type);

/// <summary>
/// Asserts that all implemented interfaces of the registered service type can be resolved from the current
Expand All @@ -66,9 +66,7 @@ public RegisterAssertions AsImplementedInterfaces()
}

private void AssertResolveAs(Type serviceType)
{
new ResolveAssertions(Subject, serviceType).As(Type);
}
=> new ResolveAssertions(Subject, serviceType, CurrentAssertionChain).As(Type);

private static List<Type> GetImplementedInterfaces(Type type)
{
Expand Down
Loading
Loading