Skip to content

Commit

Permalink
#41 Add a wrapper for the case when the select matches attributes.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexJitianu committed Jun 11, 2020
1 parent 3396c12 commit cfe20a5
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 3 deletions.
12 changes: 9 additions & 3 deletions frameworks/xspec/oxygen-results-view/unit-report-oxygen.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,17 @@
<xsl:when test="@select and node()">
<!-- Applies the XPath filter to get the subset of interest. -->
<xsl:variable name="filtered">
<xsl:variable name="expr" select="if (normalize-space(@select) = '/') then concat('.', @select, 'node()') else concat('.', @select)"/>
<xsl:evaluate xpath="$expr" context-item="."/>
<!-- #41 Put a wrapper in case the xpath returns attributes. -->
<wrapper>
<xsl:variable name="expr" select="if (normalize-space(@select) = '/') then concat('.', @select, 'node()') else concat('.', @select)"/>
<xsl:evaluate xpath="$expr" context-item="."></xsl:evaluate>
</wrapper>
</xsl:variable>
<!-- #41 Check if the wrapper received any attributes. If it hasn't, process its contents. -->
<xsl:variable name="toProcess" select="if (count($filtered/*/@*) > 0) then $filtered else $filtered/*/node()"/>

<!-- Convert to a version ready to be serialized. -->
<xsl:apply-templates select="$filtered" mode="copy">
<xsl:apply-templates select="$toProcess" mode="copy">
<xsl:with-param name="level" select="0"/>
</xsl:apply-templates>
</xsl:when>
Expand Down
79 changes: 79 additions & 0 deletions src/test/java/com/oxygenxml/xspec/jfx/XSpecMeta2Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.oxygenxml.xspec.jfx;

import java.io.File;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.oxygenxml.xspec.XSpecUtil;

import ro.sync.util.URLUtil;

/**
* Asserts the XSpec transformation meta data and the special report.
*
* @author alex_jitianu
*/
public class XSpecMeta2Test extends XSpecViewTestBase {

/**
* An assert with match on attributes.
*
* https://github.com/xspec/oXygen-XML-editor-xspec-support/issues/41
*
* @throws Exception If it fails.
*/
public void testRunXSpec() throws Exception {
URL xspecURL = getClass().getClassLoader().getResource("#41/to-test.xspec");
URL xslURL = getClass().getClassLoader().getResource("#41/to-test.xsl");
File xspecFile = URLUtil.getCanonicalFileFromFileUrl(xspecURL);
File outputFile = new File(xspecFile.getParentFile(), "to-test.html");

executeANT(xspecFile, outputFile);

String firstID = XSpecUtil.generateId("template name code-to-code");

File xmlFormatOutput = new File(xspecFile.getParentFile(), "xspec/to-test-result.xml");

assertEquals("<x:result xmlns:test=\"http://www.jenitennison.com/xslt/unit-test\"\n" +
" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\"\n" +
" xmlns:x=\"http://www.jenitennison.com/xslt/xspec\"\n" +
" xmlns:f=\"uri:some\"\n" +
" select=\"/*/@*\">\n" +
" <pseudo-attribute xmlns=\"http://www.jenitennison.com/xslt/xspec\" code=\"F\"/>\n" +
" <pseudo-attribute xmlns=\"http://www.jenitennison.com/xslt/xspec\" codeSystem=\"null\"/>\n" +
" <pseudo-attribute xmlns=\"http://www.jenitennison.com/xslt/xspec\" displayName=\"Vrouw\"/>\n" +
" </x:result>", executeXPath(xmlFormatOutput, "//*:result"));

assertEquals("<x:expect xmlns:test=\"http://www.jenitennison.com/xslt/unit-test\"\n" +
" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\"\n" +
" xmlns:x=\"http://www.jenitennison.com/xslt/xspec\"\n" +
" xmlns:f=\"uri:some\"\n" +
" select=\"/*/@*\">\n" +
" <pseudo-attribute xmlns=\"http://www.jenitennison.com/xslt/xspec\" code=\"false\"/>\n" +
" <pseudo-attribute xmlns=\"http://www.jenitennison.com/xslt/xspec\"\n" +
" codeSystem=\"2.16.840.1.113883.5.1\"/>\n" +
" <pseudo-attribute xmlns=\"http://www.jenitennison.com/xslt/xspec\" displayName=\"Vrouw\"/>\n" +
" </x:expect>", executeXPath(xmlFormatOutput, "//*:expect"));



assertTrue(outputFile.exists());

File css = new File("frameworks/xspec/oxygen-results-view/test-report.css");
File js = new File("frameworks/xspec/oxygen-results-view/test-report.js");


String htmlContent = simplify(read(outputFile.toURI().toURL()).toString());
Pattern compile = Pattern.compile("<pre class=\"embeded\\.diff\\.data\" style=\"display:none;\">.*</pre>");
Matcher matcher = compile.matcher(htmlContent);
matcher.find();
String actual = matcher.group();

assertEquals("<pre class=\"embeded.diff.data\" style=\"display:none;\"><div class=\"embeded.diff.result\" style=\"white-space:pre;\">&lt;wrapper code=\"F\" codeSystem=\"null\" displayName=\"Vrouw\"/&gt;</div><div class=\"embeded.diff.expected\" style=\"white-space:pre;\">&lt;wrapper code=\"false\" codeSystem=\"2.16.840.1.113883.5.1\" displayName=\"Vrouw\"/&gt;</div></pre>", actual);
}

private String simplify(String string) {
return string.replaceAll("<span xmlns=\"http://www.w3.org/1999/xhtml\"", "<span");
}
}
25 changes: 25 additions & 0 deletions src/test/java/com/oxygenxml/xspec/jfx/XSpecViewTestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
import javafx.concurrent.Worker.State;
import javafx.scene.web.WebEngine;
import junit.extensions.jfcunit.JFCTestCase;
import net.sf.saxon.s9api.Processor;
import net.sf.saxon.s9api.XPathCompiler;
import net.sf.saxon.s9api.XPathExecutable;
import net.sf.saxon.s9api.XdmItem;
import net.sf.saxon.s9api.XdmNode;
import ro.sync.exml.workspace.api.editor.WSEditor;
import ro.sync.exml.workspace.api.editor.documenttype.DocumentTypeInformation;
import ro.sync.exml.workspace.api.standalone.StandalonePluginWorkspace;
Expand Down Expand Up @@ -422,5 +427,25 @@ protected String filterAll(String content) {
.replaceAll("<\\?xml-stylesheet.*\\?>", "")
.replaceAll("x:test id=\"[^\"]*\"", "x:test");
}

