Skip to content

Commit

Permalink
Merge pull request #130 from msangel/fix/129
Browse files Browse the repository at this point in the history
Decrease usage of deprecated internally and better test coverage
  • Loading branch information
bkiers authored Apr 23, 2019
2 parents 209baeb + e2dbd77 commit 0bb1012
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 14 deletions.
18 changes: 10 additions & 8 deletions src/main/java/liqp/TemplateContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,33 @@ public class TemplateContext {
protected TemplateContext parent;
public final ProtectionSettings protectionSettings;
public final RenderSettings renderSettings;
public final Flavor flavor;
public final ParseSettings parseSettings;
private Map<String, Object> variables;

public TemplateContext() {
this(new ProtectionSettings.Builder().build(),
new RenderSettings.Builder().build(),
Flavor.LIQUID,
new ParseSettings.Builder().withFlavor(Flavor.LIQUID).build(),
new LinkedHashMap<String, Object>());
}

public TemplateContext(ProtectionSettings protectionSettings, RenderSettings renderSettings, Flavor flavor,
@Deprecated // Use `TemplateContext(protectionSettings, renderSettings, parseSettings, variables)` instead
public TemplateContext(ProtectionSettings protectionSettings, RenderSettings renderSettings, Flavor flavor, Map<String, Object> variables) {
this(protectionSettings, renderSettings, new ParseSettings.Builder().withFlavor(flavor).build(), variables);
}

public TemplateContext(ProtectionSettings protectionSettings, RenderSettings renderSettings, ParseSettings parseSettings,
Map<String, Object> variables) {
this.parent = null;
this.protectionSettings = protectionSettings;
this.renderSettings = renderSettings;
this.flavor = flavor;
this.parseSettings = parseSettings;
this.variables = new LinkedHashMap<String, Object>(variables);
}

public TemplateContext(TemplateContext parent) {
this(parent.protectionSettings, parent.renderSettings, parent.parseSettings, new LinkedHashMap<String, Object>());
this.parent = parent;
this.protectionSettings = parent.protectionSettings;
this.renderSettings = parent.renderSettings;
this.flavor = parent.flavor;
this.variables = new LinkedHashMap<String, Object>();
}

public void incrementIterations() {
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/liqp/tags/Include.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ public Object render(TemplateContext context, LNode... nodes) {
if(includeResource.indexOf('.') > 0) {
extension = "";
}
File includeResourceFile;
File includeResourceFile;
File includesDirectory = (File) context.get(INCLUDES_DIRECTORY_KEY);
if (includesDirectory != null) {
includeResourceFile = new File(includesDirectory, includeResource + extension);
}
}
else {
includeResourceFile = new File(context.flavor.snippetsFolderName, includeResource + extension);
}
Template template = Template.parse(includeResourceFile, context.flavor);
includeResourceFile = new File(context.parseSettings.flavor.snippetsFolderName, includeResource + extension);
}
Template template = Template.parse(includeResourceFile, context.parseSettings);
// check if there's a optional "with expression"
if(nodes.length > 1) {
Object value = nodes[1].render(context);
Expand Down
22 changes: 21 additions & 1 deletion src/test/java/liqp/tags/IncludeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.*;
Expand Down Expand Up @@ -38,7 +41,7 @@ public void renderTest() throws RecognitionException {
"color: 'red'\n" +
"shape: 'square'"));
}

@Test
public void renderTestWithIncludeDirectorySpecifiedInContextLiquidFlavor() throws Exception {
File jekyll = new File(new File("").getAbsolutePath(), "src/test/jekyll");
Expand Down Expand Up @@ -131,4 +134,21 @@ public void expressionInIncludeTagDefaultFlavorThrowsException() {

Template.parse(source).render();
}

@Test
public void includeDirectoryKeyInInputShouldChangeIncludeDirectory() throws IOException {
// given
File jekyll = new File(new File("").getAbsolutePath(), "src/test/jekyll");
File index = new File(jekyll, "index_without_quotes.html");
Template template = Template.parse(index, new ParseSettings.Builder().withFlavor(Flavor.JEKYLL).build());
Map<String, Object> data = new HashMap<String, Object>();
data.put(Include.INCLUDES_DIRECTORY_KEY, new File(new File("").getAbsolutePath(), "src/test/jekyll/alternative_includes"));

// when

String result = template.render(data);

// then
assertTrue(result.contains("ALTERNATIVE"));
}
}
1 change: 1 addition & 0 deletions src/test/jekyll/alternative_includes/header.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTERNATIVE

0 comments on commit 0bb1012

Please sign in to comment.