Skip to content

Commit

Permalink
wip draft of karate.setup() #1905
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrthomas committed Aug 6, 2022
1 parent 251efee commit 8805f3d
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 14 deletions.
18 changes: 18 additions & 0 deletions karate-core/src/main/java/com/intuit/karate/core/Feature.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,24 @@ public Step findStepByLine(int line) {
}
return null;
}

public Scenario getSetup() {
for (FeatureSection section : sections) {
if (section.isOutline()) {
continue;
}
Scenario scenario = section.getScenario();
List<Tag> tags = scenario.getTags();
if (tags != null) {
for (Tag tag : tags) {
if ("setup".equals(tag.getName())) {
return scenario;
}
}
}
}
return null;
}

public void addSection(FeatureSection section) {
section.setIndex(sections.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,15 @@ public void set(String name, Value value) {
public void set(String name, String path, Object value) {
getEngine().set(name, path, new Variable(value));
}

public Object setup() {
ScenarioEngine engine = getEngine();
Feature feature = engine.runtime.featureRuntime.feature;
Scenario scenario = feature.getSetup();
ScenarioRuntime sr = new ScenarioRuntime(engine.runtime.featureRuntime, scenario);
sr.run();
return JsValue.fromJava(sr.engine.getAllVariablesAsMap());
}

public void setXml(String name, String xml) {
getEngine().setVariable(name, XmlUtils.toXmlDoc(xml));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1761,7 +1761,6 @@ public Variable call(Variable called, Variable arg, boolean sharedScope) {
case FEATURE:
// will be always a map or a list of maps (loop call result)
Object callResult = callFeature(called.getValue(), arg, -1, sharedScope);
// this.rehydrateCallFeatureResult(callResult);
return new Variable(callResult);
default:
throw new RuntimeException("not a callable feature or js function: " + called);
Expand All @@ -1775,7 +1774,7 @@ public Variable getCallFeatureVariables(Variable featureResult) {
List resultVariables = new ArrayList();
((List) featureResult.getValue()).forEach(result -> {
if (result instanceof FeatureResult) {
resultVariables.add(this.getCallFeatureVariables(new Variable(result)).getValue());
resultVariables.add(getCallFeatureVariables(new Variable(result)).getValue());
} else {
resultVariables.add(result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public boolean tryAdvance(Consumer<? super ScenarioRuntime> action) {
if (expressionValue == null) {
String expression = currentScenario.getDynamicExpression();
dynamicRuntime = new ScenarioRuntime(featureRuntime, currentScenario);
ScenarioEngine prevEngine = ScenarioEngine.get();
try {
ScenarioEngine.set(dynamicRuntime.engine);
dynamicRuntime.engine.init();
Expand All @@ -106,11 +107,14 @@ public boolean tryAdvance(Consumer<? super ScenarioRuntime> action) {
currentScenario = null;
action.accept(dynamicRuntime);
return true; // exit early
} finally {
ScenarioEngine.set(prevEngine);
}
}
final int rowIndex = index++;
Variable rowValue;
if (expressionValue.isJsOrJavaFunction()) {
ScenarioEngine prevEngine = ScenarioEngine.get();
try {
ScenarioEngine.set(dynamicRuntime.engine);
rowValue = dynamicRuntime.engine.executeFunction(expressionValue, rowIndex);
Expand All @@ -120,6 +124,8 @@ public boolean tryAdvance(Consumer<? super ScenarioRuntime> action) {
currentScenario = null;
action.accept(dynamicRuntime);
return true; // exit early
} finally {
ScenarioEngine.set(prevEngine);
}
} else { // is list
List list = expressionValue.getValue();
Expand All @@ -140,7 +146,7 @@ public boolean tryAdvance(Consumer<? super ScenarioRuntime> action) {
action.accept(new ScenarioRuntime(featureRuntime, dynamic));
return true;
} else { // assume that this is signal to stop the dynamic scenario outline
// background.logger.info("dynamic expression complete at index: {}, not map-like: {}", rowIndex, rowValue);
dynamicRuntime.logger.info("dynamic expression complete at index: {}, not map-like: {}", rowIndex, rowValue);
currentScenario = null;
return tryAdvance(action);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,16 +314,16 @@ private static boolean isSelectedForExecution(FeatureRuntime fr, Scenario scenar
logger.trace("skipping scenario at line: {} - {}, needed: {}", scenario.getLine(), scenario.getName(), callName);
return false;
}
String callTag = feature.getCallTag();
String callTag = feature.getCallTag();
if (callTag != null && (!fr.caller.isNone() || fr.perfHook != null)) {
// only if this is a legit "call" or a gatling "call by tag"
if (tags.contains(callTag)) {
logger.info("{} - call by tag at line {}: {}", fr, scenario.getLine(), callTag);
return true;
// only if this is a legit "call" or a gatling "call by tag"
if (tags.contains(callTag)) {
logger.info("{} - call by tag at line {}: {}", fr, scenario.getLine(), callTag);
return true;
}
logger.trace("skipping scenario at line: {} with call by tag effective: {}", scenario.getLine(), callTag);
return false;
}
logger.trace("skipping scenario at line: {} with call by tag effective: {}", scenario.getLine(), callTag);
return false;
}
if (fr.caller.isNone()) {
if (tags.evaluate(fr.suite.tagSelector, fr.suite.env)) {
logger.trace("matched scenario at line: {} with tags effective: {}", scenario.getLine(), tags.getTags());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
Feature:

Background:
* print 'in background'
@ignore @setup
Scenario:
* def data = [{a:1}, {a:2}]

Scenario Outline:
* print __row

Examples:
| [{a:1}, {a:2}] |
| karate.setup().data |

0 comments on commit 8805f3d

Please sign in to comment.