From 823d524a816bfffc405337c451056d092e8ffef1 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Fri, 7 Apr 2023 14:03:45 +0200 Subject: [PATCH] Added core.utils.string tests & fixed some functions --- src/cpl_core/utils/string.py | 10 ++++ .../unittests_core/test_startup_extension.py | 16 ------ .../unittests_core/utils/string_test_case.py | 52 ++++++++++++++++++- 3 files changed, 60 insertions(+), 18 deletions(-) delete mode 100644 unittests/unittests_core/test_startup_extension.py diff --git a/src/cpl_core/utils/string.py b/src/cpl_core/utils/string.py index cfeea681..31c0c483 100644 --- a/src/cpl_core/utils/string.py +++ b/src/cpl_core/utils/string.py @@ -37,9 +37,19 @@ class String: String converted to snake_case """ # convert to train-case to CamelCase + if "_" in chars: + chars = chars.replace("_", "-") + if "-" in chars: chars = "".join(word.title() for word in chars.split("-")) + if " " in chars: + new_chars = "" + for word in chars.split(" "): + new_chars += String.first_to_upper(word) + + chars = new_chars + pattern1 = re.compile(r"(.)([A-Z][a-z]+)") pattern2 = re.compile(r"([a-z0-9])([A-Z])") file_name = re.sub(pattern1, r"\1_\2", chars) diff --git a/unittests/unittests_core/test_startup_extension.py b/unittests/unittests_core/test_startup_extension.py deleted file mode 100644 index 7c1939f3..00000000 --- a/unittests/unittests_core/test_startup_extension.py +++ /dev/null @@ -1,16 +0,0 @@ -from cpl_core.application.startup_extension_abc import StartupExtensionABC -from cpl_core.configuration.configuration_abc import ConfigurationABC -from cpl_core.dependency_injection.service_collection_abc import ServiceCollectionABC -from cpl_core.environment.application_environment_abc import ApplicationEnvironmentABC - - -class TestStartup_extension(StartupExtensionABC): - - def __init__(self): - pass - - def configure_configuration(self, config: ConfigurationABC, env: ApplicationEnvironmentABC): - pass - - def configure_services(self, services: ServiceCollectionABC, env: ApplicationEnvironmentABC): - pass diff --git a/unittests/unittests_core/utils/string_test_case.py b/unittests/unittests_core/utils/string_test_case.py index 1fd4eddb..665117e4 100644 --- a/unittests/unittests_core/utils/string_test_case.py +++ b/unittests/unittests_core/utils/string_test_case.py @@ -1,10 +1,58 @@ +import string import unittest +from cpl_core.utils import String + class StringTestCase(unittest.TestCase): def setUp(self): pass - def test_equal(self): - pass + def test_convert_to_camel_case(self): + expected = "HelloWorld" + + self.assertEqual(expected, String.convert_to_camel_case("hello-world")) + self.assertEqual(expected, String.convert_to_camel_case("hello_world")) + self.assertEqual("helloWorld", String.convert_to_camel_case("helloWorld")) + self.assertEqual(expected, String.convert_to_camel_case("Hello_world")) + self.assertEqual(expected, String.convert_to_camel_case("Hello_World")) + self.assertEqual(expected, String.convert_to_camel_case("hello world")) + + def test_convert_to_snake_case(self): + expected = "hello_world" + + self.assertEqual(expected, String.convert_to_snake_case("Hello World")) + self.assertEqual(expected, String.convert_to_snake_case("hello-world")) + self.assertEqual(expected, String.convert_to_snake_case("hello_world")) + self.assertEqual(expected, String.convert_to_snake_case("helloWorld")) + self.assertEqual(expected, String.convert_to_snake_case("Hello_world")) + self.assertEqual(expected, String.convert_to_snake_case("Hello_World")) + self.assertEqual(expected, String.convert_to_snake_case("hello world")) + + def test_first_to_upper(self): + expected = "HelloWorld" + + self.assertEqual(expected, String.first_to_upper("helloWorld")) + self.assertEqual(expected, String.first_to_upper("HelloWorld")) + + def test_first_to_lower(self): + expected = "helloWorld" + + self.assertEqual(expected, String.first_to_lower("helloWorld")) + self.assertEqual(expected, String.first_to_lower("HelloWorld")) + + def test_random_string(self): + expected = "" + + for x in range(0, 100): + rstr = String.random_string(string.ascii_letters, 4) + self.assertNotEqual(expected, rstr) + self.assertEqual(4, len(rstr)) + expected = rstr + + for x in range(0, 100): + rstr = String.random_string(string.ascii_letters, 16) + self.assertNotEqual(expected, rstr) + self.assertEqual(16, len(rstr)) + expected = rstr