-
Notifications
You must be signed in to change notification settings - Fork 170
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
adds PyList support to ForTag #390
Conversation
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.
Looks good. I'd like to see a few more tests to confirm it's correct.
Thanks!
@Test | ||
public void testTuplesWithPyList() { | ||
String template = "{% for href, caption in [('index.html', 'Index'), ('downloads.html', 'Downloads'), ('products.html', 'Products')] %}" + | ||
"<li><a href=\"{{href|e}}\">{{caption|e}}</a></li>\n" + |
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.
any reason for running this through the escape filter (e) here?
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.
this was mentioned in issue #321, therefore, I used the exact same thing to test the functionality out. there is no specific reason for choosing this template. 😅
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.
Ok, can you remove anything that's not relevant to the test then? We don't want to be testing the escape filter here.
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.
done!
"<li><a href=\"downloads.html\">Downloads</a></li>\n" + | ||
"<li><a href=\"products.html\">Products</a></li>\n"; | ||
|
||
String rendered = jinjava.render(template, context); |
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.
this could be broken out into a separate test
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.
done 👍
expected = "<p>1 2 3</p>\n" | ||
+ "<p>4 5 6</p>\n"; | ||
rendered = jinjava.render(template, context); | ||
assertEquals(rendered, expected); |
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.
could you also add a test that confirms that this works for non-string values? Perhaps add 1 + 2 + 3?
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.
I've added tests for the same.
Also, I've modified the ForTag.java file to accommodate the changes.
String entryVal = null; | ||
// safety check for size | ||
if (entries.size() >= loopVarIndex) { | ||
entryVal = Objects.toString(entries.get(loopVarIndex)); |
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.
can you add a test for this case?
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.
This is a safety check only but I've added tests where the jinjava.render
is expected to throw an exception when there are lesses values to unpack. 👍
@Test | ||
public void testTuplesWithPyList() { | ||
String template = "{% for href, caption in [('index.html', 'Index'), ('downloads.html', 'Downloads'), ('products.html', 'Products')] %}" + | ||
"<li><a href=\"{{href|e}}\">{{caption|e}}</a></li>\n" + |
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.
Ok, can you remove anything that's not relevant to the test then? We don't want to be testing the escape filter here.
Fixes #321
This pull request is wrt the above-mentioned Issue.
The support for For-Tag was limited to Maps and the else block in ForTag.java file could not handle tuples(list of tuples) types. This PR adds the needed code as well as the supporting tests.
Cheers!