Exemplo n.º 1
0
        public void Transform_ThrowsArgumentException_IfThereIsNoFieldOrPropertyWithName()
        {
            // Creating the expression "instance.AddNonExistentDelegate(handler)"
            Expression instance = Expression.Parameter(typeof(Fixture), "instance");
            Expression handler  = Expression.Parameter(typeof(Action), "handler");
            Expression input    = Expression.Call(instance, FixtureAddNonExistentDelegate, handler);

            // Validating that DelegateAddAttribute fails on non-existent field or property
            DelegateAddAttribute attribute = new DelegateAddAttribute("NonExistentMember");

            Assert.ThrowsException <ArgumentException>(() => attribute.Transform(input));
        }
Exemplo n.º 2
0
        public void Transform_ReplacesMethodCallExpressionWithAddAssignExpression_ForPropertyEvents()
        {
            // Creating the expression "instance.AddPropertyDelegate(handler)"
            ParameterExpression instance = Expression.Parameter(typeof(Fixture), "instance");
            ParameterExpression handler  = Expression.Parameter(typeof(Action), "handler");
            Expression          input    = Expression.Call(instance, FixtureAddPropertyDelegate, handler);

            // Validating that DelegateAddAttribute creates expression subscribing to PropertyDelegate
            Expression output = new DelegateAddAttribute("PropertyDelegate").Transform(input);
            Action <Fixture, Action> addPropertyDelegate = Expression.Lambda <Action <Fixture, Action> >(output, new[] { instance, handler }).Compile();

            Fixture fixture = new Fixture();
            Action  action  = () => { };

            addPropertyDelegate(fixture, action);
            CollectionAssert.Contains(fixture.PropertyDelegate.GetInvocationList(), action);
        }