Commits

Shlomi Fish committed addc17a

Also compare attributes in the got/expected XMLs.

See https://rt.cpan.org/Public/Bug/Display.html?id=88994

Comments (0)

Files changed (3)

Test-XML-Ordered/Changes

+    - Compare for presence and values of attributes of elements
+    that have them.
+        - https://rt.cpan.org/Public/Bug/Display.html?id=88994
+        - Thanks to NGLENN for the report and for some reproducing code.
+
 0.0.5   Wed 20 Feb 12:14:14 IST 2013
     - Display the offending mismatched text in a text node mismatch.
     - Normalise all space-areas to a single - not just the first one.

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

                 {
                     return $calc_prob->({param => "mismatch_ns"});
                 }
+
+                my $list_attrs = sub {
+                    my ($elem) = @_;
+
+                    my @list;
+
+                    if ($elem->moveToFirstAttribute())
+                    {
+                        my $add = sub {
+
+                            my $ns = _ns($elem);
+                            if ($ns ne 'http://www.w3.org/2000/xmlns/')
+                            {
+                                push @list,
+                                { ns => $ns, localName => $elem->localName() };
+                            }
+                        };
+
+                        $add->();
+                        while ($elem->moveToNextAttribute() > 0)
+                        {
+                            $add->();
+                        }
+                        if ($elem->moveToElement() <= 0)
+                        {
+                            die "Cannot move back to element.";
+                        }
+                    }
+
+                    foreach my $attr (@list)
+                    {
+                        $attr->{value} = $elem->getAttributeNs(
+                            $attr->{localName},
+                            $attr->{ns},
+                        ) // '';
+                    }
+
+                    return
+                    [
+                        sort {
+                            ($a->{ns} cmp $b->{ns})
+                                or
+                            ($a->{localName} cmp $b->{localName})
+                        } @list
+                    ]
+                    ;
+                };
+
+                my @got_attrs = @{$list_attrs->($self->_got())};
+                my @exp_attrs = @{$list_attrs->($self->_expected())};
+
+                while (@got_attrs and @exp_attrs)
+                {
+                    my $got_a = shift(@got_attrs);
+                    my $exp_a = shift(@exp_attrs);
+
+                    if ($got_a->{ns} ne $exp_a->{ns})
+                    {
+                        return
+                            $calc_prob->(
+                                {
+                                    param => "attr_ns",
+                                    got => $got_a->{ns},
+                                    expected => $exp_a->{ns},
+                                }
+                            );
+                    }
+                    if ($got_a->{localName} ne $exp_a->{localName})
+                    {
+                        return
+                            $calc_prob->(
+                                {
+                                    param => "attr_localName",
+                                    got => $got_a->{localName},
+                                    expected => $exp_a->{localName},
+                                }
+                            );
+                    }
+                    if ($got_a->{value} ne $exp_a->{value})
+                    {
+                        return
+                            $calc_prob->(
+                                {
+                                    param => "attr_value",
+                                    got => $got_a->{value},
+                                    expected => $exp_a->{value},
+                                }
+                            );
+                    }
+                }
+                if (@got_attrs)
+                {
+                    return $calc_prob->(
+                        {
+                            param => "extra_attr_got",
+                            got => $self->_got,
+                            expected => $self->_expected,
+                        }
+                    );
+                }
+                if (@exp_attrs)
+                {
+                    return $calc_prob->(
+                        {
+                            param => "extra_attr_expected",
+                            got => $self->_got,
+                            expected => $self->_expected,
+                        }
+                    );
+                }
                 return;
             };
 
             " ; " .
             "Expected Namespace: " . _ns($self->_expected) . " at " .$self->_expected->lineNumber();
     }
-
+    elsif ($status_struct->{param} eq "extra_attr_got")
+    {
+        return
+            "Extra attribute for got at " . $self->_got->lineNumber() .
+            " ; " .
+            "Expected at " .$self->_expected->lineNumber();
+    }
+    elsif ($status_struct->{param} eq "attr_localName")
+    {
+        return
+            "Got Attribute localName: <<$status_struct->{got}>> at " . $self->_got->lineNumber() .
+            " ; " .
+            "Expected Attribute localName: <<$status_struct->{expected}>> at  " .$self->_expected->lineNumber();
+    }
     else
     {
-        die "Unknown param";
+        die "Unknown param: $status_struct->{param}";
     }
 }
 

Test-XML-Ordered/t/attribute_compare_1.t

+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::Builder::Tester tests => 1;
+use Test::XML::Ordered qw(is_xml_ordered);
+
+# TEST
+test_out("not ok 1 - XML structure");
+test_diag("Extra attribute for got at 1 ; Expected at 1");
+test_fail(+1);
+is_xml_ordered(
+    [string => '<xml stuff="foo"/>'],
+    [string => '<xml/>'],
+    {},
+    'XML structure'
+);
+test_test("is_xml_ordered fails on extra attribute in 'have' XML.");
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.