using System; using Microsoft.Extensions.DependencyInjection; using Tand.Core; using Tand.Core.Api; using Tand.Core.Models; using Xunit; namespace Tand.Extensions.DependencyInjection.Tests { public class TandServiceExtensionsTests { public const string Greeting = "Hello, World!"; [Fact] public void RegisterService_RegisterSampleService_SuccessfullyResolve() { var calledOnEnter = false; var calledOnExit = false; var servides = new ServiceCollection(); servides.AddTand(); servides.AddSingleton(new TestDecorator(_ => { calledOnEnter = true;}, _ => { calledOnExit = true;})); servides.AddTandTransient(); var provider = servides.BuildServiceProvider(); var sampleService = provider.GetService(); Assert.NotNull(sampleService); var result = sampleService.Greet(); Assert.Equal(Greeting, result); Assert.True(calledOnEnter); Assert.True(calledOnExit); } } public class TestDecorator : ITandTarget { private readonly Action> _onEnterHandle; private readonly Action> _onLeaveHandle; public TestDecorator(Action> onEnterHandle, Action> onLeaveHandle) { _onEnterHandle = onEnterHandle; _onLeaveHandle = onLeaveHandle; } public void OnEnterMethod(CallEnterContext enterContext) => _onEnterHandle(enterContext); public void OnLeaveMethod(CallLeaveContext leaveContext) => _onLeaveHandle(leaveContext); } public interface ISampleService { [Tand(typeof(TestDecorator))] string Greet(); } public class SampleServiceImpl : ISampleService { public string Greet() => TandServiceExtensionsTests.Greeting; } }