private static TypeDetail GetTypeName(object model) { TypeDetail typeDetail = new TypeDetail(); if (IsList(model) && IsGenericList(model)) { try { Type type = model.GetType().GetGenericArguments().Single(); typeDetail.typeName = $"List<{type.Name}>"; typeDetail.variableName = $"List{type.Name}"; } catch (Exception) { Type type = model.GetType(); typeDetail.typeName = $"{type.Name}"; typeDetail.variableName = $"{type.Name}"; } } else { Type type = model.GetType(); typeDetail.typeName = $"{type.Name}"; typeDetail.variableName = $"{type.Name}"; } return(typeDetail); }
//exaple //var resultMockUnitTest = GenerateUnitTestLogic.GenerateTestMethod(MethodBase.GetCurrentMethod(), addOnPaymentConfigs, gracePeriodRequest); public static string GenerateTestMethod(this MethodBase methodBase, params object[] requestObjects) { ClearMethodNames(); List <string> mockInputFunctions = new List <string>(); List <string> mockInputMethodNames = new List <string>(); foreach (var model in requestObjects) { if (model == null) { mockInputMethodNames.Add(null); } else { TypeDetail typeDetail = GetTypeName(model); var mockInputUnitTest = GetMockInputUnitTest(methodBase, model, typeDetail); mockInputFunctions.Add(mockInputUnitTest.mockInputFunction); mockInputMethodNames.Add(mockInputUnitTest.mockInputMethodName); } } string mockInputFunction = string.Join(Environment.NewLine, mockInputFunctions); string codeMockCallFunction = GetTestMethod(methodBase, mockInputMethodNames); string resultCodeMock = $"{mockInputFunction}{Environment.NewLine}{GetTestMethodPattern(methodBase, codeMockCallFunction)}"; return(resultCodeMock); }
private static string GetMockInputMethodName(MethodBase methodBase, TypeDetail typeDetail) { string mockInputMethodName = $"GetMock{typeDetail.variableName}{methodBase.Name}"; mockInputMethodName = IfDuplicateMethodNameThenRename(mockInputMethodName); return(mockInputMethodName); }
private static string GetCodeFirstLineMockInput(TypeDetail typeDetail, string methodName) { return($"public {typeDetail.typeName} {methodName}(){Environment.NewLine}" + "{"); }
private static string GetCodeEndLineMockInput(TypeDetail typeDetail) { return($"return JsonConvert.DeserializeObject<{typeDetail.typeName}>(raw);"); }
private static (string mockInputFunction, string mockInputMethodName) GetMockInputUnitTest(MethodBase methodBase, object objects, TypeDetail typeDetail) { string mockInputMethodName = GetMockInputMethodName(methodBase, typeDetail); methodNames.Add(mockInputMethodName); string codeFirstLine = GetCodeFirstLineMockInput(typeDetail, mockInputMethodName); string codeSecondLine = GetCodeSecondLineMockInput(objects); string codeEndLine = GetCodeEndLineMockInput(typeDetail); string mockInputFunction = $"{codeFirstLine}{codeSecondLine}{Environment.NewLine}{codeEndLine}{Environment.NewLine}" + "}"; return(mockInputFunction, $"{mockInputMethodName}()"); }