Search in sources :

Example 6 with CopyState

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");
}
Also used : ConditionalOpNode(com.google.template.soy.exprtree.OperatorNodes.ConditionalOpNode) TimesOpNode(com.google.template.soy.exprtree.OperatorNodes.TimesOpNode) NotOpNode(com.google.template.soy.exprtree.OperatorNodes.NotOpNode) CopyState(com.google.template.soy.basetree.CopyState) NotEqualOpNode(com.google.template.soy.exprtree.OperatorNodes.NotEqualOpNode) Test(org.junit.Test)

Example 7 with CopyState

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:
        }
    }
}
Also used : VarRefNode(com.google.template.soy.exprtree.VarRefNode) VarDefn(com.google.template.soy.exprtree.VarDefn) LocalVar(com.google.template.soy.soytree.defn.LocalVar) CopyState(com.google.template.soy.basetree.CopyState) Test(org.junit.Test)

Example 8 with CopyState

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);
}
Also used : CopyState(com.google.template.soy.basetree.CopyState) Test(org.junit.Test)

Example 9 with CopyState

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();
}
Also used : JsSrcMain(com.google.template.soy.jssrc.internal.JsSrcMain) TemplateRegistry(com.google.template.soy.soytree.TemplateRegistry) ParseResult(com.google.template.soy.SoyFileSetParser.ParseResult) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) CopyState(com.google.template.soy.basetree.CopyState) IOException(java.io.IOException) SoyMsgBundle(com.google.template.soy.msgs.SoyMsgBundle) File(java.io.File) SoyMsgBundleHandler(com.google.template.soy.msgs.SoyMsgBundleHandler)

Aggregations

CopyState (com.google.template.soy.basetree.CopyState)9 Test (org.junit.Test)5 SoyFileNode (com.google.template.soy.soytree.SoyFileNode)2 ParseResult (com.google.template.soy.SoyFileSetParser.ParseResult)1 SourceLocation (com.google.template.soy.base.SourceLocation)1 IncrementingIdGenerator (com.google.template.soy.base.internal.IncrementingIdGenerator)1 FunctionNode (com.google.template.soy.exprtree.FunctionNode)1 IntegerNode (com.google.template.soy.exprtree.IntegerNode)1 NullNode (com.google.template.soy.exprtree.NullNode)1 ConditionalOpNode (com.google.template.soy.exprtree.OperatorNodes.ConditionalOpNode)1 MinusOpNode (com.google.template.soy.exprtree.OperatorNodes.MinusOpNode)1 NegativeOpNode (com.google.template.soy.exprtree.OperatorNodes.NegativeOpNode)1 NotEqualOpNode (com.google.template.soy.exprtree.OperatorNodes.NotEqualOpNode)1 NotOpNode (com.google.template.soy.exprtree.OperatorNodes.NotOpNode)1 TimesOpNode (com.google.template.soy.exprtree.OperatorNodes.TimesOpNode)1 VarDefn (com.google.template.soy.exprtree.VarDefn)1 VarRefNode (com.google.template.soy.exprtree.VarRefNode)1 JsSrcMain (com.google.template.soy.jssrc.internal.JsSrcMain)1 SoyMsgBundle (com.google.template.soy.msgs.SoyMsgBundle)1 SoyMsgBundleHandler (com.google.template.soy.msgs.SoyMsgBundleHandler)1