Skip to content

Commit

Permalink
Started splitting tests up between API/Spec and internals/unit.
Browse files Browse the repository at this point in the history
  • Loading branch information
tillig committed Mar 8, 2019
1 parent 8ea04f0 commit 0af5802
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 65 deletions.
19 changes: 19 additions & 0 deletions Autofac.sln
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Autofac.Benchmarks", "bench
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Autofac.Test.Scenarios.ScannedAssembly", "test\Autofac.Test.Scenarios.ScannedAssembly\Autofac.Test.Scenarios.ScannedAssembly.csproj", "{49FB428A-28D6-4A60-AF29-B9D5F7552041}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Autofac.Specification.Test", "test\Autofac.Specification.Test\Autofac.Specification.Test.csproj", "{39610DBF-4CAF-45FD-8B9F-D8928D76066A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -111,6 +113,22 @@ Global
{49FB428A-28D6-4A60-AF29-B9D5F7552041}.Release|x64.Build.0 = Release|Any CPU
{49FB428A-28D6-4A60-AF29-B9D5F7552041}.Release|x86.ActiveCfg = Release|Any CPU
{49FB428A-28D6-4A60-AF29-B9D5F7552041}.Release|x86.Build.0 = Release|Any CPU
{39610DBF-4CAF-45FD-8B9F-D8928D76066A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{39610DBF-4CAF-45FD-8B9F-D8928D76066A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{39610DBF-4CAF-45FD-8B9F-D8928D76066A}.Debug|ARM.ActiveCfg = Debug|Any CPU
{39610DBF-4CAF-45FD-8B9F-D8928D76066A}.Debug|ARM.Build.0 = Debug|Any CPU
{39610DBF-4CAF-45FD-8B9F-D8928D76066A}.Debug|x64.ActiveCfg = Debug|Any CPU
{39610DBF-4CAF-45FD-8B9F-D8928D76066A}.Debug|x64.Build.0 = Debug|Any CPU
{39610DBF-4CAF-45FD-8B9F-D8928D76066A}.Debug|x86.ActiveCfg = Debug|Any CPU
{39610DBF-4CAF-45FD-8B9F-D8928D76066A}.Debug|x86.Build.0 = Debug|Any CPU
{39610DBF-4CAF-45FD-8B9F-D8928D76066A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{39610DBF-4CAF-45FD-8B9F-D8928D76066A}.Release|Any CPU.Build.0 = Release|Any CPU
{39610DBF-4CAF-45FD-8B9F-D8928D76066A}.Release|ARM.ActiveCfg = Release|Any CPU
{39610DBF-4CAF-45FD-8B9F-D8928D76066A}.Release|ARM.Build.0 = Release|Any CPU
{39610DBF-4CAF-45FD-8B9F-D8928D76066A}.Release|x64.ActiveCfg = Release|Any CPU
{39610DBF-4CAF-45FD-8B9F-D8928D76066A}.Release|x64.Build.0 = Release|Any CPU
{39610DBF-4CAF-45FD-8B9F-D8928D76066A}.Release|x86.ActiveCfg = Release|Any CPU
{39610DBF-4CAF-45FD-8B9F-D8928D76066A}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -120,6 +138,7 @@ Global
{48EC73A9-9AA0-4063-9FEE-AE5BB43C66B0} = {DEA4A8C6-DE56-4359-A87C-472FB34132E7}
{D7A37F73-BBA3-4DAE-9648-1A753A86F968} = {48F40A36-C829-4895-99B3-1634CC6594E0}
{49FB428A-28D6-4A60-AF29-B9D5F7552041} = {DEA4A8C6-DE56-4359-A87C-472FB34132E7}
{39610DBF-4CAF-45FD-8B9F-D8928D76066A} = {DEA4A8C6-DE56-4359-A87C-472FB34132E7}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {2D16574C-61CB-4568-8490-AC9B85A721C0}
Expand Down
38 changes: 38 additions & 0 deletions test/Autofac.Specification.Test/Autofac.Specification.Test.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp2.1;net46</TargetFrameworks>
<NoWarn>$(NoWarn);CS1591;SA1602;SA1611</NoWarn>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<CodeAnalysisRuleSet>../../build/Analyzers.ruleset</CodeAnalysisRuleSet>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Autofac\Autofac.csproj" />
<ProjectReference Include="..\Autofac.Test.Scenarios.ScannedAssembly\Autofac.Test.Scenarios.ScannedAssembly.csproj" />
</ItemGroup>

<ItemGroup>
<Compile Remove="TestResults\**" />
<EmbeddedResource Remove="TestResults\**" />
<None Remove="TestResults\**" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="StyleCop.Analyzers" Version="1.1.0-beta009">
<PrivateAssets>All</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>

</Project>
95 changes: 95 additions & 0 deletions test/Autofac.Specification.Test/Registration/TypeRegistration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using System;
using Xunit;

namespace Autofac.Specification.Test.Registration
{
/// <summary>
/// Tests for fairly simple/straightforward type-as-self or
/// type-as-service registrations.
/// </summary>
public class TypeRegistration
{
private interface IA
{
}

private interface IB
{
}

private interface IC
{
}

[Fact]
public void RegisterTypeAsSupportedAndUnsupportedService()
{
var builder = new ContainerBuilder();
builder.RegisterType<A>().As<IA, IB>();
Assert.Throws<ArgumentException>(() => builder.Build());
}

[Fact]
public void RegisterTypeAsUnsupportedService()
{
var builder = new ContainerBuilder();
builder.RegisterType<string>().As<IA>();
Assert.Throws<ArgumentException>(() => builder.Build());
}

[Fact]
public void TypeRegisteredOnlyWithServiceNotRegisteredAsSelf()
{
var cb = new ContainerBuilder();
cb.RegisterType<A>().As<IA>();
var c = cb.Build();
Assert.False(c.IsRegistered<A>());
}

[Fact]
public void TypeRegisteredWithMultipleServicesCanBeResolved()
{
var target = new ContainerBuilder();
target.RegisterType<ABC>()
.As<IA, IB, IC>()
.SingleInstance();
var container = target.Build();
var a = container.Resolve<IA>();
var b = container.Resolve<IB>();
var c = container.Resolve<IC>();
Assert.NotNull(a);
Assert.Same(a, b);
Assert.Same(b, c);
}

[Fact]
public void TypeRegisteredWithoutServiceCanBeResolved()
{
var cb = new ContainerBuilder();
cb.RegisterType<A>();
var c = cb.Build();
var a = c.Resolve<A>();
Assert.NotNull(a);
Assert.IsType<A>(a);
}

[Fact]
public void TypeRegisteredWithSingleServiceCanBeResolved()
{
var cb = new ContainerBuilder();
cb.RegisterType<A>().As<IA>();
var c = cb.Build();
var a = c.Resolve<IA>();
Assert.NotNull(a);
Assert.IsType<A>(a);
}

private class A : IA
{
}

private class ABC : IA, IB, IC
{
}
}
}
11 changes: 4 additions & 7 deletions test/Autofac.Test/Autofac.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@
<TargetFrameworks>netcoreapp2.1;net46</TargetFrameworks>
<NoWarn>$(NoWarn);CS1591;SA1602;SA1611</NoWarn>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<AssemblyName>Autofac.Test</AssemblyName>
<AssemblyOriginatorKeyFile>../../Autofac.snk</AssemblyOriginatorKeyFile>
<SignAssembly>true</SignAssembly>
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
<PackageId>Autofac.Test</PackageId>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<RuntimeFrameworkVersion Condition=" '$(TargetFramework)' == 'netcoreapp1.0' ">1.1.2</RuntimeFrameworkVersion>
<GenerateAssemblyTitleAttribute>false</GenerateAssemblyTitleAttribute>
<CodeAnalysisRuleSet>../../build/Analyzers.ruleset</CodeAnalysisRuleSet>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
Expand All @@ -28,16 +25,16 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="System.Reflection.Emit" Version="4.3.0" />
<PackageReference Include="System.Reflection.Emit.Lightweight" Version="4.3.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="StyleCop.Analyzers" Version="1.1.0-beta009">
<PrivateAssets>All</PrivateAssets>
</PackageReference>
<PackageReference Include="System.Reflection.Emit" Version="4.3.0" />
<PackageReference Include="System.Reflection.Emit.Lightweight" Version="4.3.0" />
<PackageReference Include="xunit" Version="2.4.1" />
</ItemGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'net46' ">
Expand Down
55 changes: 0 additions & 55 deletions test/Autofac.Test/ContainerBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,6 @@ public class Abc : DisposeTracker, IA, IB, IC
{
}

[Fact]
public void SimpleReg()
{
var cb = new ContainerBuilder();
cb.RegisterType<Abc>();
var c = cb.Build();
var a = c.Resolve<Abc>();
Assert.NotNull(a);
Assert.IsType<Abc>(a);
}

[Fact]
public void SimpleRegIface()
{
var cb = new ContainerBuilder();
cb.RegisterType<Abc>().As<IA>();
var c = cb.Build();
var a = c.Resolve<IA>();
Assert.NotNull(a);
Assert.IsType<Abc>(a);
Assert.False(c.IsRegistered<Abc>());
}

[Fact]
public void WithExternalFactory()
{
Expand Down Expand Up @@ -254,22 +231,6 @@ public void ModuleCanRegisterModule()
container.AssertRegistered<object>();
}

[Fact]
public void RegisterTypeAsUnsupportedService()
{
var builder = new ContainerBuilder();
builder.RegisterType<string>().As<IA>();
Assert.Throws<ArgumentException>(() => builder.Build());
}

[Fact]
public void RegisterTypeAsSupportedAndUnsupportedService()
{
var builder = new ContainerBuilder();
builder.RegisterType<string>().As<IA, IB>();
Assert.Throws<ArgumentException>(() => builder.Build());
}

[Fact]
public void RegisterInstanceAsUnsupportedService()
{
Expand All @@ -286,22 +247,6 @@ public void RegisterAsUnsupportedService()
Assert.Throws<ArgumentException>(() => builder.Build());
}

[Fact]
public void RegisterThreeServices()
{
var target = new ContainerBuilder();
target.RegisterType<Abc>()
.As<IA, IB, IC>()
.SingleInstance();
var container = target.Build();
var a = container.Resolve<IA>();
var b = container.Resolve<IB>();
var c = container.Resolve<IC>();
Assert.NotNull(a);
Assert.Same(a, b);
Assert.Same(b, c);
}

[Fact]
public void InContextSpecifiesContainerScope()
{
Expand Down
3 changes: 0 additions & 3 deletions test/Autofac.Test/Properties/AssemblyInfo.cs

This file was deleted.

4 changes: 4 additions & 0 deletions test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Test Projects

- `Autofac.Test` is for unit tests that likely access internals to check on boundary conditions and so on. As versions of Autofac change and grow, these may also change and grow.
- `Autofac.Specification.Test` is for tests that access Autofac through the more standard public API - doing register and resolve calls, passing in parameters, that sort of thing. In general, if the "average user" wouldn't do it (e.g., follow the interfaces all the way down to count things like how many registration sources are there) or if it accesses internals, it shouldn't be there. This test suite is intended to stay pretty stable so we can ensure the 90% case is still working if we refactor internals and possibly make breaking changes.

0 comments on commit 0af5802

Please sign in to comment.