-
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.
Add a variable function to map from a variable to a value at runtime.
- Loading branch information
1 parent
6f5d531
commit d7b83aa
Showing
4 changed files
with
66 additions
and
0 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
51 changes: 51 additions & 0 deletions
51
src/test/java/com/hubspot/jinjava/interpret/VariableFunctionTest.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,51 @@ | ||
package com.hubspot.jinjava.interpret; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.hubspot.jinjava.Jinjava; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
import org.junit.Test; | ||
|
||
public class VariableFunctionTest { | ||
private static final Function<String, Object> VARIABLE_FUNCTION = s -> { | ||
switch (s) { | ||
case "name": | ||
return "Jared"; | ||
case "title": | ||
return "Mr."; | ||
case "surname": | ||
return "Stehler"; | ||
default: | ||
return null; | ||
} | ||
}; | ||
|
||
@Test | ||
public void willUseTheFunctionToPopulateVariables() { | ||
final Jinjava jinjava = new Jinjava(); | ||
jinjava.getGlobalContext().setVariableFunction(VARIABLE_FUNCTION); | ||
final Map<String, Object> context = new HashMap<>(); | ||
|
||
final String template = "<div>Hello, {{ title }} {{ name }} {{ surname }}!</div>"; | ||
|
||
final String renderedTemplate = jinjava.render(template, context); | ||
|
||
assertThat(renderedTemplate).isEqualTo("<div>Hello, Mr. Jared Stehler!</div>"); | ||
} | ||
|
||
@Test | ||
public void willPreferTheContextOverTheFunctionToPopulateVariables() { | ||
final Jinjava jinjava = new Jinjava(); | ||
jinjava.getGlobalContext().setVariableFunction(VARIABLE_FUNCTION); | ||
final Map<String, Object> context = new HashMap<>(); | ||
context.put("name", "Greg"); | ||
|
||
final String template = "<div>Hello, {{ title }} {{ name }} {{ surname }}!</div>"; | ||
|
||
final String renderedTemplate = jinjava.render(template, context); | ||
|
||
assertThat(renderedTemplate).isEqualTo("<div>Hello, Mr. Greg Stehler!</div>"); | ||
} | ||
} |