/// <summary> /// Implements the code snippet. /// </summary> /// <param name="instance">The instance.</param> /// <param name="codeSnippet">The code snippet.</param> /// <param name="formatFunctionParameters">if set to <c>true</c> [format function parameters].</param> public static void ImplementCodeSnippet( this CodeClass instance, CodeSnippet codeSnippet, bool formatFunctionParameters) { TraceService.WriteLine("CodeClassExtensions::ImplementCodeSnippet file" + instance.Name); if (codeSnippet.Variables != null) { foreach (string[] parts in codeSnippet.Variables .Select(variable => variable.Split(' '))) { //// variable could already exist! try { instance.ImplementVariable(parts[1], parts[0], false); } catch (Exception exception) { TraceService.WriteError("Error adding variable exception=" + exception.Message + " variable=" + parts[1]); //// if variable already exists get out - code snippet will already have been applied. return; } } } if (codeSnippet.MockVariables != null) { foreach (string[] parts in codeSnippet.MockVariables .Select(variable => variable.Split(' '))) { //// variable could already exist! try { instance.ImplementMockVariable(parts[1], parts[0], codeSnippet.MockingVariableDeclaration); } catch (Exception exception) { TraceService.WriteError("Error adding mock variable exception=" + exception.Message + " variable=" + parts[1]); //// if variable already exists get out - code snippet will already have been applied. return; } } } if (string.IsNullOrEmpty(codeSnippet.Code) == false) { instance.ImplementFunction(codeSnippet); } if (codeSnippet.Interfaces != null && codeSnippet.Interfaces.Count > 0) { IEnumerable <CodeFunction> constructors = instance.GetConstructors(); CodeFunction constructor = constructors.FirstOrDefault() ?? instance.AddDefaultConstructor(true); foreach (string variable in codeSnippet.Interfaces) { instance.ImplementInterface(constructor, variable); } if (formatFunctionParameters) { constructor.FormatParameters(); } } if (string.IsNullOrEmpty(codeSnippet.GetMockInitCode()) == false || string.IsNullOrEmpty(codeSnippet.GetMockConstructorCode()) == false) { instance.ImplementMockCode(codeSnippet); } }