Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements 'do' tag for expression statements #240

Merged
merged 4 commits into from
Oct 11, 2018
Merged
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
39 changes: 39 additions & 0 deletions src/main/java/com/hubspot/jinjava/lib/tag/DoTag.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.hubspot.jinjava.lib.tag;

import org.apache.commons.lang3.StringUtils;

import com.hubspot.jinjava.doc.annotations.JinjavaDoc;
import com.hubspot.jinjava.doc.annotations.JinjavaSnippet;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.interpret.TemplateSyntaxException;
import com.hubspot.jinjava.tree.TagNode;

@JinjavaDoc(
value = "Evaluates expression without printing out result.",
snippets = {
@JinjavaSnippet(
code = "{% do list.append('value 2') %}")
})
public class DoTag implements Tag {

@Override
public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) {

if (StringUtils.isBlank(tagNode.getHelpers())) {
throw new TemplateSyntaxException(tagNode.getMaster().getImage(), "Tag 'do' expects expression", tagNode.getLineNumber(), tagNode.getStartPosition());
}

interpreter.resolveELExpression(tagNode.getHelpers(), tagNode.getLineNumber());
return "";
}

@Override
public String getEndTagName() {
return null;
}

@Override
public String getName() {
return "do";
}
}
3 changes: 2 additions & 1 deletion src/main/java/com/hubspot/jinjava/lib/tag/TagLibrary.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ protected void registerDefaults() {
PrintTag.class,
RawTag.class,
SetTag.class,
UnlessTag.class);
UnlessTag.class,
DoTag.class);
}

public Tag getTag(String tagName) {
Expand Down
41 changes: 41 additions & 0 deletions src/test/java/com/hubspot/jinjava/lib/tag/DoTagTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.hubspot.jinjava.lib.tag;

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

import org.junit.Before;
import org.junit.Test;

import com.google.common.collect.Maps;
import com.hubspot.jinjava.Jinjava;
import com.hubspot.jinjava.interpret.Context;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.interpret.RenderResult;
import com.hubspot.jinjava.interpret.TemplateError.ErrorReason;

public class DoTagTest {

private Context context;
private JinjavaInterpreter interpreter;
private Jinjava jinjava;

@Before
public void setup() {
jinjava = new Jinjava();
interpreter = jinjava.newInterpreter();
context = interpreter.getContext();
}

@Test
public void itResolvesExpressions() {
String template = "{% set output = [] %}{% do output.append('hey') %}{{ output }}";
assertThat(jinjava.render(template, Maps.newHashMap())).isEqualTo("[hey]");
}

@Test
public void itAddsTemplateErrorOnEmptyExpression() {
String template = "{% do %}";
RenderResult renderResult = jinjava.renderForResult(template, Maps.newHashMap());
assertThat(renderResult.getErrors()).hasSize(1);
assertThat(renderResult.getErrors().get(0).getReason()).isEqualTo(ErrorReason.SYNTAX_ERROR);
}
}