use of com.google.template.soy.data.SoyDict in project closure-templates by google.
the class RenderVisitorTest method testRenderSimplePlural.
@Test
public void testRenderSimplePlural() 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", 0);
assertRenderWithData(templateBody, data, "Nobody shared photos.");
data = SoyValueConverterUtility.newDict("person", "Bob", "n_people", 1);
assertRenderWithData(templateBody, data, "Only Bob shared photos.");
data = SoyValueConverterUtility.newDict("person", "Bob", "n_people", 10);
assertRenderWithData(templateBody, data, "Bob and 9 others shared photos.");
}
use of com.google.template.soy.data.SoyDict in project closure-templates by google.
the class RenderVisitorTest method testRenderBasicCall.
@Test
public void testRenderBasicCall() throws Exception {
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}\n" + " {for $n in $goo} {$n}{/for}{\\n}\n" + "{/template}\n";
SoyDict foo = SoyValueConverterUtility.newDict("boo", "foo", "goo", SoyValueConverterUtility.newList(3, 2, 1));
SoyDict data = SoyValueConverterUtility.newDict("boo", "boo", "foo", foo, "goo", SoyValueConverterUtility.newList(1, 2, 3));
String expectedOutput = "boo 1 2 3\n" + "foo 3 2 1\n" + "foo 1 2 3\n" + "moo 1 2 3\n" + "moo 3 2 1\n" + "zoo 3 2 1\n";
assertThat(renderTemplateInFile(soyFileContent, "ns.callerTemplate", data, TEST_IJ_DATA, Predicates.<String>alwaysFalse())).isEqualTo(expectedOutput);
}
use of com.google.template.soy.data.SoyDict in project closure-templates by google.
the class RenderVisitorTest method testRenderForStmt1.
@Test
public void testRenderForStmt1() throws Exception {
String templateBody = "" + "{@param goo : list<?> }\n" + "{@param list0 : list<?> }\n" + "{@param foo : ? }\n" + "{@param boo : ? }\n" + " {for $n in $goo}\n" + " {if not isFirst($n)}{\\n}{/if}\n" + " {$n} = Sum of 1 through {index($n) + 1}.\n" + " {/for}\n" + " {\\n}\n" + " {for $i in $goo}\n" + " {for $j in $foo.goo2}\n" + " {if $i == $j} {$i + $j}{/if}\n" + " {/for}\n" + " {/for}\n" + " {sp}\n" + " {for $item in $list0}\n" + " Blah\n" + " {ifempty}\n" + " Bluh\n" + " {/for}\n" + " {for $item in $list0}\n" + " Blah\n" + " {/for}\n" + " {for $item in ['blah', 123, $boo]}\n" + " {sp}{$item}\n" + " {/for}\n";
assertRender(templateBody, "" + "1 = Sum of 1 through 1.\n" + "3 = Sum of 1 through 2.\n" + "6 = Sum of 1 through 3.\n" + "10 = Sum of 1 through 4.\n" + "15 = Sum of 1 through 5.\n" + "21 = Sum of 1 through 6.\n" + " 2 6 12 20 30 42 Bluh blah 123 8");
// Test iteration over map keys.
templateBody = "" + "{@param myMap : map<string, ?> }\n" + " {for $key in mapKeys($myMap)}\n" + " {if isFirst($key)}\n" + " [\n" + " {/if}\n" + " {$key}: {$myMap[$key]}\n" + " {if isLast($key)}\n" + " ]\n" + " {else}\n" + " ,{sp}\n" + " {/if}\n" + " {/for}\n";
SoyDict data = SoyValueConverterUtility.newDict("myMap", SoyValueConverterUtility.newDict("aaa", "Blah", "bbb", 17));
String output = renderWithData(templateBody, data);
assertThat(ImmutableSet.of("[aaa: Blah, bbb: 17]", "[bbb: 17, aaa: Blah]")).contains(output);
}
use of com.google.template.soy.data.SoyDict in project closure-templates by google.
the class StreamingPrintDirectivesTest method testStreamingDisablesRuntimeTypeChecks.
/**
* This test demonstrates a change in behavior when streaming print directives are in use, they
* can elide some runtime type checking. This is because the compiler explicitly puts in {@code
* checkcast} instructions whenever calling {@link SoyValueProvider#resolve} (see every caller of
* {@link ExpressionDetacher#resolveSoyValueProvider(Expression)}). However, when we are able to
* stream a soy value provider we can't insert a {@code checkcast} instruction because we never
* actually calculate the full value.
*
* <p>We could change SoyValueProvider.renderAndResolve to accept a TypePredicate and we could
* sometimes enforce it, but for the time being this isn't happening.
*/
@Test
public void testStreamingDisablesRuntimeTypeChecks() throws IOException {
CompiledTemplates templates = compileFile("{namespace ns}", "", "{template .streamable}", " {@param i : int}", " {$i |streaming}", "{/template}", "", "{template .nonstreamable}", " {@param i : int}", " {$i |nonstreaming}", "{/template}");
RenderContext context = getDefaultContext(templates);
SoyDict badParam = SoyValueConverterUtility.newDict("i", "notAnInt");
BufferingAppendable output = BufferingAppendable.buffering();
templates.getTemplateFactory("ns.streamable").create(badParam, EMPTY_DICT).render(output, context);
assertThat(output.getAndClearBuffer()).isEqualTo("(stream: notAnInt)");
try {
templates.getTemplateFactory("ns.nonstreamable").create(badParam, EMPTY_DICT).render(output, context);
fail("Expected ClassCastException");
} catch (ClassCastException cce) {
assertThat(cce).hasMessageThat().isEqualTo("com.google.template.soy.data.restricted.StringData cannot be cast to " + "com.google.template.soy.data.restricted.IntegerData");
}
}
use of com.google.template.soy.data.SoyDict in project closure-templates by google.
the class BytecodeCompilerTest method testParamValidation.
@Test
public void testParamValidation() throws Exception {
CompiledTemplates templates = TemplateTester.compileTemplateBody("{@param foo : int}", "{$foo ?: -1}");
CompiledTemplate.Factory singleParam = templates.getTemplateFactory("ns.foo");
RenderContext context = getDefaultContext(templates);
BufferingAppendable builder = LoggingAdvisingAppendable.buffering();
SoyDict params = SoyValueConverterUtility.newDict("foo", IntegerData.forValue(1));
singleParam.create(params, EMPTY_DICT).render(builder, context);
assertThat(builder.getAndClearBuffer()).isEqualTo("1");
singleParam.create(EMPTY_DICT, EMPTY_DICT).render(builder, context);
assertThat(builder.getAndClearBuffer()).isEqualTo("-1");
templates = TemplateTester.compileTemplateBody("{@inject foo : int}", "{$foo}");
CompiledTemplate.Factory singleIj = templates.getTemplateFactory("ns.foo");
context = getDefaultContext(templates);
params = SoyValueConverterUtility.newDict("foo", IntegerData.forValue(1));
singleIj.create(SoyValueConverter.EMPTY_DICT, params).render(builder, context);
assertThat(builder.getAndClearBuffer()).isEqualTo("1");
params = SoyValueConverterUtility.newDict();
singleIj.create(SoyValueConverter.EMPTY_DICT, params).render(builder, context);
assertThat(builder.getAndClearBuffer()).isEqualTo("null");
}
Aggregations