compiler-jx: tests for source maps in MXML Script blocks
Project: http://git-wip-us.apache.org/repos/asf/flex-falcon/repo Commit: http://git-wip-us.apache.org/repos/asf/flex-falcon/commit/f39511d2 Tree: http://git-wip-us.apache.org/repos/asf/flex-falcon/tree/f39511d2 Diff: http://git-wip-us.apache.org/repos/asf/flex-falcon/diff/f39511d2 Branch: refs/heads/master Commit: f39511d2caf89114e0ceb7b0df1de14bf1978092 Parents: 3ab4711 Author: Josh Tynjala <[email protected]> Authored: Tue Mar 21 16:06:53 2017 -0700 Committer: Josh Tynjala <[email protected]> Committed: Tue Mar 21 16:07:16 2017 -0700 ---------------------------------------------------------------------- .../sourcemaps/TestSourceMapMXMLScript.java | 77 ++++++++++++++++++++ .../internal/test/FlexJSSourceMapTestBase.java | 64 ++++++++++++++++ .../compiler/internal/test/FlexJSTestBase.java | 33 +++++++++ 3 files changed, 174 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/f39511d2/compiler-jx/src/test/java/org/apache/flex/compiler/internal/codegen/mxml/sourcemaps/TestSourceMapMXMLScript.java ---------------------------------------------------------------------- diff --git a/compiler-jx/src/test/java/org/apache/flex/compiler/internal/codegen/mxml/sourcemaps/TestSourceMapMXMLScript.java b/compiler-jx/src/test/java/org/apache/flex/compiler/internal/codegen/mxml/sourcemaps/TestSourceMapMXMLScript.java new file mode 100644 index 0000000..591e6e5 --- /dev/null +++ b/compiler-jx/src/test/java/org/apache/flex/compiler/internal/codegen/mxml/sourcemaps/TestSourceMapMXMLScript.java @@ -0,0 +1,77 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.flex.compiler.internal.codegen.mxml.sourcemaps; + +import org.apache.flex.compiler.definitions.IClassDefinition; +import org.apache.flex.compiler.definitions.IDefinition; +import org.apache.flex.compiler.internal.codegen.js.flexjs.JSFlexJSEmitter; +import org.apache.flex.compiler.internal.test.FlexJSSourceMapTestBase; +import org.apache.flex.compiler.internal.test.TestBase; +import org.apache.flex.compiler.tree.as.IASNode; +import org.apache.flex.compiler.tree.as.IFunctionNode; +import org.apache.flex.compiler.tree.as.IVariableNode; +import org.apache.flex.compiler.tree.mxml.IMXMLDocumentNode; +import org.apache.flex.compiler.tree.mxml.IMXMLScriptNode; + +import org.junit.Test; +import static org.junit.Assert.assertTrue; + +public class TestSourceMapMXMLScript extends FlexJSSourceMapTestBase +{ + @Test + public void testField() + { + String code = "var foo;"; + + IVariableNode node = (IVariableNode) getASNode(code, IVariableNode.class); + IMXMLDocumentNode dnode = (IMXMLDocumentNode) node + .getAncestorOfType(IMXMLDocumentNode.class); + IClassDefinition definition = dnode.getClassDefinition(); + ((JSFlexJSEmitter)(mxmlBlockWalker.getASEmitter())).getModel().setCurrentClass(definition); + mxmlBlockWalker.visitDocument(dnode); + String definitionName = definition.getQualifiedName(); + assertTrue(definitionName.startsWith(getClass().getSimpleName())); + int endColumn = definitionName.length() + 14; + ///**\n * @export\n * @type {*}\n */\nFalconTest_A.prototype.foo + assertMapping(node, 0, 4, 42, 0, 42, endColumn); // foo + } + + @Test + public void testMethod() + { + String code = "function foo(){};"; + + IFunctionNode node = (IFunctionNode) getASNode(code, IFunctionNode.class); + IMXMLDocumentNode dnode = (IMXMLDocumentNode) node + .getAncestorOfType(IMXMLDocumentNode.class); + IClassDefinition definition = dnode.getClassDefinition(); + ((JSFlexJSEmitter)(mxmlBlockWalker.getASEmitter())).getModel().setCurrentClass(definition); + mxmlBlockWalker.visitDocument(dnode); + String definitionName = definition.getQualifiedName(); + assertTrue(definitionName.startsWith(getClass().getSimpleName())); + int nameEndColumn = definitionName.length() + 14; + ///**\n * @export\n * @type {*}\n */\nFalconTest_A.prototype.foo + assertMapping(node, 0, 9, 38, 0, 38, nameEndColumn); // foo + assertMapping(node, 0, 0, 38, nameEndColumn, 38, nameEndColumn + 11); // = function + assertMapping(node, 0, 12, 38, nameEndColumn + 11, 38, nameEndColumn + 12); // ( + assertMapping(node, 0, 13, 38, nameEndColumn + 12, 38, nameEndColumn + 13); // ) + assertMapping(node, 0, 14, 38, nameEndColumn + 14, 38, nameEndColumn + 15); // { + assertMapping(node, 0, 15, 39, 0, 39, 1); // } + } +} http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/f39511d2/compiler-jx/src/test/java/org/apache/flex/compiler/internal/test/FlexJSSourceMapTestBase.java ---------------------------------------------------------------------- diff --git a/compiler-jx/src/test/java/org/apache/flex/compiler/internal/test/FlexJSSourceMapTestBase.java b/compiler-jx/src/test/java/org/apache/flex/compiler/internal/test/FlexJSSourceMapTestBase.java new file mode 100644 index 0000000..9be5b2a --- /dev/null +++ b/compiler-jx/src/test/java/org/apache/flex/compiler/internal/test/FlexJSSourceMapTestBase.java @@ -0,0 +1,64 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.flex.compiler.internal.test; + +import java.util.List; + +import org.apache.flex.compiler.codegen.js.IMappingEmitter; +import org.apache.flex.compiler.tree.as.IASNode; +import org.apache.flex.compiler.tree.mxml.IMXMLNode; + +import com.google.debugging.sourcemap.FilePosition; +import static org.junit.Assert.assertTrue; + +public class FlexJSSourceMapTestBase extends FlexJSTestBase +{ + protected void assertMapping(IASNode node, int nodeStartLine, int nodeStartColumn, + int outStartLine, int outStartColumn, int outEndLine, int outEndColumn) + { + int sourceStartLine = nodeStartLine + node.getLine(); + int sourceStartColumn = nodeStartColumn; + if (nodeStartLine == 0) + { + sourceStartColumn += node.getColumn(); + } + boolean foundMapping = false; + IMappingEmitter emitter = (IMappingEmitter) mxmlEmitter; + List<IMappingEmitter.SourceMapMapping> mappings = emitter.getSourceMapMappings(); + for (IMappingEmitter.SourceMapMapping mapping : mappings) + { + FilePosition sourcePosition = mapping.sourceStartPosition; + FilePosition startPosition = mapping.destStartPosition; + FilePosition endPosition = mapping.destEndPosition; + if (sourcePosition.getLine() == sourceStartLine + && sourcePosition.getColumn() == sourceStartColumn + && startPosition.getLine() == outStartLine + && startPosition.getColumn() == outStartColumn + && endPosition.getLine() == outEndLine + && endPosition.getColumn() == outEndColumn) + { + foundMapping = true; + break; + } + } + assertTrue("Mapping not found for node " + node.getNodeID() + ". Expected " + + "source: (" + nodeStartLine + ", " + nodeStartColumn + "), dest: (" + outStartLine + ", " + outStartColumn + ") to (" + outEndLine + ", " + outEndColumn + ")", + foundMapping); + } +} http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/f39511d2/compiler-jx/src/test/java/org/apache/flex/compiler/internal/test/FlexJSTestBase.java ---------------------------------------------------------------------- diff --git a/compiler-jx/src/test/java/org/apache/flex/compiler/internal/test/FlexJSTestBase.java b/compiler-jx/src/test/java/org/apache/flex/compiler/internal/test/FlexJSTestBase.java index c3f379c..30fcebc 100644 --- a/compiler-jx/src/test/java/org/apache/flex/compiler/internal/test/FlexJSTestBase.java +++ b/compiler-jx/src/test/java/org/apache/flex/compiler/internal/test/FlexJSTestBase.java @@ -26,8 +26,10 @@ import org.apache.flex.compiler.internal.driver.mxml.flexjs.MXMLFlexJSBackend; import org.apache.flex.compiler.internal.mxml.MXMLNamespaceMapping; import org.apache.flex.compiler.internal.projects.FlexJSProject; import org.apache.flex.compiler.mxml.IMXMLNamespaceMapping; +import org.apache.flex.compiler.tree.as.IASNode; import org.apache.flex.compiler.tree.mxml.IMXMLFileNode; import org.apache.flex.compiler.tree.mxml.IMXMLNode; +import org.apache.flex.compiler.tree.mxml.IMXMLScriptNode; import org.apache.flex.utils.FilenameNormalization; import org.apache.flex.utils.ITestAdapter; import org.apache.flex.utils.TestAdapterFactory; @@ -47,6 +49,7 @@ public class FlexJSTestBase extends TestBase asEmitter = backend.createEmitter(writer); mxmlEmitter = backend.createMXMLEmitter(writer); + asEmitter.setParentEmitter(mxmlEmitter); asBlockWalker = backend.createWalker(project, errors, asEmitter); mxmlBlockWalker = backend.createMXMLWalker(project, errors, @@ -105,19 +108,49 @@ public class FlexJSTestBase extends TestBase public static final int WRAP_LEVEL_NONE = 0; public static final int WRAP_LEVEL_DOCUMENT = 1; + protected IASNode getASNode(String code, Class<? extends IASNode> type) + { + code = "" + + "<basic:Application xmlns:fx=\"http://ns.adobe.com/mxml/2009\" xmlns:basic=\"library://ns.apache.org/flexjs/basic\"><fx:Script><![CDATA[" + + code + "]]></fx:Script></basic:Application>"; + + IMXMLFileNode node = compileMXML(code); + + return findFirstASDescendantOfType(node, type); + } + protected IMXMLNode getNode(String code, Class<? extends IMXMLNode> type, int wrapLevel) { if (wrapLevel >= WRAP_LEVEL_DOCUMENT) + { code = "" + "<basic:Application xmlns:fx=\"http://ns.adobe.com/mxml/2009\" xmlns:basic=\"library://ns.apache.org/flexjs/basic\">" + code + "</basic:Application>"; + } IMXMLFileNode node = compileMXML(code); return findFirstDescendantOfType(node, type); } + protected IASNode findFirstASDescendantOfType(IMXMLNode node, + Class<? extends IASNode> nodeType) + { + IMXMLScriptNode scriptNode = (IMXMLScriptNode) findFirstDescendantOfType(node, IMXMLScriptNode.class); + if (scriptNode != null) + { + for (IASNode asNode : scriptNode.getASNodes()) + { + if (nodeType.isInstance(asNode)) + { + return asNode; + } + } + } + return null; + } + protected IMXMLNode findFirstDescendantOfType(IMXMLNode node, Class<? extends IMXMLNode> nodeType) {
