Search in sources :

Example 6 with PyExpr

use of com.google.template.soy.pysrc.restricted.PyExpr in project closure-templates by google.

the class StrSubFunctionTest method testComputeForPySrc_endIndex.

@Test
public void testComputeForPySrc_endIndex() {
    StrSubFunction strSub = new StrSubFunction();
    PyExpr base = new PyStringExpr("'foobar'", Integer.MAX_VALUE);
    PyExpr start = new PyExpr("3", Integer.MAX_VALUE);
    PyExpr end = new PyExpr("5", Integer.MAX_VALUE);
    assertThat(strSub.computeForPySrc(ImmutableList.of(base, start, end))).isEqualTo(new PyStringExpr("('foobar')[3:5]", Integer.MAX_VALUE));
}
Also used : PyExpr(com.google.template.soy.pysrc.restricted.PyExpr) PyStringExpr(com.google.template.soy.pysrc.restricted.PyStringExpr) Test(org.junit.Test)

Example 7 with PyExpr

use of com.google.template.soy.pysrc.restricted.PyExpr in project closure-templates by google.

the class StrSubFunctionTest method testComputeForPySrc_noEndIndex.

@Test
public void testComputeForPySrc_noEndIndex() {
    StrSubFunction strSub = new StrSubFunction();
    PyExpr base = new PyStringExpr("'foobar'", Integer.MAX_VALUE);
    PyExpr start = new PyExpr("3", Integer.MAX_VALUE);
    assertThat(strSub.computeForPySrc(ImmutableList.of(base, start))).isEqualTo(new PyStringExpr("('foobar')[3:]", Integer.MAX_VALUE));
}
Also used : PyExpr(com.google.template.soy.pysrc.restricted.PyExpr) PyStringExpr(com.google.template.soy.pysrc.restricted.PyStringExpr) Test(org.junit.Test)

Example 8 with PyExpr

use of com.google.template.soy.pysrc.restricted.PyExpr in project closure-templates by google.

the class BidiUnicodeWrapDirectiveTest method testApplyForPySrc.

@Test
public void testApplyForPySrc() {
    BidiUnicodeWrapDirective codeSnippet = new BidiUnicodeWrapDirective(SharedRestrictedTestUtils.BIDI_GLOBAL_DIR_FOR_PY_ISRTL_CODE_SNIPPET_PROVIDER);
    PyExpr data = new PyStringExpr("'data'");
    assertThat(codeSnippet.applyForPySrc(data, ImmutableList.<PyExpr>of()).getText()).isEqualTo("bidi.unicode_wrap(-1 if IS_RTL else 1, 'data')");
}
Also used : PyExpr(com.google.template.soy.pysrc.restricted.PyExpr) PyStringExpr(com.google.template.soy.pysrc.restricted.PyStringExpr) Test(org.junit.Test)

Example 9 with PyExpr

use of com.google.template.soy.pysrc.restricted.PyExpr in project closure-templates by google.

the class CleanHtmlDirectiveTest method testApplyForPySrc_optionalSafeTags.

@Test
public void testApplyForPySrc_optionalSafeTags() {
    CleanHtmlDirective cleanHtml = new CleanHtmlDirective();
    PyExpr data = new PyExpr("'data'", Integer.MAX_VALUE);
    // All possible OptionalSafeTags.
    ImmutableList<PyExpr> optionalSafeTagsAsPyExprs = Arrays.stream(OptionalSafeTag.values()).map(OptionalSafeTag.TO_TAG_NAME).map(input -> new PyExpr(String.format("'%s'", input), Integer.MAX_VALUE)).collect(toImmutableList());
    assertThat(cleanHtml.applyForPySrc(data, optionalSafeTagsAsPyExprs).getText()).isEqualTo("sanitize.clean_html('data', ['li', 'ol', 'span', 'ul'])");
    // Only the specified optional safe tags are passed to $$cleanHtml.
    PyExpr span = new PyExpr("'span'", Integer.MAX_VALUE);
    assertThat(cleanHtml.applyForPySrc(data, ImmutableList.of(span)).getText()).isEqualTo("sanitize.clean_html('data', ['span'])");
    // Invalid optional safe tags.
    try {
        PyExpr unsupported = new PyExpr("'unsupported'", Integer.MAX_VALUE);
        cleanHtml.applyForPySrc(data, ImmutableList.of(unsupported));
        fail("Non-whitelisted tag allowed to be sent as a safe-tag to 'clean_html'.");
    } catch (IllegalArgumentException e) {
    // Test passes.
    }
    try {
        cleanHtml.applyForPySrc(data, ImmutableList.of(new PyExpr("'li, ul'", Integer.MAX_VALUE)));
        fail("Invalid format allowed to be used as a safe-tag in 'clean_html'");
    } catch (IllegalArgumentException e) {
    // Test passes.
    }
    // Invalid parameter syntax.
    try {
        cleanHtml.applyForPySrc(data, ImmutableList.of(new PyExpr("$myExtraSafeTags", Integer.MAX_VALUE)));
        fail("Non-String allowed to be used as a safe-tag in 'clean_html'");
    } catch (IllegalArgumentException e) {
        assertThat(e).hasMessageThat().isEqualTo("The cleanHtml directive expects arguments to be tag name string " + "literals, such as 'span'. Encountered: $myExtraSafeTags");
    }
}
Also used : Arrays(java.util.Arrays) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) JUnit4(org.junit.runners.JUnit4) Truth.assertThat(com.google.common.truth.Truth.assertThat) PyExpr(com.google.template.soy.pysrc.restricted.PyExpr) OptionalSafeTag(com.google.template.soy.shared.internal.TagWhitelist.OptionalSafeTag) SoyValue(com.google.template.soy.data.SoyValue) JsExpr(com.google.template.soy.jssrc.restricted.JsExpr) SanitizedContent(com.google.template.soy.data.SanitizedContent) UnsafeSanitizedContentOrdainer(com.google.template.soy.data.UnsafeSanitizedContentOrdainer) ImmutableList(com.google.common.collect.ImmutableList) StringData(com.google.template.soy.data.restricted.StringData) Assert.fail(org.junit.Assert.fail) ContentKind(com.google.template.soy.data.SanitizedContent.ContentKind) AbstractSoyPrintDirectiveTestCase(com.google.template.soy.shared.AbstractSoyPrintDirectiveTestCase) PyExpr(com.google.template.soy.pysrc.restricted.PyExpr) Test(org.junit.Test)

