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

Adding unit tests to test and reproduce several scenarios with readin… #1436

Closed
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 @@ -994,7 +994,14 @@ public void doc(String exp) {
public void init() { // not in constructor because it has to be on Runnable.run() thread
JS = JsEngine.local();
logger.trace("js context: {}", JS);
setVariables(runtime.magicVariables);
setVariables(runtime.magicVariables, true);
if(this.runtime.scenario.isOutlineExample()) {
Map<String, Object> exampleData = runtime.scenario.getExampleData();
setVariables(exampleData, false);
}
if (this.runtime.caller.arg != null && this.runtime.caller.arg.isMap()) {
setVariables(this.runtime.caller.arg.getValue(), false);
}
attachVariables(); // re-hydrate any functions from caller or background
setHiddenVariable(KARATE, bridge);
setHiddenVariable(READ, readFunction);
Expand Down Expand Up @@ -1205,26 +1212,36 @@ public void setHiddenVariable(String key, Object value) {
}

public void setVariable(String key, Object value) {
this.setVariable(key, value, false);
}

public void setVariable(String key, Object value, boolean isScenarioScopeOnly) {
Variable v;
if (value instanceof Variable) {
v = (Variable) value;
} else {
v = new Variable(value);
}
vars.put(key, v);
if(!isScenarioScopeOnly) {
vars.put(key, v);
}
if (JS != null) {
JS.put(key, v.getValue());
}
if (children != null) {
children.forEach(c -> c.setVariable(key, value));
children.forEach(c -> c.setVariable(key, value, isScenarioScopeOnly));
}
}

public void setVariables(Map<String, Object> map) {
this.setVariables(map, false);
}

public void setVariables(Map<String, Object> map, boolean isScenarioScopeOnly) {
if (map == null) {
return;
}
map.forEach((k, v) -> setVariable(k, v));
map.forEach((k, v) -> setVariable(k, v, isScenarioScopeOnly));
}

private static Map<String, Variable> copy(Map<String, Variable> source, boolean deep) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public ScenarioRuntime(FeatureRuntime featureRuntime, Scenario scenario, Scenari
// detaching is as good as cloning
// the detach is needed as the js-engine will be different
Map<String, Variable> detached = background.engine.detachVariables();
detached.forEach((k, v) -> magicVariables.put(k, v.getValue()));
detached.forEach((k, v) -> this.engine.vars.put(k, v)); // maybe this should go into this.engine.vars
Copy link
Contributor Author

Choose a reason for hiding this comment

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

forgot to remove the comment from the check in

result.addStepResults(background.result.getStepResults());
}
dryRun = featureRuntime.suite.dryRun;
Expand Down Expand Up @@ -248,6 +248,8 @@ protected void logError(String message) {
}

private Map<String, Object> initMagicVariables() {
// do not init anything in this map that couldn't be considered a magic var (e.g. __arg, __loop)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

need to add the other two vars in the comment for documentation purposes

// those should remain in the scope of the scenario being executed only
Map<String, Object> map = new HashMap();
if (caller.isNone()) { // if feature called via java api
if (caller.arg != null && caller.arg.isMap()) {
Expand All @@ -256,13 +258,9 @@ private Map<String, Object> initMagicVariables() {
} else {
map.put("__arg", caller.arg);
map.put("__loop", caller.getLoopIndex());
if (caller.arg != null && caller.arg.isMap()) {
map.putAll(caller.arg.getValue());
}
}
if (scenario.isOutlineExample() && !scenario.isDynamic()) { // init examples row magic variables
Map<String, Object> exampleData = scenario.getExampleData();
exampleData.forEach((k, v) -> map.put(k, v));
map.put("__row", exampleData);
map.put("__num", scenario.getExampleIndex());
// TODO breaking change configure outlineVariablesAuto deprecated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,22 @@ void testSetXml() {

@Test
void testJsRead() {
run("js-read.feature");
run("jsread/js-read.feature");
}

@Test
void testJsRead2() {
run("jsread/js-read-2.feature");
}

@Test
void testJsRead3() {
run("jsread/js-read-3.feature");
}

@Test
void testJsRead4() {
run("jsread/js-read-4.feature");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,23 +140,23 @@ void testCallKarateFeature() {
"def b = 'bar'",
"def res = call read('called1.feature')"
);
matchVar("res", "{ a: 1, b: 'bar', foo: { hello: 'world' }, configSource: 'normal', __arg: null, __loop: -1 }");
matchVar("res", "{ a: 1, b: 'bar', foo: { hello: 'world' }, configSource: 'normal' }");
run(
"def b = 'bar'",
"def res = call read('called1.feature') { foo: 'bar' }"
);
matchVar("res", "{ a: 1, b: 'bar', foo: { hello: 'world' }, configSource: 'normal', __arg: { foo: 'bar' }, __loop: -1 }");
matchVar("res", "{ a: 1, b: 'bar', foo: { hello: 'world' }, configSource: 'normal' }");
run(
"def b = 'bar'",
"def res = call read('called1.feature') [{ foo: 'bar' }]"
);
matchVar("res", "[{ a: 1, b: 'bar', foo: { hello: 'world' }, configSource: 'normal', __arg: { foo: 'bar' }, __loop: 0 }]");
matchVar("res", "[{ a: 1, b: 'bar', foo: { hello: 'world' }, configSource: 'normal' } ]");
run(
"def b = 'bar'",
"def fun = function(i){ if (i == 1) return null; return { index: i } }",
"def res = call read('called1.feature') fun"
);
matchVar("res", "[{ a: 1, b: 'bar', foo: { hello: 'world' }, configSource: 'normal', __arg: { index: 0 }, __loop: 0, index: 0, fun: '#ignore' }]");
matchVar("res", "[{ a: 1, b: 'bar', foo: { hello: 'world' }, configSource: 'normal', index: 0, fun: '#ignore' }]");
}

@Test
Expand Down Expand Up @@ -215,7 +215,7 @@ void testCallFromJs() {
run(
"def res = karate.call('called1.feature')"
);
matchVar("res", "{ a: 1, foo: { hello: 'world' }, configSource: 'normal', __arg: null, __loop: -1 }");
matchVar("res", "{ a: 1, foo: { hello: 'world' }, configSource: 'normal' }");
}

@Test
Expand Down
11 changes: 0 additions & 11 deletions karate-core/src/test/java/com/intuit/karate/core/js-read.feature

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Feature:

Background:
* def anotherVariable = 'hello'
* def data = [{ name: 'one' }, { name: 'two' }]

Scenario Outline:
* match name == "#present"
* match anotherVariable == "hello"

Examples:
| data |

Scenario Outline:
* match name == "#present"
* match anotherVariable == "hello"

Examples:
| name |
| test |

Scenario Outline:
* match name == "#present"
* match anotherVariable == "hello"
* def params = { 'foo': 'bar' }
* call read('js-read-called-2.feature') params

Examples:
| data |
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"errorvar":[{"id":1},{"id":2}]}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
Feature:

Background:
* def anotherVariable = 'hello'
* def x = read('js-read-3.json')
* def data = x.thirderror
* def backgroundVar =
"""
{"foo": '#(data)' }
"""

Scenario Outline:
* match backgroundVar == { "foo": "#(data)" }
* match id == "#present"
* match anotherVariable == "hello"

Examples:
| data |

Scenario Outline:
* def param =
"""
{ bar: '#(backgroundVar)'}
"""
* match param == { bar: '#(backgroundVar)'}
* match id == "#present"
* match anotherVariable == "hello"

Examples:
| data |


Scenario Outline:
* def params =
"""
{ backgroundVar: '#(backgroundVar)'}
"""
* print params
* match id == "#present"
* match anotherVariable == "hello"
* call read('js-read-called-2.feature') params

Examples:
| data |
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"thirderror":[{"id":1},{"id":2}]}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Feature:

Background:

Scenario:
* def params = { 'foo': 'bar' }
* call read('js-read-called-2.feature') params

Scenario:
* def params = { 'foo': 'bar' }
* call read('js-read-called-3.feature') params
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"fourtherror":[{"id":1},{"id":2}]}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"fiftherror":[{"id":1},{"id":2}]}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Feature:

Background:
#* call read('js-read-3.json')
* call read('../utils-reuse-common.feature')

Scenario:
* print 'arg: ' + __arg
* match __arg == "#present"
* match __arg == "#notnull"
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Feature:

#see https://github.com/intuit/karate/pull/1436
Background: js-read-called-3 is calling a feature in background and assigning it's result.
* def called = call read('../utils-reuse-common.feature')

Scenario:
* print 'arg: ' + __arg
* match __arg == "#present"
* match __arg == "#notnull"
* match __arg == { 'foo': 'bar' }
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Feature:

@name=checkUsageOfFunc
Scenario: trigger call fun from parent read feature
* def val = call fun
* match val == 2

@name=checkReadingJsonKeys
Scenario: check json data from parent read feature
#* print error
* match error[0].id == 1

@name=checkReadingSecondJsonKeys
Scenario: check json data from parent read feature
#* print error
* match errorvar[0].id == 1

@name=checkReadingThirdJsonKeys
Scenario: check json data from parent read feature
* match thirderror[0].id == 1

@name=checkReadingFourthJsonKeys
Scenario: check json data from parent read feature
* match fourtherror[0].id == 1

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Feature:

Background:
* call read 'js-read-5.json'
* def storedVar2 = call read 'js-read-5.json'

Scenario:
* print 'step in Scenario to trigger background steps'
#* karate.set('storedVar', storedVar)
#* karate.set('storedVar', `call read 'js-read-3.json'`)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Feature:

Background:
* call read 'js-read-4.json'
* def storedVarBackground = call read 'js-read-4.json'

Scenario:
* print 'test'
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Feature:

Background:
* call read 'js-read-3.json'
* def storedVar = call read 'js-read-3.json'

Scenario:
* print 'step in Scenario to trigger background steps'
#* karate.set('storedVar', storedVar)
#* karate.set('storedVar', `call read 'js-read-3.json'`)
Loading