Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.

Port regex-based block formatting from vscode-python #217

Merged
merged 5 commits into from
Oct 9, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
305 changes: 305 additions & 0 deletions src/Analysis/Engine/Test/BlockFormatterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,305 @@
// Python Tools for Visual Studio
// Copyright(c) Microsoft Corporation
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the License); you may not use
// this file except in compliance with the License. You may obtain a copy of the
// License at http://www.apache.org/licenses/LICENSE-2.0
//
// THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
// OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY
// IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
// MERCHANTABLITY OR NON-INFRINGEMENT.
//
// See the Apache Version 2.0 License for specific language governing
// permissions and limitations under the License.

using System;
using System.IO;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.Python.LanguageServer;
using Microsoft.Python.LanguageServer.Implementation;
using Microsoft.PythonTools.Analysis;
using Microsoft.PythonTools.Analysis.FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TestUtilities;

namespace AnalysisTests {
[TestClass]
public class BlockFormatterTests {
public TestContext TestContext { get; set; }

[TestInitialize]
public void TestInitialize() {
TestEnvironmentImpl.TestInitialize($"{TestContext.FullyQualifiedTestClassName}.{TestContext.TestName}");
}

[TestCleanup]
public void TestCleanup() {
TestEnvironmentImpl.TestCleanup();
}

[TestMethod, Priority(0)]
public void NullReader() {
Func<Task<TextEdit[]>> func = () => BlockFormatter.ProvideEdits(null, new Position(), new FormattingOptions());
func.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be("reader");
}

[TestMethod, Priority(0)]
public async Task FirstLine() {
using (var reader = new StringReader("")) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need using for StringReader when it is used explicitly. It doesn't hold any resources.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can certainly remove it, but I'm a little bit wary of not disposing of it just because we know it doesn't do anything given that a future .NET release could easily implement it differently.

Copy link

@MikhailArkhipov MikhailArkhipov Oct 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you are really concerned about resources, then use string.Empty and not "" ;-)
Actually, I believe it used to be disposable and held resources back in a day, but not anymore and no one disposes of it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's disposable because it's a subclass of TextReader, which is IDisposable.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll fix that string.Empty thing though. Muscle memory from other languages which don't allocate an empty string...

var edits = await BlockFormatter.ProvideEdits(reader, new Position { line = 0, character = 4 }, new FormattingOptions());
edits.Should().BeEmpty();
}
}

[TestMethod, Priority(0)]
public void TooShort() {
using (var reader = new StringReader("a + b")) {
Func<Task<TextEdit[]>> func = () => BlockFormatter.ProvideEdits(reader, new Position { line = 1, character = 4 }, new FormattingOptions());
func.Should().Throw<ArgumentException>().And.ParamName.Should().Be("position");
}
}

[TestMethod, Priority(0)]
public async Task NoMatch() {
var code = @"d = {
'a': a,
'b':
";
using (var reader = new StringReader(code)) {
var edits = await BlockFormatter.ProvideEdits(reader, new Position { line = 2, character = 7 }, new FormattingOptions());
edits.Should().BeEmpty();
}
}


[DataRow("elseBlocksFirstLine2.py", 3, 7, true, 2, 0, 2)]
[DataRow("elseBlocksFirstLine4.py", 3, 9, true, 4, 0, 4)]
[DataRow("elseBlocksFirstLineTab.py", 3, 6, false, 4, 0, 1)]
[DataTestMethod, Priority(0)]
public async Task ElseBlock(string filename, int line, int col, bool insertSpaces, int tabSize, int startCharacter, int endCharacter) {
var position = new Position { line = line, character = col };
var options = new FormattingOptions { insertSpaces = insertSpaces, tabSize = tabSize };

var src = TestData.GetPath("TestData", "Formatting", filename);

using (var reader = new StreamReader(src)) {
var edits = await BlockFormatter.ProvideEdits(reader, position, options);
edits.Should().OnlyHaveTextEdit("", (line, startCharacter, line, endCharacter));
}
}

[DataRow(6, 22, 0, 2)]
[DataRow(35, 13, 0, 2)]
[DataRow(54, 19, 0, 2)]
[DataRow(76, 9, 0, 2)]
[DataRow(143, 22, 0, 2)]
[DataRow(172, 11, 0, 2)]
[DataRow(195, 12, 0, 2)]
[DataTestMethod, Priority(0)]
public async Task TryBlockTwoSpace(int line, int col, int startCharacter, int endCharacter) {
var position = new Position { line = line, character = col };
var options = new FormattingOptions { insertSpaces = true, tabSize = 2 };

var src = TestData.GetPath("TestData", "Formatting", "tryBlocks2.py");

using (var reader = new StreamReader(src)) {
var edits = await BlockFormatter.ProvideEdits(reader, position, options);
edits.Should().OnlyHaveTextEdit("", (line, startCharacter, line, endCharacter));
}
}

