-
Notifications
You must be signed in to change notification settings - Fork 168
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
Whitespace Control with New Line Trimming Disabled Tests #477
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
be2ce15
#422: Added inline verbatim description to Verbatim tag documentation
nward1234 8688f86
Merge remote-tracking branch 'upstream/master'
nward1234 7c83a70
Added tests for whitespace control for templates when New Line Trimmi…
nward1234 1a25dc2
Added javadoc to describe the purpose of several existing tests and c…
nward1234 43aa3fd
Added a 3rd elseif in the template for the For Loop with Nested If St…
nward1234 b366a45
Merge remote-tracking branch 'upstream/master'
nward1234 3e5c798
Added tests with 2 ifelse in a nested if and also 3 ifelse in a neste…
nward1234 9fd732c
Added test of Nested If with One ifelse statement
nward1234 b1657bc
Improved and added additional whitespace control tests.
nward1234 284bc19
Merge remote-tracking branch 'upstream/master'
nward1234 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
154 changes: 154 additions & 0 deletions
154
pebble/src/test/java/com/mitchellbosecke/pebble/template/tests/PebbleTestContext.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,154 @@ | ||
package com.mitchellbosecke.pebble.template.tests; | ||
|
||
import java.io.IOException; | ||
import java.io.StringWriter; | ||
import java.io.Writer; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.apache.commons.io.FileUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.mitchellbosecke.pebble.PebbleEngine; | ||
import com.mitchellbosecke.pebble.loader.StringLoader; | ||
import com.mitchellbosecke.pebble.template.PebbleTemplate; | ||
|
||
/** | ||
* Used by Pebble Template Tests to simply the test code and therefore | ||
* make it easier to understand what is tested by each test. | ||
* | ||
* A separate instance of this class should be instantiated for each | ||
* template test, i.e. for each instance of a template. | ||
* | ||
* @author nathanward | ||
*/ | ||
public class PebbleTestContext { | ||
|
||
private final Logger logger = LoggerFactory.getLogger(PebbleTestContext.class); | ||
|
||
/** | ||
* The path relative to the project root directory where template files and | ||
* expected output files are stored for test purposes. | ||
*/ | ||
private String testFileBasePath = "src/test/resources/template-tests"; | ||
|
||
/** | ||
* The Pebble template context to be used as input for a Pebble Template. | ||
*/ | ||
private Map<String, Object> templateContext = null; | ||
|
||
/** | ||
* Whether or not the Pebble Engine instantiated will | ||
* enable New Line Trimming on the Builder when this class instantiates | ||
* a Pebble Engine instance. Defaults to <code>true</code>. | ||
*/ | ||
private boolean newLineTrimming = true; | ||
|
||
/** | ||
* Initialize the Pebble Template Context. | ||
*/ | ||
public PebbleTestContext() { | ||
this.templateContext = new HashMap<String, Object>(); | ||
} | ||
|
||
public void setNewLineTrimming(boolean b) { | ||
this.newLineTrimming = b; | ||
} | ||
|
||
/** | ||
* Put an object into the Pebble template context to be used as input for | ||
* when the template is executed. One or more items can be put into the template | ||
* context. | ||
* | ||
* @param name Template input/parameter name (i.e. key) for use within the template | ||
* @param value The object that will be referred to by the given name in the template | ||
*/ | ||
public void setTemplateInput(String name, Object value) { | ||
this.templateContext.put(name, value); | ||
} | ||
|
||
/** | ||
* Load the specified template file and execute the template using a | ||
* Pebble Engine using the default Builder (classpath and file builder). | ||
* | ||
* @param templateFilename The template filename relative to the Test File Base Path | ||
* | ||
* @return The output of the template as a string. | ||
* | ||
* @throws IOException Thrown if the template file is not found. | ||
*/ | ||
public String executeTemplateFromFile(String templateFilename) throws IOException { | ||
PebbleEngine pebbleEngine = new PebbleEngine.Builder() | ||
.newLineTrimming(this.newLineTrimming).strictVariables(true).build(); | ||
return this.executeTemplateFromFile(templateFilename, pebbleEngine); | ||
} | ||
|
||
/** | ||
* Execute a template given a template file and a Pebble Engine instance. | ||
* | ||
* @param templateFilename The template filename relative to the Test File Base Path | ||
* @param pebbleEngine | ||
* @return | ||
* @throws IOException | ||
*/ | ||
public String executeTemplateFromFile(String templateFilename, PebbleEngine pebbleEngine) throws IOException { | ||
Path path = Paths.get(this.testFileBasePath, templateFilename); | ||
logger.debug("Executing template file: {}", path.toString()); | ||
return this.executeTemplate(path.toAbsolutePath().toString(), pebbleEngine); | ||
} | ||
|
||
/** | ||
* Load the specified template file and execute the template using a | ||
* Pebble Engine using the default Builder (classpath and file builder). | ||
* | ||
* @param templateString The template content as a string | ||
* | ||
* @return The output of the template as a string. | ||
* | ||
* @throws IOException Thrown if the template file is not found. | ||
*/ | ||
public String executeTemplateFromString(String templateString) throws IOException { | ||
PebbleEngine pebbleEngine = new PebbleEngine.Builder().loader(new StringLoader()) | ||
.newLineTrimming(this.newLineTrimming).build(); | ||
return this.executeTemplate(templateString, pebbleEngine); | ||
} | ||
|
||
/** | ||
* Load the specified template file and execute the template using the template input | ||
* that has previously been specified using the setTemplateInput() method. | ||
* | ||
* @param templateFilename The template filename relative to the Test File Base Path | ||
* @param pebbleEngine The Pebble Engine to be used to execute the template | ||
* @return The output of the template as a string. | ||
* | ||
* @throws IOException Thrown if the template file is not found. | ||
*/ | ||
public String executeTemplate(String templateName, PebbleEngine pebbleEngine) throws IOException { | ||
PebbleTemplate template = pebbleEngine.getTemplate(templateName); | ||
Writer writer = new StringWriter(); | ||
template.evaluate(writer, this.templateContext); | ||
String templateOutput = writer.toString(); | ||
logger.debug("Template Output: {}", templateOutput); | ||
return templateOutput; | ||
} | ||
|
||
/** | ||
* Get the Expected Output content for the given filename so that the | ||
* base path to the expected template output file does not have to be | ||
* specified in the actual test code. | ||
* | ||
* @param filename The name of the file that contains the expected template output. | ||
* @return The content of the expected template output file as a string. | ||
* @throws IOException Thrown if the file by the given name is not found. | ||
*/ | ||
public String getExpectedOutput(String filename) throws IOException { | ||
Path path = Paths.get(this.testFileBasePath, filename); | ||
logger.debug("Expected template output file: {}", path.toAbsolutePath()); | ||
String expectedOutput = FileUtils.readFileToString(path.toFile(), "UTF-8"); | ||
return expectedOutput; | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do you really need assertj for your tests ? I'd like to stick with minimal dependencies
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since I see that we have Hamcrest matchers, I would not mind using the Junit
org.junit.Assert.assertTha()
with Hamcrest Matchers instead of AssertJ. However, I would rather use AssertJ because it is more readable than Hamcrest, although either is much more readable than JUnit alone. AssertJ also is easier to use than Hamcrest because the fluent methods allows IDEs to provide method autosuggestions whereas you have to know the Matcher class names and include static imports for each Hamcrest matcher.Would you say more about the reason for sticking with minimal dependencies? I'd like to understand your concerns.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Eric. I see that you merged the pull request. Did you decide that you are OK with the additional AssertJ dependency? If not, let me know and I will change it. I would like to hear more about why minimal dependencies is important to you. I plan to make more tests and hope to continue contributing to Pebble. So, I would like to understand your concerns and I want to be sure that you are fine with my changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah i'm ok with assertJ. I personnaly use it too. I just don't want to have a lot of unit tests made with differents libraries (some with google truth, others with assertj or junit or hamcrest, etc)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah! Now, that makes sense. I certainly can understand and agree with that. I think that we should have some standards like this. We can always decide to change the standard and then migrate to the new standard overtime when there is a reason to do so. At some point soon, I would be glad to replace the small amount of Hamcrest Matchers used in the code and standardize on AssertJ. I would not mind also using AssertJ where JUnit eventually, but I would think that would be a lower priority. I would also be glad to start documenting coding standards as needed. It would be just documenting existing standards that aren't down on paper yet so people know up front. In any case, I'll continue using AssertJ and get started on the larger task of addressing the whitespace control issues.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It appears that your standards indentation is 2 characters in Java code. I can live with that if that is your standard. However, I'd like to confirm that you prefer 2 characters. I'm used to 4 characters for Java, which is the default for Eclipse. I know that Javascript is typically 2 characters, but I have never seen 2 characters used for Java before. So, I just wanted to confirm that you want it to be 2 characters. If so, I will continue with 2 characters, which is what I have done in my changes so far to be consistent with the existing code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes I use google style as coding standard. It is available here : https://github.com/PebbleTemplates/pebble/blob/master/intellij-java-google-style.xml
or https://github.com/google/styleguide/blob/gh-pages/intellij-java-google-style.xml