Commits

Shlomi Fish  committed 956187b

Fix a bug with handling empty elements.

  • Participants
  • Parent commits 8ffb44f

Comments (0)

Files changed (3)

File Test-XML-Ordered/Changes

     - Display the offending mismatched text in a text node mismatch.
     - Normalise all space-areas to a single - not just the first one.
+    - Add a fix for handling empty elements - e.g: <img /> vs.
+    <img></img>.
 
 0.0.4   Wed 20 Feb 09:54:31 IST 2013
     - Fix a bug where we compared ->name() instead of ->localname().

File Test-XML-Ordered/lib/Test/XML/Ordered.pm

         }
         elsif ($type == XML_READER_TYPE_ELEMENT())
         {
-            if ($self->_got->localName() ne $self->_expected->localName())
+            my $check = sub {
+                if ($self->_got->localName() ne $self->_expected->localName())
+                {
+                    return $calc_prob->({param => "element_name"});
+                }
+                if (_ns($self->_got) ne _ns($self->_expected))
+                {
+                    return $calc_prob->({param => "mismatch_ns"});
+                }
+                return;
+            };
+
+            if (my $ret = $check->())
             {
-                return $calc_prob->({param => "element_name"});
+                return $ret;
             }
-            if (_ns($self->_got) ne _ns($self->_expected))
+
+            my $is_got_empty = $self->_got->isEmptyElement;
+            my $is_expected_empty = $self->_expected->isEmptyElement;
+
+            if ($is_got_empty && (!$is_expected_empty))
             {
-                return $calc_prob->({param => "mismatch_ns"});
+                $self->_read_expected();
+                if (my $ret = $check->())
+                {
+                    return $ret;
+                }
+            }
+            elsif ($is_expected_empty && (!$is_got_empty))
+            {
+                $self->_read_got();
+                if (my $ret = $check->())
+                {
+                    return $ret;
+                }
             }
         }
     }

File Test-XML-Ordered/t/xml_compare1.t

 
 use Test::XML::Ordered qw(is_xml_ordered);
 
-use Test::More tests => 3;
+use Test::More tests => 4;
 
 my @common = (validation => 0, load_ext_dtd => 0, no_network => 1);
 
 
 }
 
+{
+    my $xml_source = <<'EOF';
+<?xml version="1.0" encoding="utf-8"?>
+<document xmlns="http://web-cpan.berlios.de/modules/XML-Grammar-Screenplay/screenplay-xml-0.2/"><head></head><body id="index"><scene id="top"><scene id="david_and_goliath"><description><para>David and <ulink url="http://en.wikipedia.org/wiki/Goliath">Goliath</ulink> are
+standing by each other.</para></description><saying character="David"><para>I will kill you.
+</para></saying><saying character="Goliath"><para>no way, you little idiot!
+</para></saying><saying character="David"><para>yes way!
+</para><para>In the name of <ulink url="http://real-allah.tld/">Allah, the
+<italics>merciful</italics>, real merciful</ulink>, I will show you the power of my sling.
+</para><para>I shall sling you and bing you till infinity.
+</para></saying><description><para>David takes his sling. <image url="sling.png" alt="a sling" title="Photo of David’s sling."></image> . </para></description><saying character="Goliath"><para>I'm still <ulink url="http://wait.tld/">waiting</ulink>.
+</para></saying><saying character="David"><para>so you are.
+</para></saying><description><para>David puts a stone in his sling and shoots Goliath. He hits.</para></description></scene></scene></body></document>
+EOF
+
+    my $final_source = <<'EOF';
+<?xml version='1.0' encoding='utf-8'?>
+<document xmlns="http://web-cpan.berlios.de/modules/XML-Grammar-Screenplay/screenplay-xml-0.2/">
+    <head>
+    </head>
+    <body id="index">
+<scene id="top">
+    <scene id="david_and_goliath">
+        <description>
+            <para>
+                David and
+                <ulink url="http://en.wikipedia.org/wiki/Goliath">Goliath</ulink>
+                are standing by each other.
+            </para>
+        </description>
+        <saying character="David">
+            <para>
+                I will kill you.
+            </para>
+        </saying>
+        <saying character="Goliath">
+            <para>
+                no way, you little idiot!
+            </para>
+        </saying>
+        <saying character="David">
+            <para>
+                yes way!
+            </para>
+            <para>
+                In the name of <ulink url="http://real-allah.tld/">Allah, the <italics>merciful</italics>, real merciful</ulink>, I will show you the power
+                of my sling.
+            </para>
+            <para>
+                I shall sling you and bing you till infinity.
+            </para>
+        </saying>
+        <description>
+            <para>
+                David takes his sling. <image url="sling.png" alt="a sling" title="Photo of David’s sling."/> .
+            </para>
+        </description>
+    <saying character="Goliath">
+        <para>
+            I'm still <ulink url="http://wait.tld/">waiting</ulink>.
+        </para>
+    </saying>
+    <saying character="David">
+        <para>
+            so you are.
+        </para>
+    </saying>
+    <description>
+        <para>
+            David puts a stone in his sling and shoots Goliath. He hits.
+        </para>
+    </description>
+</scene>
+</scene>
+</body>
+</document>
+EOF
+
+    # TEST
+    is_xml_ordered(
+        [ string => $final_source, @common, ],
+        [ string => $xml_source, @common, ],
+        {},
+        "<image /> with empty tag",
+    );
+
+}
+
 # TEST
 ok (1, "Finished");