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

adds PyList support to ForTag #390

Merged
merged 3 commits into from
Jan 27, 2020
Merged

Conversation

gabru-md
Copy link
Contributor

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!

@gabru-md gabru-md requested a review from boulter January 21, 2020 18:52
Copy link
Contributor

@boulter boulter left a 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" +
Copy link
Contributor

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?

Copy link
Contributor Author

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. 😅

Copy link
Contributor

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.

Copy link
Contributor Author

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);
Copy link
Contributor

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

Copy link
Contributor Author

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);
Copy link
Contributor

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?

Copy link
Contributor Author

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));
Copy link
Contributor

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?

Copy link
Contributor Author

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. 👍

@gabru-md gabru-md requested a review from boulter January 27, 2020 14:16
@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" +
Copy link
Contributor

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.

@boulter boulter merged commit 4661af3 into HubSpot:master Jan 27, 2020
@gabru-md gabru-md deleted the gabru-md-for-fix branch March 17, 2020 12:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

for tuple not working
2 participants