Skip to content

Commit

Permalink
Fixing log levels analyzer (#151)
Browse files Browse the repository at this point in the history
* Fixing log levels analyzer
Updating substring comment to show on what method it should be applied
Udating hardcoded message check, so first converts the string to lowercase

* Renaming parameter from onMethod to inMethod

* Updating substring comment used to v2
  • Loading branch information
manumafe98 authored Mar 27, 2024
1 parent dffc75f commit addcf0c
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ public void visit(MethodDeclaration node, OutputCollector output) {
output.addComment(new AvoidHardCodedTestCases());
return;
}

if (!node.getNameAsString().equals(REFORMAT) && doesNotCallMethod(node, SUBSTRING)) {
output.addComment(new UseSubstringMethod());
output.addComment(new UseSubstringMethod(node.getNameAsString()));
return;
}

Expand All @@ -69,9 +69,10 @@ public void visit(MethodDeclaration node, OutputCollector output) {
}

private static boolean containsHarcodedString(MethodDeclaration node) {
List<StringLiteralExpr> hardcodedStrings = node.findAll(StringLiteralExpr.class,
x -> x.getValue().contains("ERROR") || x.getValue().contains("WARNING")
|| x.getValue().contains("INFO"));
List<StringLiteralExpr> hardcodedStrings = node.findAll(StringLiteralExpr.class, x -> {
String value = x.getValue().toLowerCase();
return value.contains("error") || value.contains("warning") || value.contains("info");
});

return hardcodedStrings.size() > 1;
}
Expand Down
17 changes: 15 additions & 2 deletions src/main/java/analyzer/exercises/loglevels/UseSubstringMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,27 @@

import analyzer.Comment;

import java.util.Map;

/**
* @see <a href="https://github.com/exercism/website-copy/blob/main/analyzer-comments/java/log-levels/use_substring_method.md">Markdown Template</a>
* @see <a href="https://github.com/exercism/website-copy/blob/main/analyzer-comments/java/log-levels/use_substring_method_v2.md">Markdown Template</a>
*/
class UseSubstringMethod extends Comment {
private final String inMethod;

public UseSubstringMethod(String inMethod) {
this.inMethod = inMethod;
}

@Override
public String getKey() {
return "java.log-levels.use_substring_method";
return "java.log-levels.use_substring_method_v2";
}

@Override
public Map<String, String> getParameters() {
return Map.of(
"inMethod", this.inMethod);
}

@Override
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/analyzer/AnalyzerIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ void needforspeed(String scenario) throws IOException {
@ParameterizedTest
@ValueSource(strings = {
"ExemplarSolution",
"HardCodingLogLevels",
"HardCodingLogLevelsUpperCase",
"HardCodingLogLevelsLowerCase",
"NoReuseLogLevel",
"NoReuseMessage",
"NoReuseOfBothMethods",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"comments": [
{
"comment": "java.general.avoid_hard_coded_test_cases",
"params": {},
"type": "essential"
},
{
"comment": "java.general.feedback_request",
"params": {},
"type": "informative"
}
]
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
{
"comments": [
{
"comment": "java.log-levels.use_substring_method",
"params": {},
"comment": "java.log-levels.use_substring_method_v2",
"params": {
"inMethod": "message"
},
"type": "actionable"
},
{
"comment": "java.log-levels.use_substring_method_v2",
"params": {
"inMethod": "logLevel"
},
"type": "actionable"
},
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
{
"comments": [
{
"comment": "java.log-levels.use_substring_method",
"params": {},
"comment": "java.log-levels.use_substring_method_v2",
"params": {
"inMethod": "logLevel"
},
"type": "actionable"
},
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
{
"comments": [
{
"comment": "java.log-levels.use_substring_method",
"params": {},
"comment": "java.log-levels.use_substring_method_v2",
"params": {
"inMethod": "message"
},
"type": "actionable"
},
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package scenarios.loglevels;

public class LogLevels {
public static String message(String logLine) {
return logLine.substring(logLine.indexOf(":") + 1).trim();
}

public static String logLevel(String logLine) {
if (logLine.toLowerCase().contains("info"))
return "info";
if (logLine.toLowerCase().contains("warning"))
return "warning";

return "error";
}

public static String reformat(String logLine) {
return message(logLine) + " (" + logLevel(logLine) + ")";
}
}
6 changes: 4 additions & 2 deletions tests/log-levels/no-substring-used/expected_analysis.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
{
"comments": [
{
"comment": "java.log-levels.use_substring_method",
"params": {},
"comment": "java.log-levels.use_substring_method_v2",
"params": {
"inMethod": "message"
},
"type": "actionable"
},
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
class LogLevels {
public static String message(String logLine) {
return logLine.split("]: ")[1]
.trim();
return logLine.split("]: ")[1].trim();
}

public static String logLevel(String logLine) {
Expand Down

0 comments on commit addcf0c

Please sign in to comment.