Commits

Shlomi Fish committed 626bd6b

Test the MultiXSLT parameterized role functionality.

  • Participants
  • Parent commits 6bde41b
  • Branches make_Role_XSLT_parameterized

Comments (0)

Files changed (4)

File XML-GrammarBase/MANIFEST

 Build.PL
 Changes
+MANIFEST
+META.json
+META.yml
+Makefile.PL
+README
+TODO.pod
 inc/Test/Run/Builder.pm
 lib/XML/GrammarBase.pm
 lib/XML/GrammarBase/Role/DataDir.pm
 lib/XML/GrammarBase/Role/RelaxNG.pm
 lib/XML/GrammarBase/Role/XSLT.pm
 lib/XML/GrammarBase/Role/XSLT/Global.pm
-Makefile.PL
-MANIFEST
-META.yml
-README
 t/00-load.t
+t/data/fiction-xml-invalid-test.xml
+t/data/fiction-xml-test-docbook-xslt-output.docbook.xml
+t/data/fiction-xml-test-html-xslt-output.xhtml
+t/data/fiction-xml-test.xml
+t/data/fiction-xml-to-docbook.xslt
+t/data/fiction-xml-to-html.xslt
+t/data/fiction-xml.rng
 t/data/screenplay-xml/xml/with-tags-inside-paragraphs.xml
-t/data/fiction-xml.rng
-t/data/fiction-xml-invalid-test.xml
-t/data/fiction-xml-test.xml
-t/data/fiction-xml-test-html-xslt-output.xhtml
-t/data/fiction-xml-to-html.xslt
+t/multi-xslt.t
 t/pod-coverage.t
 t/pod.t
 t/rng.t
 t/xslt.t
-META.json
-TODO.pod

File XML-GrammarBase/t/data/fiction-xml-test-docbook-xslt-output.docbook.xml

+<?xml version="1.0" encoding="UTF-8"?>
+<article xmlns="http://docbook.org/ns/docbook" xmlns:fic="http://web-cpan.berlios.de/modules/XML-Grammar-Fortune/fiction-xml-0.2/" xmlns:db="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xml:id="index" xml:lang="" version="5.0">
+  <db:info>
+    <db:title>David vs. Goliath - Part I</db:title>
+  </db:info>
+  <section xml:id="top">
+    <db:info>
+      <db:title>The Top Section</db:title>
+    </db:info>
+    <db:para>
+    King <db:link xlink:href="http://en.wikipedia.org/wiki/David">David</db:link> and Goliath were standing by each other.
+    </db:para>
+    <db:para>
+    David said unto Goliath: "I will shoot you. I <db:emphasis role="bold">swear</db:emphasis> I will"
+    </db:para>
+    <section xml:id="goliath">
+      <db:info>
+        <db:title>Goliath's Response</db:title>
+      </db:info>
+      <db:para>
+    Goliath was not amused.
+    </db:para>
+      <db:para>
+    He said to David: "Oh, really. <db:emphasis>David</db:emphasis>, the red-headed!".
+    </db:para>
+      <db:para>
+    David started listing Goliath's disadvantages:
+    </db:para>
+      <db:blockquote xml:lang="fr-FR" xml:id="my_blockquote">
+    <db:orderedlist>
+    <db:listitem>
+    <db:para>
+    You're slow.
+    </db:para>
+    </db:listitem>
+    <db:listitem>
+    <db:para>
+    You're big.
+    </db:para>
+    </db:listitem>
+    <db:listitem>
+    <db:para>
+    You're stupid.
+    </db:para>
+    </db:listitem>
+    </db:orderedlist>
+    <db:para>
+    Goliath was insulted and told David he should use bullets for the
+    points, instead:
+    </db:para>
+    <db:itemizedlist>
+    <db:listitem>
+    <db:para>
+    David.
+    </db:para>
+    </db:listitem>
+    <db:listitem>
+    <db:para>
+    Saul.
+    </db:para>
+    </db:listitem>
+    <db:listitem>
+    <db:para>
+    Jonathan.
+    </db:para>
+    </db:listitem>
+    <db:listitem>
+    <db:para>
+    Michal.
+    </db:para>
+    </db:listitem>
+    <db:listitem>
+    <db:para>
+    Meyrav.
+    </db:para>
+    </db:listitem>
+    </db:itemizedlist>
+    </db:blockquote>
+    </section>
+  </section>
+</article>

File XML-GrammarBase/t/data/fiction-xml-to-docbook.xslt

