From 1d96212315dcc4dd6af35847debd39dd36582bfc Mon Sep 17 00:00:00 2001 From: root Date: Sat, 2 Sep 2023 23:13:54 +0200 Subject: [PATCH 1/2] added optional '= {}' to REGEX_DECL_NAME so it is considered as a declaration to align instead of an assignment --- c_formatter_42/formatters/helper.py | 6 +++--- tests/formatters/test_align.py | 20 ++++++++++++++++---- tests/formatters/test_hoist.py | 16 ++++++++++------ 3 files changed, 29 insertions(+), 13 deletions(-) diff --git a/c_formatter_42/formatters/helper.py b/c_formatter_42/formatters/helper.py index 7f8656f..6b70dd7 100644 --- a/c_formatter_42/formatters/helper.py +++ b/c_formatter_42/formatters/helper.py @@ -3,10 +3,10 @@ # ::: :::::::: # # helper.py :+: :+: :+: # # +:+ +:+ +:+ # -# By: kiokuless +#+ +:+ +#+ # +# By: root +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2020/10/04 11:38:00 by cacharle #+# #+# # -# Updated: 2023/07/17 02:28:52 by kiokuless ### ########.fr # +# Updated: 2023/09/02 23:13:44 by root ### ########.fr # # # # **************************************************************************** # @@ -17,7 +17,7 @@ # regex for a c variable/function name REGEX_NAME = r"\**[a-zA-Z_*()]\w*" # regex for a name in a declaration context (with array and function ptr) -REGEX_DECL_NAME = r"\(?{name}(\[.*\])*(\)\(.*\))?".format(name=REGEX_NAME) +REGEX_DECL_NAME = r"\(?{name}(\[.*\])*(\s\=\s{{.*}})?(\)\(.*\))?".format(name=REGEX_NAME) def locally_scoped(func): diff --git a/tests/formatters/test_align.py b/tests/formatters/test_align.py index 222b132..874d3a9 100644 --- a/tests/formatters/test_align.py +++ b/tests/formatters/test_align.py @@ -1,14 +1,14 @@ -# ############################################################################ # +# **************************************************************************** # # # # ::: :::::::: # # test_align.py :+: :+: :+: # # +:+ +:+ +:+ # -# By: cacharle +#+ +:+ +#+ # +# By: root +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2020/10/04 12:19:45 by cacharle #+# #+# # -# Updated: 2021/02/11 20:35:54 by charles ### ########.fr # +# Updated: 2023/09/02 20:26:16 by root ### ########.fr # # # -# ############################################################################ # +# **************************************************************************** # import pytest @@ -791,3 +791,15 @@ def test_align_func_typedef(): int\t\t\t\t\t\tbar(); """ assert output == align(input) + + +def test_align_array_initialization(): + input = """ +int\ta; +static int\txs[4] = {1, 2, 3, 4}; +""" + output = """ +int\t\t\ta; +static int\txs[4] = {1, 2, 3, 4}; +""" + assert output == align(input) diff --git a/tests/formatters/test_hoist.py b/tests/formatters/test_hoist.py index d00b56d..b682cea 100644 --- a/tests/formatters/test_hoist.py +++ b/tests/formatters/test_hoist.py @@ -1,14 +1,14 @@ -# ############################################################################ # +# **************************************************************************** # # # # ::: :::::::: # # test_hoist.py :+: :+: :+: # # +:+ +:+ +:+ # -# By: cacharle +#+ +:+ +#+ # +# By: root +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2020/10/04 12:29:07 by cacharle #+# #+# # -# Updated: 2021/02/11 22:16:57 by charles ### ########.fr # +# Updated: 2023/09/02 22:43:13 by root ### ########.fr # # # -# ############################################################################ # +# **************************************************************************** # import pytest @@ -201,13 +201,17 @@ def test_hoist_array_initialization(): input = """ void foo(void) { -\tint xs[4] = {1, 2, 3, 4}; +\tint a = 2; +\tstatic int\txs[4] = {1, 2, 3, 4}; } """ output = """ void foo(void) { -\tint xs[4] = {1, 2, 3, 4}; +\tint\ta; +\tstatic int\txs[4] = {1, 2, 3, 4}; + +\ta = 2; } """ assert output == hoist(input) From 05d0dd8f415175ce4199e8639749665a26d458b8 Mon Sep 17 00:00:00 2001 From: root Date: Sun, 3 Sep 2023 11:06:32 +0200 Subject: [PATCH 2/2] black --- c_formatter_42/formatters/helper.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/c_formatter_42/formatters/helper.py b/c_formatter_42/formatters/helper.py index 6b70dd7..71f4b21 100644 --- a/c_formatter_42/formatters/helper.py +++ b/c_formatter_42/formatters/helper.py @@ -6,7 +6,7 @@ # By: root +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2020/10/04 11:38:00 by cacharle #+# #+# # -# Updated: 2023/09/02 23:13:44 by root ### ########.fr # +# Updated: 2023/09/03 11:05:57 by root ### ########.fr # # # # **************************************************************************** # @@ -17,7 +17,9 @@ # regex for a c variable/function name REGEX_NAME = r"\**[a-zA-Z_*()]\w*" # regex for a name in a declaration context (with array and function ptr) -REGEX_DECL_NAME = r"\(?{name}(\[.*\])*(\s\=\s{{.*}})?(\)\(.*\))?".format(name=REGEX_NAME) +REGEX_DECL_NAME = r"\(?{name}(\[.*\])*(\s\=\s{{.*}})?(\)\(.*\))?".format( + name=REGEX_NAME +) def locally_scoped(func):