use of com.google.template.soy.SoyModule in project closure-templates by google.
the class JspbTest method testHeaderParamFieldImport.
// Proto import tests
/**
* Test to check that soy code that accesses proto fields, correctly generate JS that includes
* imports for the field types.
*/
@Test
public void testHeaderParamFieldImport() {
Injector injector = Guice.createInjector(new SoyModule());
SoyJsSrcOptions jsSrcOptions = new SoyJsSrcOptions();
jsSrcOptions.setShouldProvideRequireSoyNamespaces(true);
GenJsCodeVisitor genJsCodeVisitor = JsSrcMain.createVisitor(jsSrcOptions, injector.getInstance(SoyTypeRegistry.class));
genJsCodeVisitor.jsCodeBuilder = new JsCodeBuilder();
String testFileContent = "{namespace boo.foo}\n" + "\n" + "/** */\n" + "{template .goo autoescape=\"deprecated-noncontextual\"}\n" + " {@param moo : example.ExampleExtendable}\n" + " {$moo.someExtensionField}\n" + "{/template}\n";
ParseResult parseResult = SoyFileSetParserBuilder.forFileContents(testFileContent).declaredSyntaxVersion(SyntaxVersion.V2_0).typeRegistry(REGISTRY).parse();
// Verify that the import symbol got required.
String expectedJsFileContentStart = "// This file was automatically generated from no-path.\n" + "// Please don't edit this file by hand.\n" + "\n" + "/**\n" + " * @fileoverview Templates in namespace boo.foo.\n" + " * @public\n" + " */\n" + "\n" + "goog.provide('boo.foo');\n" + "\n" + "goog.require('proto.example.ExampleExtendable');\n" + "goog.require('proto.example.SomeExtension');\n" + "goog.require('soy.asserts');\n" + "\n" + "\n" + "/**\n" + " * @param {boo.foo.goo.Params} opt_data\n" + " * @param {Object<string, *>=} opt_ijData\n" + " * @param {Object<string, *>=} opt_ijData_deprecated\n" + " * @return {string}\n" + " * @suppress {checkTypes|uselessCode}\n" + " */\n" + "boo.foo.goo = function(opt_data, opt_ijData, opt_ijData_deprecated) {\n" + " opt_ijData = opt_ijData_deprecated || opt_ijData;\n" + " var $tmp = opt_data.moo.$jspbMessageInstance || opt_data.moo;\n" + " /** @type {proto.example.ExampleExtendable} */\n" + " var moo = soy.asserts.assertType(" + "$tmp instanceof proto.example.ExampleExtendable, " + "'moo', $tmp, 'proto.example.ExampleExtendable');\n" + " return '' + moo.getExtension(proto.example.SomeExtension.someExtensionField);\n" + "};\n" + "/**\n" + " * @typedef {{\n" + " * moo: proto.example.ExampleExtendable,\n" + " * }}\n" + " */\n" + "boo.foo.goo.Params;\n" + "if (goog.DEBUG) {\n" + " boo.foo.goo.soyTemplateName = 'boo.foo.goo';\n" + "}\n" + "";
List<String> jsFilesContents = genJsCodeVisitor.gen(parseResult.fileSet(), parseResult.registry(), ErrorReporter.exploding());
assertThat(jsFilesContents.get(0)).isEqualTo(expectedJsFileContentStart);
}
use of com.google.template.soy.SoyModule in project closure-templates by google.
the class SoySauceTest method setUp.
@Before
public void setUp() throws Exception {
Injector injector = Guice.createInjector(new SoyModule());
SoyFileSet.Builder builder = injector.getInstance(SoyFileSet.Builder.class);
builder.add(SoySauceTest.class.getResource("strict.soy"));
builder.add(SoySauceTest.class.getResource("non_strict.soy"));
sauce = builder.build().compileTemplates();
}
use of com.google.template.soy.SoyModule in project closure-templates by google.
the class FeaturesUsage method execMain.
private void execMain(String[] args) throws IOException {
CmdLineParser cmdLineParser = new CmdLineParser(this);
cmdLineParser.setUsageWidth(100);
try {
cmdLineParser.parseArgument(args);
} catch (CmdLineException cle) {
System.err.println("\nError: " + cle.getMessage() + "\n\n");
System.err.println(USAGE_PREFIX);
cmdLineParser.printUsage(System.err);
System.exit(1);
}
Injector injector = Guice.createInjector(new SoyModule());
SoyFileSet.Builder sfsBuilder = injector.getInstance(SoyFileSet.Builder.class);
SoyFileSet sfs = sfsBuilder.add(Resources.getResource("simple.soy")).add(Resources.getResource("features.soy")).setCompileTimeGlobals(Resources.getResource("FeaturesUsage_globals.txt")).build();
SoyTofu tofu = sfs.compileToTofu().forNamespace("soy.examples.features");
SoyMsgBundle msgBundle;
if (locale.length() > 0) {
// Use translations from an XLIFF file.
SoyMsgBundleHandler msgBundleHandler = injector.getInstance(SoyMsgBundleHandler.class);
URL xliffResource = Resources.getResource(XLIFF_RESOURCE_PREFIX + locale + ".xlf");
msgBundle = msgBundleHandler.createFromResource(xliffResource);
if (msgBundle.getLocaleString() == null) {
throw new IOException("Error reading message resource \"" + XLIFF_RESOURCE_PREFIX + locale + ".xlf\".");
}
} else {
// Use the messages from the Soy source files.
msgBundle = null;
}
// Note: In the examples below, I sometimes use the version of render() that takes a SoyMapData
// and sometimes use the version that takes a Map<String, ?>. They both work. The version that
// takes a SoyMapData is more efficient if you need to reuse the same template data object for
// multiple calls of render() (because the version that takes a Map<String, ?> internally
// converts it to a new SoyMapData on every call).
writeExampleHeader("demoComments");
System.out.println(tofu.newRenderer(DEMO_COMMENTS).setMsgBundle(msgBundle).render());
writeExampleHeader("demoLineJoining");
System.out.println(tofu.newRenderer(DEMO_LINE_JOINING).setMsgBundle(msgBundle).render());
writeExampleHeader("demoRawTextCommands");
System.out.println(tofu.newRenderer(DEMO_RAW_TEXT_COMMANDS).setMsgBundle(msgBundle).setContentKind(SanitizedContent.ContentKind.TEXT).render());
writeExampleHeader("demoPrint");
System.out.println(tofu.newRenderer(DEMO_PRINT).setData(new SoyMapData(DemoPrintSoyTemplateInfo.BOO, "Boo!", DemoPrintSoyTemplateInfo.TWO, 2)).setMsgBundle(msgBundle).render());
writeExampleHeader("demoAutoescapeTrue");
System.out.println(tofu.newRenderer(DEMO_AUTOESCAPE_TRUE).setData(new SoyMapData(DemoAutoescapeTrueSoyTemplateInfo.ITALIC_HTML, "<i>italic</i>")).setMsgBundle(msgBundle).render());
writeExampleHeader("demoMsg");
System.out.println(tofu.newRenderer(DEMO_MSG).setData(ImmutableMap.of(DemoMsgSoyTemplateInfo.NAME, "Ed", DemoMsgSoyTemplateInfo.LABS_URL, "http://labs.google.com")).setMsgBundle(msgBundle).render());
writeExampleHeader("demoIf");
System.out.println(tofu.newRenderer(DEMO_IF).setData(new SoyMapData("pi", 3.14159)).setMsgBundle(msgBundle).render());
System.out.println(tofu.newRenderer(DEMO_IF).setData(new SoyMapData("pi", 2.71828)).setMsgBundle(msgBundle).render());
System.out.println(tofu.newRenderer(DEMO_IF).setData(new SoyMapData("pi", 1.61803)).setMsgBundle(msgBundle).render());
writeExampleHeader("demoSwitch");
System.out.println(tofu.newRenderer(DEMO_SWITCH).setData(ImmutableMap.of("name", "Fay")).setMsgBundle(msgBundle).render());
System.out.println(tofu.newRenderer(DEMO_SWITCH).setData(ImmutableMap.of("name", "Go")).setMsgBundle(msgBundle).render());
System.out.println(tofu.newRenderer(DEMO_SWITCH).setData(ImmutableMap.of("name", "Hal")).setMsgBundle(msgBundle).render());
System.out.println(tofu.newRenderer(DEMO_SWITCH).setData(ImmutableMap.of("name", "Ivy")).setMsgBundle(msgBundle).render());
writeExampleHeader("demoFor");
SoyListData persons = new SoyListData();
persons.add(new SoyMapData("name", "Jen", "numWaffles", 1));
persons.add(new SoyMapData("name", "Kai", "numWaffles", 3));
persons.add(new SoyMapData("name", "Lex", "numWaffles", 1));
persons.add(new SoyMapData("name", "Mel", "numWaffles", 2));
System.out.println(tofu.newRenderer(DEMO_FOR).setData(new SoyMapData(DemoForSoyTemplateInfo.PERSONS, persons)).setMsgBundle(msgBundle).render());
writeExampleHeader("demoFor_Range");
System.out.println(tofu.newRenderer(DEMO_FOR_RANGE).setData(new SoyMapData(DemoForRangeSoyTemplateInfo.NUM_LINES, 3)).setMsgBundle(msgBundle).render());
writeExampleHeader("demoCallWithoutParam");
System.out.println(tofu.newRenderer(DEMO_CALL_WITHOUT_PARAM).setData(new SoyMapData(DemoCallWithoutParamSoyTemplateInfo.NAME, "Neo", DemoCallWithoutParamSoyTemplateInfo.TRIP_INFO, new SoyMapData("name", "Neo", "destination", "The Matrix"))).setMsgBundle(msgBundle).render());
writeExampleHeader("demoCallWithParam");
System.out.println(tofu.newRenderer(DEMO_CALL_WITH_PARAM).setData(ImmutableMap.of(DemoCallWithParamSoyTemplateInfo.NAME, "Oz", DemoCallWithParamSoyTemplateInfo.COMPANION_NAME, "Pip", DemoCallWithParamSoyTemplateInfo.DESTINATIONS, ImmutableList.of("Gillikin Country", "Munchkin Country", "Quadling Country", "Winkie Country"))).setMsgBundle(msgBundle).render());
writeExampleHeader("demoCallWithParamBlock");
System.out.println(tofu.newRenderer(DEMO_CALL_WITH_PARAM_BLOCK).setData(new SoyMapData(DemoCallWithParamBlockSoyTemplateInfo.NAME, "Quo")).setMsgBundle(msgBundle).render());
writeExampleHeader("demoExpressions");
SoyListData students = new SoyListData();
students.add(new SoyMapData("name", "Rob", "major", "Physics", "year", 1999));
students.add(new SoyMapData("name", "Sha", "major", "Finance", "year", 1980));
students.add(new SoyMapData("name", "Tim", "major", "Engineering", "year", 2005));
students.add(new SoyMapData("name", "Uma", "major", "Biology", "year", 1972));
System.out.println(tofu.newRenderer(DEMO_EXPRESSIONS).setData(new SoyMapData(DemoExpressionsSoyTemplateInfo.STUDENTS, students, DemoExpressionsSoyTemplateInfo.CURRENT_YEAR, 2008)).setMsgBundle(msgBundle).render());
writeExampleHeader("demoDoubleBraces");
System.out.println(tofu.newRenderer(DEMO_DOUBLE_BRACES).setData(ImmutableMap.of(DemoDoubleBracesSoyTemplateInfo.SET_NAME, "prime numbers", DemoDoubleBracesSoyTemplateInfo.SET_MEMBERS, ImmutableList.of(2, 3, 5, 7, 11, 13))).setMsgBundle(msgBundle).setContentKind(SanitizedContent.ContentKind.TEXT).render());
// The Hebrew in the following example comes out as question marks in the output because
// System.out (and by default stdout generally) is set up to use a Latin encoding. To see
// this really in action, run the Javascript example.
writeExampleHeader("demoBidiSupport");
System.out.println(tofu.newRenderer(DEMO_BIDI_SUPPORT).setData(ImmutableMap.of(DemoBidiSupportSoyTemplateInfo.TITLE, "2008: A BiDi Odyssey", DemoBidiSupportSoyTemplateInfo.AUTHOR, "John Doe, Esq.", DemoBidiSupportSoyTemplateInfo.YEAR, "1973", DemoBidiSupportSoyTemplateInfo.KEYWORDS, ImmutableList.of("Bi(Di)", "2008 (\u05E9\u05E0\u05D4)", "2008 (year)"))).setMsgBundle(msgBundle).render());
}
Aggregations