use of com.google.template.soy.soytree.SoyFileSetNode in project closure-templates by google.
the class SoyExprForPySubject method translatesTo.
/**
* Asserts the subject translates to the expected PyExpr including verification of the exact
* PyExpr class (e.g. {@code PyStringExpr.class}).
*
* @param expectedPyExpr the expected result of translation
* @param expectedClass the expected class of the resulting PyExpr
*/
public void translatesTo(PyExpr expectedPyExpr, Class<? extends PyExpr> expectedClass) {
SoyFileSetNode soyTree = SoyFileSetParserBuilder.forTemplateContents(untypedTemplateBodyForExpression(getSubject())).options(opts).parse().fileSet();
PrintNode node = (PrintNode) SharedTestUtils.getNode(soyTree, 0);
ExprNode exprNode = node.getExpr();
PyExpr actualPyExpr = new TranslateToPyExprVisitor(localVarExprs, ErrorReporter.exploding()).exec(exprNode);
assertThat(actualPyExpr.getText()).isEqualTo(expectedPyExpr.getText());
assertThat(actualPyExpr.getPrecedence()).isEqualTo(expectedPyExpr.getPrecedence());
if (expectedClass != null) {
assertThat(actualPyExpr.getClass()).isEqualTo(expectedClass);
}
}
use of com.google.template.soy.soytree.SoyFileSetNode in project closure-templates by google.
the class VeLogInstrumentationVisitorTest method runPass.
/**
* Parses the given input as a template content.
*/
private static SoyFileSetNode runPass(String input) {
String soyFile = Joiner.on('\n').join("{namespace ns}", "", "{template .t}", input, "{/template}");
ParseResult result = SoyFileSetParserBuilder.forFileContents(soyFile).desugarHtmlNodes(false).typeRegistry(new SoyTypeRegistry.Builder().addDescriptors(ImmutableList.of(com.google.template.soy.testing.Foo.getDescriptor())).build()).setLoggingConfig(LOGGING_CONFIG).addSoyFunction(new TestLoggingFunction()).errorReporter(ErrorReporter.exploding()).parse();
TemplateRegistry templateRegistry = result.registry();
SoyFileSetNode soyTree = result.fileSet();
new VeLogInstrumentationVisitor(templateRegistry).exec(soyTree);
return soyTree;
}
use of com.google.template.soy.soytree.SoyFileSetNode in project closure-templates by google.
the class InsertMsgsVisitorTest method testFallbackMsgsUsingMsgBundle.
@Test
public void testFallbackMsgsUsingMsgBundle() {
SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(FALLBACK_TEST_FILE_CONTENT).parse().fileSet();
TemplateNode template = (TemplateNode) SharedTestUtils.getNode(soyTree);
// Before.
assertThat(template.numChildren()).isEqualTo(4);
assertThat(((MsgFallbackGroupNode) template.getChild(0)).numChildren()).isEqualTo(2);
assertThat(((MsgFallbackGroupNode) template.getChild(1)).numChildren()).isEqualTo(2);
assertThat(((MsgFallbackGroupNode) template.getChild(2)).numChildren()).isEqualTo(2);
assertThat(((MsgFallbackGroupNode) template.getChild(3)).numChildren()).isEqualTo(2);
// Build the translated message bundle.
List<SoyMsg> translatedMsgs = Lists.newArrayList();
MsgNode trans1FirstInstance = ((MsgFallbackGroupNode) template.getChild(1)).getChild(0);
translatedMsgs.add(SoyMsg.builder().setId(MsgUtils.computeMsgIdForDualFormat(trans1FirstInstance)).setLocaleString("x-zz").setParts(ImmutableList.<SoyMsgPart>of(SoyMsgRawTextPart.of("ztrans1"))).build());
MsgNode trans2FirstInstance = ((MsgFallbackGroupNode) template.getChild(2)).getChild(1);
translatedMsgs.add(SoyMsg.builder().setId(MsgUtils.computeMsgIdForDualFormat(trans2FirstInstance)).setLocaleString("x-zz").setParts(ImmutableList.<SoyMsgPart>of(SoyMsgRawTextPart.of("ztrans2"))).build());
SoyMsgBundle msgBundle = new SoyMsgBundleImpl("x-zz", translatedMsgs);
// Execute the visitor.
new InsertMsgsVisitor(msgBundle, FAIL).insertMsgs(template);
// After.
assertThat(template.numChildren()).isEqualTo(4);
assertThat(((RawTextNode) template.getChild(0)).getRawText()).isEqualTo("noTrans1");
assertThat(((RawTextNode) template.getChild(1)).getRawText()).isEqualTo("ztrans1");
assertThat(((RawTextNode) template.getChild(2)).getRawText()).isEqualTo("ztrans2");
assertThat(((RawTextNode) template.getChild(3)).getRawText()).isEqualTo("ztrans1");
}
use of com.google.template.soy.soytree.SoyFileSetNode in project closure-templates by google.
the class InsertMsgsVisitorTest method testPlrselMsgsUsingSoySource.
@Test
public void testPlrselMsgsUsingSoySource() {
SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(PLRSEL_TEST_FILE_CONTENT).parse().fileSet();
TemplateNode template = (TemplateNode) SharedTestUtils.getNode(soyTree);
assertThat(template.numChildren()).isEqualTo(2);
// Execute the visitor.
ErrorReporter errorReporter = ErrorReporter.createForTest();
new InsertMsgsVisitor(null, /* msgBundle */
errorReporter).insertMsgs(template);
assertThat(errorReporter.getErrors()).hasSize(2);
assertThat(errorReporter.getErrors().get(0).toString()).contains("JS code generation currently only supports plural/select messages when " + "shouldGenerateGoogMsgDefs is true.");
assertThat(errorReporter.getErrors().get(1).toString()).contains("JS code generation currently only supports plural/select messages when " + "shouldGenerateGoogMsgDefs is true.");
}
use of com.google.template.soy.soytree.SoyFileSetNode in project closure-templates by google.
the class InsertMsgsVisitorTest method testBasicMsgsUsingMsgBundle.
@Test
public void testBasicMsgsUsingMsgBundle() {
SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(BASIC_TEST_FILE_CONTENT).parse().fileSet();
TemplateNode template = (TemplateNode) SharedTestUtils.getNode(soyTree);
// Before.
assertThat(template.numChildren()).isEqualTo(5);
MsgNode msg = ((MsgFallbackGroupNode) template.getChild(2)).getChild(0);
assertThat(msg.numChildren()).isEqualTo(5);
MsgHtmlTagNode msgHtmlTag2 = (MsgHtmlTagNode) ((MsgPlaceholderNode) msg.getChild(2)).getChild(0);
assertThat(msgHtmlTag2.numChildren()).isEqualTo(3);
MsgHtmlTagNode msgHtmlTag4 = (MsgHtmlTagNode) ((MsgPlaceholderNode) msg.getChild(4)).getChild(0);
assertThat(msgHtmlTag4.numChildren()).isEqualTo(1);
assertThat(((RawTextNode) msgHtmlTag4.getChild(0)).getRawText()).isEqualTo("</a>");
// Build the translated message bundle.
List<SoyMsg> translatedMsgs = Lists.newArrayList();
// Original (en): random{{FOO}}{{START_LINK}}slimy{{END_LINK}}
// Translation (x-zz): {{START_LINK}}zslimy{{END_LINK}}{{FOO}}zrandom
translatedMsgs.add(SoyMsg.builder().setId(MsgUtils.computeMsgIdForDualFormat(msg)).setLocaleString("x-zz").setParts(ImmutableList.of(new SoyMsgPlaceholderPart("START_LINK", /* placeholderExample= */
null), SoyMsgRawTextPart.of("zslimy"), new SoyMsgPlaceholderPart("END_LINK", /* placeholderExample= */
null), new SoyMsgPlaceholderPart("FOO", /* placeholderExample= */
null), SoyMsgRawTextPart.of("zrandom"))).build());
// Note: This bundle has no translation for the message "dairy{$moo}".
SoyMsgBundle msgBundle = new SoyMsgBundleImpl("x-zz", translatedMsgs);
// Execute the visitor.
new InsertMsgsVisitor(msgBundle, FAIL).insertMsgs(template);
// After.
assertThat(template.numChildren()).isEqualTo(12);
assertThat(((PrintNode) template.getChild(0)).getExpr().toSourceString()).isEqualTo("$boo");
assertThat(((RawTextNode) template.getChild(1)).getRawText()).isEqualTo("scary ");
assertThat(((RawTextNode) template.getChild(2)).getRawText()).isEqualTo("<a href=\"");
assertThat(((PrintNode) template.getChild(3)).getExpr().toSourceString()).isEqualTo("$goo");
assertThat(((RawTextNode) template.getChild(4)).getRawText()).isEqualTo("\">");
assertThat(((RawTextNode) template.getChild(5)).getRawText()).isEqualTo("zslimy");
assertThat(((RawTextNode) template.getChild(6)).getRawText()).isEqualTo("</a>");
assertThat(((PrintNode) template.getChild(7)).getExpr().toSourceString()).isEqualTo("$foo");
assertThat(((RawTextNode) template.getChild(8)).getRawText()).isEqualTo("zrandom");
assertThat(((RawTextNode) template.getChild(9)).getRawText()).isEqualTo(" ");
assertThat(((RawTextNode) template.getChild(10)).getRawText()).isEqualTo("dairy");
assertThat(((PrintNode) template.getChild(11)).getExpr().toSourceString()).isEqualTo("$moo");
}
Aggregations