use of com.google.template.soy.basetree.CopyState in project closure-templates by google.
the class AbstractOperatorNodeTest method testToSourceString2.
@Test
public void testToSourceString2() {
// Test expression: not $x ? $x != $x : $x * $x
//
// The expression tree looks like this:
// [ConditionalOpNode] n0
// [NotOpNode] n1
// [VarRefNode] $x
// [NotEqualOpNode] n2
// [VarRefNode] $x
// [VarRefNode] $x
// [TimesOpNode] n3
// [VarRefNode] $x
// [VarRefNode] $x
// Root n0.
ConditionalOpNode n0 = new ConditionalOpNode(X);
// Children of n0.
NotOpNode n1 = new NotOpNode(X);
NotEqualOpNode n2 = new NotEqualOpNode(X);
TimesOpNode n3 = new TimesOpNode(X);
n0.addChild(n1);
n0.addChild(n2);
n0.addChild(n3);
// Child of n1.
n1.addChild(x);
// Children of n2.
n2.addChild(x.copy(new CopyState()));
n2.addChild(x.copy(new CopyState()));
// Children of n3.
n3.addChild(x.copy(new CopyState()));
n3.addChild(x.copy(new CopyState()));
assertThat(n0.toSourceString()).isEqualTo("not $x ? $x != $x : $x * $x");
}
use of com.google.template.soy.basetree.CopyState in project closure-templates by google.
the class SoyTreeUtilsTest method testClone.
@Test
public final void testClone() throws Exception {
SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(SOY_SOURCE_FOR_TESTING_CLONING).declaredSyntaxVersion(SyntaxVersion.V2_4).parse().fileSet();
SoyFileSetNode clone = soyTree.copy(new CopyState());
assertEquals(1, clone.numChildren());
assertEquals(clone.getChild(0).toSourceString(), soyTree.getChild(0).toSourceString());
// All the localvarnodes, there is one of each type
ForNonemptyNode forNonemptyNode = Iterables.getOnlyElement(SoyTreeUtils.getAllNodesOfType(clone, ForNonemptyNode.class));
LetValueNode letValueNode = Iterables.getOnlyElement(SoyTreeUtils.getAllNodesOfType(clone, LetValueNode.class));
for (VarRefNode varRef : SoyTreeUtils.getAllNodesOfType(clone, VarRefNode.class)) {
VarDefn defn = varRef.getDefnDecl();
LocalVar local;
switch(varRef.getName()) {
case "local":
local = (LocalVar) defn;
assertSame(letValueNode, local.declaringNode());
assertSame(letValueNode.getVar(), local);
break;
case "item":
local = (LocalVar) defn;
assertSame(forNonemptyNode, local.declaringNode());
assertSame(forNonemptyNode.getVar(), defn);
break;
default:
}
}
}
use of com.google.template.soy.basetree.CopyState in project closure-templates by google.
the class VeLogNodeTest method testClonePreservesId.
@Test
public void testClonePreservesId() {
VeLogNode logNode = parseVeLog("{velog Bar}<div></div>{/velog}");
assertThat(logNode.toSourceString()).isEqualTo("{velog Bar}<div></div>{/velog}");
assertThat(logNode.getName().identifier()).isEqualTo("Bar");
assertThat(logNode.getLoggingId()).isEqualTo(1);
VeLogNode copy = logNode.copy(new CopyState());
assertThat(copy.toSourceString()).isEqualTo("{velog Bar}<div></div>{/velog}");
assertThat(copy.getName().identifier()).isEqualTo("Bar");
assertThat(copy.getLoggingId()).isEqualTo(1);
}
use of com.google.template.soy.basetree.CopyState in project closure-templates by google.
the class SoyFileSet method compileToJsSrcFiles.
/**
* Compiles this Soy file set into JS source code files and writes these JS files to disk.
*
* @param outputPathFormat The format string defining how to build the output file path
* corresponding to an input file path.
* @param inputFilePathPrefix The prefix prepended to all input file paths (can be empty string).
* @param jsSrcOptions The compilation options for the JS Src output target.
* @param locales The list of locales. Can be an empty list if not applicable.
* @param msgPlugin The {@link SoyMsgPlugin} to use, or null if not applicable
* @param messageFilePathFormat The message file path format, or null if not applicable.
* @throws SoyCompilationException If compilation fails.
* @throws IOException If there is an error in opening/reading a message file or opening/writing
* an output JS file.
*/
@SuppressWarnings("deprecation")
void compileToJsSrcFiles(String outputPathFormat, String inputFilePathPrefix, SoyJsSrcOptions jsSrcOptions, List<String> locales, @Nullable SoyMsgPlugin msgPlugin, @Nullable String messageFilePathFormat) throws IOException {
ParseResult result = preprocessJsSrcResults(jsSrcOptions);
SoyFileSetNode soyTree = result.fileSet();
TemplateRegistry registry = result.registry();
if (locales.isEmpty()) {
// Not generating localized JS.
new JsSrcMain(apiCallScopeProvider, typeRegistry).genJsFiles(soyTree, registry, jsSrcOptions, null, null, outputPathFormat, inputFilePathPrefix, errorReporter);
} else {
checkArgument(msgPlugin != null, "a message plugin must be provided when generating localized sources");
checkArgument(messageFilePathFormat != null, "a messageFilePathFormat must be provided when generating localized sources");
// Generating localized JS.
for (String locale : locales) {
SoyFileSetNode soyTreeClone = soyTree.copy(new CopyState());
String msgFilePath = MainEntryPointUtils.buildFilePath(messageFilePathFormat, locale, null, inputFilePathPrefix);
SoyMsgBundle msgBundle = new SoyMsgBundleHandler(msgPlugin).createFromFile(new File(msgFilePath));
if (msgBundle.getLocaleString() == null) {
// begins with "en", because falling back to the Soy source will probably be fine.
if (!locale.startsWith("en")) {
throw new IOException("Error opening or reading message file " + msgFilePath);
}
}
new JsSrcMain(apiCallScopeProvider, typeRegistry).genJsFiles(soyTreeClone, registry, jsSrcOptions, locale, msgBundle, outputPathFormat, inputFilePathPrefix, errorReporter);
}
}
throwIfErrorsPresent();
reportWarnings();
}
Aggregations