Search in sources :

Example 46 with JsExpr

use of com.google.template.soy.jssrc.restricted.JsExpr in project closure-templates by google.

the class BidiSpanWrapDirectiveTest method testApplyForJsSrc.

@Test
public void testApplyForJsSrc() {
    JsExpr dataRef = new JsExpr("opt_data.myKey", Integer.MAX_VALUE);
    assertThat(BIDI_SPAN_WRAP_DIRECTIVE_FOR_STATIC_LTR.applyForJsSrc(dataRef, ImmutableList.<JsExpr>of()).getText()).isEqualTo("soy.$$bidiSpanWrap(1, opt_data.myKey)");
    assertThat(BIDI_SPAN_WRAP_DIRECTIVE_FOR_STATIC_RTL.applyForJsSrc(dataRef, ImmutableList.<JsExpr>of()).getText()).isEqualTo("soy.$$bidiSpanWrap(-1, opt_data.myKey)");
    BidiSpanWrapDirective codeSnippet = new BidiSpanWrapDirective(SharedRestrictedTestUtils.BIDI_GLOBAL_DIR_FOR_JS_ISRTL_CODE_SNIPPET_PROVIDER);
    assertThat(codeSnippet.applyForJsSrc(dataRef, ImmutableList.<JsExpr>of()).getText()).isEqualTo("soy.$$bidiSpanWrap(IS_RTL?-1:1, opt_data.myKey)");
}
Also used : JsExpr(com.google.template.soy.jssrc.restricted.JsExpr) Test(org.junit.Test)

Example 47 with JsExpr

use of com.google.template.soy.jssrc.restricted.JsExpr in project closure-templates by google.

the class BidiGlobalDirFunctionTest method testComputeForJsSrc.

@Test
public void testComputeForJsSrc() {
    assertThat(BIDI_GLOBAL_DIR_FUNCTION_FOR_STATIC_LTR.computeForJsSrc(ImmutableList.<JsExpr>of())).isEqualTo(new JsExpr("1", Integer.MAX_VALUE));
    assertThat(BIDI_GLOBAL_DIR_FUNCTION_FOR_STATIC_RTL.computeForJsSrc(ImmutableList.<JsExpr>of())).isEqualTo(new JsExpr("-1", Integer.MAX_VALUE));
    BidiGlobalDirFunction codeSnippet = new BidiGlobalDirFunction(SharedRestrictedTestUtils.BIDI_GLOBAL_DIR_FOR_JS_ISRTL_CODE_SNIPPET_PROVIDER);
    assertThat(codeSnippet.computeForJsSrc(ImmutableList.<JsExpr>of())).isEqualTo(new JsExpr("IS_RTL?-1:1", Operator.CONDITIONAL.getPrecedence()));
}
Also used : JsExpr(com.google.template.soy.jssrc.restricted.JsExpr) Test(org.junit.Test)

Example 48 with JsExpr

use of com.google.template.soy.jssrc.restricted.JsExpr in project closure-templates by google.

the class BidiMarkFunctionTest method testComputeForJsSrc.

@Test
public void testComputeForJsSrc() {
    assertThat(BIDI_MARK_FUNCTION_FOR_STATIC_LTR.computeForJsSrc(ImmutableList.<JsExpr>of())).isEqualTo(new JsExpr("'\\u200E'", Integer.MAX_VALUE));
    assertThat(BIDI_MARK_FUNCTION_FOR_STATIC_RTL.computeForJsSrc(ImmutableList.<JsExpr>of())).isEqualTo(new JsExpr("'\\u200F'", Integer.MAX_VALUE));
    BidiMarkFunction codeSnippet = new BidiMarkFunction(SharedRestrictedTestUtils.BIDI_GLOBAL_DIR_FOR_JS_ISRTL_CODE_SNIPPET_PROVIDER);
    assertThat(codeSnippet.computeForJsSrc(ImmutableList.<JsExpr>of())).isEqualTo(new JsExpr("(IS_RTL?-1:1) < 0 ? '\\u200F' : '\\u200E'", Operator.CONDITIONAL.getPrecedence()));
}
Also used : JsExpr(com.google.template.soy.jssrc.restricted.JsExpr) Test(org.junit.Test)

Example 49 with JsExpr

use of com.google.template.soy.jssrc.restricted.JsExpr in project closure-templates by google.

