/// <summary> /// Checks that a single line of input text is formatted as expected. /// </summary> /// <param name="text">Input code to format</param> /// <param name="expected">The expected result from the formatter. If null, then text is used.</param> /// <param name="line">The line number to request to be formatted.</param> /// <param name="languageVersion">Python language version to format.</param> /// <param name="editStart">Where the edit should begin (i.e. when whitespace or a multi-line string begins a line).</param> public static void AssertSingleLineFormat(string text, string expected = null, int line = 1, PythonLanguageVersion languageVersion = PythonLanguageVersion.V37, int editStart = 1) { if (text == null) { throw new ArgumentNullException(nameof(text)); } if (expected == null) { expected = text; } using (var reader = new StringReader(text)) { var lineFormatter = new LineFormatter(reader, languageVersion); var edits = lineFormatter.FormatLine(line); edits.Should().OnlyContain(new TextEdit { newText = expected, range = new Range { start = new SourceLocation(line, editStart), end = new SourceLocation(line, text.Split('\n')[line - 1].Length + 1) } }); } }
public void FormatLineTest(string lm0, string ts0, string s0, string[] s1) { var d = new LineFormatter(new LineMargins(lm0), new TabStops(ts0)); var r1 = d.FormatLine(s0); Assert.Equal(s1, r1); }
public static void AssertNoEdits(string text, int line = 1, PythonLanguageVersion languageVersion = PythonLanguageVersion.V37) { if (text == null) { throw new ArgumentNullException(nameof(text)); } using (var reader = new StringReader(text)) { var lineFormatter = new LineFormatter(reader, languageVersion); lineFormatter.FormatLine(line).Should().BeEmpty(); } }
public void GrammarFile() { var src = TestData.GetPath("TestData", "Formatting", "pythonGrammar.py"); string fileContents; using (var reader = new StreamReader(src, true)) { fileContents = reader.ReadToEnd(); } var lines = fileContents.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None); using (var reader = new StringReader(fileContents)) { var lineFormatter = new LineFormatter(reader, PythonLanguageVersion.V37); for (var i = 0; i < lines.Length; i++) { var lineNum = i + 1; var edits = lineFormatter.FormatLine(lineNum); edits.Should().NotBeNull().And.HaveCountLessOrEqualTo(1); if (edits.Length == 0) { continue; } var edit = edits[0]; var start = edit.range.start; var end = edit.range.end; start.line.Should().Be(i); end.line.Should().Be(i); var lineText = lines[i]; edit.newText.Should().Be(lineText.Substring(start.character, end.character - start.character - 1), $"because line {lineNum} should be unchanged"); } } }