Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing log levels analyzer #151

Merged
merged 3 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,29 @@

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>
*/
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";
manumafe98 marked this conversation as resolved.
Show resolved Hide resolved
}

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

@Override
public Type getType() {
return Type.ACTIONABLE;
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
Expand Up @@ -2,7 +2,16 @@
"comments": [
{
"comment": "java.log-levels.use_substring_method",
"params": {},
"params": {
"inMethod": "message"
},
"type": "actionable"
},
{
"comment": "java.log-levels.use_substring_method",
"params": {
"inMethod": "logLevel"
},
"type": "actionable"
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
"comments": [
{
"comment": "java.log-levels.use_substring_method",
"params": {},
"params": {
"inMethod": "logLevel"
},
"type": "actionable"
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
"comments": [
{
"comment": "java.log-levels.use_substring_method",
"params": {},
"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) + ")";
}
}
4 changes: 3 additions & 1 deletion tests/log-levels/no-substring-used/expected_analysis.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
"comments": [
{
"comment": "java.log-levels.use_substring_method",
"params": {},
"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