+<?xml version="1.0" encoding="UTF-8"?>
+<xsl:stylesheet version = '1.0'
+    xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
+    xmlns:fic="http://web-cpan.berlios.de/modules/XML-Grammar-Fortune/fiction-xml-0.2/"
+    xmlns="http://docbook.org/ns/docbook"
+    xmlns:db="http://docbook.org/ns/docbook"
+    xmlns:xlink="http://www.w3.org/1999/xlink">
+
+<xsl:output method="xml" encoding="UTF-8" indent="yes"
+ />
+
+<xsl:template match="/">
+        <xsl:apply-templates select="//fic:body" />
+</xsl:template>
+
+<xsl:template match="fic:body">
+    <article>
+        <xsl:attribute name="xml:id">
+            <xsl:value-of select="@xml:id" />
+        </xsl:attribute>
+        <xsl:attribute name="xml:lang">
+            <xsl:value-of select="@xml:lang" />
+        </xsl:attribute>
+        <xsl:attribute name="version">5.0</xsl:attribute>
+        <db:info>
+            <db:title>
+                <xsl:value-of select="fic:title" />
+            </db:title>
+        </db:info>
+        <xsl:apply-templates select="fic:section" />
+    </article>
+</xsl:template>
+
+<xsl:template match="fic:section">
+    <section>
+        <xsl:copy-of select="@xml:id" />
+        <xsl:if test="@xml:lang">
+            <xsl:copy-of select="@xml:lang" />
+        </xsl:if>
+        <!-- Make the title the title attribute or "ID" if does not exist. -->
+        <db:info>
+            <db:title>
+                <xsl:choose>
+                    <xsl:when test="fic:title">
+                        <xsl:value-of select="fic:title" />
+                    </xsl:when>
+                    <xsl:otherwise>
+                        <xsl:value-of select="@xml:id" />
+                    </xsl:otherwise>
+                </xsl:choose>
+            </db:title>
+        </db:info>
+        <xsl:apply-templates select="fic:section|fic:blockquote|fic:p|fic:ol|fic:ul|fic:programlisting" />
+    </section>
+</xsl:template>
+
+<xsl:template match="fic:p">
+    <db:para>
+        <xsl:apply-templates />
+    </db:para>
+</xsl:template>
+
+<xsl:template match="fic:b">
+    <db:emphasis role="bold">
+        <xsl:apply-templates/>
+    </db:emphasis>
+</xsl:template>
+
+<xsl:template name="common_attributes">
+    <xsl:if test="@xlink:href">
+        <xsl:copy-of select="@xlink:href" />
+    </xsl:if>
+    <xsl:if test="@xml:lang">
+        <xsl:copy-of select="@xml:lang" />
+    </xsl:if>
+    <xsl:if test="@xml:id">
+        <xsl:copy-of select="@xml:id" />
+    </xsl:if>
+</xsl:template>
+
+<xsl:template match="fic:blockquote">
+    <db:blockquote>
+        <xsl:call-template name="common_attributes" />
+        <xsl:apply-templates/>
+    </db:blockquote>
+</xsl:template>
+
+<xsl:template match="fic:i">
+    <db:emphasis>
+        <xsl:apply-templates/>
+    </db:emphasis>
+</xsl:template>
+
+<xsl:template match="fic:ol">
+    <db:orderedlist>
+        <xsl:apply-templates/>
+    </db:orderedlist>
+</xsl:template>
+
+<xsl:template match="fic:ul">
+    <db:itemizedlist>
+        <xsl:apply-templates/>
+    </db:itemizedlist>
+</xsl:template>
+
+<xsl:template match="fic:programlisting">
+    <db:programlisting>
+        <xsl:apply-templates/>
+    </db:programlisting>
+</xsl:template>
+
+<xsl:template match="fic:li">
+    <db:listitem>
+        <xsl:apply-templates/>
+    </db:listitem>
+</xsl:template>
+
+<xsl:template match="fic:span">
+    <xsl:variable name="tag_name">
+        <xsl:choose>
+            <xsl:when test="@xlink:href">
+                <xsl:value-of select="'db:link'" />
+            </xsl:when>
+            <xsl:otherwise>
+                <xsl:value-of select="'db:phrase'" />
+            </xsl:otherwise>
+        </xsl:choose>
+    </xsl:variable>
+    <xsl:element name="{$tag_name}">
+        <xsl:call-template name="common_attributes" />
+        <xsl:apply-templates/>
+    </xsl:element>
+</xsl:template>
+
+</xsl:stylesheet>

File XML-GrammarBase/t/multi-xslt.t

