Skip to content

Commit

Permalink
Primary ctor working
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathias Stenbom committed Oct 2, 2024
1 parent b834096 commit a4bcedf
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 50 deletions.
63 changes: 16 additions & 47 deletions Saspect.Test/GeneratorTest.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// avoid adding System usings here... yeah right.

using System;
using Autofac;
using Autofac.Builder;
using NUnit.Framework;
using Saspect.Autofac;
using Saspect.Test.Samples;
using SharpTestsEx;
using Sinjector;

Expand All @@ -17,9 +17,11 @@ public void ContainerSetup(IContainerSetupContext context)
{
context.AddService<AspectInterceptor>();

context.AddService<NestedClassSample.NestedClass>();
context.AddService<CtorSample>();
context.AddService<PrimaryCtorSample>();
context.AddService<DependencySample>();
context.AddService<Aspect1Attribute.Aspect1>();
context.AddService<Aspect1>();
}

public void ContainerRegistrationSetup<TLimit, TConcreteReflectionActivatorData, TRegistrationStyle>(IRegistrationBuilder<TLimit, TConcreteReflectionActivatorData, TRegistrationStyle> registration) where TConcreteReflectionActivatorData : ConcreteReflectionActivatorData
Expand All @@ -30,56 +32,23 @@ public void ContainerRegistrationSetup<TLimit, TConcreteReflectionActivatorData,
public IComponentContext Container;

[Test]
public void ShouldGenerateCtor()
public void ShouldManageNestedClass()
{
Container.Resolve<CtorSample>().AspectedMethod();
Container.Resolve<NestedClassSample.NestedClass>()
.Should().Be.OfType<NestedClassSample_NestedClassAspected>();
}

// [Test]
// public void ShouldGeneratePrimaryCtor()
// {
// Container.Resolve<PrimaryCtorSample>().AspectedMethod();
// }

public class CtorSample
[Test]
public void ShouldGenerateCtor()
{
public CtorSample(DependencySample injected)
{
}

[Aspect1]
public virtual void AspectedMethod()
{
}
Container.Resolve<CtorSample>()
.Should().Be.OfType<CtorSampleAspected>();
}
//
// public class PrimaryCtorSample(DependencySample injected)
// {
// [Aspect1]
// public virtual void AspectedMethod()
// {
// }
// }

public class Aspect1Attribute : AspectAttribute
[Test]
public void ShouldGeneratePrimaryCtor()
{
public Aspect1Attribute() : base(typeof(Aspect1))
{
}

public class Aspect1 : IAspect
{
public void OnBeforeInvocation(InvocationInfo invocation)
{
}

public void OnAfterInvocation(Exception exception, InvocationInfo invocation)
{
}
}
Container.Resolve<PrimaryCtorSample>()
.Should().Be.OfType<PrimaryCtorSampleAspected>();
}
}

public class DependencySample
{
}
}
14 changes: 14 additions & 0 deletions Saspect.Test/Samples/Aspect1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;

namespace Saspect.Test.Samples;

public class Aspect1 : IAspect
{
public void OnBeforeInvocation(InvocationInfo invocation)
{
}

public void OnAfterInvocation(Exception exception, InvocationInfo invocation)
{
}
}
8 changes: 8 additions & 0 deletions Saspect.Test/Samples/Aspect1Attribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Saspect.Test.Samples;

public class Aspect1Attribute : AspectAttribute
{
public Aspect1Attribute() : base(typeof(Aspect1))
{
}
}
13 changes: 13 additions & 0 deletions Saspect.Test/Samples/CtorSample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Saspect.Test.Samples;

public class CtorSample
{
public CtorSample(DependencySample injected)
{
}

[Aspect1]
public virtual void AspectedMethod()
{
}
}
3 changes: 3 additions & 0 deletions Saspect.Test/Samples/DependencySample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace Saspect.Test.Samples;

public class DependencySample;
12 changes: 12 additions & 0 deletions Saspect.Test/Samples/NestedClassSample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Saspect.Test.Samples;

public class NestedClassSample
{
public class NestedClass
{
[Aspect1]
public virtual void AspectedMethod()
{
}
}
}
9 changes: 9 additions & 0 deletions Saspect.Test/Samples/PrimaryCtorSample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Saspect.Test.Samples;

public class PrimaryCtorSample(DependencySample injected)

Check warning on line 3 in Saspect.Test/Samples/PrimaryCtorSample.cs

View workflow job for this annotation

GitHub Actions / build

Parameter 'injected' is unread.

Check warning on line 3 in Saspect.Test/Samples/PrimaryCtorSample.cs

View workflow job for this annotation

GitHub Actions / build

Parameter 'injected' is unread.
{
[Aspect1]
public virtual void AspectedMethod()
{
}
}
7 changes: 4 additions & 3 deletions Saspect/GeneratorOutput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,16 @@ private static void generateCtor(StringBuilder code, ClassDeclarationSyntax clas
code.Append($@"
public {className}");

var ctor = clasz.Members.OfType<ConstructorDeclarationSyntax>().FirstOrDefault();
var firstCtor = clasz.Members.OfType<ConstructorDeclarationSyntax>().FirstOrDefault();
var primaryCtor = clasz.ParameterList;

if (ctor == null)
if (firstCtor == null && primaryCtor == null)
{
code.Append($"(Saspect.AspectInterceptor interceptor)");
return;
}

var parameterz = ctor.ParameterList;
var parameterz = firstCtor?.ParameterList ?? primaryCtor;

var parameters = string.Join(", ",
parameterz.Parameters
Expand Down

0 comments on commit a4bcedf

Please sign in to comment.