Skip to content

Commit

Permalink
Show how @parameterized could replace @data_provider
Browse files Browse the repository at this point in the history
  • Loading branch information
lpetre committed Nov 19, 2021
1 parent d0f8fa9 commit 842aab3
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 75 deletions.
149 changes: 74 additions & 75 deletions libcst/_nodes/tests/test_assert.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,47 @@

from typing import Any

from parameterized import parameterized, param

import libcst as cst
from libcst import parse_statement
from libcst._nodes.tests.base import CSTNodeTest
from libcst.helpers import ensure_type
from libcst.metadata import CodeRange
from libcst.testing.utils import data_provider


class AssertConstructionTest(CSTNodeTest):
@data_provider(
@parameterized.expand(
(
# Simple assert
{
"node": cst.Assert(cst.Name("True")),
"code": "assert True",
"parser": None,
"expected_position": None,
},
# Assert with message
{
"node": cst.Assert(
param(
"Simple assert",
node=cst.Assert(cst.Name("True")),
code="assert True",
parser=None,
expected_position=None,
),
param(
"Assert with message",
node=cst.Assert(
cst.Name("True"), cst.SimpleString('"Value should be true"')
),
"code": 'assert True, "Value should be true"',
"parser": None,
"expected_position": None,
},
# Whitespace oddities test
{
"node": cst.Assert(
code='assert True, "Value should be true"',
parser=None,
expected_position=None,
),
param(
"Whitespace oddities test",
node=cst.Assert(
cst.Name("True", lpar=(cst.LeftParen(),), rpar=(cst.RightParen(),)),
whitespace_after_assert=cst.SimpleWhitespace(""),
),
"code": "assert(True)",
"parser": None,
"expected_position": CodeRange((1, 0), (1, 12)),
},
# Whitespace rendering test
{
"node": cst.Assert(
code="assert(True)",
parser=None,
expected_position=CodeRange((1, 0), (1, 12)),
),
param(
"Whitespace rendering test",
node=cst.Assert(
whitespace_after_assert=cst.SimpleWhitespace(" "),
test=cst.Name("True"),
comma=cst.Comma(
Expand All @@ -54,37 +55,35 @@ class AssertConstructionTest(CSTNodeTest):
),
msg=cst.SimpleString('"Value should be true"'),
),
"code": 'assert True , "Value should be true"',
"parser": None,
"expected_position": CodeRange((1, 0), (1, 39)),
},
code='assert True , "Value should be true"',
parser=None,
expected_position=CodeRange((1, 0), (1, 39)),
),
)
)
def test_valid(self, **kwargs: Any) -> None:
def test_valid(self, _name: str, **kwargs: Any) -> None:
self.validate_node(**kwargs)

@data_provider(
@parameterized.expand(
(
# Validate whitespace handling
{
"get_node": (
param(
"Validate whitespace handling",
get_node=(
lambda: cst.Assert(
cst.Name("True"),
whitespace_after_assert=cst.SimpleWhitespace(""),
)
),
"expected_re": "Must have at least one space after 'assert'",
},
# Validate comma handling
{
"get_node": (
lambda: cst.Assert(test=cst.Name("True"), comma=cst.Comma())
),
"expected_re": "Cannot have trailing comma after 'test'",
},
expected_re="Must have at least one space after 'assert'",
),
param(
"Validate comma handling",
get_node=(lambda: cst.Assert(test=cst.Name("True"), comma=cst.Comma())),
expected_re="Cannot have trailing comma after 'test'",
),
)
)
def test_invalid(self, **kwargs: Any) -> None:
def test_invalid(self, _name: str, **kwargs: Any) -> None:
self.assert_invalid(**kwargs)


Expand All @@ -95,39 +94,39 @@ def _assert_parser(code: str) -> cst.Assert:


class AssertParsingTest(CSTNodeTest):
@data_provider(
@parameterized.expand(
(
# Simple assert
{
"node": cst.Assert(cst.Name("True")),
"code": "assert True",
"parser": _assert_parser,
"expected_position": None,
},
# Assert with message
{
"node": cst.Assert(
param(
"simple assert",
node=cst.Assert(cst.Name("True")),
code="assert True",
parser=_assert_parser,
expected_position=None,
),
param(
"assert with message",
node=cst.Assert(
cst.Name("True"),
cst.SimpleString('"Value should be true"'),
comma=cst.Comma(whitespace_after=cst.SimpleWhitespace(" ")),
),
"code": 'assert True, "Value should be true"',
"parser": _assert_parser,
"expected_position": None,
},
# Whitespace oddities test
{
"node": cst.Assert(
code='assert True, "Value should be true"',
parser=_assert_parser,
expected_position=None,
),
param(
"whitespace oddities test",
node=cst.Assert(
cst.Name("True", lpar=(cst.LeftParen(),), rpar=(cst.RightParen(),)),
whitespace_after_assert=cst.SimpleWhitespace(""),
),
"code": "assert(True)",
"parser": _assert_parser,
"expected_position": None,
},
# Whitespace rendering test
{
"node": cst.Assert(
code="assert(True)",
parser=_assert_parser,
expected_position=None,
),
param(
"whitespace rendering test",
node=cst.Assert(
whitespace_after_assert=cst.SimpleWhitespace(" "),
test=cst.Name("True"),
comma=cst.Comma(
Expand All @@ -136,11 +135,11 @@ class AssertParsingTest(CSTNodeTest):
),
msg=cst.SimpleString('"Value should be true"'),
),
"code": 'assert True , "Value should be true"',
"parser": _assert_parser,
"expected_position": None,
},
code='assert True , "Value should be true"',
parser=_assert_parser,
expected_position=None,
),
)
)
def test_valid(self, **kwargs: Any) -> None:
def test_valid(self, _name: str, **kwargs: Any) -> None:
self.validate_node(**kwargs)
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ hypothesis>=4.36.0
hypothesmith>=0.0.4
jupyter>=1.0.0
nbsphinx>=0.4.2
parameterized>=0.8.1
prompt-toolkit>=2.0.9
pyre-check==0.9.3
setuptools_scm>=6.0.1
Expand Down

0 comments on commit 842aab3

Please sign in to comment.