+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More tests => 10;
+
+package MyGrammar::MultiXSLT;
+
+use MooX 'late';
+
+use File::Spec;
+
+use XML::GrammarBase::Role::RelaxNG;
+use XML::GrammarBase::Role::XSLT;
+
+with ('XML::GrammarBase::Role::RelaxNG');
+with XSLT(output_format => 'html');
+with XSLT(output_format => 'docbook');
+
+has '+module_base' => (default => 'XML-GrammarBase');
+has '+data_dir' => (default => File::Spec->catdir(File::Spec->curdir(), "t", "data"));
+has '+rng_schema_basename' => (default => 'fiction-xml.rng');
+
+has '+to_html_xslt_transform_basename' => (default => 'fiction-xml-to-html.xslt');
+has '+to_docbook_xslt_transform_basename' => (default => 'fiction-xml-to-docbook.xslt');
+
+package main;
+
+use Test::XML::Ordered qw(is_xml_ordered);
+
+use File::Temp qw(tempfile);
+
+my @is_xml_common = (validation => 0, load_ext_dtd => 0, no_network => 1);
+
+sub my_is_xml
+{
+    local $Test::Builder::Level = $Test::Builder::Level + 1;
+
+    my ($got, $expected, $blurb) = @_;
+
+    return is_xml_ordered(
+        [ @{$got}, @is_xml_common, ],
+        [ @{$expected}, @is_xml_common, ],
+        {},
+        $blurb,
+    );
+}
+
+sub _utf8_slurp
+{
+    my $filename = shift;
+
+    open my $in, '<', $filename
+        or die "Cannot open '$filename' for slurping - $!";
+
+    binmode $in, ':encoding(utf8)';
+
+    local $/;
+    my $contents = <$in>;
+
+    close($in);
+
+    return $contents;
+}
+
+# TEST:$c=0;
+sub test_file
+{
+    my $args = shift;
+
+    my $input_fn = $args->{input_fn};
+    my $output_fn = $args->{output_fn};
+    my $output_format = $args->{output_format};
+
+    my $xslt = MyGrammar::MultiXSLT->new();
+
+    {
+        my $final_source = $xslt->perform_xslt_translation(
+            {
+                output_format => $output_format,
+                source => {file => $input_fn, },
+                output => "string",
+            }
+        );
+
+        my $xml_source = _utf8_slurp($output_fn);
+
+        # TEST:$c++;
+        my_is_xml(
+            [ string => $final_source, ],
+            [ string => $xml_source, ],
+            "'$input_fn' generated good output on source/input_filename - output - string"
+        );
+    }
+
+    {
+        my $final_source = $xslt->perform_xslt_translation(
+            {
+                output_format => $output_format,
+                source => {string_ref => \(_utf8_slurp($input_fn)) },
+                output => "string",
+            }
+        );
+
+        my $xml_source = _utf8_slurp($output_fn);
+
+        # TEST:$c++;
+        my_is_xml(
+            [ string => $final_source, ],
+            [ string => $xml_source, ],
+            "'$input_fn' generated good output on source/string_ref - output - string"
+        );
+    }
+
+    {
+        my $final_dom = $xslt->perform_xslt_translation(
+            {
+                output_format => $output_format,
+                source => {string_ref => \(_utf8_slurp($input_fn)) },
+                output => "dom",
+            }
+        );
+
+        my $xml_source = _utf8_slurp($output_fn);
+
+        # TEST:$c++;
+        my_is_xml(
+            [ string => $final_dom->toString(), ],
+            [ string => $xml_source, ],
+            "'$input_fn' generated good output on source/string_ref - output - dom"
+        );
+    }
+
+    {
+        my ($fh, $filename) = tempfile();
+
+        $xslt->perform_xslt_translation(
+            {
+                output_format => $output_format,
+                source => {string_ref => \(_utf8_slurp($input_fn)) },
+                output => {file => $filename, },
+            }
+        );
+
+        my $xml_source = _utf8_slurp($output_fn);
+        my $final_source = _utf8_slurp($filename);
+
+        # TEST:$c++;
+        my_is_xml(
+            [ string => $final_source, ],
+            [ string => $xml_source, ],
+            "'$input_fn' generated good output on source/string_ref - output/file"
+        );
+    }
+
+    {
+        my ($fh, $filename) = tempfile();
+
+        $xslt->perform_xslt_translation(
+            {
+                output_format => $output_format,
+                source => {string_ref => \(_utf8_slurp($input_fn)) },
+                output => {fh => $fh, },
+            }
+        );
+
+        close($fh);
+
+        my $xml_source = _utf8_slurp($output_fn);
+        my $final_source = _utf8_slurp($filename);
+
+        # TEST:$c++;
+        my_is_xml(
+            [ string => $final_source, ],
+            [ string => $xml_source, ],
+            "'$input_fn' generated good output on source/string_ref - output/fh"
+        );
+    }
+    return;
+}
+
+# TEST:$test_file=$c;
+
+# TEST*$test_file
+test_file(
+    {
+        output_format => 'html',
+        input_fn => File::Spec->catfile(
+            File::Spec->curdir(), "t", "data", "fiction-xml-test.xml",
+        ),
+        output_fn => File::Spec->catfile(
+            File::Spec->curdir(), "t", "data", "fiction-xml-test-html-xslt-output.xhtml",
+        ),
+    }
+);
+
+# TEST*$test_file
+test_file(
+    {
+        output_format => 'docbook',
+        input_fn => File::Spec->catfile(
+            File::Spec->curdir(), "t", "data", "fiction-xml-test.xml",
+        ),
+        output_fn => File::Spec->catfile(
+            File::Spec->curdir(), "t", "data", "fiction-xml-test-docbook-xslt-output.docbook.xml",
+        ),
+    }
+);