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

Updating log levels analyzer to only analyze exercise default methods #177

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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 @@ -29,6 +29,7 @@ public class LogLevelsAnalyzer extends VoidVisitorAdapter<OutputCollector> imple
private static final String LOG_LEVEL = "logLevel";
private static final String FORMAT = "format";
private static List<String> EXPECTED_METHODS = List.of("substring", "split");
private static final List<String> METHODS_TO_ANALYZE = List.of(MESSAGE, LOG_LEVEL, REFORMAT);

@Override
public void analyze(Solution solution, OutputCollector output) {
Expand All @@ -43,6 +44,10 @@ public void analyze(Solution solution, OutputCollector output) {

@Override
public void visit(MethodDeclaration node, OutputCollector output) {
if (!METHODS_TO_ANALYZE.contains(node.getNameAsString())) {
return;
}
Comment on lines +47 to +49
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this bypasses all checks on helper methods. I'm not sure that is a good idea to do by default. For example, consider a solution that contains hard-coded test case data in a helper method instead of one of the main methods, I think you want to catch those right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kinda think is an overkill to analyzer helper method as well, mainly because they can be a sort of test to validate the other methods of their code or something like that, in the example of the user that opened this issue you can see that the actual result method wasn't use in any other part of the code but it was triggering a comment


if (containsHarcodedString(node)) {
output.addComment(new AvoidHardCodedTestCases());
return;
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 @@ -141,7 +141,8 @@ void needforspeed(String scenario) throws IOException {
"NotUsingExpectedMethodsOnLogLevel",
"NotUsingExpectedMethodsOnMessage",
"NotUsingExpectedMethodsOnLogLevelAndMessage",
"UsingStringFormat"
"UsingStringFormat",
"UsingExtraHelperMethod"
})
void loglevels(String scenario) throws IOException {
var path = Path.of("log-levels", scenario + ".java");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"comments": [
{
"comment": "java.general.exemplar",
"params": {
"exerciseName": "Log Levels"
},
"type": "celebratory"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package scenarios.loglevels;

public class LogLevels {

public static String message(String logLine) {
int startMessageIndex = logLine.indexOf("]:") + 2;
String result = logLine.substring(startMessageIndex, logLine.length());
return result.trim();
}

public static String logLevel(String logLine) {
int startLevelIndex = logLine.indexOf("[") + 1;
int endLevelIndex = logLine.indexOf("]");
return logLine.substring(startLevelIndex, endLevelIndex).toLowerCase();
}

public static String reformat(String logLine) {
return LogLevels.message(logLine) + " (" + LogLevels.logLevel(logLine) + ")";
}

public static void result() {
String logLine = "[ERROR]: Invalid operation";
String formattedString = LogLevels.reformat(logLine);
}
}