Beispiel #1
0
        public override string ToCSharpCode(ReadOnlyCollection <string> arguments, string newValueName, bool useRandoopContracts, ContractState contractStates
                                            )
        {
            // TODO assert that arguments.Count is correct.
            StringBuilder code    = new StringBuilder();
            string        retType =
                SourceCodePrinting.ToCodeString(constructor.DeclaringType);

            code.Append(retType + " " + newValueName + " = ");
            code.Append("new " + SourceCodePrinting.ToCodeString(constructor.DeclaringType));
            code.Append("(");
            for (int i = 0; i < arguments.Count; i++)
            {
                if (i > 0)
                {
                    code.Append(" , ");
                }

                // Cast.
                code.Append("(");
                code.Append(SourceCodePrinting.ToCodeString(ParameterTypes[i]));
                code.Append(")");
                code.Append(arguments[i]);
            }
            code.Append(");");

            var assertion = string.Empty;

            if (useRandoopContracts)
            {
                assertion = new RandoopContractAssertionGenerator().Compute(constructor, newValueName, contractStates);
            }

            code.Append(assertion);

            return(code.ToString());
        }
Beispiel #2
0
        // arguments[0] is the receiver. if static method, arguments[0] ignored.
        //
        // TODO: This method can be largely improved. For one, it should
        // be broken up into smaller methods depending on the nature of
        // the method (regular method, operator, property, etc.).
        public override string ToCSharpCode(ReadOnlyCollection <string> arguments, string newValueName, bool useRandoopContracts, ContractState contractStates)
        {
            StringBuilder code = new StringBuilder();

            //return value
            if (MethodIsVoid() == false)
            {
                string retType = SourceCodePrinting.ToCodeString(method.ReturnType);
                code.Append(retType + " " + newValueName + " = ");
            }

            //for overloaded operators
            bool   isOverloadedOp = false;
            string overloadOp     = "";

            bool   isCastOp = false;
            string castOp   = "";

            if (method.IsStatic)
            {
                //dont append the type name in case of operator overloading
                //operator overloading?

                if (ReflectionUtils.IsOverloadedOperator(method, out overloadOp))
                {
                    isOverloadedOp = true;
                }
                else if (ReflectionUtils.IsCastOperator(method, out castOp))
                {
                    isCastOp = true;
                }
                else if (method.IsSpecialName && method.Name.StartsWith("op_"))
                {
                    castOp = "(NOT HANDLED: " + method.ToString() + ")"; // TODO improve this message.
                }
                else
                {
                    isOverloadedOp = false;
                    isCastOp       = false;
                    code.Append(SourceCodePrinting.ToCodeString(method.DeclaringType));
                    code.Append(".");
                }
            }
            else
            {
                Type methodDeclaringType = ParameterTypes[0];

                code.Append("((");
                code.Append(SourceCodePrinting.ToCodeString(methodDeclaringType));
                code.Append(")");
                code.Append(arguments[0]);
                code.Append(")");
                code.Append(".");
            }

            //check if its a property
            //TODO: setter should not have a return value
            bool isSetItem = false;
            bool isItemOf  = false;
            bool isGetItem = false;
            bool isChars   = false;

            if (method.IsSpecialName)
            {
                HandleSpecialName(code, isOverloadedOp, isCastOp, ref isSetItem, ref isItemOf, ref isGetItem);
            }
            else
            {
                code.Append(method.Name);
                code.Append("(");
            }

            //arguments tuples

            for (int i = 1; i < arguments.Count; i++)
            {
                //we also need to remove the typename that comes with the static method
                if (isOverloadedOp && i == 2)
                {
                    code.Append(overloadOp);
                }
                else if (isCastOp && i == 1)
                {
                    code.Append(castOp);
                }
                else if (isSetItem && i == arguments.Count - 1)
                {
                    code.Append("] = ");
                }
                else
                {
                    if (i > 1)
                    {
                        code.Append(", ");
                    }
                }


                // Cast.
                code.Append("(");
                code.Append(SourceCodePrinting.ToCodeString(ParameterTypes[i]));
                code.Append(")");
                code.Append(arguments[i]);
            }

            if (!method.IsSpecialName)
            {
                code.Append(")");
            }
            else if (isGetItem || isChars || isItemOf)
            {
                code.Append("]");
            }
            code.Append(";");

            #region assertion
            ////[email protected] adds for capture return value for regression assertion -- start////
            //CASE1 -- output-plan-only stage: the execution of the plan hasn't happen yet
            //CASE2 -- execution has happened: return value is "not null & primitive+string"
            //CASE3 -- execution has happened: return value is "not null & not primitive+string"
            //CASE4 -- execution has happened: return value is "null"
            //CASE5 -- execution has happened: execution fails, return value is "null"("RANDOOPFAIL")

            //CASE1
            //if (timesReturnValRetrived >= ReturnValue.Count) //timesExecuted == ReturnValue.Count
            if (timesReturnValRetrieved >= timesExecuted)
            {
                Logger.Debug(method.Name + "[" + timesReturnValRetrieved.ToString()
                             + "+] didn't execute yet. Output-plan-only stage.");

                return(code.ToString()); //skip adding regression assertion
            }

            Logger.Debug(method.Name + "[" + timesReturnValRetrieved.ToString()
                         + "] get-return-value stage.");

            object tempval = ReturnValue[timesReturnValRetrieved];  // timesReturnValRetrived == ReturnValue.Count - 1

            //b.Append("\t//" + retType + "?=" + tempval.GetType().ToString() + ";\n"); //for debug

            var assertion = string.Empty;
            if (useRandoopContracts)
            {
                assertion = new RandoopContractAssertionGenerator().Compute(method, newValueName, arguments[0], contractStates);
            }

            if (assertion.Contains("Assert.IsTrue") == false)
            {
                assertion = new RegressionAssertionGenerator().GenerateRegressionAssertion(tempval, newValueName, timesReturnValRetrieved);
            }

            code.Append(assertion);

            timesReturnValRetrieved++;

            ////[email protected] adds for capture return value for regression assertion -- end////
            #endregion assertion

            return(code.ToString());
        }