Skip to content

Commit

Permalink
add Container build callsite, add object creation callsite
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavopsantos committed Dec 5, 2023
1 parent 3f065d1 commit d1087fc
Show file tree
Hide file tree
Showing 22 changed files with 364 additions and 250 deletions.
28 changes: 22 additions & 6 deletions Assets/Reflex.Tests/DecorateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ public static Number FromValue(int value)
};
}
}

public class DoubledNumber : INumber
{
private readonly INumber _number;
public DoubledNumber(INumber number) => _number = number;
public int Get() => _number.Get() * 2;
}

public class HalvedNumber : INumber
{
private readonly INumber _number;
Expand All @@ -59,7 +59,7 @@ public ResilientBundleManager(IBundleManager bundleManager)
{
}
}

[Test]
public void Decorate_ShouldDecorateAllMatchingContracts()
{
Expand All @@ -74,7 +74,7 @@ public void Decorate_ShouldDecorateAllMatchingContracts()
numbers.Length.Should().Be(3);
numbers.Should().BeEquivalentTo(new int[] {2, 4, 6});
}

[Test]
public void Decorate_ShouldBeAbleToNestDecorations()
{
Expand All @@ -92,6 +92,22 @@ public void Decorate_ShouldBeAbleToNestDecorations()
var number = container.Single<INumber>();
number.Get().Should().Be(80);
}

[Test]
public void Decorate_ShouldBeAbleToNestDecorations2()
{
var container = new ContainerDescriptor("")
.AddSingleton(Number.FromValue(10), typeof(INumber))
.AddDecorator(typeof(DoubledNumber), typeof(INumber))
.AddDecorator(typeof(DoubledNumber), typeof(INumber))
.AddSingleton(Number.FromValue(8), typeof(INumber))
.AddDecorator(typeof(HalvedNumber), typeof(INumber))
.AddDecorator(typeof(HalvedNumber), typeof(INumber))
.Build();

var numbers = container.All<INumber>();
numbers.Select(n => n.Get()).Should().BeEquivalentTo(new int[] {10, 2});
}

[Test]
public void DecoratedContract_ShouldReplaceOnlyDecoratedContract()
Expand All @@ -104,7 +120,7 @@ public void DecoratedContract_ShouldReplaceOnlyDecoratedContract()
container.Resolve<IManager>().GetType().Should().Be<BundleManager>();
container.Resolve<IBundleManager>().GetType().Should().Be<ResilientBundleManager>();
}

[Test]
public void DecoratedSingleton_ShouldDecorateWithSingleton()
{
Expand All @@ -122,7 +138,7 @@ public void DecoratedSingleton_ShouldDecorateWithSingleton()

distinctInstances.Count.Should().Be(1);
}

[Test]
public void DecoratedTransient_ShouldDecorateWithTransient()
{
Expand Down
7 changes: 7 additions & 0 deletions Assets/Reflex/Core/ContainerDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public Container Build()
Build(out var disposables, out var resolversByContract, out var toStart);
var container = new Container(_name, resolversByContract, disposables);
container.SetParent(_parent);
RegisterBuildCallSite(container);

// Clear references
_name = null;
Expand Down Expand Up @@ -211,5 +212,11 @@ private static void ValidateContracts(Type concrete, params Type[] contracts)
}
}
}

[Conditional("REFLEX_DEBUG")]
private static void RegisterBuildCallSite(Container container)
{
container.GetDebugProperties().BuildCallsite.AddRange(Diagnosis.GetCallSite(4));
}
}
}
3 changes: 3 additions & 0 deletions Assets/Reflex/Diagnosis.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions Assets/Reflex/Diagnosis/Diagnosis.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using UnityEngine;

namespace Reflex
{
internal static class Diagnosis
{
public static List<CallSite> GetCallSite(int skipFrames)
{
var result = new List<CallSite>();
var stackTrace = new StackTrace(skipFrames, true);
var frames = stackTrace.GetFrames();

foreach (var frame in frames.Where(f => f.GetFileName() != null))
{
var methodName = frame.GetMethod()?.Name;
var className = frame.GetMethod()?.DeclaringType?.FullName;
var lineNumber = frame.GetFileLineNumber();
var filePath = GetUnityPath(frame.GetFileName());
result.Add(new CallSite(className, methodName, filePath, lineNumber));
}

return result;
}

private static string GetUnityPath(string path)
{
return path.Replace("\\", "/").Replace(Application.dataPath, "Assets");
}
}
}
3 changes: 3 additions & 0 deletions Assets/Reflex/Diagnosis/Diagnosis.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit d1087fc

Please sign in to comment.