Пример #1
0
        /// <summary>
        /// Method which is called on every method in a class to be amended
        /// </summary>
        public override void Amend(Method method)
        {
            if (!ShouldAmend(method.MethodInfo))
            {
                return;
            }

            Console.WriteLine(String.Format("Amending {0}", method.Name));
            base.Amend(method);

            method.Before(Logger.LogMethodBefore);
            method.After(Logger.LogMethodAfter);
        }
Пример #2
0
        /// <summary>
        /// Apply amendments to existing methods to implement the behavior being tested.
        /// </summary>
        /// <param name="method"></param>
        public override void Amend(Method method)
        {
            switch (method.Name)
            {
            // Modify Multiply to also set the Result property to the resulting value
            case "Multiply":
                method.Before <int, int>((instance, methodName, parameters) =>
                {
                    instance.Result = parameters.Param1 * parameters.Param2;

                    // Return null to indicate that the original parameters should not be modified
                    return(null);
                });

                method.AddAttribute(Attribute <TestAttribute> .Create(typeof(string)));
                method.AddAttribute(Attribute <TestAttribute> .Create());
                method.AddAttribute(Attribute <TestAttribute> .Create(5));
                method.AddAttribute(Attribute <TestAttribute> .Create(new string[] { "Testing", "Two" }));
                break;

            // Modify Multiply2 to also set the Result property to the resulting value
            case "Multiply2":
                method.Before((instance, methodName, parameters) =>
                {
                    instance.Result = (int)parameters[0] * (int)parameters[1];

                    // Return null to indicate that the original parameters should not be modified
                    return(null);
                });
                break;

            // Modify Divide to change the second parameter value to 1 every time
            case "Divide":
                method.Before <int, int>((instance, methodName, parameters) =>
                {
                    parameters.Param2 = 1;

                    // Return the updated parameters to cause the new values to be used by the original implementation
                    return(parameters);
                });
                break;

            // Modify Divide2 to change the second parameter value to 1 every time
            case "Divide2":
                method.Before((instance, methodName, parameters) =>
                {
                    parameters[1] = 1;

                    // Return the updated parameters to cause the new values to be used by the original implementation
                    return(parameters);
                });
                break;

            // Replace implementation of Square to correct coding error
            case "Square":
                method.Implement <int, int>((instance, methodName, parameters) => parameters.Param1 * parameters.Param1);
                break;

            // Modify Double to double each of the input values
            case "Double":
                method.After <int[]>((instance, methodName, parameters) =>
                {
                    for (int i = 0; i < parameters.Param1.Length; i++)
                    {
                        parameters.Param1[i] = parameters.Param1[i] * 2;
                    }
                });
                break;

            // Modify Double to double each of the input values
            case "Double2":
                method.After((instance, methodName, parameters) =>
                {
                    for (int i = 0; i < ((int[])parameters[0]).Length; i++)
                    {
                        ((int[])parameters[0])[i] = ((int[])parameters[0])[i] * 2;
                    }
                });
                break;

            // Modify Sum to return the sum of the input values
            case "Sum":
                method.After <int[], long>((instance, methodName, parameters, returnValue) =>
                {
                    return(parameters.Param1.Sum());
                });
                break;


            // Modify Sum to return the sum of the input values
            case "Sum2":
                method.After <long>((instance, methodName, parameters, returnValue) =>
                {
                    return(((int[])parameters[0]).Sum());
                });
                break;

            // Modify the input values but ignore the return value
            case "Sum3":
                method.After((instance, methodName, parameters) =>
                {
                    for (int i = 1; i < ((int[])parameters[0]).Length; i++)
                    {
                        ((int[])parameters[0])[i] = ((int[])parameters[0])[i - 1] + ((int[])parameters[0])[i];
                    }
                });
                break;
            }
        }