Search in sources :

Example 6 with SoyDict

use of com.google.template.soy.data.SoyDict in project closure-templates by google.

the class TofuExceptionsTest method testExceptions_badType.

@Test
public void testExceptions_badType() throws Exception {
    SoyDict data = SoyValueConverterUtility.newDict("foo", "not a record");
    // This is an exception that occurs during template calling due to a type checkin
    try {
        tofu.newRenderer("ns.callerTemplate").setData(data).render();
        fail();
    } catch (SoyTofuException ste) {
        assertThat(ste).hasCauseThat().isNull();
        assertThat(ste).hasMessageThat().isEqualTo("Parameter type mismatch: attempt to bind value 'not a record' (a StringData) to " + "parameter 'foo' which has a declared type of '[bad: string, boo: int]'.");
        assertThat(ste.getStackTrace()[0].toString()).isEqualTo("ns.calleeTemplate(no-path:8)");
        assertThat(ste.getStackTrace()[1].toString()).isEqualTo("ns.callerTemplate(no-path:5)");
    }
}
Also used : SoyTofuException(com.google.template.soy.tofu.SoyTofuException) SoyDict(com.google.template.soy.data.SoyDict) Test(org.junit.Test)

Example 7 with SoyDict

use of com.google.template.soy.data.SoyDict in project closure-templates by google.

the class AugmentMapFunction method computeForJava.

// IntelliJ
@SuppressWarnings("ConstantConditions")
@Override
public SoyValue computeForJava(List<SoyValue> args) {
    SoyValue arg0 = args.get(0);
    SoyValue arg1 = args.get(1);
    Preconditions.checkArgument(arg0 instanceof SoyLegacyObjectMap, "First argument to augmentMap() function is not SoyLegacyObjectMap.");
    Preconditions.checkArgument(arg1 instanceof SoyLegacyObjectMap, "Second argument to augmentMap() function is not SoyLegacyObjectMap.");
    // TODO: Support map with nonstring key.
    Preconditions.checkArgument(arg0 instanceof SoyDict, "First argument to augmentMap() function is not SoyDict. Currently, augmentMap() doesn't" + " support maps that are not dicts (it is a todo).");
    Preconditions.checkArgument(arg1 instanceof SoyDict, "Second argument to augmentMap() function is not SoyDict. Currently, augmentMap() doesn't" + " support maps that are not dicts (it is a todo).");
    return BasicFunctionsRuntime.augmentMap((SoyDict) arg0, (SoyDict) arg1);
}
Also used : SoyLegacyObjectMap(com.google.template.soy.data.SoyLegacyObjectMap) SoyDict(com.google.template.soy.data.SoyDict) SoyValue(com.google.template.soy.data.SoyValue)

Example 8 with SoyDict

use of com.google.template.soy.data.SoyDict in project closure-templates by google.

the class RenderVisitorTest method testRenderNestedSelects.

@Test
public void testRenderNestedSelects() throws Exception {
    String templateBody = "{@param gender1: ?}\n" + "{@param gender2: ?}\n" + "{@param person1: ?}\n" + "{@param person2: ?}\n" + "  {msg desc=\"Nested selects\"}\n" + "     {select $gender1}\n" + "      {case 'female'}\n" + "        {select $gender2}\n" + "          {case 'female'}\n" + "             {$person1} shared her photos with {$person2} and her friends.\n" + "          {default}\n" + "             {$person1} shared her photos with {$person2} and his friends.\n" + "        {/select}\n" + "      {default}\n" + "        {select $gender2}\n" + "          {case 'female'}" + "              {$person1} shared his photos with {$person2} and her friends.\n" + "          {default}" + "              {$person1} shared his photos with {$person2} and his friends.\n" + "        {/select}\n" + "    {/select}\n" + "  {/msg}\n";
    SoyDict data = SoyValueConverterUtility.newDict("person1", "Alice", "gender1", "female", "person2", "Lara", "gender2", "female");
    assertRenderWithData(templateBody, data, "Alice shared her photos with Lara and her friends.");
    data = SoyValueConverterUtility.newDict("person1", "Alice", "gender1", "female", "person2", "Mark", "gender2", "male");
    assertRenderWithData(templateBody, data, "Alice shared her photos with Mark and his friends.");
    data = SoyValueConverterUtility.newDict("person1", "Bob", "gender1", "male", "person2", "Mark", "gender2", "male");
    assertRenderWithData(templateBody, data, "              Bob shared his photos with Mark and his friends.");
    data = SoyValueConverterUtility.newDict("person1", "Bob", "gender1", "male", "person2", "Lara", "gender2", "female");
    assertRenderWithData(templateBody, data, "              Bob shared his photos with Lara and her friends.");
}
Also used : SoyDict(com.google.template.soy.data.SoyDict) Test(org.junit.Test)

