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

Improvements to debugger #1513

Merged
merged 1 commit into from
Mar 14, 2021
Merged
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 @@ -126,7 +126,7 @@ private static Table toTable(KarateParser.TableContext ctx) {
return new Table(rows, lineNumbers);
}

private static final String TRIPLE_QUOTES = "\"\"\"";
public static final String TRIPLE_QUOTES = "\"\"\"";

private static int indexOfFirstText(String s) {
int pos = 0;
Expand Down
2 changes: 1 addition & 1 deletion karate-core/src/main/java/com/intuit/karate/core/Step.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class Step {
private String docString;
private Table table;

private static final List<String> PREFIXES = Arrays.asList("*", "Given", "When", "Then", "And", "But");
public static final List<String> PREFIXES = Arrays.asList("*", "Given", "When", "Then", "And", "But");

public void parseAndUpdateFrom(String text) {
final String stepText = text.trim();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
*/
package com.intuit.karate.debug;

import com.intuit.karate.core.FeatureParser;
import com.intuit.karate.core.Step;

import java.util.HashMap;
import java.util.Map;

Expand All @@ -37,11 +40,35 @@ public class Breakpoint {
public final int id;
public final int line;
public final boolean verified;
public final String condition;

public Breakpoint(Map<String, Object> map) {
id = ++nextId;
line = (Integer) map.get("line");
line = (Integer) map.get("line");
verified = true;

String breakpointCondition = (String) map.get("condition");
if (breakpointCondition != null) {
// remove Cucumber prefix
String conditionText = breakpointCondition.trim();
for (String prefix : Step.PREFIXES) {
if (conditionText.startsWith(prefix)) {
conditionText = conditionText.substring(prefix.length());
break;
}
}
conditionText = conditionText.trim();

// if docstring to get the syntax highlight, remove the triple quotes
if (conditionText.startsWith(FeatureParser.TRIPLE_QUOTES) && conditionText.endsWith(FeatureParser.TRIPLE_QUOTES)) {
conditionText = conditionText.substring(FeatureParser.TRIPLE_QUOTES.length());
conditionText = conditionText.substring(0, (conditionText.length() - FeatureParser.TRIPLE_QUOTES.length()));
}
condition = conditionText.trim();
} else {
condition = null;
}

}

public int getId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private SourceBreakpoints lookup(String pathEnd) {
return null;
}

protected boolean isBreakpoint(Step step, int line) {
protected boolean isBreakpoint(Step step, int line, ScenarioRuntime context) {
Feature feature = step.getFeature();
File file = feature.getResource().getFile();
if (file == null) {
Expand All @@ -120,7 +120,7 @@ protected boolean isBreakpoint(Step step, int line) {
if (sb == null) {
return false;
}
return sb.isBreakpoint(line);
return sb.isBreakpoint(line, context);
}

protected String normalizePath(String path) {
Expand Down Expand Up @@ -339,12 +339,16 @@ private void handleRequest(DapMessage req, ChannelHandlerContext ctx) {
} else {
ScenarioEngine.set(evalContext.engine);
evaluatePreStep(evalContext);
Throwable engineFailedReason = evalContext.engine.getFailedReason();
evalContext.engine.setFailedReason(null);
// TODO: candidate to evaluate several steps in a scenario fashion
Result evalResult = evalContext.evalAsStep(expression);
if (evalResult.isFailed()) {
result = "[error] " + evalResult.getError().getMessage();
} else {
result = "[done]";
}
evalContext.engine.setFailedReason(engineFailedReason); // reset engine failed reason to original failure status
}
ctx.write(response(req)
.body("result", result)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public boolean beforeStep(Step step, ScenarioRuntime context) {
return stop("step");
} else {
int line = step.getLine();
if (handler.isBreakpoint(step, line)) {
if (handler.isBreakpoint(step, line, context)) {
return stop("breakpoint");
} else {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
package com.intuit.karate.debug;

import com.intuit.karate.Json;
import com.intuit.karate.core.ScenarioRuntime;
import com.intuit.karate.core.Variable;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand All @@ -39,13 +42,23 @@ public class SourceBreakpoints {
public final List<Breakpoint> breakpoints;
public final boolean sourceModified;

public boolean isBreakpoint(int line) {
public boolean isBreakpoint(int line, ScenarioRuntime context) {
if (breakpoints == null || breakpoints.isEmpty()) {
return false;
}
for (Breakpoint b : breakpoints) {
if (b.line == line) {
return true;
if (b.condition == null) {
return true;
} else {
Variable evalCondition = context.engine.evalKarateExpression(b.condition);
if (evalCondition != null && evalCondition.type != Variable.Type.BOOLEAN) {
// if the condition is not a boolean then what are you doing trying to use it as a condition?
return true;
}

return evalCondition != null && evalCondition.isTrue();
}
}
}
return false;
Expand Down