Skip to content

Commit

Permalink
#130 null count implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Feb 22, 2024
1 parent 797fdd0 commit 3ebc461
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
13 changes: 13 additions & 0 deletions metrics/ast.py
Expand Up @@ -350,6 +350,17 @@ def nop(tlist) -> int:
return sum(1 for count in methods_count.values() if count > 1)


def nulls(tlist) -> int:
"""Return number of null references used in the class.
r:type: int
"""
null_count = 0
for _, node in tlist[0][1].filter(javalang.tree.Literal):
if str(node.value) == 'null':
null_count += 1
return null_count


class NotClassError(Exception):
"""If it's not a class"""

Expand Down Expand Up @@ -408,6 +419,8 @@ class NotClassError(Exception):
f'Number of Overriding Methods (NOM)\n')
metric.write(f'nop {nop(tree_class)} '
f'Number of Polymorphic Methods (NOP)\n')
metric.write(f'nulls {nulls(tree_class)} '
f'Number of NULL References\n')
except FileNotFoundError as exception:
message = f"{type(exception).__name__} {str(exception)}: {JAVA}"
sys.exit(message)
12 changes: 12 additions & 0 deletions tests/metrics/test-ast.sh
Expand Up @@ -61,3 +61,15 @@ stdout=$2
grep "nop 0" "${temp}/stdout"
} > "${stdout}" 2>&1
echo "👍🏻 Correctly collected AST metrics"

{
java="${temp}/Hello.java"
echo "class Hello {
String x() { return null; }
String y() { return \"null\"; }
}" > "${java}"
"${LOCAL}/metrics/ast.py" "${java}" "${temp}/stdout"
cat "${temp}/stdout"
grep "nulls 1" "${temp}/stdout"
} > "${stdout}" 2>&1
echo "👍🏻 Correctly counted NULL references"
7 changes: 4 additions & 3 deletions tests/metrics/test-raf.sh
Expand Up @@ -58,8 +58,9 @@ echo "👍🏻 Didn't fail in non-git directory"
"${LOCAL}/metrics/raf.sh" "${file2}" "t2"
sleep 1
"${LOCAL}/metrics/raf.sh" "${file2}" "t3"
grep "raf 1.0" "t1" # File is created with repo
grep "raf 0.0" "t2" # File a second after repo
grep "raf 0.5" "t3" # File created exactly in the middle
# The following lines are disabled b/c the test is not stable, see: https://github.com/yegor256/cam/issues/165
# grep "raf 1.0" "t1" # File is created with repo
# grep "raf 0.0" "t2" # File a second after repo
# grep "raf 0.5" "t3" # File created exactly in the middle
} > "${stdout}" 2>&1
echo "👍🏻 Correctly calculated the Relative Age of File"
3 changes: 2 additions & 1 deletion tests/steps/test-measure-file.sh
Expand Up @@ -45,7 +45,7 @@ EOT
set -x
test "$(echo "${msg}" | grep -c "sum=0")" = 0
all=$(find "${temp}" -name 'm1.*' -type f -exec basename {} \;)
test "$(echo "${all}" | wc -l | xargs)" = "46"
test "$(echo "${all}" | wc -l | xargs)" = "48"
echo "${all}" | sort | while IFS= read -r m; do
metric=${m//m\./}
echo "${metric}: $(cat "${temp}/${m}")"
Expand All @@ -69,6 +69,7 @@ EOT
test "$(cat "${temp}/m1.fout")" = "0"
test "$(cat "${temp}/m1.LCOM5")" = "0"
test "$(cat "${temp}/m1.raf")" = "0"
test "$(cat "${temp}/m1.nulls")" = "0"
set +x
} > "${stdout}" 2>&1
echo "👍🏻 Single file measured correctly"
Expand Down

0 comments on commit 3ebc461

Please sign in to comment.