Example 10 with PyExpr

use of com.google.template.soy.pysrc.restricted.PyExpr in project closure-templates by google.

the class TruncateDirectiveTest method testApplyForPySrc.

@Test
public void testApplyForPySrc() {
    TruncateDirective truncateDirective = new TruncateDirective();
    PyExpr data = new PyStringExpr("'data'", Integer.MAX_VALUE);
    PyExpr dataRef = new PyExpr("opt_data[myKey]", Integer.MAX_VALUE);
    PyExpr maxLenExpr = new PyExpr("8", Integer.MAX_VALUE);
    PyExpr trueExpr = new PyExpr("True", Integer.MAX_VALUE);
    PyExpr falseExpr = new PyExpr("False", Integer.MAX_VALUE);
    assertThat(truncateDirective.applyForPySrc(data, ImmutableList.of(maxLenExpr)).getText()).isEqualTo("directives.truncate('data', 8, True)");
    assertThat(truncateDirective.applyForPySrc(data, ImmutableList.of(maxLenExpr, trueExpr)).getText()).isEqualTo("directives.truncate('data', 8, True)");
    assertThat(truncateDirective.applyForPySrc(data, ImmutableList.of(maxLenExpr, falseExpr)).getText()).isEqualTo("directives.truncate('data', 8, False)");
    assertThat(truncateDirective.applyForPySrc(dataRef, ImmutableList.of(maxLenExpr)).getText()).isEqualTo("directives.truncate(str(opt_data[myKey]), 8, True)");
}
Also used : PyExpr(com.google.template.soy.pysrc.restricted.PyExpr) PyStringExpr(com.google.template.soy.pysrc.restricted.PyStringExpr) Test(org.junit.Test)

Aggregations

PyExpr (com.google.template.soy.pysrc.restricted.PyExpr)82 Test (org.junit.Test)58 PyStringExpr (com.google.template.soy.pysrc.restricted.PyStringExpr)21 PyFunctionExprBuilder (com.google.template.soy.pysrc.restricted.PyFunctionExprBuilder)6 LinkedHashMap (java.util.LinkedHashMap)4 SoyPySrcPrintDirective (com.google.template.soy.pysrc.restricted.SoyPySrcPrintDirective)3 SoyPrintDirective (com.google.template.soy.shared.restricted.SoyPrintDirective)3 SoyNode (com.google.template.soy.soytree.SoyNode)3 ExprNode (com.google.template.soy.exprtree.ExprNode)2 ExprRootNode (com.google.template.soy.exprtree.ExprRootNode)2 PyListExpr (com.google.template.soy.pysrc.restricted.PyListExpr)2 MsgPluralNode (com.google.template.soy.soytree.MsgPluralNode)2 PrintNode (com.google.template.soy.soytree.PrintNode)2 SoyFileSetNode (com.google.template.soy.soytree.SoyFileSetNode)2 ParentSoyNode (com.google.template.soy.soytree.SoyNode.ParentSoyNode)2 Supplier (com.google.common.base.Supplier)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Truth.assertThat (com.google.common.truth.Truth.assertThat)1