the class CleanHtmlDirectiveTest method testApplyForJsSrc_optionalSafeTags.

@Test
public void testApplyForJsSrc_optionalSafeTags() {
    CleanHtmlDirective cleanHtml = new CleanHtmlDirective();
    JsExpr dataRef = new JsExpr("opt_data.myKey", Integer.MAX_VALUE);
    // All possible OptionalSafeTags.
    ImmutableList<JsExpr> optionalSafeTagsAsJsExprs = Arrays.stream(OptionalSafeTag.values()).map(OptionalSafeTag.TO_TAG_NAME).map(input -> new JsExpr(String.format("'%s'", input), Integer.MAX_VALUE)).collect(toImmutableList());
    assertThat(cleanHtml.applyForJsSrc(dataRef, optionalSafeTagsAsJsExprs).getText()).isEqualTo("soy.$$cleanHtml(opt_data.myKey, ['li', 'ol', 'span', 'ul'])");
    // Only the specified optional safe tags are passed to $$cleanHtml.
    assertThat(cleanHtml.applyForJsSrc(dataRef, ImmutableList.of(new JsExpr("'span'", Integer.MAX_VALUE))).getText()).isEqualTo("soy.$$cleanHtml(opt_data.myKey, ['span'])");
    // Invalid optional safe tags.
    try {
        cleanHtml.applyForJsSrc(dataRef, ImmutableList.of(new JsExpr("'unsupported'", Integer.MAX_VALUE)));
        fail();
    } catch (IllegalArgumentException e) {
    // Test passes.
    }
    try {
        cleanHtml.applyForJsSrc(dataRef, ImmutableList.of(new JsExpr("'li, ul'", Integer.MAX_VALUE)));
        fail();
    } catch (IllegalArgumentException e) {
    // Test passes.
    }
    // Invalid parameter syntax.
    try {
        cleanHtml.applyForJsSrc(dataRef, ImmutableList.of(new JsExpr("$myExtraSafeTags", Integer.MAX_VALUE)));
        fail();
    } 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) JsExpr(com.google.template.soy.jssrc.restricted.JsExpr) Test(org.junit.Test)

Example 50 with JsExpr

use of com.google.template.soy.jssrc.restricted.JsExpr in project closure-templates by google.

the class InsertWordBreaksDirectiveTest method testApplyForJsSrc.

@Test
public void testApplyForJsSrc() {
    InsertWordBreaksDirective insertWordBreaksDirective = new InsertWordBreaksDirective();
    JsExpr dataRef = new JsExpr("opt_data.myKey", Integer.MAX_VALUE);
    JsExpr arg = new JsExpr("8", Integer.MAX_VALUE);
    assertThat(insertWordBreaksDirective.applyForJsSrc(dataRef, ImmutableList.of(arg)).getText()).isEqualTo("soy.$$insertWordBreaks(opt_data.myKey, 8)");
}
Also used : JsExpr(com.google.template.soy.jssrc.restricted.JsExpr) Test(org.junit.Test)

Aggregations

JsExpr (com.google.template.soy.jssrc.restricted.JsExpr)64 Test (org.junit.Test)42 CodeChunk (com.google.template.soy.jssrc.dsl.CodeChunk)3 ImmutableList (com.google.common.collect.ImmutableList)2 RequiresCollector (com.google.template.soy.jssrc.dsl.CodeChunk.RequiresCollector)2 SoyLibraryAssistedJsSrcPrintDirective (com.google.template.soy.jssrc.restricted.SoyLibraryAssistedJsSrcPrintDirective)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)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 SanitizedContent (com.google.template.soy.data.SanitizedContent)1 ContentKind (com.google.template.soy.data.SanitizedContent.ContentKind)1 SoyValue (com.google.template.soy.data.SoyValue)1 UnsafeSanitizedContentOrdainer (com.google.template.soy.data.UnsafeSanitizedContentOrdainer)1 SoyString (com.google.template.soy.data.restricted.SoyString)1 StringData (com.google.template.soy.data.restricted.StringData)1 ExprRootNode (com.google.template.soy.exprtree.ExprRootNode)1 StringNode (com.google.template.soy.exprtree.StringNode)1 WithValue (com.google.template.soy.jssrc.dsl.CodeChunk.WithValue)1 SoyJsSrcPrintDirective (com.google.template.soy.jssrc.restricted.SoyJsSrcPrintDirective)1