-
Notifications
You must be signed in to change notification settings - Fork 168
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 #510 from HubSpot/exp-test-safe-v3
Expression test parity v3
- Loading branch information
Showing
36 changed files
with
985 additions
and
49 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
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
30 changes: 30 additions & 0 deletions
30
src/main/java/com/hubspot/jinjava/lib/exptest/IsBooleanExpTest.java
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.hubspot.jinjava.lib.exptest; | ||
|
||
import com.hubspot.jinjava.doc.annotations.JinjavaDoc; | ||
import com.hubspot.jinjava.doc.annotations.JinjavaParam; | ||
import com.hubspot.jinjava.doc.annotations.JinjavaSnippet; | ||
import com.hubspot.jinjava.interpret.JinjavaInterpreter; | ||
|
||
@JinjavaDoc( | ||
value = "Return true if object is a boolean (in a strict sense, not in its ability to evaluate to a truthy expression)", | ||
input = @JinjavaParam(value = "value", type = "object", required = true), | ||
snippets = { | ||
@JinjavaSnippet( | ||
code = "{% if true is boolean %}\n" + | ||
" <!--this code will always render-->\n" + | ||
"{% endif %}" | ||
) | ||
} | ||
) | ||
public class IsBooleanExpTest implements ExpTest { | ||
|
||
@Override | ||
public String getName() { | ||
return "boolean"; | ||
} | ||
|
||
@Override | ||
public boolean evaluate(Object var, JinjavaInterpreter interpreter, Object... args) { | ||
return var instanceof Boolean; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/hubspot/jinjava/lib/exptest/IsEqExpTest.java
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.hubspot.jinjava.lib.exptest; | ||
|
||
import com.hubspot.jinjava.doc.annotations.JinjavaDoc; | ||
|
||
@JinjavaDoc(value = "", aliasOf = "equalto") | ||
public class IsEqExpTest extends IsEqualToExpTest { | ||
|
||
@Override | ||
public String getName() { | ||
return "eq"; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/hubspot/jinjava/lib/exptest/IsEqualsSymbolExpTest.java
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.hubspot.jinjava.lib.exptest; | ||
|
||
import com.hubspot.jinjava.doc.annotations.JinjavaDoc; | ||
|
||
@JinjavaDoc(value = "", aliasOf = "equalto") | ||
public class IsEqualsSymbolExpTest extends IsEqualToExpTest { | ||
|
||
@Override | ||
public String getName() { | ||
return "=="; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/com/hubspot/jinjava/lib/exptest/IsFalseExpTest.java
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.hubspot.jinjava.lib.exptest; | ||
|
||
import com.hubspot.jinjava.doc.annotations.JinjavaDoc; | ||
import com.hubspot.jinjava.doc.annotations.JinjavaParam; | ||
import com.hubspot.jinjava.doc.annotations.JinjavaSnippet; | ||
import com.hubspot.jinjava.interpret.JinjavaInterpreter; | ||
|
||
@JinjavaDoc( | ||
value = "Return true if object is a boolean and false", | ||
input = @JinjavaParam(value = "value", type = "object", required = true), | ||
snippets = { | ||
@JinjavaSnippet( | ||
code = "{% if false is false %}\n" + | ||
" <!--this code will always render-->\n" + | ||
"{% endif %}" | ||
) | ||
} | ||
) | ||
public class IsFalseExpTest implements ExpTest { | ||
|
||
@Override | ||
public String getName() { | ||
return "false"; | ||
} | ||
|
||
@Override | ||
public boolean evaluate(Object var, JinjavaInterpreter interpreter, Object... args) { | ||
return var instanceof Boolean && !(Boolean) var; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/com/hubspot/jinjava/lib/exptest/IsFloatExpTest.java
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.hubspot.jinjava.lib.exptest; | ||
|
||
import com.hubspot.jinjava.doc.annotations.JinjavaDoc; | ||
import com.hubspot.jinjava.doc.annotations.JinjavaParam; | ||
import com.hubspot.jinjava.doc.annotations.JinjavaSnippet; | ||
import com.hubspot.jinjava.interpret.JinjavaInterpreter; | ||
import java.math.BigDecimal; | ||
|
||
@JinjavaDoc( | ||
value = "Return true if object is a float", | ||
input = @JinjavaParam(value = "value", type = "object", required = true), | ||
snippets = { | ||
@JinjavaSnippet( | ||
code = "{% if num is float %}\n" + | ||
" <!--code to render if num contains an floating point value-->\n" + | ||
"{% endif %}" | ||
) | ||
} | ||
) | ||
public class IsFloatExpTest implements ExpTest { | ||
|
||
@Override | ||
public String getName() { | ||
return "float"; | ||
} | ||
|
||
@Override | ||
public boolean evaluate(Object var, JinjavaInterpreter interpreter, Object... args) { | ||
return ( | ||
var instanceof Double || | ||
var instanceof Float || | ||
(var instanceof BigDecimal && ((BigDecimal) var).scale() > 0) | ||
); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/main/java/com/hubspot/jinjava/lib/exptest/IsGeTest.java
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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.hubspot.jinjava.lib.exptest; | ||
|
||
import com.hubspot.jinjava.doc.annotations.JinjavaDoc; | ||
import com.hubspot.jinjava.doc.annotations.JinjavaParam; | ||
import com.hubspot.jinjava.doc.annotations.JinjavaSnippet; | ||
import com.hubspot.jinjava.el.TruthyTypeConverter; | ||
import com.hubspot.jinjava.interpret.JinjavaInterpreter; | ||
import com.hubspot.jinjava.interpret.TemplateSyntaxException; | ||
import de.odysseus.el.misc.BooleanOperations; | ||
import de.odysseus.el.misc.TypeConverter; | ||
|
||
@JinjavaDoc( | ||
value = "Returns true if the first object's value is greater than or equal to the second object's value", | ||
input = @JinjavaParam(value = "first", type = "object", required = true), | ||
params = { | ||
@JinjavaParam( | ||
value = "other", | ||
type = "object", | ||
desc = "Another object to compare against", | ||
required = true | ||
) | ||
}, | ||
snippets = { | ||
@JinjavaSnippet( | ||
code = "{% if foo.expression is ge 42 %}\n" + | ||
" the foo attribute evaluates to the constant 42\n" + | ||
"{% endif %}\n" | ||
), | ||
@JinjavaSnippet( | ||
desc = "Usage with the selectattr filter", | ||
code = "{{ users|selectattr(\"num\", \"ge\", \"2\") }}" | ||
) | ||
} | ||
) | ||
public class IsGeTest implements ExpTest { | ||
private static final TypeConverter TYPE_CONVERTER = new TruthyTypeConverter(); | ||
|
||
@Override | ||
public String getName() { | ||
return "ge"; | ||
} | ||
|
||
@Override | ||
public boolean evaluate(Object var, JinjavaInterpreter interpreter, Object... args) { | ||
if (args.length == 0) { | ||
throw new TemplateSyntaxException( | ||
interpreter, | ||
getName(), | ||
"requires 1 argument (other object to compare against)" | ||
); | ||
} | ||
|
||
return BooleanOperations.ge(TYPE_CONVERTER, var, args[0]); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/hubspot/jinjava/lib/exptest/IsGreaterThanExpTest.java
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.hubspot.jinjava.lib.exptest; | ||
|
||
import com.hubspot.jinjava.doc.annotations.JinjavaDoc; | ||
|
||
@JinjavaDoc(value = "", aliasOf = "gt") | ||
public class IsGreaterThanExpTest extends IsGtTest { | ||
|
||
@Override | ||
public String getName() { | ||
return "greaterthan"; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/hubspot/jinjava/lib/exptest/IsGreaterThanOrEqualToSymbolExpTest.java
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.hubspot.jinjava.lib.exptest; | ||
|
||
import com.hubspot.jinjava.doc.annotations.JinjavaDoc; | ||
|
||
@JinjavaDoc(value = "", aliasOf = "ge") | ||
public class IsGreaterThanOrEqualToSymbolExpTest extends IsGeTest { | ||
|
||
@Override | ||
public String getName() { | ||
return ">="; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/hubspot/jinjava/lib/exptest/IsGreaterThanSymbolExpTest.java
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.hubspot.jinjava.lib.exptest; | ||
|
||
import com.hubspot.jinjava.doc.annotations.JinjavaDoc; | ||
|
||
@JinjavaDoc(value = "", aliasOf = "gt") | ||
public class IsGreaterThanSymbolExpTest extends IsGtTest { | ||
|
||
@Override | ||
public String getName() { | ||
return ">"; | ||
} | ||
} |
Oops, something went wrong.