-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #249 from HubSpot/support-selectattr-expression
Support expressions in selectattr/rejectattr
- Loading branch information
Showing
2 changed files
with
48 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,10 +3,12 @@ | |
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.Lists; | ||
import com.hubspot.jinjava.Jinjava; | ||
|
||
|
@@ -18,9 +20,9 @@ public class SelectAttrFilterTest { | |
public void setup() { | ||
jinjava = new Jinjava(); | ||
jinjava.getGlobalContext().put("users", Lists.newArrayList( | ||
new User(0, false, "[email protected]", new Option(0, "option0")), | ||
new User(1, true, "[email protected]", new Option(1, "option1")), | ||
new User(2, false, null, new Option(2, "option2")))); | ||
new User(0, false, "[email protected]", new Option(0, "option0"), ImmutableList.of(new Option(0, "option0"))), | ||
new User(1, true, "[email protected]", new Option(1, "option1"), ImmutableList.of(new Option(1, "option1"))), | ||
new User(2, false, null, new Option(2, "option2"), ImmutableList.of(new Option(2, "option2"))))); | ||
} | ||
|
||
@Test | ||
|
@@ -56,18 +58,30 @@ public void selectAttrWithNestedProperty() { | |
.isEqualTo("[2]"); | ||
} | ||
|
||
@Test | ||
public void selectAttrWithNestedFilter() { | ||
|
||
assertThat(jinjava.render("{{ users|selectattr(\"optionList|map('id')\", 'containing', 1) }}", new HashMap<String, Object>())) | ||
.isEqualTo("[1]"); | ||
|
||
assertThat(jinjava.render("{{ users|selectattr(\"optionList|map('name')\", 'containing', 'option2') }}", new HashMap<String, Object>())) | ||
.isEqualTo("[2]"); | ||
} | ||
|
||
|
||
public static class User { | ||
private long num; | ||
private boolean isActive; | ||
private String email; | ||
private Option option; | ||
private List<Option> optionList; | ||
|
||
public User(long num, boolean isActive, String email, Option option) { | ||
public User(long num, boolean isActive, String email, Option option, List<Option> optionList) { | ||
this.num = num; | ||
this.isActive = isActive; | ||
this.email = email; | ||
this.option = option; | ||
this.optionList = optionList; | ||
} | ||
|
||
public long getNum() { | ||
|
@@ -86,6 +100,10 @@ public Option getOption() { | |
return option; | ||
} | ||
|
||
public List<Option> getOptionList() { | ||
return optionList; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return num + ""; | ||
|