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

Fixing reject filter evaluation when it has argument(s) #790

Merged
merged 3 commits into from
Dec 8, 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
47 changes: 10 additions & 37 deletions src/main/java/com/hubspot/jinjava/lib/filter/RejectFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import com.hubspot.jinjava.util.ForLoop;
import com.hubspot.jinjava.util.ObjectIterator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@JinjavaDoc(
value = "Filters a sequence of objects by applying a test to the object and rejecting the ones with the test succeeding.",
Expand All @@ -31,49 +33,20 @@
)
}
)
public class RejectFilter implements Filter {
public class RejectFilter extends SelectFilter {

@Override
public String getName() {
return "reject";
}

@Override
public Object filter(Object var, JinjavaInterpreter interpreter, String... args) {
List<Object> result = new ArrayList<>();

if (args.length < 1) {
throw new TemplateSyntaxException(
interpreter,
getName(),
"requires 1 argument (name of expression test to filter by)"
);
}

if (args[0] == null) {
throw new InvalidArgumentException(interpreter, this, InvalidReason.NULL, 0);
}

ExpTest expTest = interpreter.getContext().getExpTest(args[0]);
if (expTest == null) {
throw new InvalidArgumentException(
interpreter,
this,
InvalidReason.EXPRESSION_TEST,
0,
args[0]
);
}

ForLoop loop = ObjectIterator.getLoop(var);
while (loop.hasNext()) {
Object val = loop.next();

if (!expTest.evaluate(val, interpreter)) {
result.add(val);
}
}

return result;
boolean evaluate(
JinjavaInterpreter interpreter,
Object[] expArgs,
ExpTest expTest,
Object val
) {
return !super.evaluate(interpreter, expArgs, expTest, val);
}
}
11 changes: 10 additions & 1 deletion src/main/java/com/hubspot/jinjava/lib/filter/SelectFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,20 @@ public Object filter(
while (loop.hasNext()) {
Object val = loop.next();

if (expTest.evaluate(val, interpreter, expArgs)) {
if (evaluate(interpreter, expArgs, expTest, val)) {
result.add(val);
}
}

return result;
}

boolean evaluate(
JinjavaInterpreter interpreter,
Object[] expArgs,
ExpTest expTest,
Object val
) {
return expTest.evaluate(val, interpreter, expArgs);
}
}
43 changes: 43 additions & 0 deletions src/test/java/com/hubspot/jinjava/lib/filter/RejectFilterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.hubspot.jinjava.lib.filter;

import com.google.common.collect.Lists;
import com.hubspot.jinjava.BaseJinjavaTest;
import org.junit.Before;
import org.junit.Test;

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


import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

public class RejectFilterTest extends BaseJinjavaTest {

@Before
public void setup() {
jinjava.getGlobalContext().put("numbers", Lists.newArrayList(1L, 2L, 3L, 4L, 5L));
}

@Test
public void testReject() {
assertThat(jinjava.render("{{numbers|reject('odd')}}", new HashMap<>()))
.isEqualTo("[2, 4]");
assertThat(jinjava.render("{{numbers|reject('even')}}", new HashMap<>()))
.isEqualTo("[1, 3, 5]");
}

@Test
public void testRejectWithEqualToAttr() {
assertThat(jinjava.render("{{numbers|reject('equalto', 3)}}", new HashMap<>()))
.isEqualTo("[1, 2, 4, 5]");
}

@Test
public void itThrowsInvalidArgumentForNullExpTestArgument() {
Map<String, Object> context = new HashMap<>();
context.put("test", null);
assertThatThrownBy(() -> jinjava.render("{{numbers|reject(test, 3)}}", context))
.hasMessageContaining("'exp_test' argument cannot be null");
}
}