[DataRow(15, 21)]
[DataRow(47, 12)]
[DataRow(157, 25)]
[DataTestMethod, Priority(0)]
public async Task TryBlockTwoSpaceNoEdits(int line, int col) {
var position = new Position { line = line, character = col };
var options = new FormattingOptions { insertSpaces = true, tabSize = 2 };

var src = TestData.GetPath("TestData", "Formatting", "tryBlocks2.py");

using (var reader = new StreamReader(src)) {
var edits = await BlockFormatter.ProvideEdits(reader, position, options);
edits.Should().BeEmpty();
}
}

[DataRow(6, 22, 0, 4)]
[DataRow(35, 13, 0, 4)]
[DataRow(54, 19, 0, 4)]
[DataRow(76, 9, 0, 4)]
[DataRow(143, 22, 0, 4)]
[DataRow(172, 11, 0, 4)]
[DataRow(195, 12, 0, 4)]
[DataTestMethod, Priority(0)]
public async Task TryBlockFourSpace(int line, int col, int startCharacter, int endCharacter) {
var position = new Position { line = line, character = col };
var options = new FormattingOptions { insertSpaces = true, tabSize = 4 };

var src = TestData.GetPath("TestData", "Formatting", "tryBlocks4.py");

using (var reader = new StreamReader(src)) {
var edits = await BlockFormatter.ProvideEdits(reader, position, options);
edits.Should().OnlyHaveTextEdit("", (line, startCharacter, line, endCharacter));
}
}

[DataRow(15, 21)]
[DataRow(47, 12)]
[DataRow(157, 25)]
[DataTestMethod, Priority(0)]
public async Task TryBlockFourSpaceNoEdits(int line, int col) {
var position = new Position { line = line, character = col };
var options = new FormattingOptions { insertSpaces = true, tabSize = 4 };

var src = TestData.GetPath("TestData", "Formatting", "tryBlocks4.py");

using (var reader = new StreamReader(src)) {
var edits = await BlockFormatter.ProvideEdits(reader, position, options);
edits.Should().BeEmpty();
}
}

[DataRow(6, 22, 0, 2)]
[DataRow(35, 13, 0, 2)]
[DataRow(54, 19, 0, 2)]
[DataRow(76, 9, 0, 2)]
[DataRow(143, 22, 0, 2)]
[DataRow(172, 11, 0, 3)]
[DataRow(195, 12, 0, 2)]
[DataTestMethod, Priority(0)]
public async Task TryBlockTab(int line, int col, int startCharacter, int endCharacter) {
var position = new Position { line = line, character = col };
var options = new FormattingOptions { insertSpaces = false, tabSize = 4 };

var src = TestData.GetPath("TestData", "Formatting", "tryBlocksTab.py");
var newText = new string('\t', endCharacter - 1);

using (var reader = new StreamReader(src)) {
var edits = await BlockFormatter.ProvideEdits(reader, position, options);
edits.Should().OnlyHaveTextEdit(newText, (line, startCharacter, line, endCharacter));
}
}

[DataRow(4, 18, 0, 2)]
[DataRow(7, 18, 0, 2)]
[DataRow(21, 18, 0, 2)]
[DataRow(38, 7, 0, 2)]
[DataRow(47, 13, 0, 2)]
[DataRow(57, 9, 0, 2)]
[DataRow(66, 20, 0, 2)]
[DataRow(69, 20, 0, 2)]
[DataRow(83, 20, 0, 2)]
[DataRow(109, 15, 0, 2)]
[DataRow(119, 11, 0, 2)]
[DataRow(134, 9, 0, 2)]
[DataTestMethod, Priority(0)]
public async Task ElseBlockTwoSpace(int line, int col, int startCharacter, int endCharacter) {
var position = new Position { line = line, character = col };
var options = new FormattingOptions { insertSpaces = true, tabSize = 2 };

var src = TestData.GetPath("TestData", "Formatting", "elseBlocks2.py");

using (var reader = new StreamReader(src)) {
var edits = await BlockFormatter.ProvideEdits(reader, position, options);
edits.Should().OnlyHaveTextEdit("", (line, startCharacter, line, endCharacter));
}
}