/**
* Executes XPath.
*
* @param resource Input.
* @param xpath XPath expression.
*
* @return Result.
*
* @throws Exception if it fails.
*/
protected String executeXPath(File resource, String xpath) throws Exception {
Processor processor = new Processor(false);
XdmNode build = processor.newDocumentBuilder().build(resource);
XPathCompiler newXPathCompiler = processor.newXPathCompiler();

XdmItem evaluateSingle = newXPathCompiler.evaluateSingle(xpath, build);

return evaluateSingle.toString();
}

}
27 changes: 27 additions & 0 deletions src/test/resources/#41/to-test.xsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:template name="code-to-code" as="attribute()+">
<xsl:param name="value" as="attribute(value)" select="."/>
<xsl:param name="codeMap" as="element()*"/>

<xsl:variable name="out" as="element()">
<xsl:choose>
<xsl:when test="$codeMap[@inValue = $value]">
<xsl:copy-of select="$codeMap[@inValue = $value]"/>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>

<xsl:attribute name="code" select="$out/@code"/>
<xsl:attribute name="codeSystem" select="$out/@codeSystem"/>
<xsl:if test="$out/@displayName">
<xsl:attribute name="displayName" select="$out/@displayName"/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
20 changes: 20 additions & 0 deletions src/test/resources/#41/to-test.xspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<x:description xmlns:x="http://www.jenitennison.com/xslt/xspec"
xmlns:f="uri:some"
stylesheet="to-test.xsl">

<x:scenario label="template name code-to-code">
<x:call template="code-to-code">
<x:param name="value" select="f:gender/@value">
<f:gender value="female"/>
</x:param>
<x:param name="codeMap">
<map code="F" codeSystem="null" inValue="female" displayName="Vrouw"/>
</x:param>
</x:call>
<x:expect label="attributes" select="geslacht/@*">
<geslacht code="false" codeSystem="2.16.840.1.113883.5.1" displayName="Vrouw"/>
</x:expect>
</x:scenario>

</x:description>

0 comments on commit cfe20a5

Please sign in to comment.