//[Ignore("Waiting on Actipro's next release.")] public void TestCommentOrdering() { CSharpFormatSettings settings = new CSharpFormatSettings(); settings.ReorderBaseConstructs = true; TestFile("CSharpFormatter\\Test Files\\CommentOrdering.cs", true, settings); }
public void Comment_Blocks_Set_To_True() { const string methodBody = "// Comment \n /* Another Comment */ \n return 0xFFFFFFFF + 1;"; string expectedText = "{\n\t/* Comment */\n\t/* Another Comment */\n\treturn 0xFFFFFFFF + 1;\n}\n".Replace("\n", Environment.NewLine); CSharpFormatSettings settings = new CSharpFormatSettings(); settings.CommentLinesAsCommentBlock = true; TestBodyText(methodBody, expectedText, settings); }
public void As_Part_Of_A_Single_Line_Block_With_No_Braces() { const string methodBody = "if (true)\n// Comment\nActualStatement();"; string expectedText = "{\n\tif (true) \n\t\t// Comment\n\t\tActualStatement();\n}\n".Replace("\n", Environment.NewLine); CSharpFormatSettings settings = new CSharpFormatSettings(); settings.AddBracesToSingleLineBlocks = false; settings.SingleLineBlocksOnSameLineAsParent = false; TestBodyText(methodBody, expectedText, settings); }
///<summary> /// Copies the format settings from the given object. ///</summary> ///<param name="formatSettings">The object to copy the settings from.</param> public void SetFrom(CSharpFormatSettings formatSettings) { AddBracesToSingleLineBlocks = formatSettings.AddBracesToSingleLineBlocks; InlineSingleLineGettersAndSetters = formatSettings.InlineSingleLineGettersAndSetters; MaintainWhitespace = formatSettings.MaintainWhitespace; OmitEmptyStatements = formatSettings.OmitEmptyStatements; PutBracesOnNewLines = formatSettings.PutBracesOnNewLines; SingleLineBlocksOnSameLineAsParent = formatSettings.SingleLineBlocksOnSameLineAsParent; ReorderBaseConstructs = formatSettings.ReorderBaseConstructs; CommentLinesAsCommentBlock = formatSettings.CommentLinesAsCommentBlock; }
public static CSharpParser TestFullText(string code, string expectedText, CSharpFormatSettings formatSettings) { CSharpParser parser = new CSharpParser(); parser.FormatSettings.SetFrom(formatSettings); parser.ParseCode(code); Assert.That(parser.ErrorOccurred, Is.False, "Parser errors occurred:\n" + GetSyntaxErrors(parser)); Assert.That(parser.CreatedCodeRoot.ToString(), Is.EqualTo(expectedText)); return(parser); }
public void EmptyStatementsRemoved() { const string methodBody = "string i = \"\"; \r\n;"; string expectedText = "{\n\tstring i = \"\";\n}\n".Replace("\n", Environment.NewLine); CSharpFormatSettings settings = new CSharpFormatSettings(); settings.OmitEmptyStatements = true; TestBodyText(methodBody, expectedText, settings); expectedText = "{\n\tstring i = \"\";\n\t;\n}\n".Replace("\n", Environment.NewLine); settings.OmitEmptyStatements = false; TestBodyText(methodBody, expectedText, settings); }
public void WhileStatements() { const string methodBody = @" while(true) { if(false) continue; if(true) break; }"; string expectedText = "{\n\twhile(true)\n\t{\n\t\tif (false) continue;\n\t\tif (true) break;\n\t}\n}\n" .Replace("\n", Environment.NewLine); CSharpFormatSettings settings = new CSharpFormatSettings(); settings.AddBracesToSingleLineBlocks = false; settings.SingleLineBlocksOnSameLineAsParent = true; TestBodyText(methodBody, expectedText, settings); }
public static void TestBodyText(string methodBody, string expectedText, CSharpFormatSettings formatSettings) { string code = string.Format("public class Class1\r\n{0}\r\npublic void Method1(string param1)\r\n{0}\r\n{2}\r\n{1}\r\n{1}", '{', '}', methodBody); CSharpParser parser = new CSharpParser(); parser.FormatSettings.SetFrom(formatSettings); parser.ParseCode(code); Assert.That(parser.ErrorOccurred, Is.False, "Parser errors occurred:\n" + GetSyntaxErrors(parser)); ICodeRoot codeRoot = parser.CreatedCodeRoot; Class clazz = (Class)codeRoot.WalkChildren()[0]; Function con = (Function)clazz.WalkChildren()[0]; Assert.That(con.Name, Is.EqualTo("Method1")); Assert.That(con.BodyText, Is.EqualTo(expectedText)); }
public void TestFile(string filename, bool stripText, CSharpFormatSettings settings) { string fileText = File.ReadAllText(filename); CSharpParser formatter = new CSharpParser(); formatter.FormatSettings.SetFrom(settings); formatter.ParseCode(fileText); Assert.IsFalse(formatter.ErrorOccurred); string formattedText = formatter.CreatedCodeRoot.ToString(); formattedText = Slyce.Common.Utility.StandardizeLineBreaks(formattedText, Slyce.Common.Utility.LineBreaks.Windows); string strippedFileText = Diff.StripWhitespace(new[] { fileText }, stripText)[0].Replace("\t", " "); string strippedFormattedText = Diff.StripWhitespace(new[] { formattedText }, stripText)[0].Replace("\t", " "); Assert.That(strippedFormattedText, Is.EqualTo(strippedFileText)); }
public void AddBracesToSingleLineBlocks() { const string methodBody = "if (true) return; else Console.WriteLine();"; string expectedText = "{\n\tif (true) \n\t{\n\t\treturn;\n\t}\n\telse \n\t{\n\t\tConsole.WriteLine();\n\t}\n}\n" .Replace("\n", Environment.NewLine); CSharpFormatSettings settings = new CSharpFormatSettings(); settings.AddBracesToSingleLineBlocks = true; settings.PutBracesOnNewLines = true; TestBodyText(methodBody, expectedText, settings); expectedText = "{\n\tif (true) return;\n\telse Console.WriteLine();\n}\n".Replace("\n", Environment.NewLine); settings.AddBracesToSingleLineBlocks = false; settings.SingleLineBlocksOnSameLineAsParent = true; TestBodyText(methodBody, expectedText, settings); expectedText = "{\n\tif (true) \n\t\treturn;\n\telse \n\t\tConsole.WriteLine();\n}\n".Replace("\n", Environment.NewLine); settings.AddBracesToSingleLineBlocks = false; settings.SingleLineBlocksOnSameLineAsParent = false; TestBodyText(methodBody, expectedText, settings); }