Search in sources :

Example 21 with SoyDict

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

the class EvalVisitorTest method testEvalMapLiteral.

@Test
public void testEvalMapLiteral() throws Exception {
    SoyDict result = (SoyDict) eval("[:]");
    assertThat(result.getItemKeys()).isEmpty();
    result = (SoyDict) eval("['aaa': 'blah', 'bbb': 123, $foo.bar: $boo]");
    assertThat(result.getItemKeys()).hasSize(3);
    assertThat(result.getField("aaa").stringValue()).isEqualTo("blah");
    assertThat(result.getField("bbb").integerValue()).isEqualTo(123);
    assertThat(result.getField("baz").integerValue()).isEqualTo(8);
    // trailing comma
    result = (SoyDict) eval("['aaa': 'blah', 'bbb': 123, $foo.bar: $boo,]");
    assertThat(result.getItemKeys()).hasSize(3);
    assertThat(result.getField("aaa").stringValue()).isEqualTo("blah");
    assertThat(result.getField("bbb").integerValue()).isEqualTo(123);
    assertThat(result.getField("baz").integerValue()).isEqualTo(8);
    result = (SoyDict) eval("quoteKeysIfJs([:])");
    assertThat(result.getItemKeys()).isEmpty();
    result = (SoyDict) eval("quoteKeysIfJs( ['aaa': 'blah', 'bbb': 123, $foo.bar: $boo] )");
    assertThat(result.getItemKeys()).hasSize(3);
    assertThat(result.getField("aaa").stringValue()).isEqualTo("blah");
    assertThat(result.getField("bbb").integerValue()).isEqualTo(123);
    assertThat(result.getField("baz").integerValue()).isEqualTo(8);
    // Test last value overwrites earlier value for the same key.
    result = (SoyDict) eval("['baz': 'blah', $foo.bar: 'bluh']");
    assertThat(result.getField("baz").stringValue()).isEqualTo("bluh");
}
Also used : SoyDict(com.google.template.soy.data.SoyDict) Test(org.junit.Test)

Example 22 with SoyDict

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

the class RenderVisitorTest method testRenderPluralNestedInSelect.

@Test
public void testRenderPluralNestedInSelect() throws Exception {
    String templateBody = "  {@param person : ?}\n" + "  {@param n_people : ?}\n" + "  {@param gender : ?}\n" + "  {msg desc=\"Plural nested inside select\"}\n" + "    {select $gender}\n" + "      {case 'female'}\n" + "        {plural $n_people}\n" + "          {case 0}{$person} added nobody to her circle.\n" + "          {case 1}{$person} added one person to her circle.\n" + "          {default}{$person} added {$n_people} people to her circle.\n" + "        {/plural}\n" + "      {default}\n" + "        {plural $n_people}\n" + "          {case 0}{$person} added nobody to his circle.\n" + "          {case 1}{$person} added one person to his circle.\n" + "          {default}{$person} added {$n_people} people to his circle.\n" + "        {/plural}\n" + "   {/select}\n" + "  {/msg}\n";
    SoyDict data = SoyValueConverterUtility.newDict("person", "Alice", "gender", "female", "n_people", 0);
    assertRenderWithData(templateBody, data, "Alice added nobody to her circle.");
    data = SoyValueConverterUtility.newDict("person", "Alice", "gender", "female", "n_people", 1);
    assertRenderWithData(templateBody, data, "Alice added one person to her circle.");
    data = SoyValueConverterUtility.newDict("person", "Alice", "gender", "female", "n_people", 10);
    assertRenderWithData(templateBody, data, "Alice added 10 people to her circle.");
    data = SoyValueConverterUtility.newDict("person", "Bob", "gender", "male", "n_people", 10);
    assertRenderWithData(templateBody, data, "Bob added 10 people to his circle.");
}
Also used : SoyDict(com.google.template.soy.data.SoyDict) Test(org.junit.Test)

Example 23 with SoyDict

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

the class RenderVisitorTest method testRenderPlrselComplexConstructs.

@Test
public void testRenderPlrselComplexConstructs() throws Exception {
    String templateBody = "  {@param person : [gender: string, name: string]}\n" + "  {@param invitees : list<string>}\n" + "  {msg desc=\"[ICU Syntax] A sample nested message with complex constructs\"}\n" + "    {select $person.gender}\n" + "      {case 'female'}\n" + "        {plural length($invitees) offset=\"1\"}\n" + "          {case 0}{$person.name} added nobody to her circle.\n" + "          {case 1}{$person.name} added {$invitees[0]} to her circle.\n" + "          {default}{$person.name} added {$invitees[0]} and " + "{remainder(length($invitees))} others to her circle.\n" + "        {/plural}\n" + "      {default}\n" + "        {plural length($invitees) offset=\"1\"}\n" + "          {case 0}{$person.name} added nobody to his circle.\n" + "          {case 1}{$person.name} added {$invitees[0]} to his circle.\n" + "          {default}{$person.name} added {$invitees[0]} and " + "{remainder(length($invitees))} others to his circle.\n" + "        {/plural}\n" + "    {/select}\n" + "  {/msg}\n";
    SoyDict data = SoyValueConverterUtility.newDict("person", SoyValueConverterUtility.newDict("name", "Alice", "gender", "female"), "invitees", SoyValueConverterUtility.newList("Anna", "Brent", "Chris", "Darin"));
    assertRenderWithData(templateBody, data, "Alice added Anna and 3 others to her circle.");
    data = SoyValueConverterUtility.newDict("person", SoyValueConverterUtility.newDict("name", "Bob", "gender", "male"), "invitees", SoyValueConverterUtility.newList("Anna", "Brent", "Chris", "Darin"));
    assertRenderWithData(templateBody, data, "Bob added Anna and 3 others to his circle.");
}
Also used : SoyDict(com.google.template.soy.data.SoyDict) Test(org.junit.Test)

