Skip to content

Commit

Permalink
Align tests with [Template] and align settings separately (#696)
Browse files Browse the repository at this point in the history
* Add support for aligning [Template] tests

* Align [Template] tests and align settings separately
  • Loading branch information
bhirsz authored May 20, 2024
1 parent 7d8b51f commit 6272c29
Show file tree
Hide file tree
Showing 19 changed files with 668 additions and 54 deletions.
34 changes: 34 additions & 0 deletions docs/releasenotes/unreleased/transformers.1.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
Aligning test cases with [Template] (#657)
------------------------------------------

Robotidy now aligns test cases with ``[Template]``. It is done by ``AlignTestCasesSection``.

Note that there is also ``AlignTemplatedTestCases`` which uses completely different approach for aligning test cases
and only supports suites with ``Test Template`` setting.

Previously, following code::

*** Test Cases ***
Testing Random List
[Template] Validate Random List Selection
${SIMPLE LIST} 2
${MIXED LIST} 3
${NESTED LIST} 4

would not be aligned and would be formatted to::

*** Test Cases ***
Testing Random List
[Template] Validate Random List Selection
${SIMPLE LIST} 2
${MIXED LIST} 3
${NESTED LIST} 4

Now test cases with ``[Template]`` are formatted properly::

*** Test Cases ***
Testing Random List
[Template] Validate Random List Selection
${SIMPLE LIST} 2
${MIXED LIST} 3
${NESTED LIST} 4
15 changes: 15 additions & 0 deletions docs/releasenotes/unreleased/transformers.2.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Align comments in AlignKeywordsSection and AlignTestCasesSection (#657)
-----------------------------------------------------------------------

It is now possible to align lines with comments in ``AlignKeywordsSection`` and ``AlignTestCasesSection``
transformers. Enable it by configuring ``align_comments`` parameter to ``True``.

It is especially useful if you want to use comments to name the aligned columns. For example::

*** Test Cases ***
Testing Random List
[Template] Validate Random List Selection
# collection nbr items
${SIMPLE LIST} 2 # first test
${MIXED LIST} 3 # second test
${NESTED LIST} 4 # third test
37 changes: 37 additions & 0 deletions docs/releasenotes/unreleased/transformers.3.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
Align settings separately in AlignKeywordsSection and AlignTestCasesSection (#657)
----------------------------------------------------------------------------------

It is now possible to align settings separately from rest of the code in keyword / test case. Configure it
using ``align_settings_separately`` parameter::

robotidy -c AlignKeywordsSection:align_settings_separately=True src
robotidy -c AlignTestCasesSection:align_settings_separately=True src

Since this type of alignment depends on the width of the column it only works together with ``auto`` alignment type.

For example following code::

*** Test Cases ***
Test Password Policy Minimum Length Input Errors
[Timeout] 10 min
[Tags] tag tag
Log ${argument_name}
Perform Action And Wait For Result ${argument_name}

It is by default (with ``alignment_type=auto`` and ``align_settings_separately=False``) formatted to::

*** Test Cases ***
Test Password Policy Minimum Length Input Errors
[Timeout] 10 min
[Tags] tag tag
Log ${argument_name}
Perform Action And Wait For Result ${argument_name}

With ``alignment_type=auto`` and ``align_settings_separately=True`` it is formatted to::

*** Test Cases ***
Test Password Policy Minimum Length Input Errors
[Timeout] 10 min
[Tags] tag tag
Log ${argument_name}
Perform Action And Wait ${argument_name}
63 changes: 62 additions & 1 deletion docs/source/transformers/AlignKeywordsSection.rst
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ See example (for ``fixed`` alignment type and default width ``24``):
*** Keywords ***
Keyword
# the wole line containing too long token is ignored
# the whole line containing too long token is ignored
${assign} Looooooooonger Keyword Name ${argument} Short
Short Short Short Short
Single
Expand Down Expand Up @@ -296,6 +296,67 @@ will result in the following transformation:
# fits, will be aligned but not split
Keyword argument
Align comments
---------------

Comments are not aligned by default. You can enable it by configuring ``align_comments``::

robotidy -c AlignKeywordsSection:align_comments=True src

It is especially useful if you want to use comments to name the aligned columns. For example::

*** Test Cases ***
Testing Random List
[Template] Validate Random List Selection
# collection nbr items
${SIMPLE LIST} 2 # first test
${MIXED LIST} 3 # second test
${NESTED LIST} 4 # third test

Align settings separately
-------------------------

Settings are aligned together with the rest of the code in the keyword. You can configure it to be aligned separately.
It allows you to use different widths of the columns for settings (if ``alignment_type`` is set to ``auto``).
You can enable it by configuring ``align_settings_separately``::

robotidy -c AlignKeywordsSection:alignment_type=auto:align_settings_separately=True src

.. tab-set::

.. tab-item:: Before

.. code:: robotframework
*** Keywords ***
Keyword
[Arguments] ${argument_name}
[Tags] tag tag
Log ${argument_name}
Perform Action And Wait For Result ${argument_name}
.. tab-item:: align_settings_separately=False (default)

.. code:: robotframework
*** Keywords ***
Keyword
[Arguments] ${argument_name}
[Tags] tag tag
Log ${argument_name}
Perform Action And Wait ${argument_name}
.. tab-item:: align_settings_separately=True

.. code:: robotframework
*** Keywords ***
Keyword
[Arguments] ${argument_name}
[Tags] tag tag
Log ${argument_name}
Perform Action And Wait ${argument_name}
Skip formatting
----------------
It is possible to use the following arguments to skip formatting of the code:
Expand Down
5 changes: 3 additions & 2 deletions docs/source/transformers/AlignTemplatedTestCases.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
AlignTemplatedTestCases
================================

Align templated Test Cases to columns.
Align suites with Test Template to columns.

For non-templated test cases use ``AlignTestCasesSection`` transformer.
For non-templated test cases use ``AlignTestCasesSection`` transformer. Test cases that are templated with
``[Template]`` setting should also use ``AlignTestCasesSection``.

.. |TRANSFORMERNAME| replace:: AlignTemplatedTestCases
.. include:: disabled_hint.txt
Expand Down
83 changes: 79 additions & 4 deletions docs/source/transformers/AlignTestCasesSection.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

AlignTestCasesSection
================================
Align ``*** Test Cases ***`` section to columns.

Align ``*** Test Cases ***`` section to columns.

.. |TRANSFORMERNAME| replace:: AlignTestCasesSection
.. include:: disabled_hint.txt

Align keyword calls and settings into columns with predefined width in non-templated test cases.
Align keyword calls and settings into columns with predefined width in test cases.
There are two possible alignment types (configurable via ``alignment_type``):

- ``fixed`` (default): pad the tokens to the fixed width of the column
Expand All @@ -20,8 +20,8 @@ The width of the column sets limit to the maximum width of the column. Default w
With ``fixed`` alignment each column have fixed width (and tokens that does not fit
go into ``overflow`` state - see :ref:`overflow tests`).

``auto`` alignment align tokens to the longest token in the column - the column width can be shorter than
configured width (but no longer).
``auto`` alignment align tokens to the longest token in the column - the column width can be shorter or equal to
configured width.

See examples of the alignment types:

Expand All @@ -38,6 +38,11 @@ See examples of the alignment types:
... arg
... value
Templated test case
[Template] Templated Keyword
first_arg second_arg
value ${500}
.. tab-item:: fixed (default)

.. code:: robotframework
Expand All @@ -49,6 +54,11 @@ See examples of the alignment types:
... arg
... value
Templated test case
[Template] Templated Keyword
first_arg second_arg
value ${500}
.. tab-item:: auto

.. code:: robotframework
Expand All @@ -60,6 +70,11 @@ See examples of the alignment types:
... arg
... value
Templated test case
[Template] Templated Keyword
first_arg second_arg
value ${500}
The ``auto`` alignment often leads to more compact code. But ``fixed`` setting offers more stability - adding new,
slightly longer variable or keyword call will not change alignment of the other lines.

Expand Down Expand Up @@ -296,6 +311,66 @@ will result in the following transformation:
# fits, will be aligned but not split
Keyword argument
Align comments
---------------

Comments are not aligned by default. You can enable it by configuring ``align_comments``::

robotidy -c AlignTestCasesSection:align_comments=True src

It is especially useful if you want to use comments to name the aligned columns. For example::

*** Test Cases ***
Testing Random List
[Template] Validate Random List Selection
# collection nbr items
${SIMPLE LIST} 2 # first test
${MIXED LIST} 3 # second test
${NESTED LIST} 4 # third test

Align settings separately
-------------------------

Settings are aligned together with the rest of the code in the keyword. You can configure it to be aligned separately.
It allows you to use different widths of the columns for settings (if ``alignment_type`` is set to ``auto``).
You can enable it by configuring ``align_settings_separately``::

robotidy -c AlignTestCasesSection:alignment_type=auto:align_settings_separately=True src

.. tab-set::

.. tab-item:: Before

.. code:: robotframework
*** Test Cases ***
Test Password Policy Minimum Length Input Errors
[Timeout] 10 min
[Tags] tag tag
Log ${argument_name}
Perform Action And Wait For Result ${argument_name}
.. tab-item:: align_settings_separately=False (default)

.. code:: robotframework
*** Test Cases ***
Test Password Policy Minimum Length Input Errors
[Timeout] 10 min
[Tags] tag tag
Log ${argument_name}
Perform Action And Wait For Result ${argument_name}
.. tab-item:: align_settings_separately=True

.. code:: robotframework
*** Test Cases ***
Test Password Policy Minimum Length Input Errors
[Timeout] 10 min
[Tags] tag tag
Log ${argument_name}
Perform Action And Wait ${argument_name}
Skip formatting
----------------
Expand Down
12 changes: 11 additions & 1 deletion robotidy/transformers/AlignKeywordsSection.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,20 @@ def __init__(
alignment_type: str = "fixed",
handle_too_long: str = "overflow",
compact_overflow_limit: int = 2,
align_comments: bool = False,
align_settings_separately: bool = False,
skip_documentation: str = "True", # noqa - override skip_documentation from Skip
skip: Skip = None,
):
super().__init__(widths, alignment_type, handle_too_long, compact_overflow_limit, skip)
super().__init__(
widths,
alignment_type,
handle_too_long,
compact_overflow_limit,
align_comments,
align_settings_separately,
skip,
)

@skip_if_disabled
def visit_Keyword(self, node): # noqa
Expand Down
14 changes: 12 additions & 2 deletions robotidy/transformers/AlignTestCasesSection.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,25 @@ def __init__(
alignment_type: str = "fixed",
handle_too_long: str = "overflow",
compact_overflow_limit: int = 2,
align_comments: bool = False,
align_settings_separately: bool = False,
skip_documentation: str = "True", # noqa - override skip_documentation from Skip
skip: Skip = None,
):
super().__init__(widths, alignment_type, handle_too_long, compact_overflow_limit, skip)
super().__init__(
widths,
alignment_type,
handle_too_long,
compact_overflow_limit,
align_comments,
align_settings_separately,
skip,
)

def visit_File(self, node): # noqa
if is_suite_templated(node):
return node
return self.generic_visit(node)
super().visit_File(node)

@skip_if_disabled
def visit_TestCase(self, node): # noqa
Expand Down
Loading

0 comments on commit 6272c29

Please sign in to comment.