sh_cpl/unittests/unittests_cli/generate_test_case.py

101 lines
4.3 KiB
Python
Raw Permalink Normal View History

2022-05-26 14:47:36 +02:00
import os.path
2022-05-26 13:49:31 +02:00
from cpl_core.utils import String
2022-12-01 23:23:48 +01:00
from unittests_cli.abc.command_test_case import CommandTestCase
2022-05-26 15:29:49 +02:00
from unittests_cli.constants import PLAYGROUND_PATH
2022-05-26 14:47:36 +02:00
from unittests_shared.cli_commands import CLICommands
2022-05-26 13:49:31 +02:00
2022-12-01 23:23:48 +01:00
class GenerateTestCase(CommandTestCase):
2023-02-20 15:55:20 +01:00
_project = "test-console"
_t_path = "test"
2022-05-26 13:49:31 +02:00
@classmethod
def setUpClass(cls):
2022-12-01 23:23:48 +01:00
CommandTestCase.setUpClass()
2023-02-20 15:55:20 +01:00
CLICommands.new("console", cls._project, "--ab", "--s", "--venv")
def setUp(self):
os.chdir(PLAYGROUND_PATH)
def _test_file(self, schematic: str, suffix: str, path=None):
2022-12-08 14:23:46 +01:00
file = f'GeneratedFile{"OnReady" if schematic == "event" else ""}'
expected_path = f'generated_file{"_on_ready" if schematic == "event" else ""}{suffix}.py'
if path is not None:
2023-02-20 15:55:20 +01:00
file = f"{path}/{file}"
expected_path = f"{path}/{expected_path}"
2022-12-08 14:23:46 +01:00
CLICommands.generate(schematic, file)
file_path = os.path.abspath(os.path.join(PLAYGROUND_PATH, expected_path))
2022-05-26 14:47:36 +02:00
file_exists = os.path.exists(file_path)
self.assertTrue(file_exists)
def _test_file_with_project(self, schematic: str, suffix: str, path=None, enter=True):
2022-12-08 14:23:46 +01:00
file = f'GeneratedFile{"OnReady" if schematic == "event" else ""}'
excepted_path = f'generated_file{"_on_ready" if schematic == "event" else ""}{suffix}.py'
if path is not None:
2022-12-08 14:23:46 +01:00
excepted_path = f'{self._project}/src/{String.convert_to_snake_case(self._project)}/{path}/generated_file_in_project{"_on_ready" if schematic == "event" else ""}{suffix}.py'
if enter:
os.chdir(path)
2022-12-08 14:23:46 +01:00
excepted_path = f'{path}/src/{String.convert_to_snake_case(self._project)}/generated_file_in_project{"_on_ready" if schematic == "event" else ""}{suffix}.py'
2022-12-08 14:23:46 +01:00
file = f'{path}/GeneratedFileInProject{"OnReady" if schematic == "event" else ""}'
CLICommands.generate(schematic, file)
file_path = os.path.abspath(os.path.join(PLAYGROUND_PATH, excepted_path))
self.assertTrue(os.path.exists(file_path))
2022-05-26 14:47:36 +02:00
def test_abc(self):
2023-02-20 15:55:20 +01:00
self._test_file("abc", "_abc")
self._test_file("abc", "_abc", path=self._t_path)
self._test_file("abc", "_abc", path=f"{self._t_path}/{self._t_path}")
self._test_file_with_project("abc", "_abc", path=self._project)
os.chdir(f"src/{String.convert_to_snake_case(self._project)}")
self._test_file_with_project("abc", "_abc", path="test", enter=False)
2022-05-26 14:47:36 +02:00
def test_class(self):
2023-02-20 15:55:20 +01:00
self._test_file("class", "")
self._test_file("class", "", path=self._t_path)
self._test_file_with_project("class", "", path=self._project)
2022-05-26 14:47:36 +02:00
def test_enum(self):
2023-02-20 15:55:20 +01:00
self._test_file("enum", "_enum")
self._test_file("enum", "_enum", path=self._t_path)
self._test_file_with_project("enum", "_enum", path=self._project)
os.chdir(f"src/{String.convert_to_snake_case(self._project)}")
self._test_file_with_project("enum", "_enum", path="test", enter=False)
2022-05-26 14:47:36 +02:00
def test_pipe(self):
2023-02-20 15:55:20 +01:00
self._test_file("pipe", "_pipe")
self._test_file("pipe", "_pipe", path=self._t_path)
self._test_file_with_project("pipe", "_pipe", path=self._project)
2022-05-26 14:47:36 +02:00
def test_service(self):
2023-02-20 15:55:20 +01:00
self._test_file("service", "_service")
self._test_file_with_project("service", "_service", path=self._project)
2022-05-26 14:47:36 +02:00
def test_settings(self):
2023-02-20 15:55:20 +01:00
self._test_file("settings", "_settings")
self._test_file_with_project("settings", "_settings", path=self._project)
2022-05-26 14:47:36 +02:00
def test_test_case(self):
2023-02-20 15:55:20 +01:00
self._test_file("test-case", "_test_case")
self._test_file_with_project("test-case", "_test_case", path=self._project)
2022-05-26 14:47:36 +02:00
def test_thread(self):
2023-02-20 15:55:20 +01:00
self._test_file("thread", "_thread")
self._test_file_with_project("thread", "_thread", path=self._project)
2022-05-26 14:47:36 +02:00
def test_validator(self):
2023-02-20 15:55:20 +01:00
self._test_file("validator", "_validator")
self._test_file_with_project("validator", "_validator", path=self._project)
2022-12-08 14:23:46 +01:00
def test_discord_command(self):
2023-02-20 15:55:20 +01:00
self._test_file("command", "_command")
self._test_file_with_project("command", "_command", path=self._project)
2022-12-08 14:23:46 +01:00
def test_discord_event(self):
2023-02-20 15:55:20 +01:00
self._test_file("event", "_event")
self._test_file_with_project("event", "_event", path=self._project)