[OpenLayers-Dev] Testing for equivalence in XML with xml_eq

Tim Schaub tschaub at openplans.org
Fri Feb 22 16:04:15 EST 2008


Hey-

So, I assume that the people for whom this is relevant already read 
commit messages, but I thought I'd document an addition to our testing 
framework here anyway.

If you're writing tests for anything that does parsing of XML, I've 
added a method to Test.AnotherWay test objects that will be useful.  The 
new xml_eq method is similar to html_eq except it works for generic xml 
(and works in Safari and FF3 where html_eq fails).

Use is pretty straightforward.  You can compare element nodes to element 
nodes, nodes to strings, or strings to strings.

I added a single script to our run-tests.html page that extends 
Test.AnotherWay:
http://trac.openlayers.org/browser/trunk/openlayers/tests/xml_eq.js

Though I grimace when I see this much code in email, I'll paste a couple 
examples:

// Pretend we're on a test page in a test function
var format = new OpenLayers.Format.XML();
var doc, got, exp;

// Create a doc and check that root is equivalent to some string
doc = format.read(
     "<chicken>soup</chicken>"
);
got = doc.documentElement;
exp = "<chicken>soup</chicken>";
t.xml_eq(got, exp, "passes");

// See what happens with bad expectations
exp = "<chicken>fat</chicken>";
t.xml_eq(got, exp, "fails");
// Fails with the following message:
// Bad child 0 for element chicken: Node value mismatch: got soup
// but expected fat

// Test xml with different namespace aliases
got = "<foo:chicken xmlns:foo='namespace'>soup</foo:chicken>";
exp = "<bar:chicken xmlns:bar='namespace'>soup</bar:chicken>";
t.xml_eq(got, exp, "passes");

// Assert that prefixes are equal
t.xml_eq(got, exp, "fails", {prefix: true});
// Fails with the following message:
// Node name mismatch: got foo:chicken but expected bar:chicken


Etc.

Tim




More information about the Dev mailing list