-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use fixtures for sample data in unit tests
Rationale: This is more elegant way to give modifiable data than using the `copy` module explicitly.
- Loading branch information
Showing
7 changed files
with
205 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import copy | ||
import enum | ||
import pytest | ||
|
||
from enumecg.definitions import EnumDefinition, EnumMemberDefinition, EnumDocumentation | ||
|
||
|
||
class Status(enum.Enum): | ||
"""An example enumeration for testing | ||
This is a long description of the test enum.""" | ||
|
||
INITIALIZING = "initializing" | ||
WAITING_FOR_INPUT = "waitingForInput" | ||
BUSY = "busy" | ||
|
||
|
||
"""The :class:`definitions.EnumDefinition` expected to be generated from :class:`Status` | ||
This is global constant. For copy that can be modified, please use the | ||
:func:`status_definition()` fixture. | ||
""" | ||
STATUS_DEFINITION = EnumDefinition( | ||
label_enum_typename="StatusLabel", | ||
enhanced_enum_typename="EnhancedStatus", | ||
value_type_typename="std::string_view", | ||
members=[ | ||
EnumMemberDefinition( | ||
enumerator_name="INITIALIZING", | ||
enumerator_value_constant_name="INITIALIZING_VALUE", | ||
enumerator_value_initializers='"initializing"', | ||
), | ||
EnumMemberDefinition( | ||
enumerator_name="WAITING_FOR_INPUT", | ||
enumerator_value_constant_name="WAITING_FOR_INPUT_VALUE", | ||
enumerator_value_initializers='"waitingForInput"', | ||
), | ||
EnumMemberDefinition( | ||
enumerator_name="BUSY", | ||
enumerator_value_constant_name="BUSY_VALUE", | ||
enumerator_value_initializers='"busy"', | ||
), | ||
], | ||
associate_namespace_name="Statuses", | ||
) | ||
|
||
"""``dict`` representation of :const:`STATUS_DEFINITION` | ||
This is global constant. For copy that can be modified, please use the | ||
:func:`status_definition_dict()` fixture. | ||
""" | ||
STATUS_DEFINITION_DICT = { | ||
"typename": "Status", | ||
"docstring": Status.__doc__, | ||
"members": { | ||
"INITIALIZING": "initializing", | ||
"WAITING_FOR_INPUT": "waitingForInput", | ||
"BUSY": "busy", | ||
}, | ||
} | ||
|
||
|
||
"""Sample nested enum definition | ||
This is global constant. For copy that can be modified, please use the | ||
:func:`nested_enum_definition_dict()` fixture. | ||
""" | ||
NESTED_ENUM_DEFINITION_DICT = { | ||
"typename": "NestedEnum", | ||
"members": {"enumerator": (0, ("string", True))}, | ||
} | ||
|
||
|
||
@pytest.fixture | ||
def status_definition(): | ||
"""Return deep copy of :const:`STATUS_DEFINITION` | ||
This definition is expected to be generated from | ||
:class:`Status`. Can be modified by a test case because this is a | ||
copy. | ||
""" | ||
return copy.deepcopy(STATUS_DEFINITION) | ||
|
||
|
||
@pytest.fixture | ||
def status_definition_dict(): | ||
"""Return deep copy of :const:`STATUS_DEFINITION_DICT` | ||
This definition is expected to be generated from | ||
:class:`Status`. Can be modified by a test case because this is a | ||
copy. | ||
""" | ||
return copy.deepcopy(STATUS_DEFINITION_DICT) | ||
|
||
|
||
@pytest.fixture | ||
def nested_enum_definition_dict(): | ||
"""Return deep copy of :const:`NESTED_ENUM_DEFINITION_DICT` | ||
This definition is expected to be generated from | ||
:class:`Status`. Can be modified by a test case because this is a | ||
copy. | ||
""" | ||
return copy.deepcopy(NESTED_ENUM_DEFINITION_DICT) | ||
|
||
|
||
@pytest.fixture | ||
def status_documentation(): | ||
"""Return :class:`definitions.EnumDocumentation` expected to be generated from :class:`Status`""" | ||
return EnumDocumentation( | ||
short_description="An example enumeration for testing", | ||
long_description="This is a long description of the test enum.", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,12 @@ | ||
from enumecg import generate, generator | ||
from enumecg.generators import CodeGenerator | ||
|
||
from .common import STATUS_DEFINITION | ||
|
||
|
||
def test_generator_function_should_return_code_generator(): | ||
assert type(generator()) is CodeGenerator | ||
|
||
|
||
def test_generate_should_return_code(): | ||
assert generate(STATUS_DEFINITION, documentation="doxygen") == CodeGenerator( | ||
def test_generate_should_return_code(status_definition): | ||
assert generate(status_definition, documentation="doxygen") == CodeGenerator( | ||
documentation="doxygen" | ||
).generate_enum_definitions(STATUS_DEFINITION) | ||
).generate_enum_definitions(status_definition) |
Oops, something went wrong.