Example 24 with SoyDict

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

the class RenderVisitorTest method testRenderSimpleSelect.

@Test
public void testRenderSimpleSelect() throws Exception {
    String templateBody = "{@param person: ?}\n" + "{@param gender: ?}\n" + "  {msg desc=\"Simple select message\"}\n" + "    {select $gender}\n" + "      {case 'female'}{$person} shared her photos.\n" + "      {default}{$person} shared his photos.\n" + "    {/select}\n" + "  {/msg}\n";
    SoyDict data = SoyValueConverterUtility.newDict("person", "The president", "gender", "female");
    assertRenderWithData(templateBody, data, "The president shared her photos.");
    data = SoyValueConverterUtility.newDict("person", "The president", "gender", "male");
    assertRenderWithData(templateBody, data, "The president shared his photos.");
}
Also used : SoyDict(com.google.template.soy.data.SoyDict) Test(org.junit.Test)

Example 25 with SoyDict

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

the class RenderVisitorTest method testRenderFuture.

@Test
public void testRenderFuture() throws Exception {
    final StringBuilder progress = new StringBuilder();
    Flushable flushable = new Flushable() {

        @Override
        public void flush() {
            progress.append("flush;");
        }
    };
    String soyFileContent = "{namespace ns}\n" + "\n" + "/** @param boo @param foo @param goo */\n" + "{template .callerTemplate autoescape=\"deprecated-noncontextual\"}\n" + "  {call .calleeTemplate data=\"all\" /}\n" + "  {call .calleeTemplate data=\"$foo\" /}\n" + "  {call .calleeTemplate data=\"all\"}\n" + "    {param boo: $foo.boo /}\n" + "  {/call}\n" + "  {call .calleeTemplate data=\"all\"}\n" + "    {param boo: 'moo' /}\n" + "  {/call}\n" + "  {call .calleeTemplate data=\"$foo\"}\n" + "    {param boo}moo{/param}\n" + "  {/call}\n" + "  {call .calleeTemplate}\n" + "    {param boo}zoo{/param}\n" + "    {param goo: $foo.goo /}\n" + "  {/call}\n" + "{/template}\n" + "\n" + "/**\n" + " * @param boo\n" + " * @param goo\n" + " */\n" + "{template .calleeTemplate autoescape=\"deprecated-noncontextual\"}\n" + "  {$boo}{$ij.future}\n" + "  {for $n in $goo} {$n}{/for}{\\n}\n" + "{/template}\n";
    TemplateRegistry templateRegistry = SoyFileSetParserBuilder.forFileContents(soyFileContent).errorReporter(FAIL).parse().registry();
    SoyDict foo = SoyValueConverterUtility.newDict("boo", new TestFuture("foo", progress), "goo", SoyValueConverterUtility.newList(3, 2, 1));
    SoyDict data = SoyValueConverterUtility.newDict("boo", new TestFuture("boo", progress), "foo", foo, "goo", SoyValueConverterUtility.newList(1, 2, 3));
    SoyRecord testIj = SoyValueConverterUtility.newDict("future", new TestFuture("ij", progress));
    StringBuilder outputSb = new StringBuilder();
    CountingFlushableAppendable output = new CountingFlushableAppendable(outputSb, flushable);
    RenderVisitor rv = new RenderVisitor(new EvalVisitorFactoryImpl(), output, templateRegistry, data, testIj, Predicates.<String>alwaysFalse(), null, xidRenamingMap, cssRenamingMap, false);
    rv.exec(templateRegistry.getBasicTemplate("ns.callerTemplate"));
    String expectedOutput = "booij 1 2 3\n" + "fooij 3 2 1\n" + "fooij 1 2 3\n" + "mooij 1 2 3\n" + "mooij 3 2 1\n" + "zooij 3 2 1\n";
    assertThat(outputSb.toString()).isEqualTo(expectedOutput);
    assertThat(progress.toString()).isEqualTo("booflush;ijflush;foo");
}
Also used : TemplateRegistry(com.google.template.soy.soytree.TemplateRegistry) SoyRecord(com.google.template.soy.data.SoyRecord) SoyDict(com.google.template.soy.data.SoyDict) Flushable(java.io.Flushable) 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