use of com.google.template.soy.SoyFileSetParser.ParseResult 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.SoyFileSetParser.ParseResult in project closure-templates by google.
the class GenJsCodeVisitorTest method testOnlyOneRequireStatementPerPluginNamespace.
@Test
public void testOnlyOneRequireStatementPerPluginNamespace() {
String testFileContent = "{namespace boo.foo}\n" + "\n" + "/** Test template. */\n" + "{template .goo autoescape=\"deprecated-noncontextual\"}\n" + " {call for.function.aaa data=\"all\" /}\n" + " {noopRequire()}\n" + "{/template}\n";
ParseResult parseResult = SoyFileSetParserBuilder.forFileContents(testFileContent).addSoyFunction(NOOP_REQUIRE_SOY_FUNCTION).parse();
// ------ Using Closure, provide/require Soy namespaces and required JS for function ------
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('also.for.function');\n" + "goog.require('for.function');\n" + "\n";
jsSrcOptions.setShouldProvideRequireSoyNamespaces(true);
List<String> jsFilesContents = genJsCodeVisitor.gen(parseResult.fileSet(), parseResult.registry(), ErrorReporter.exploding());
assertThat(jsFilesContents.get(0)).startsWith(expectedJsFileContentStart);
}
use of com.google.template.soy.SoyFileSetParser.ParseResult in project closure-templates by google.
the class GenJsCodeVisitorTest method testSoyFile.
@Test
public void testSoyFile() {
String testFileContent = "{namespace boo.foo}\n" + "\n" + "/** Test template. */\n" + "{template .goo autoescape=\"deprecated-noncontextual\"}\n" + " {call .goo data=\"all\" /}\n" + // not defined in this file
" {call boo.woo.hoo data=\"all\" /}\n" + "{/template}\n";
ParseResult parseResult = SoyFileSetParserBuilder.forFileContents(testFileContent).parse();
// ------ Using Closure, provide/require Soy namespaces ------
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('boo.woo');\n" + "\n";
jsSrcOptions.setShouldProvideRequireSoyNamespaces(true);
List<String> jsFilesContents = genJsCodeVisitor.gen(parseResult.fileSet(), parseResult.registry(), ErrorReporter.exploding());
assertThat(jsFilesContents.get(0)).startsWith(expectedJsFileContentStart);
// ------ Using Closure, provide/require JS functions ------
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.goo');\n" + "\n" + "goog.require('boo.woo.hoo');\n" + "\n";
jsSrcOptions.setShouldProvideRequireSoyNamespaces(false);
jsSrcOptions.setShouldProvideRequireJsFunctions(true);
jsFilesContents = genJsCodeVisitor.gen(parseResult.fileSet(), parseResult.registry(), ErrorReporter.exploding());
assertThat(jsFilesContents.get(0)).startsWith(expectedJsFileContentStart);
// ------ Using Closure, provide both Soy namespaces and JS functions ------
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" + "goog.provide('boo.foo.goo');\n" + "\n" + "goog.require('boo.woo');\n" + "\n";
jsSrcOptions.setShouldProvideRequireJsFunctions(false);
jsSrcOptions.setShouldProvideRequireSoyNamespaces(true);
jsSrcOptions.setShouldProvideBothSoyNamespacesAndJsFunctions(true);
jsFilesContents = genJsCodeVisitor.gen(parseResult.fileSet(), parseResult.registry(), ErrorReporter.exploding());
assertThat(jsFilesContents.get(0)).startsWith(expectedJsFileContentStart);
}
use of com.google.template.soy.SoyFileSetParser.ParseResult in project closure-templates by google.
the class GenJsCodeVisitorTest method testStrictLetAddsAppropriateRequires.
// Regression test for a bug where the logic for ordaining strict letcontent blocks failed to
// propagate the necessary requires for the ordainer functions.
@Test
public void testStrictLetAddsAppropriateRequires() {
jsSrcOptions.setShouldProvideRequireSoyNamespaces(true);
String soyNodeCode = "{let $text kind=\"text\"}foo{/let}{let $html kind=\"html\"}foo{/let}\n";
ParseResult parseResult = SoyFileSetParserBuilder.forTemplateContents(AutoEscapingType.STRICT, soyNodeCode).parse();
String jsFilesContents = genJsCodeVisitor.gen(parseResult.fileSet(), parseResult.registry(), ErrorReporter.exploding()).get(0);
assertThat(jsFilesContents).contains("goog.require('soydata')");
assertThat(jsFilesContents).contains("goog.require('soydata.VERY_UNSAFE')");
}
use of com.google.template.soy.SoyFileSetParser.ParseResult in project closure-templates by google.
the class GenJsCodeVisitorTest method testGoogModuleGeneration.
@Test
public void testGoogModuleGeneration() {
jsSrcOptions.setShouldGenerateGoogModules(true);
String testFileContent = "" + "{namespace boo.foo}\n" + "\n" + "/** Test template. */\n" + "{template .goo}\n" + " {call boo.bar.one /}\n" + " {call boo.bar.two /}\n" + "{/template}\n";
String expectedJsCode = "" + "// 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.module('boo.foo');\n" + "\n" + "goog.require('soydata.VERY_UNSAFE');\n" + "var $import1 = goog.require('boo.bar');\n" + "var $templateAlias1 = $import1.one;\n" + "var $templateAlias2 = $import1.two;\n" + "\n" + "\n" + "/**\n" + " * @param {Object<string, *>=} opt_data\n" + " * @param {Object<string, *>=} opt_ijData\n" + " * @param {Object<string, *>=} opt_ijData_deprecated\n" + " * @return {!goog.soy.data.SanitizedHtml}\n" + " * @suppress {checkTypes|uselessCode}\n" + " */\n" + "var $goo = function(opt_data, opt_ijData, opt_ijData_deprecated) {\n" + " opt_ijData = opt_ijData_deprecated || opt_ijData;\n" + " return soydata.VERY_UNSAFE.ordainSanitizedHtml(" + "$templateAlias1(null, opt_ijData) + " + "$templateAlias2(null, opt_ijData));\n" + "};\n" + "exports.goo = $goo;\n" + "if (goog.DEBUG) {\n" + " $goo.soyTemplateName = 'boo.foo.goo';\n" + "}\n";
ParseResult parseResult = SoyFileSetParserBuilder.forFileContents(testFileContent).parse();
assertThat(genJsCodeVisitor.gen(parseResult.fileSet(), parseResult.registry(), ErrorReporter.exploding()).get(0)).isEqualTo(expectedJsCode);
}
Aggregations