Skip to content

Commit

Permalink
Testing: Check that the max_line_length is working when formating code
Browse files Browse the repository at this point in the history
  • Loading branch information
ccordoba12 committed Mar 29, 2022
1 parent 82cc322 commit b531830
Show file tree
Hide file tree
Showing 4 changed files with 262 additions and 13 deletions.
56 changes: 56 additions & 0 deletions spyder/plugins/editor/widgets/tests/assets/autopep8_max_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""

import os
import sys


# %% functions
def d():
def inner(): return 2
return inner
# ---- func 1 and 2


def func1():
for i in range(3):
print(i)


def func2():
if True:
pass
# ---- other functions


def a():
pass


def b():
pass


def c():
pass


# %% classes
class Class1:
def __init__(self):
super(
Class1, self).__init__()
self.x = 2

def method3(self):
pass

def method2(self):
pass

def method1(self):
pass
70 changes: 70 additions & 0 deletions spyder/plugins/editor/widgets/tests/assets/black_max_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""

import os
import sys


# %% functions
def d():
def inner():
return 2

return inner


# ---- func 1 and 2
def func1():
for i in range(
3
):
print(i)


def func2():
if True:
pass


# ---- other functions
def a():
pass


def b():
pass


def c():
pass


# %% classes
class Class1:
def __init__(
self,
):
super(
Class1,
self,
).__init__()
self.x = 2

def method3(
self,
):
pass

def method2(
self,
):
pass

def method1(
self,
):
pass
68 changes: 68 additions & 0 deletions spyder/plugins/editor/widgets/tests/assets/yapf_max_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""

import os
import sys


# %% functions
def d():

def inner():
return 2

return inner


# ---- func 1 and 2
def func1():
for i in range(
3):
print(i)


def func2():
if True:
pass


# ---- other functions
def a():
pass


def b():
pass


def c():
pass


# %% classes
class Class1:

def __init__(
self):
super(
Class1,
self
).__init__(
)
self.x = 2

def method3(
self):
pass

def method2(
self):
pass

def method1(
self):
pass
81 changes: 68 additions & 13 deletions spyder/plugins/editor/widgets/tests/test_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@

# Third party imports
import pytest
import yapf

# Qt imports
from qtpy.QtGui import QTextCursor
import yapf

# Local imports
from spyder.config.manager import CONF
from spyder.utils.programs import is_module_installed


# ---- Auxiliary constants and functions
HERE = osp.dirname(osp.abspath(__file__))
ASSETS = osp.join(HERE, 'assets')


autopep8 = pytest.param(
'autopep8',
marks=pytest.mark.skipif(
Expand All @@ -50,8 +50,14 @@
)


def get_formatter_values(formatter, newline, range_fmt=False):
suffix = 'range' if range_fmt else 'result'
def get_formatter_values(formatter, newline, range_fmt=False, max_line=False):
if range_fmt:
suffix = 'range'
elif max_line:
suffix = 'max_line'
else:
suffix = 'result'

original_file = osp.join(ASSETS, 'original_file.py')
formatted_file = osp.join(ASSETS, '{0}_{1}.py'.format(formatter, suffix))

Expand All @@ -66,6 +72,7 @@ def get_formatter_values(formatter, newline, range_fmt=False):
return text, result


# ---- Tests
@pytest.mark.slow
@pytest.mark.order(1)
@pytest.mark.parametrize('formatter', [autopep8, yapf, black])
Expand All @@ -76,10 +83,13 @@ def test_document_formatting(formatter, newline, completions_codeeditor,
code_editor, completion_plugin = completions_codeeditor
text, expected = get_formatter_values(formatter, newline)

# After this call the manager needs to be reinitialized
CONF.set('completions',
('provider_configuration', 'lsp', 'values','formatting'),
formatter)
# Set formatter
CONF.set(
'completions',
('provider_configuration', 'lsp', 'values','formatting'),
formatter
)
completion_plugin.after_configuration_update([])
qtbot.wait(2000)

# Set text in editor
Expand Down Expand Up @@ -114,10 +124,12 @@ def test_document_range_formatting(formatter, newline, completions_codeeditor,
code_editor, completion_plugin = completions_codeeditor
text, expected = get_formatter_values(formatter, newline, range_fmt=True)

# After this call the manager needs to be reinitialized
CONF.set('completions',
('provider_configuration', 'lsp', 'values','formatting'),
formatter)
# Set formatter
CONF.set(
'completions',
('provider_configuration', 'lsp', 'values','formatting'),
formatter
)
completion_plugin.after_configuration_update([])
qtbot.wait(2000)

Expand Down Expand Up @@ -151,3 +163,46 @@ def test_document_range_formatting(formatter, newline, completions_codeeditor,
# Wait to text to be formatted
qtbot.wait(2000)
assert code_editor.get_text_with_eol() == expected


@pytest.mark.slow
@pytest.mark.order(1)
@pytest.mark.parametrize('formatter', [autopep8, black])
def test_max_line_length(formatter, completions_codeeditor, qtbot):
"""Validate autoformatting with a different value of max_line_length."""
code_editor, completion_plugin = completions_codeeditor
text, expected = get_formatter_values(
formatter, newline='\n', max_line=True)
max_line_length = 20

# Set formatter and max line length options
CONF.set(
'completions',
('provider_configuration', 'lsp', 'values', 'formatting'),
formatter
)
CONF.set(
'completions',
('provider_configuration', 'lsp', 'values', 'pycodestyle/max_line_length'),
max_line_length
)
completion_plugin.after_configuration_update([])
qtbot.wait(2000)

# Set text in editor
code_editor.set_text(text)

# Notify changes
with qtbot.waitSignal(
code_editor.completions_response_signal, timeout=30000):
code_editor.document_did_change()

# Perform formatting
with qtbot.waitSignal(
code_editor.completions_response_signal, timeout=30000):
code_editor.format_document()

# Wait for text to be formatted
qtbot.wait(2000)

assert code_editor.get_text_with_eol() == expected

0 comments on commit b531830

Please sign in to comment.