public static void ReadAndTest(string filePath, CSharpFormattingOptions policy = null, TextEditorOptions options = null) { if (File.Exists(filePath)) { filePath = Path.GetFullPath(filePath); var code = File.ReadAllText(filePath); var document = new ReadOnlyDocument(code); if (policy == null) { policy = FormattingOptionsFactory.CreateMono(); policy.AlignToFirstIndexerArgument = policy.AlignToFirstMethodCallArgument = true; } options = options ?? new TextEditorOptions { IndentBlankLines = false, EolMarker = "\n" }; var engine = new CacheIndentEngine(new CSharpIndentEngine(document, options, policy) { EnableCustomIndentLevels = true }); int errors = 0; foreach (var ch in code) { if (options.EolMarker[0] == ch) { if (!(engine.LineBeganInsideMultiLineComment || engine.LineBeganInsideVerbatimString)) { if (engine.CurrentIndent.Length > 0) { if (engine.NeedsReindent) { errors++; Console.WriteLine(string.Format("Indent: {2}, Current indent: {3} in {0}:{1}", filePath, engine.Location.Line, engine.ThisLineIndent.Length, engine.CurrentIndent.Length)); } } } } engine.Push(ch); } Assert.AreEqual(0, errors); } else { Assert.Fail("File " + filePath + " doesn't exist."); } }
public static void ReadAndTest(string filePath, OptionSet options = null) { if (File.Exists(filePath)) { filePath = Path.GetFullPath(filePath); var code = File.ReadAllText(filePath); var document = SourceText.From(code); if (options == null) { options = FormattingOptionsFactory.CreateMono(); //policy.AlignToFirstIndexerArgument = policy.AlignToFirstMethodCallArgument = true; } var engine = new CacheIndentEngine(new CSharpIndentEngine(options) { EnableCustomIndentLevels = true }); int errors = 0; var newLine = options.GetOption(FormattingOptions.NewLine, LanguageNames.CSharp); foreach (var ch in code) { if (newLine [0] == ch) { if (!(engine.LineBeganInsideMultiLineComment || engine.LineBeganInsideVerbatimString)) { if (engine.CurrentIndent.Length > 0) { if (engine.NeedsReindent) { errors++; var line = document.Lines.GetLineFromPosition(engine.Offset); Console.WriteLine(string.Format("Indent: {2}, Current indent: {3} in {0}:{1}", filePath, line.LineNumber, GetIndentString(engine.ThisLineIndent), GetIndentString(engine.CurrentIndent))); } } } } engine.Push(ch); } Assert.AreEqual(0, errors, "file has errors"); } else { Assert.Fail("File " + filePath + " doesn't exist."); } }