Skip to content

Commit

Permalink
also resolve karate.filterKeys #2224
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrthomas committed Jan 8, 2023
1 parent e1442c6 commit 0182584
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
Expand Up @@ -397,7 +397,8 @@ public Object filter(Value o, Value f) {
}

public Object filterKeys(Value o, Value... args) {
if (!o.hasMembers() || args.length == 0) {
Variable v = new Variable(o);
if (!v.isMap()) {
return JsMap.EMPTY;
}
List<String> keys = new ArrayList();
Expand All @@ -415,21 +416,21 @@ public Object filterKeys(Value o, Value... args) {
}
}
} else {
for (Value v : args) {
keys.add(v.toString());
for (Value key : args) {
keys.add(key.toString());
}
}
Map map = new LinkedHashMap(keys.size());
Map map = v.getValue();
Map result = new LinkedHashMap(keys.size());
for (String key : keys) {
if (key == null) {
continue;
}
Value v = o.getMember(key);
if (v != null) {
map.put(key, v.as(Object.class));
if (map.containsKey(key)) {
result.put(key, map.get(key));
}
}
return new JsMap(map);
return new JsMap(result);
}

public void forEach(Value o, Value f) {
Expand Down
Expand Up @@ -358,11 +358,20 @@ void testCollectionsInFunctions() {
"def fun2 = function(arg){ return karate.keysOf(arg) }",
"def fooKeys = fun2(foo)",
"def fun3 = function(arg){ return karate.valuesOf(arg) }",
"def fooVals = fun3(foo)"
"def fooVals = fun3(foo)",
"def fun4 = function(arg){ return karate.filterKeys(arg, 'a')}",
"def filt1 = fun4(foo)",
"def fun5 = function(arg){ return karate.filterKeys(arg, 'a', 'b')}",
"def filt2 = fun5(foo)",
"def fun6 = function(arg){ return karate.filterKeys(arg, ['a', 'b'])}",
"def filt3 = fun6(foo)"
);
assertEquals(get("fooSize"), 3);
matchVar("fooKeys", "['a', 'b', 'c']");
matchVar("fooVals", "[1, 2, 3]");
matchVar("filt1", "{ a: 1 }");
matchVar("filt2", "{ a: 1, b: 2 }");
matchVar("filt3", "{ a: 1, b: 2 }");
}

@Test
Expand Down

0 comments on commit 0182584

Please sign in to comment.