//array logic private string GetArrayParamValue(CodeTypeRef member, int depth) { //var baseType = ((CodeElement)member.Parent).ProjectItem.ContainingProject.CodeModel.CreateCodeTypeRef(DTEHelper.GetBaseTypeFromArray(member.AsString)); // var baseType = TryToGuessGenericArgument(member); var baseType = member.ElementType; if (baseType == null) { return(string.Empty); } var typeFullName = string.Format("{0}[]", baseType.AsFullName.RemoveSystemFromStr()); if (baseType.TypeKind == vsCMTypeRef.vsCMTypeRefCodeType) { //typed Array var objAsStr = string.Format("{0}new {1}() {{\r\n{2}{0}}}\r\n", Spacing.Get(depth + 1), baseType.AsFullName, IterateMembers(baseType.CodeType, depth + 1)); return(string.Format("new {0} {{\r\n{1}{2}}}", typeFullName, objAsStr, Spacing.Get(depth))); } else { //generic array, such as string/int // var ListAry = new String[] { "yay" }; return(string.Format("new {0} {{ {1} }}", typeFullName, GetParamValue(baseType, "", depth + 1))); } }
private string GenPrivateClassesAtTop() { var frmtStr = Spacing.Get(2) + "private {0} {1};\r\n"; var str = string.Empty; var substitutes = string.Empty; foreach (var className in _parts.PrivateClassesAtTop) { var privClassName = DTEHelper.GenPrivateClassNameAtTop(className); str += string.Format(frmtStr, className, privClassName); // _parts.InitCode = string.Format("{0}{1} = Substitute.For<{2}>();\r\n", Spacing.Get(3), privClassName, className) + _parts.InitCode; substitutes += string.Format("{0}{1} = Substitute.For<{2}>();\r\n", Spacing.Get(3), privClassName, className); } if (string.IsNullOrWhiteSpace(substitutes) == false) { substitutes += "\r\n"; } _parts.InitCode = substitutes + _parts.InitCode; if (_parts.HasConstructor || _parts.IsStaticClass == false) { str += string.Format(frmtStr, _parts.SelectedClass.Name, "_testTarget"); } return(str); }
private string GetInputsBeforeFunctionParams(CodeFunction member) { if (member.Parameters == null || member.Parameters.OfType <CodeParameter>().Any() == false) { return(string.Empty); } var strInputs = ""; var paramCount = 0; foreach (CodeParameter param in member.Parameters.OfType <CodeParameter>()) { paramCount++; if (param.Type.AsString == "System.Guid" || param.Type.AsString == "System.DateTime" || param.Type.CodeType.Kind == vsCMElement.vsCMElementEnum) { continue; } if (param.Type != null && param.Type.TypeKind == vsCMTypeRef.vsCMTypeRefCodeType) { //get an input object for the class input var setValue = _genner.GenerateFunctionParamForClassInput(param.Type); if (setValue != null) { strInputs += string.Format("{0}var param{1} = {2}();\r\n", Spacing.Get(3), paramCount, setValue); } else { strInputs += string.Format("{0}dynamic param{1} = new {{}};//Could not generate param{1} \r\n", Spacing.Get(3), paramCount); } } } return(strInputs); }
private void GenerateConstructor(CodeFunction member) { _parts.HasConstructor = true; var paramsStr = _genner.GenerateFunctionParamValues(member, false); _parts.InitCode += string.Format("{0}_testTarget = new {1}({2});\r\n", Spacing.Get(3), member.Name, paramsStr); }
public string GenerateClassStr(CodeClass selectedClass, int depth = 0) { var str = string.Format("var obj = new {0}() {{\r\n", selectedClass.FullName); str += IterateMembers((CodeType)selectedClass, depth); str += Spacing.Get(depth) + "};"; return(str); }
public string GetParam(CodeTypeRef member, string paramName, int depth) { if (depth > 7) { return(string.Empty); //prevent ifinite loop } try { return(string.Format("{0}{1} = {2},\r\n", Spacing.Get(depth), paramName, GetParamValue(member, paramName, depth))); } catch (Exception ex) { return(string.Format("{0}//{1} = failed\r\n", Spacing.Get(depth), paramName)); } }
//list logic private string GetListParamValue(CodeTypeRef member, int depth) { var strippedMember = TryToGuessGenericArgument(member); if (strippedMember == null) { return(string.Empty); } if (strippedMember.TypeKind == vsCMTypeRef.vsCMTypeRefCodeType) { //typed List var objAsStr = string.Format("{0}new {1}() {{\r\n{2}{0}}}\r\n", Spacing.Get(depth + 1), strippedMember.AsFullName, IterateMembers(strippedMember.CodeType, depth + 1)); return(string.Format("new List<{1}>() {{\r\n{2}{0}}}", Spacing.Get(depth), strippedMember.AsFullName, objAsStr)); } else { //generic list, such as string/int // var ListString = new List<System.String>() { "yay" }; return(string.Format("new List<{0}>() {{ {1} }}", strippedMember.AsFullName.RemoveSystemFromStr(), GetParamValue(strippedMember, "", depth + 1))); } }
public string GetParamValue(CodeTypeRef member, string paramName, int depth) { var strippedMember = StripGenerics(member); // if (member.CodeType == null) return "Error"; if (member == null) { return("Error"); } AddNameSpace(member); if (strippedMember.TypeKind == vsCMTypeRef.vsCMTypeRefArray) { //array return(GetArrayParamValue(member, depth)); } else if (strippedMember.TypeKind == vsCMTypeRef.vsCMTypeRefCodeType && strippedMember.AsString == "System.DateTime") { //DateTime var year = DateTime.Now.Year; var month = DateTime.Now.Month; var day = DateTime.Now.Day; return(string.Format("new DateTime({0}, {1}, {2})", year, month, day)); } else if (strippedMember.TypeKind == vsCMTypeRef.vsCMTypeRefCodeType && strippedMember.CodeType != null && strippedMember.CodeType.Members != null && strippedMember.CodeType.Members.Count > 0 && strippedMember.CodeType.Kind == vsCMElement.vsCMElementEnum) { //Enums return(strippedMember.CodeType.Members.Item(1).FullName); } else if (strippedMember.TypeKind == vsCMTypeRef.vsCMTypeRefCodeType && strippedMember.AsString == "System.Guid") { //Guid return(string.Format("new Guid(\"{0}\")", Guid.NewGuid())); } else if (strippedMember.TypeKind == vsCMTypeRef.vsCMTypeRefCodeType && IsCodeTypeAList(strippedMember.CodeType.Name)) { return(GetListParamValue(member, depth)); } else if (strippedMember.TypeKind == vsCMTypeRef.vsCMTypeRefCodeType) { var paramsInConstructorStr = string.Empty; if (strippedMember.CodeType.Members != null) { //var constructor = member.CodeType.Members.OfType<CodeFunction>().FirstOrDefault(x => x.FunctionKind == vsCMFunction.vsCMFunctionConstructor); var constructor = strippedMember.CodeType.Members.OfType <CodeFunction>().FirstOrDefault(x => x.FunctionKind == vsCMFunction.vsCMFunctionConstructor); if (constructor != null && constructor.Kind == vsCMElement.vsCMElementFunction) { paramsInConstructorStr = GenerateFunctionParamValues(constructor, false); } } var includedNewLineInParams = string.Empty; //var initializerStr = IterateMembers(member.CodeType, depth); var initializerStr = IterateMembers(strippedMember.CodeType, depth); if (string.IsNullOrWhiteSpace(initializerStr) == false) { includedNewLineInParams = "\r\n"; initializerStr += Spacing.Get(depth); } //defined types/objects we have created return(string.Format("new {0}({2}) {{{3}{1}}}", strippedMember.AsString, initializerStr, paramsInConstructorStr, includedNewLineInParams)); } else if (strippedMember.TypeKind == vsCMTypeRef.vsCMTypeRefString) { //string return(string.Format("\"{0}\"", Words.Gen(_opts.WordsInStrings))); } else if (strippedMember.TypeKind == vsCMTypeRef.vsCMTypeRefChar) { //char var chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; return("'" + chars[StaticRandom.Instance.Next(0, chars.Length)] + "'"); } else if (strippedMember.TypeKind == vsCMTypeRef.vsCMTypeRefBool) { //bool return(StaticRandom.Instance.Next(0, 1) == 1 ? "true" : "false"); } else if (strippedMember.TypeKind == vsCMTypeRef.vsCMTypeRefDecimal || strippedMember.TypeKind == vsCMTypeRef.vsCMTypeRefDouble || strippedMember.TypeKind == vsCMTypeRef.vsCMTypeRefFloat || strippedMember.TypeKind == vsCMTypeRef.vsCMTypeRefInt || strippedMember.TypeKind == vsCMTypeRef.vsCMTypeRefLong || strippedMember.AsString == "uint" || strippedMember.AsString == "ulong") { //numbers (except short) if (_opts.IntLength == 0) { return("0"); } return(StaticRandom.Instance.Next(_opts.GetMaxIntLength()).ToString()); } else if (strippedMember.TypeKind == vsCMTypeRef.vsCMTypeRefShort) { //short if (_opts.IntLength == 0) { return("0"); } var maxRnd = _opts.IntLength > 4 ? 9999 : _opts.GetMaxIntLength(); return(StaticRandom.Instance.Next(maxRnd).ToString()); } else if (strippedMember.TypeKind == vsCMTypeRef.vsCMTypeRefByte) { //byte return("new Byte()"); } else if (strippedMember.TypeKind == vsCMTypeRef.vsCMTypeRefObject) { //object return("new Object()"); } else if (strippedMember.AsString == "sbyte") { //sbyte return(StaticRandom.Instance.Next(-128, 127).ToString()); } else if (strippedMember.AsString == "ushort") //no, YOU'RE SHORT! { //ushort return(StaticRandom.Instance.Next(65535).ToString()); } else { //skip return(""); } }
private string GenInterfaceMocking() { //goal //async _someClass.SomeFunction(Arg.Any<Guid>()).Returns(Task.FromResult(SomeClass)); //reg _someClass.SomeFunction(Arg.Any<Guid>()).Returns(SomeClass); var str = string.Empty; Log("\r\nGenerating Interfaces\r\n"); foreach (CodeInterface face in _parts.Interfaces) //foreach interface found in our test class { if (face.Kind != vsCMElement.vsCMElementInterface) { continue; } _genner.AddNameSpace(face.Namespace); foreach (CodeFunction member in face.Members.OfType <CodeFunction>()) //foreach function in interface { try { var returnType = string.Empty; var isAsync = member.Type.CodeType.FullName.Contains("System.Threading.Tasks.Task"); var strippedMember = _genner.StripGenerics(member.Type); if (strippedMember == null) { throw new Exception("Could not strip generic"); } if (strippedMember.TypeKind == vsCMTypeRef.vsCMTypeRefVoid || strippedMember.AsFullName == "System.Threading.Tasks.Task") { continue; //no need to mock return type for Void functions } Log(member.FullName + "\r\n"); if (strippedMember.TypeKind == vsCMTypeRef.vsCMTypeRefCodeType && (strippedMember.AsString != "System.Guid" && strippedMember.AsString != "System.DateTime" && strippedMember.CodeType.Kind != vsCMElement.vsCMElementEnum)) { _genner.GenerateFunctionParamForClassInput(member.Type); ////_genner.GenerateFunctionParamForClassInput(member.Type.CodeType.Name, member.Type.CodeType.FullName, member.Type); //make sure we have created this type so we can return it //returnType = _parts.GetParamFunctionName(memberNoTask.CodeType.FullName) + "()"; returnType = _parts.GetParamFunctionName(strippedMember.CodeType.FullName); if (returnType == null) { returnType = "new {}"; } else { returnType += "()"; } } else { returnType = _genner.GetParamValue(member.Type, "", 0); } //if (returnType == "GetList()") //{ // var tmp5 = 5; //} if (isAsync) { returnType = string.Format("Task.FromResult({0})", returnType); } str += string.Format("{0}{1}.{2}({3}).Returns({4});\r\n", Spacing.Get(3), DTEHelper.GenPrivateClassNameAtTop(face.Name), member.Name, GetInterfaceArgs(member), returnType); } catch (Exception ex) { str += string.Format("{0}//Could not generate {1}.{2}; \r\n", Spacing.Get(3), face.Name, member.Name); } } } // str = str.ReplaceLastOccurrence("\r\n", ""); return(str); }
private void GenerateEmptyConstructor() { _parts.HasConstructor = true; _parts.InitCode += string.Format("{0}_testTarget = new {1}();\r\n", Spacing.Get(3), _parts.SelectedClass.Name); }