Example 9 with SoyDict

use of com.google.template.soy.data.SoyDict in project closure-templates by google.

the class RenderVisitorTest method testRenderPluralWithRuntimeErrors.

@Test
public void testRenderPluralWithRuntimeErrors() throws Exception {
    String templateBody = "{@param n_people: ?}\n" + "{@param person: ?}\n" + "  {msg desc=\"Simple plural message\"}\n" + "    {plural $n_people offset=\"1\"}\n" + "      {case 0}Nobody shared photos.\n" + "      {case 1}Only {$person} shared photos.\n" + "      {default}{$person} and {remainder($n_people)} others shared photos.\n" + "    {/plural}\n" + "  {/msg}\n";
    SoyDict data = SoyValueConverterUtility.newDict("person", "Bob", "n_people", "nobody");
    assertRenderExceptionWithData(templateBody, data, "Plural expression \"$n_people\" doesn't evaluate to number.");
}
Also used : SoyDict(com.google.template.soy.data.SoyDict) Test(org.junit.Test)

Example 10 with SoyDict

use of com.google.template.soy.data.SoyDict in project closure-templates by google.

the class RenderVisitorTest method testRenderPluralWithEmbeddedHtmlElements.

@Test
public void testRenderPluralWithEmbeddedHtmlElements() throws Exception {
    /**
     * Link to open up the email options dialog.
     *
     * @param num {number} Number of people who will be notified via email.
     */
    String templateBody = "{@param num: ?}\n" + "{msg desc=\"[ICU Syntax] Explanatory text saying that with current\n" + "     settings, $num people will be notified via email\"}\n" + "   {plural $num}\n" + "     {case 0}\n" + "         Notify people via email &rsaquo;\n" + "     {case 1}\n" + "         Notify{sp}\n" + "         <span class=\"{css('sharebox-id-email-number')}\">{$num}</span>{sp}\n" + "         person via email &rsaquo;\n" + "     {default}\n" + "         Notify{sp}\n" + "         <span class=\"{css('sharebox-id-email-number')}\">{$num}</span>{sp}\n" + "         people via email &rsaquo;\n" + "   {/plural}\n" + " {/msg}\n";
    SoyDict data = SoyValueConverterUtility.newDict("num", 1);
    assertRenderWithData(templateBody, data, "Notify <span class=\"sharebox-id-email-number\">1</span> person via email &rsaquo;");
    data = SoyValueConverterUtility.newDict("num", 10);
    assertRenderWithData(templateBody, data, "Notify <span class=\"sharebox-id-email-number\">10</span> people via email &rsaquo;");
}
Also used : SoyDict(com.google.template.soy.data.SoyDict) Test(org.junit.Test)

Aggregations

SoyDict (com.google.template.soy.data.SoyDict)26 Test (org.junit.Test)25 SoyTofuException (com.google.template.soy.tofu.SoyTofuException)6 BufferingAppendable (com.google.template.soy.data.LoggingAdvisingAppendable.BufferingAppendable)2 SoyFutureException (com.google.template.soy.data.SoyFutureException)2 SoyLegacyObjectMap (com.google.template.soy.data.SoyLegacyObjectMap)2 SoyValue (com.google.template.soy.data.SoyValue)2 CompiledTemplate (com.google.template.soy.jbcsrc.shared.CompiledTemplate)2 CompiledTemplates (com.google.template.soy.jbcsrc.shared.CompiledTemplates)2 RenderContext (com.google.template.soy.jbcsrc.shared.RenderContext)2 SoyRecord (com.google.template.soy.data.SoyRecord)1 SoyValueProvider (com.google.template.soy.data.SoyValueProvider)1 SoyMapImpl (com.google.template.soy.data.internal.SoyMapImpl)1 TemplateMetadata (com.google.template.soy.jbcsrc.shared.TemplateMetadata)1 TemplateRegistry (com.google.template.soy.soytree.TemplateRegistry)1 Flushable (java.io.Flushable)1 HashMap (java.util.HashMap)1