[DataRow(345, 18)]
[DataRow(359, 18)]
[DataTestMethod, Priority(0)]
public async Task ElseBlockTwoSpaceNoEdits(int line, int col) {
var position = new Position { line = line, character = col };
var options = new FormattingOptions { insertSpaces = true, tabSize = 2 };

var src = TestData.GetPath("TestData", "Formatting", "elseBlocks2.py");

using (var reader = new StreamReader(src)) {
var edits = await BlockFormatter.ProvideEdits(reader, position, options);
edits.Should().BeEmpty();
}
}

[DataRow(4, 18, 0, 4)]
[DataRow(7, 18, 0, 4)]
[DataRow(21, 18, 0, 4)]
[DataRow(38, 7, 0, 4)]
[DataRow(47, 13, 0, 4)]
[DataRow(57, 9, 0, 4)]
[DataRow(66, 20, 0, 4)]
[DataRow(69, 20, 0, 4)]
[DataRow(83, 20, 0, 4)]
[DataRow(109, 15, 0, 4)]
[DataRow(119, 11, 0, 4)]
[DataRow(134, 9, 0, 4)]
[DataTestMethod, Priority(0)]
public async Task ElseBlockFourSpace(int line, int col, int startCharacter, int endCharacter) {
var position = new Position { line = line, character = col };
var options = new FormattingOptions { insertSpaces = true, tabSize = 4 };

var src = TestData.GetPath("TestData", "Formatting", "elseBlocks4.py");

using (var reader = new StreamReader(src)) {
var edits = await BlockFormatter.ProvideEdits(reader, position, options);
edits.Should().OnlyHaveTextEdit("", (line, startCharacter, line, endCharacter));
}
}

[DataRow(345, 18)]
[DataTestMethod, Priority(0)]
public async Task ElseBlockFourSpaceNoEdits(int line, int col) {
var position = new Position { line = line, character = col };
var options = new FormattingOptions { insertSpaces = true, tabSize = 4 };

var src = TestData.GetPath("TestData", "Formatting", "elseBlocks4.py");

using (var reader = new StreamReader(src)) {
var edits = await BlockFormatter.ProvideEdits(reader, position, options);
edits.Should().BeEmpty();
}
}

[DataRow(4, 18, 0, 1)]
[DataRow(7, 18, 0, 1)]
[DataRow(21, 18, 0, 1)]
[DataRow(38, 7, 0, 1)]
[DataRow(47, 13, 0, 1)]
[DataRow(57, 9, 0, 1)]
[DataRow(66, 20, 0, 1)]
[DataRow(69, 20, 0, 1)]
[DataRow(83, 20, 0, 1)]
[DataRow(109, 15, 0, 1)]
[DataRow(119, 11, 0, 1)]
[DataRow(134, 9, 0, 1)]
[DataTestMethod, Priority(0)]
public async Task ElseBlockTab(int line, int col, int startCharacter, int endCharacter) {
var position = new Position { line = line, character = col };
var options = new FormattingOptions { insertSpaces = true, tabSize = 2 };

var src = TestData.GetPath("TestData", "Formatting", "elseBlocksTab.py");

using (var reader = new StreamReader(src)) {
var edits = await BlockFormatter.ProvideEdits(reader, position, options);
edits.Should().OnlyHaveTextEdit("", (line, startCharacter, line, endCharacter));
}
}

[DataRow(345, 18)]
[DataTestMethod, Priority(0)]
public async Task ElseBlockTabNoEdits(int line, int col) {
var position = new Position { line = line, character = col };
var options = new FormattingOptions { insertSpaces = true, tabSize = 2 };

var src = TestData.GetPath("TestData", "Formatting", "elseBlocksTab.py");

using (var reader = new StreamReader(src)) {
var edits = await BlockFormatter.ProvideEdits(reader, position, options);
edits.Should().BeEmpty();
}
}
}
}
7 changes: 7 additions & 0 deletions src/Analysis/Engine/Test/LanguageServerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,13 @@ public async Task OnTypeFormatting() {
edits = await s.SendDocumentOnTypeFormatting(uri, new SourceLocation(4, 1), "\n");
edits.Should().OnlyHaveTextEdit("x += 1", (2, 4, 2, 9));
}

using (var s = await CreateServer()) {
var uri = await AddModule(s, "if x:\n pass\n else:");

var edits = await s.SendDocumentOnTypeFormatting(uri, new SourceLocation(3, 9), ":");
edits.Should().OnlyHaveTextEdit("", (2, 0, 2, 4));
}
}

class GetAllExtensionProvider : ILanguageServerExtensionProvider {
Expand Down
Loading