From 6dda7283e8eab3b41e61800db681e652293d5d13 Mon Sep 17 00:00:00 2001 From: Lillias Date: Sat, 30 Mar 2024 13:37:47 +0300 Subject: [PATCH 1/5] Add metric implementation & test --- metrics/ast.py | 16 +++++++++++++++- tests/metrics/test-ast.sh | 1 + 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/metrics/ast.py b/metrics/ast.py index 5b34fa4e..fa295f18 100755 --- a/metrics/ast.py +++ b/metrics/ast.py @@ -347,7 +347,19 @@ def varcomp(tlist: list[tuple[Any, javalang.tree.ClassDeclaration]]) -> float: return (parts / variables) if variables != 0 else 0 -def nop(tlist: list[tuple[Any, javalang.tree.ClassDeclaration]]) -> int: +def pcn(tlist: list[tuple[Any, javalang.tree.ClassDeclaration]]) -> int: + """Return number of words in the name of a class. + r:type: float + """ + words = 0 + classname = tlist[0][1].name + # By naming convention Java classes names use PascalCase. + # Nevertheless, this metric considers both PascalCasse and camelCase naming conventions. + words = re.findall(r'[A-Z]?[a-z]+|[A-Z]+(?=[A-Z]|$)', classname) + return len(words) + + +def nop(tlist) -> int: """Return number of polymorphic methods in main class. Methods of nested classes are skipped. r:type: int @@ -419,6 +431,8 @@ class NotClassError(Exception): f'Number of Class Annotations\n') metric.write(f'varcomp {varcomp(tree_class)} ' f'Average number of parts in variable names\n') + metric.write(f'pcn {pcn(tree_class)} ' + f'Number of words in the name of a class\n') metric.write(f'mhf {mhf(tree_class)} ' f'Method Hiding Factor (MHF), which is the ratio of private \ and protected methods to total methods\n') diff --git a/tests/metrics/test-ast.sh b/tests/metrics/test-ast.sh index 201f9d5c..bf93fdfc 100755 --- a/tests/metrics/test-ast.sh +++ b/tests/metrics/test-ast.sh @@ -49,6 +49,7 @@ stdout=$2 grep "final 1 " "${temp}/stdout" grep "noca 1 " "${temp}/stdout" grep "varcomp 2.75 " "${temp}/stdout" + grep "pcn 1" "${temp}/stdout" grep "mhf 1.0 " "${temp}/stdout" grep "smhf 0 " "${temp}/stdout" grep "ahf 0.25 " "${temp}/stdout" From 424af482e1cdd90af7edb4d550441961051d3481 Mon Sep 17 00:00:00 2001 From: Lillias Date: Sat, 30 Mar 2024 13:42:41 +0300 Subject: [PATCH 2/5] Delete unnecessary variable --- metrics/ast.py | 1 - 1 file changed, 1 deletion(-) diff --git a/metrics/ast.py b/metrics/ast.py index fa295f18..4fbc002f 100755 --- a/metrics/ast.py +++ b/metrics/ast.py @@ -351,7 +351,6 @@ def pcn(tlist: list[tuple[Any, javalang.tree.ClassDeclaration]]) -> int: """Return number of words in the name of a class. r:type: float """ - words = 0 classname = tlist[0][1].name # By naming convention Java classes names use PascalCase. # Nevertheless, this metric considers both PascalCasse and camelCase naming conventions. From 8ca92ddec0389f9eda2f5862f8aaf332aafde202 Mon Sep 17 00:00:00 2001 From: Lillias Date: Sat, 30 Mar 2024 13:58:56 +0300 Subject: [PATCH 3/5] Return nop method typings --- metrics/ast.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metrics/ast.py b/metrics/ast.py index 4fbc002f..bdc4921f 100755 --- a/metrics/ast.py +++ b/metrics/ast.py @@ -358,7 +358,7 @@ def pcn(tlist: list[tuple[Any, javalang.tree.ClassDeclaration]]) -> int: return len(words) -def nop(tlist) -> int: +def nop(tlist: list[tuple[Any, javalang.tree.ClassDeclaration]]) -> int: """Return number of polymorphic methods in main class. Methods of nested classes are skipped. r:type: int From 4a5fd8eaf9c76196344b27ae8cf33e9cc5a4b337 Mon Sep 17 00:00:00 2001 From: Lillias Date: Sat, 30 Mar 2024 16:26:26 +0300 Subject: [PATCH 4/5] Change doc return type --- metrics/ast.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metrics/ast.py b/metrics/ast.py index bdc4921f..e87e97af 100755 --- a/metrics/ast.py +++ b/metrics/ast.py @@ -349,7 +349,7 @@ def varcomp(tlist: list[tuple[Any, javalang.tree.ClassDeclaration]]) -> float: def pcn(tlist: list[tuple[Any, javalang.tree.ClassDeclaration]]) -> int: """Return number of words in the name of a class. - r:type: float + r:type: int """ classname = tlist[0][1].name # By naming convention Java classes names use PascalCase. From d3cfce63eaedbd2a3b711775b4cbb17075ea8bbf Mon Sep 17 00:00:00 2001 From: Lillias Date: Sat, 30 Mar 2024 19:31:11 +0300 Subject: [PATCH 5/5] Update metrics number in test --- tests/steps/test-measure-file.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/steps/test-measure-file.sh b/tests/steps/test-measure-file.sh index 6d1cec7f..839ff3ab 100755 --- a/tests/steps/test-measure-file.sh +++ b/tests/steps/test-measure-file.sh @@ -45,7 +45,7 @@ EOT set -x test "$(echo "${msg}" | grep -c "sum=0")" = 0 all=$(find "${temp}" -name 'm1.*' -type f -exec basename {} \; | sort) - expected=49 + expected=50 actual=$(echo "${all}" | wc -l | xargs) if [ ! "${actual}" = "${expected}" ]; then echo "Exactly ${expected} metrics were expected, but ${actual} were actually collected"