public void ExpressionsMethodTest() { var expressions = new string[] { "methods:", "methods:*", "methods:", "methods:Do*" }; foreach (var expression in expressions) { var aspect = new MyAspect <IService>(expression); IService service = aspect.Build(new Service()); service.DoWork(); service.DoAnotherWork(); service.Property1 = true; try { service.ThrowException(); } catch { } if ("methods:Do*".Equals(expression)) { Assert.Equal(2 * 3, aspect.ExecutionCounter); //2 * is for the Do* method (2 times) executions Assert.Equal(0, aspect.ThrowCounter); } else { Assert.Equal(3 * 3 - 1, aspect.ExecutionCounter); //3 * is for the method (2 times) executions, -1 for the throw that prevents the after execution Assert.Equal(1, aspect.ThrowCounter); } } }
public void ThrowTest() { var aspect0 = new MyAspect <IService>(); IService service = Aspect <IService> .Build( new Service(), aspect0 ); bool externalException = false; try { service.ThrowException(); } catch { externalException = true; } Assert.True(aspect0.OnBeforeReached); Assert.True(aspect0.OnAroundReached); Assert.False(aspect0.OnAfterReached); Assert.True(aspect0.OnThrowReached); Assert.True(externalException); }
public void NoExpressionTest() { var expressions = new string[] { null, "", "*", "*:*", "methods|properties", "methods|properties:*", }; foreach (var expression in expressions) { var aspect = new MyAspect <IService>(expression); IService service = aspect.Build(new Service()); service.DoWork(); service.Property1 = true; try { service.ThrowException(); } catch { } Assert.Equal(3 * 3 - 1, aspect.ExecutionCounter); //3 * is for the method (2 times) and property (1 time) executions, -1 for the throw that prevents the after execution Assert.Equal(1, aspect.ThrowCounter); } }
public void ExpressionsPropertiesTest() { var expressions = new string[] { "properties:", "properties:*", "properties:", "properties:Property*" }; foreach (var expression in expressions) { var aspect = new MyAspect <IService>(expression); IService service = aspect.Build(new Service()); service.DoWork(); service.DoAnotherWork(); service.Property1 = true; service.Property2 = true; try { service.ThrowException(); } catch { } Assert.Equal(2 * 3, aspect.ExecutionCounter); //2 * is for the properties (3 times) executions Assert.Equal(0, aspect.ThrowCounter); } }
public IEnumerable <AspectInstance> ProvideAspects(object targetElement) { var myAspect = new MyAspect(); var assembly = (Assembly)targetElement; foreach (var type in assembly.GetTypes()) { if (/* type is a valid target */) { foreach (var methodInfo in type.GetMethods()) { yield return(new AspectInstance(methodInfo, myAspect)); } } } }
public void AroundTest() { var aspect0 = new MyAspect <IService>(false, false); IService service = Aspect <IService> .Build( new Service(), aspect0 ); bool result = service.DoWork(); Assert.True(aspect0.OnBeforeReached); Assert.True(aspect0.OnAroundReached); Assert.False(aspect0.OnAfterReached); Assert.False(aspect0.OnThrowReached); Assert.False(result); }
public void BasicCreationTest() { var aspect0 = new MyAspect <IService>(); var aspect1 = new MyAspect <IService>(); IService service = Aspect <IService> .Build( new Service(), aspect0, aspect1 ); service.DoWork(); Assert.True(aspect0.OnBeforeReached); Assert.True(aspect0.OnAroundReached); Assert.True(aspect0.OnAfterReached); Assert.False(aspect0.OnThrowReached); Assert.True(aspect1.OnBeforeReached); Assert.True(aspect1.OnAroundReached); Assert.True(aspect1.OnAfterReached); Assert.False(aspect1.OnThrowReached); }