Exemple #1
0
        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);
                }
            }
        }
Exemple #2
0
        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);
        }
Exemple #3
0
        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);
            }
        }
Exemple #4
0
        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);
            }
        }
Exemple #5
0
    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));
                }
            }
        }
    }
Exemple #6
0
        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);
        }
Exemple #7
0
        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);
        }