Skip to content
Draft
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
26 changes: 4 additions & 22 deletions src/test/java/com/hubspot/jinjava/FilterOverrideTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

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

import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.lib.filter.Filter;
import com.hubspot.jinjava.testobjects.FilterOverrideTestObjects;
import java.util.HashMap;
import org.junit.Test;

Expand All @@ -16,26 +15,9 @@ public void itAllowsUsersToOverrideBuiltInFilters() {

assertThat(jinjava.render(template, new HashMap<>())).isEqualTo("11");

jinjava.getGlobalContext().registerClasses(DescriptiveAddFilter.class);
jinjava
.getGlobalContext()
.registerClasses(FilterOverrideTestObjects.DescriptiveAddFilter.class);
assertThat(jinjava.render(template, new HashMap<>())).isEqualTo("5 + 6 = 11");
}

public static class DescriptiveAddFilter implements Filter {

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

@Override
public Object filter(Object var, JinjavaInterpreter interpreter, String... args) {
return (
var +
" + " +
args[0] +
" = " +
(Integer.parseInt(var.toString()) + Integer.parseInt(args[0]))
);
}
}
}
202 changes: 36 additions & 166 deletions src/test/java/com/hubspot/jinjava/el/ExpressionResolverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;

import com.google.common.collect.ForwardingList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
Expand All @@ -18,10 +17,9 @@
import com.hubspot.jinjava.interpret.TemplateError;
import com.hubspot.jinjava.interpret.TemplateError.ErrorItem;
import com.hubspot.jinjava.interpret.TemplateError.ErrorReason;
import com.hubspot.jinjava.objects.PyWrapper;
import com.hubspot.jinjava.objects.date.PyishDate;
import com.hubspot.jinjava.testobjects.ExpressionResolverTestObjects;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -163,7 +161,8 @@ public void itResolvesDictValWithDotParam() {

@Test
public void itResolvesMapValOnCustomObject() {
MyCustomMap dict = new MyCustomMap();
ExpressionResolverTestObjects.MyCustomMap dict =
new ExpressionResolverTestObjects.MyCustomMap();
context.put("thedict", dict);

Object val = interpreter.resolveELExpression("thedict['foo']", -1);
Expand All @@ -177,7 +176,8 @@ public void itResolvesMapValOnCustomObject() {

@Test
public void itResolvesOtherMethodsOnCustomMapObject() {
MyCustomMap dict = new MyCustomMap();
ExpressionResolverTestObjects.MyCustomMap dict =
new ExpressionResolverTestObjects.MyCustomMap();
context.put("thedict", dict);

Object val = interpreter.resolveELExpression("thedict.size", -1);
Expand All @@ -190,67 +190,6 @@ public void itResolvesOtherMethodsOnCustomMapObject() {
assertThat(val2.toString()).isEqualTo("[foo=bar, two=2, size=777]");
}

public static final class MyCustomMap implements Map<String, String> {

Map<String, String> data = ImmutableMap.of("foo", "bar", "two", "2", "size", "777");

@Override
public int size() {
return data.size();
}

@Override
public boolean isEmpty() {
return data.isEmpty();
}

@Override
public boolean containsKey(Object key) {
return data.containsKey(key);
}

@Override
public boolean containsValue(Object value) {
return data.containsValue(value);
}

@Override
public String get(Object key) {
return data.get(key);
}

@Override
public String put(String key, String value) {
return null;
}

@Override
public String remove(Object key) {
return null;
}

@Override
public void putAll(Map<? extends String, ? extends String> m) {}

@Override
public void clear() {}

@Override
public Set<String> keySet() {
return data.keySet();
}

@Override
public Collection<String> values() {
return data.values();
}

@Override
public Set<Entry<String, String>> entrySet() {
return data.entrySet();
}
}

@Test
public void itResolvesInnerDictVal() {
Map<String, Object> dict = Maps.newHashMap();
Expand All @@ -274,24 +213,6 @@ public void itResolvesInnerListVal() {
assertThat(val).isEqualTo("val");
}

public static class MyCustomList<T> extends ForwardingList<T> implements PyWrapper {

private final List<T> list;

public MyCustomList(List<T> list) {
this.list = list;
}

@Override
protected List<T> delegate() {
return list;
}

public int getTotalCount() {
return list.size();
}
}

@Test
public void itRecordsFilterNames() {
Object val = interpreter.resolveELExpression("2.3 | round", -1);
Expand All @@ -301,7 +222,9 @@ public void itRecordsFilterNames() {

@Test
public void callCustomListProperty() {
List<Integer> myList = new MyCustomList<>(Lists.newArrayList(1, 2, 3, 4));
List<Integer> myList = new ExpressionResolverTestObjects.MyCustomList<>(
Lists.newArrayList(1, 2, 3, 4)
);

context.put("mylist", myList);
Object val = interpreter.resolveELExpression("mylist.total_count", -1);
Expand Down Expand Up @@ -353,15 +276,15 @@ public void syntaxError() {

@Test
public void itWrapsDates() {
context.put("myobj", new MyClass(new Date(0)));
context.put("myobj", new ExpressionResolverTestObjects.MyClass(new Date(0)));
Object result = interpreter.resolveELExpression("myobj.date", -1);
assertThat(result).isInstanceOf(PyishDate.class);
assertThat(result.toString()).isEqualTo("1970-01-01 00:00:00");
}

@Test
public void blackListedProperties() {
context.put("myobj", new MyClass(new Date(0)));
context.put("myobj", new ExpressionResolverTestObjects.MyClass(new Date(0)));
interpreter.resolveELExpression("myobj.class.methods[0]", -1);

assertThat(interpreter.getErrorsCopy()).isNotEmpty();
Expand All @@ -373,14 +296,14 @@ public void blackListedProperties() {

@Test
public void itWillNotReturnClassObjectProperties() {
context.put("myobj", new MyClass(new Date(0)));
context.put("myobj", new ExpressionResolverTestObjects.MyClass(new Date(0)));
Object clazz = interpreter.resolveELExpression("myobj.clazz", -1);
assertThat(clazz).isNull();
}

@Test
public void blackListedMethods() {
context.put("myobj", new MyClass(new Date(0)));
context.put("myobj", new ExpressionResolverTestObjects.MyClass(new Date(0)));
interpreter.resolveELExpression("myobj.wait()", -1);

assertThat(interpreter.getErrorsCopy()).isNotEmpty();
Expand All @@ -390,7 +313,7 @@ public void blackListedMethods() {

@Test
public void itWillNotReturnClassObjects() {
context.put("myobj", new MyClass(new Date(0)));
context.put("myobj", new ExpressionResolverTestObjects.MyClass(new Date(0)));
interpreter.resolveELExpression("myobj.getClass()", -1);

assertThat(interpreter.getErrorsCopy()).isNotEmpty();
Expand Down Expand Up @@ -517,29 +440,35 @@ public void itStoresResolvedFunctions() {

@Test
public void presentOptionalProperty() {
context.put("myobj", new OptionalProperty(null, "foo"));
context.put("myobj", new ExpressionResolverTestObjects.OptionalProperty(null, "foo"));
assertThat(interpreter.resolveELExpression("myobj.val", -1)).isEqualTo("foo");
assertThat(interpreter.getErrorsCopy()).isEmpty();
}

@Test
public void emptyOptionalProperty() {
context.put("myobj", new OptionalProperty(null, null));
context.put("myobj", new ExpressionResolverTestObjects.OptionalProperty(null, null));
assertThat(interpreter.resolveELExpression("myobj.val", -1)).isNull();
assertThat(interpreter.getErrorsCopy()).isEmpty();
}

@Test
public void presentNestedOptionalProperty() {
context.put("myobj", new OptionalProperty(new MyClass(new Date(0)), "foo"));
context.put(
"myobj",
new ExpressionResolverTestObjects.OptionalProperty(
new ExpressionResolverTestObjects.MyClass(new Date(0)),
"foo"
)
);
assertThat(Objects.toString(interpreter.resolveELExpression("myobj.nested.date", -1)))
.isEqualTo("1970-01-01 00:00:00");
assertThat(interpreter.getErrorsCopy()).isEmpty();
}

@Test
public void emptyNestedOptionalProperty() {
context.put("myobj", new OptionalProperty(null, null));
context.put("myobj", new ExpressionResolverTestObjects.OptionalProperty(null, null));
assertThat(interpreter.resolveELExpression("myobj.nested.date", -1)).isNull();
assertThat(interpreter.getErrorsCopy()).isEmpty();
}
Expand All @@ -548,7 +477,12 @@ public void emptyNestedOptionalProperty() {
public void presentNestedNestedOptionalProperty() {
context.put(
"myobj",
new NestedOptionalProperty(new OptionalProperty(new MyClass(new Date(0)), "foo"))
new ExpressionResolverTestObjects.NestedOptionalProperty(
new ExpressionResolverTestObjects.OptionalProperty(
new ExpressionResolverTestObjects.MyClass(new Date(0)),
"foo"
)
)
);
assertThat(
Objects.toString(interpreter.resolveELExpression("myobj.nested.nested.date", -1))
Expand All @@ -559,7 +493,8 @@ public void presentNestedNestedOptionalProperty() {

@Test
public void itResolvesLazyExpressionsToTheirUnderlyingValue() {
TestClass testClass = new TestClass();
ExpressionResolverTestObjects.TestClass testClass =
new ExpressionResolverTestObjects.TestClass();
Supplier<String> lazyString = () -> result("hallelujah", testClass);

context.put("myobj", ImmutableMap.of("test", LazyExpression.of(lazyString, "")));
Expand All @@ -580,7 +515,8 @@ public void itResolvesNullLazyExpressions() {

@Test
public void itResolvesSuppliersOnlyIfResolved() {
TestClass testClass = new TestClass();
ExpressionResolverTestObjects.TestClass testClass =
new ExpressionResolverTestObjects.TestClass();
Supplier<String> lazyString = () -> result("hallelujah", testClass);

context.put(
Expand All @@ -596,7 +532,8 @@ public void itResolvesSuppliersOnlyIfResolved() {

@Test
public void itResolvesLazyExpressionsInNested() {
Supplier<TestClass> lazyObject = TestClass::new;
Supplier<ExpressionResolverTestObjects.TestClass> lazyObject =
ExpressionResolverTestObjects.TestClass::new;

context.put("myobj", ImmutableMap.of("test", LazyExpression.of(lazyObject, "")));

Expand Down Expand Up @@ -675,75 +612,8 @@ public void itAddsInvalidInputErrorWhenArithmeticExceptionIsThrown() {
.isEqualTo(ErrorReason.INVALID_INPUT);
}

public String result(String value, TestClass testClass) {
public String result(String value, ExpressionResolverTestObjects.TestClass testClass) {
testClass.touch();
return value;
}

public static class TestClass {

private boolean touched = false;
private String name = "Amazing test class";

public boolean isTouched() {
return touched;
}

public void touch() {
this.touched = true;
}

public String getName() {
return name;
}
}

public static final class MyClass {

private Date date;

MyClass(Date date) {
this.date = date;
}

public Class getClazz() {
return this.getClass();
}

public Date getDate() {
return date;
}
}

public static final class OptionalProperty {

private MyClass nested;
private String val;

OptionalProperty(MyClass nested, String val) {
this.nested = nested;
this.val = val;
}

public Optional<MyClass> getNested() {
return Optional.ofNullable(nested);
}

public Optional<String> getVal() {
return Optional.ofNullable(val);
}
}

public static final class NestedOptionalProperty {

private OptionalProperty nested;

public NestedOptionalProperty(OptionalProperty nested) {
this.nested = nested;
}

public Optional<OptionalProperty> getNested() {
return Optional.ofNullable(nested);
}
}
}
Loading