/// <summary> /// Updates the class stats from the method stats /// </summary> /// <param name="stats">The class stats</param> /// <param name="method">The method nt</param> public static void zUpdate(this ClassNTStats_ stats, MethodNT_ method) { var methodStats = method.Statistics; // Totals stats.TotalMethods++; if (method.Header.Header_Scope == enCode_Scope._public) { stats.TotalMethods_Public++; } // Lines - stats.MethodTotalLines += methodStats.MethodTotalLines; stats.MethodTotalBodyLines += methodStats.MethodTotalBodyLines; if (stats.MethodMaxLines < methodStats.MethodTotalBodyLines) { stats.MethodMaxLines = methodStats.MethodTotalBodyLines; } stats.MethodAvgLines = stats.MethodTotalBodyLines / stats.TotalMethods; stats.ClassTotalCommentLines += methodStats.MethodTotalCommentLines; //stats.ClassTotalLines += methodStats.MethodTotalLines; ----- Already calculated //stats.ClassTotalCodeLines += methodStats.MethodTotalBodyLines; ----- Already calculated // Code stats.CodeMaintainability += methodStats.CodeMaintainability; stats.CodeComplexity += methodStats.CodeComplexity; }
public static void MethodNTstats_Test() { int ii; #region Test1: // =========================================== var source = new List <string> { "/// <summary>", "/// Return the specified chars from the right of the input string.", "/// </summary>", "/// <param name=\"inputStr\">The input string.</param>", "/// <param name=\"chars\">The chars.</param>", "/// <returns>string</returns>", "[BlueprintRule_Method(Ignore = true)]", "public string SubStr_Right(string inputStr, int chars)", "{", " if (chars > inputStr.Length) return \"\";", " int position = inputStr.Length - chars;", " return inputStr.Substring(position, chars);", "}" }; ii = 0; var method = MethodNT_.Create(source, ref ii, 12); Assert.Equal(true, method.Attribute_Rule.Ignore); Assert.Equal(13, method.Statistics.MethodTotalLines); Assert.Equal(5, method.Statistics.MethodTotalBodyLines); Assert.Equal(2, method.Statistics.CodeComplexity); Assert.Equal(1, method.Statistics.CodeMaintainability); #endregion #region Test2: // =========================================== #endregion }
public static MethodNTstats_ Create(List <string> sourceLines, ref int ii, MethodNT_ method) { return(Create(sourceLines, ref ii, method.Comment, method.Header, method.Attribute_Lines.Count)); }
public void ClassWriteRead_Test() { #region Create the test data // =========================================== var source = new List <string> { "using System;", "", "namespace Blueprint.lib.Rules.Types", "{", " /// <summary>", " /// Money convertions", " /// </summary>", " [BlueprintRule_Class(enBlueprint_ClassNetworkType.Node_Action)]", " [BlueprintCodeInjection_(typeof(Controller_BlueprintLogger), true)]", " public sealed class Types_Money", " {", " public string Method1()", " {", " return \"Test\";", " }", " ", " /// <summary>", " /// A Double extension method that converts the @this to a money.", " /// </summary>", " /// <param name=\"this\">The @this to act on.</param>", " /// <returns>@this as a Double.</returns>", " public Double ToMoney(Double @this)", " {", " return Math.Round(@this, 2);", " }", " }", "}" }; // Write the lines var folderPath = Test_Config.TestFolder(@"Text/cSharp/"); //@"C:\test\stream\header.txt"; var file = folderPath + "Types_Money.cs"; _lamed.lib.IO.RW.File_Write(file, source.ToArray(), true); #endregion bool error; ClassNTBlueprintRule_ methodRule; var Class1 = ClassNT_.Create(source, out error, out methodRule, file); var Class2 = ClassNT_.Create(file, out error, out methodRule); MethodNT_ method = Class1.Method_Find("public Double ToMoney(Double @this)"); string errorMsg; Assert.True(_lamed.Types.Object.IsEqual(Class1, Class2, out errorMsg), errorMsg); //Assert.Equal(Class1, Class2); Assert.NotNull(method); Assert.Equal("public Double ToMoney(Double @this)", method.Header.Method_HeaderLine); // Test exceptions var method1 = Class1.Method_Find("public Double ToMoney(Double @this)", true); var method2 = Class1.Method_Find("public Double ToMoney(Double @this)"); Assert.Equal(method, method1); Assert.Equal(null, method2); }
/// <summary> /// Parses the specified sourceLines. /// </summary> /// <param name="sourceLines">The sourceLines.</param> /// <param name="ii">The ii.</param> /// <param name="classRef">The class reference.</param> /// <param name="error">if set to <c>true</c> [error].</param> /// <param name="classHeader">The class header.</param> /// <param name="methods">The tag_ methods.</param> /// <param name="properties">The tag_ properties.</param> /// <param name="statistics">The statistics.</param> /// <param name="blueprintRule">The blueprint rule.</param> public static void Parse_Class(List <string> sourceLines, ref int ii, ClassNT_ classRef, out bool error, out ClassNTHeader_ classHeader, out List <MethodNT_> methods, out List <PropertyNT_> properties, out ClassNTStats_ statistics, out ClassNTBlueprintRule_ blueprintRule) { // ======================================================================================= // Note: The ii parameter is not used but is required by the ISourceCodeTemplate interface // ======================================================================================= // Create property classes methods = new List <MethodNT_>(); // Methods contained in the class properties = new List <PropertyNT_>(); blueprintRule = null; var Setup_RegionStack = new Stack <Tuple <string, int, bool> >(); // Region name, line no, //Setup_SourceCode = sourceLines; // Variables needed to parse the body error = false; int iBlocks = 0; //bool header = true; bool methodStart = false; bool commentStart = false; statistics = ClassNTStats_.Create(); statistics.ClassTotalLines = sourceLines.Count; //// Parse the header information //_Header = new sourceClassHeader_(sourceLines, ref ii, Tag_Statistics); classHeader = ClassNTHeader_.Create(sourceLines, out ii, statistics); if (classHeader != null) { blueprintRule = ClassNTBlueprintRule_.Create(classHeader.NameSpace_AttributeLines); } //ii++; //bool classHelp = false; string methodScope = ""; int iiMethodStart = ii; int iiMethodEnd = 0; // Body ==================================== for (; ii < sourceLines.Count; ii++) { string line = sourceLines[ii]; if (iBlocks == 0) { #region -[regions if (line.Contains("#region")) { var region = line; "#region ".zVar_Next(ref region); if (region == "") { region = "region"; // Needs unit test } Setup_RegionStack.Push(Tuple.Create(region, ii, true)); // True indicate that a new region was found } if (line.Contains("#endregion")) { Setup_RegionStack.Pop(); if (Setup_RegionStack.Count > 0) // Needs unit test { Tuple <string, int, bool> region = Setup_RegionStack.Pop(); Setup_RegionStack.Push(Tuple.Create(region.Item1, region.Item2, false)); // Change value to false of top item on stack } } #endregion if (!commentStart) { if (line.Contains("/// ") && !line.Contains("////")) { commentStart = true; iiMethodStart = ii; } } if (methodStart == false) { #region -[Method Start if (line.zContains_Any("(", ")") && // Most methods will have this line.zContains_Any(out methodScope, StringComparison.CurrentCulture, "private", "public", "internal") && // Most methods will have this line.zContains_All("=", "new") == false && // Methods will not have this line.zContains_Any("/// ", " class ", " get ") == false && // Methods header line will not have these line.Trim().Substring(0, 1) != "[") // This is a flag { if (line.zContains_All("(", "=") == false || line.IndexOf("=") >= line.IndexOf("(")) // If there is a '(' and '=' --> '(' must always be first { methodStart = true; if (commentStart == false) { iiMethodStart = ii; } //iBlocks = 0; } // For a method the ';' will always follow the '(' and the ')' if (line.zContains_All("(", ";") && line.IndexOf(";") <= line.IndexOf("(")) // Needs unit test { methodStart = false; } } else if (commentStart && (line.Contains("/// ") == false && line.Contains("[Pure]") == false)) { commentStart = false; } if (line.zContains_All("{", "}") == false) { continue; //<================================ } #endregion } } // This is a simple property if (line.zContains_All("get", "{", "??", "}") && line.Contains(".zContains_All") == false) // Ignore this line in myself { var test = line.Trim(); if (test.Length <= 2 || test.Substring(0, 2) != "//") // Make sure line is not commented out { var propertyLine = sourceLines[ii - 2].Trim(); var property = PropertyNT_.Create(propertyLine); properties.Add(property); } } // ================================================Method Body if (line.Contains("{")) { iBlocks++; commentStart = false; } #region -[Method End if (line.Contains("}") || (line.zContains_Any("private", "public", "internal") && line.Contains(");"))) // or contains private / public with ); // private static extern bool SetForegroundWindow(IntPtr hWnd); { if (line.Contains("}")) { iBlocks--; } if (iBlocks == 0) { iiMethodEnd = ii; #region debug //var debug = false; //if (debug) // || methodStart == false //{ // var source = LamedalCore_.Instance.Types.List.String.ToString(sourceLines, "".NL(), false, iiMethodStart,iiMethodEnd + 1); // ("Class Method found:" + source).zException_Show(action: enExceptionAction.ShowMessage); // break; //} #endregion if (methodStart == true) { methodStart = false; var method = MethodNT_.Create(sourceLines, ref iiMethodStart, iiMethodEnd, classRef); methods.Add(method); statistics.zUpdate(method); } } } #endregion } }