Skip to content

Commit

Permalink
Merge pull request #147 from HubSpot/resolved-expressions-to-parent
Browse files Browse the repository at this point in the history
Add resolved expressions recursively up the context chain
  • Loading branch information
jmp3833 authored Aug 31, 2017
2 parents 0c15fa0 + c278d51 commit b943faa
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/main/java/com/hubspot/jinjava/interpret/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ public void setAutoEscape(Boolean autoEscape) {

public void addResolvedExpression(String expression) {
resolvedExpressions.add(expression);
if (getParent() != null) {
getParent().addResolvedExpression(expression);
}
}

public Set<String> getResolvedExpressions() {
Expand All @@ -186,6 +189,9 @@ public boolean wasExpressionResolved(String expression) {

public void addResolvedValue(String value) {
resolvedValues.add(value);
if (getParent() != null) {
getParent().addResolvedValue(value);
}
}

public Set<String> getResolvedValues() {
Expand All @@ -200,8 +206,11 @@ public Set<String> getResolvedFunctions() {
return ImmutableSet.copyOf(resolvedFunctions);
}

public void addResolvedFunction(String value) {
resolvedFunctions.add(value);
public void addResolvedFunction(String function) {
resolvedFunctions.add(function);
if (getParent() != null) {
getParent().addResolvedFunction(function);
}
}

public List<? extends Node> getSuperBlock() {
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/com/hubspot/jinjava/interpret/ContextTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,16 @@ public void itAddsResolvedValuesFromAnotherContextObject() {
assertThat(context.getResolvedFunctions()).contains(RESOLVED_FUNCTION);
assertThat(context.getResolvedExpressions()).contains(RESOLVED_EXPRESSION);
}

@Test
public void itRecursivelyAddsValuesUpTheContextChain() {
Context child = new Context(context);
child.addResolvedValue(RESOLVED_VALUE);
child.addResolvedFunction(RESOLVED_FUNCTION);
child.addResolvedExpression(RESOLVED_EXPRESSION);

assertThat(context.getResolvedValues()).contains(RESOLVED_VALUE);
assertThat(context.getResolvedFunctions()).contains(RESOLVED_FUNCTION);
assertThat(context.getResolvedExpressions()).contains(RESOLVED_EXPRESSION);
}
}

0 comments on commit b943faa

Please sign in to comment.