OpenLayers OpenLayers

Changeset 6344

Show
Ignore:
Timestamp:
02/22/08 15:08:28 (9 months ago)
Author:
tschaub
Message:

Modifying xml_eq so that prefixes for element and attribute nodes are not tested by default. Namespace URI is always tested for both. If you also want to confirm that prefixes are equal, test with options.prefix true. Modifying tests for the XML format to use xml_eq. (see #1383)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/openlayers/tests/Format/test_XML.html

    r6131 r6344  
    4444         
    4545        var format = new OpenLayers.Format.XML(); 
    46         t.plan(format.xmldom ? 11 : 10); 
     46        t.plan(format.xmldom ? 10 : 9); 
    4747 
    4848        var doc = format.read(text); 
     
    5353        t.ok(doc.documentElement, 
    5454             "ok to access doc.documentElement"); 
    55         t.eq(doc.documentElement.nodeName, "ol:root", 
    56              "doc root has the correct node name"); 
    57         t.eq(doc.documentElement.childNodes[1].firstChild.nodeValue, "junk2", 
    58              "second child of doc root has correct child node"); 
     55        t.xml_eq(doc.documentElement, text, 
     56                 "doc.documentElement correctly read"); 
    5957         
    6058        // read can also be called on the prototype directly 
     
    6664        t.ok(doc.documentElement, 
    6765             "ok to access doc.documentElement"); 
    68         t.eq(doc.documentElement.nodeName, "ol:root", 
    69              "doc root has the correct node name"); 
    70         t.eq(doc.documentElement.childNodes[1].firstChild.nodeValue, "junk2", 
    71              "second child of doc root has correct child node"); 
     66        t.xml_eq(doc.documentElement, text, 
     67                 "doc.documentElement correctly read"); 
    7268         
    7369        // where appropriate, make sure doc is loaded into xmldom property 
    7470        if(format.xmldom) { 
    75             t.eq(format.xmldom.documentElement.childNodes[1].firstChild.nodeValue, 
    76                  "junk2", 
    77                  "second child of doc root has correct child node"); 
     71            t.xml_eq(format.xmldom.documentElement, text, 
     72                     "xmldom.documentElement contains equivalent xml"); 
    7873        } 
     74         
     75        // test equivalence with different namespace alias 
     76        var pre1 =  
     77            "<pre1:parent xmlns:pre1='http://namespace'>" + 
     78                "<pre1:child1>value2</pre1:child1>" + 
     79                "<pre1:child2 pre1:attr1='foo'>value2</pre1:child2>" + 
     80                "<pre1:child3 chicken:attr='hot' xmlns:chicken='http://soup'/>" + 
     81            "</pre1:parent>"; 
     82        var pre2 =  
     83            "<pre2:parent xmlns:pre2='http://namespace'>" + 
     84                "<pre2:child1>value2</pre2:child1>" + 
     85                "<pre2:child2 pre2:attr1='foo'>value2</pre2:child2>" + 
     86                "<pre2:child3 pea:attr='hot' xmlns:pea='http://soup'/>" + 
     87            "</pre2:parent>"; 
     88        var doc1 = format.read(pre1); 
     89        t.xml_eq(doc1.documentElement, pre2, "read correctly sets namespaces"); 
     90         
    7991    } 
    8092 
  • trunk/openlayers/tests/xml_eq.js

    r6341 r6344  
    33 * Adds a xml_eq method to AnotherWay test objects. 
    44 * 
    5  * Function: Test.AnotherWay._test_object_t.xml_eq 
    6  * Test if two XML nodes are equivalent.  Tests for same node types, same 
    7  *     node names, same namespace prefix and URI, same attributes, and 
    8  *     recursively tests child nodes. 
    9  * 
    10  * (code) 
    11  * t.xml_eq(got, expected, message); 
    12  * (end) 
    13  *  
    14  * Parameters: 
    15  * got - {DOMElement | String} A DOM node or XML string to test. 
    16  * expected - {DOMElement | String} The expected DOM node or XML string. 
    17  * msg - {String} A message to print with test output. 
    18  *  
    195 */ 
    206 
    217(function() { 
    22      
    23     /** 
    24      * Function: stringFormat 
    25      * Given a string with tokens in the form ${token}, return a string 
    26      *     with tokens replaced with properties from the given context 
    27      *     object.  Represent a literal "${" by doubling it, e.g. "${${". 
    28      * 
    29      * Parameters: 
    30      * template - {String} A string with tokens to be replaced.  A template 
    31      *     has the form "literal ${token}" where the token will be replaced 
    32      *     by the value of context["token"]. 
    33      * context - {Object} An optional object with properties corresponding 
    34      *     to the tokens in the format string.  If no context is sent, the 
    35      *     window object will be used. 
    36      * 
    37      * Returns: 
    38      * {String} A string with tokens replaced from the context object. 
    39      */ 
    40     function formatString(template, context) { 
    41         if(!context) { 
    42             context = window; 
    43         } 
    44         var tokens = template.split("${"); 
    45         var item, last; 
    46         for(var i=1; i<tokens.length; i++) { 
    47             item = tokens[i]; 
    48             last = item.indexOf("}");  
    49             if(last > 0) {  
    50                 tokens[i] = context[item.substring(0, last)] + 
    51                             item.substring(++last);  
    52             } else { 
    53                 tokens[i] = "${" + item; 
    54             } 
    55         } 
    56         return tokens.join(""); 
    57     } 
    588 
    599    /** 
     
    11868     * got - {Object} 
    11969     * expected - {Object} 
    120      * msg - {String} The message to be thrown.  Can be formatted for use with 
    121      *     formatString where got and expected (cast to string) will b
    122      *     substituted
     70     * msg - {String} The message to be thrown.  This message will be appended 
     71     *     with ": got {got} but expected {expected}" where got and expected ar
     72     *     replaced with string representations of the above arguments
    12373     */ 
    12474    function assertEqual(got, expected, msg) { 
     
    13383            expected = "null"; 
    13484        } 
    135         if(!msg) { 
    136             msg = "got ${got} but expected ${expected}"; 
    137         } 
    13885        if(got != expected) { 
    139             throw formatString(msg, {got: got, expected: expected})
     86            throw msg + ": got " + got + " but expected " + expected
    14087        } 
    14188    } 
     
    15198     * got - {DOMElement} 
    15299     * expected - {DOMElement} 
    153      */ 
    154     function assertElementNodesEqual(got, expected) { 
     100     * options - {Object} Optional object for configuring test options.  Set 
     101     *     'prefix' property to true in order to compare element and attribute 
     102     *     prefixes (namespace uri always tested).  By default, prefixes 
     103     *     are not tested. 
     104     */ 
     105    function assertElementNodesEqual(got, expected, options) { 
     106        var testPrefix = (options && options.prefix === true); 
     107         
    155108        // compare types 
    156         assertEqual( 
    157             got.nodeType, expected.nodeType, 
    158             "Node type mismatch: got ${got} but expected ${expected}" 
    159         ); 
     109        assertEqual(got.nodeType, expected.nodeType, "Node type mismatch"); 
    160110         
    161111        // compare names 
    162         assertEqual( 
    163             got.nodeName, expected.nodeName, 
    164             "Node name mismatch: got ${got} but expected ${expected}" 
    165         ); 
    166          
     112        var gotName = testPrefix ? 
     113            got.nodeName : got.nodeName.split(":").pop(); 
     114        var expName = testPrefix ? 
     115            expected.nodeName : expected.nodeName.split(":").pop(); 
     116        assertEqual(gotName, expName, "Node name mismatch"); 
     117         
     118        // for text nodes compare value 
     119        if(got.nodeType == 3) { 
     120            assertEqual( 
     121                got.nodeValue, expected.nodeValue, "Node value mismatch" 
     122            ); 
     123        } 
    167124        // for element type nodes compare namespace, attributes, and children 
    168         if(got.nodeType == 1) { 
     125        else if(got.nodeType == 1) { 
    169126             
    170127            // test namespace alias and uri 
    171128            if(got.prefix || expected.prefix) { 
    172                 assertEqual( 
    173                     got.prefix, expected.prefix, 
    174                     "Bad prefix for " + got.nodeName + 
    175                     ": got ${got} but expected ${expected}" 
    176                 ); 
     129                if(testPrefix) { 
     130                    assertEqual( 
     131                        got.prefix, expected.prefix, 
     132                        "Bad prefix for " + got.nodeName 
     133                    ); 
     134                } 
    177135            } 
    178136            if(got.namespaceURI || expected.namespaceURI) { 
    179137                assertEqual( 
    180138                    got.namespaceURI, expected.namespaceURI, 
    181                     "Bad namespaceURI for " + got.nodeName + 
    182                     ": got ${got} but expected ${expected}" 
     139                    "Bad namespaceURI for " + got.nodeName 
    183140                ); 
    184141            } 
     
    187144            var gotAttrLen = 0; 
    188145            var gotAttr = {}; 
    189             var expectedAttrLen = 0; 
    190             var expectedAttr = {}; 
    191             var ga, ea
     146            var expAttrLen = 0; 
     147            var expAttr = {}; 
     148            var ga, ea, gn, en
    192149            for(var i=0; i<got.attributes.length; ++i) { 
    193150                ga = got.attributes[i]; 
    194                 if(ga.name.split(":").shift() != "xmlns") { 
    195                     gotAttr[ga.name] = ga.value; 
    196                     ++gotAttrLen; 
     151                if(ga.specified === undefined || ga.specified === true) { 
     152                    if(ga.name.split(":").shift() != "xmlns") { 
     153                        gn = testPrefix ? ga.name : ga.name.split(":").pop(); 
     154                        gotAttr[gn] = ga; 
     155                        ++gotAttrLen; 
     156                    } 
    197157                } 
    198158            } 
    199159            for(var i=0; i<expected.attributes.length; ++i) { 
    200160                ea = expected.attributes[i]; 
    201                 if(ea.name.split(":").shift() != "xmlns") { 
    202                     expectedAttr[ea.name] = ea.value; 
    203                     ++expectedAttrLen; 
     161                if(ea.specified === undefined || ea.specified === true) { 
     162                    if(ea.name.split(":").shift() != "xmlns") { 
     163                        en = testPrefix ? ea.name : ea.name.split(":").pop(); 
     164                        expAttr[en] = ea; 
     165                        ++expAttrLen; 
     166                    } 
    204167                } 
    205168            } 
    206169            assertEqual( 
    207                 gotAttrLen, expectedAttrLen, 
    208                 "Attributes length mismatch for " + got.nodeName + 
    209                 ": got ${got} but expected ${expected}" 
     170                gotAttrLen, expAttrLen, 
     171                "Attributes length mismatch for " + got.nodeName 
    210172            ); 
    211173            var gv, ev; 
    212174            for(var name in gotAttr) { 
     175                if(expAttr[name] == undefined) { 
     176                    throw "Attribute name " + gotAttr[name].name + " expected for element " + got.nodeName; 
     177                } 
     178                // test attribute namespace 
    213179                assertEqual( 
    214                     gotAttr[name], expectedAttr[name], 
     180                    gotAttr[name].namespaceURI, expAttr[name].namespaceURI, 
     181                    "Attribute namespace mismatch for element " + 
     182                    got.nodeName + " attribute name " + gotAttr[name].name 
     183                ); 
     184                // test attribute value 
     185                assertEqual( 
     186                    gotAttr[name].value, expAttr[name].value, 
    215187                    "Attribute value mismatch for element " + got.nodeName + 
    216                     " attribute " + name + ": got ${got} but expected ${expected}" 
     188                    " attribute name " + gotAttr[name].name 
    217189                ); 
    218190            } 
     
    221193            assertEqual( 
    222194                got.childNodes.length, expected.childNodes.length, 
    223                 "Children length mismatch for " + got.nodeName + 
    224                 ": got ${got} but expected ${expected}" 
     195                "Children length mismatch for " + got.nodeName 
    225196            ); 
    226197            for(var j=0; j<got.childNodes.length; ++j) { 
     
    230201                    ); 
    231202                } catch(err) { 
    232                     throw "Bad child " + j + " for node " + got.nodeName + ": " + err; 
     203                    throw "Bad child " + j + " for element " + got.nodeName + ": " + err; 
    233204                } 
    234205            } 
     
    237208    } 
    238209     
    239     // assign test function to AnotherWay test prototype 
    240     var proto = Test.AnotherWay._test_object_t.prototype;     
    241     proto.xml_eq = function(got, expected, msg) {         
     210    /** 
     211     * Function: Test.AnotherWay._test_object_t.xml_eq 
     212     * Test if two XML nodes are equivalent.  Tests for same node types, same 
     213     *     node names, same namespace URI, same attributes, and recursively 
     214     *     tests child nodes for same criteria. 
     215     * 
     216     * (code) 
     217     * t.xml_eq(got, expected, message); 
     218     * (end) 
     219     *  
     220     * Parameters: 
     221     * got - {DOMElement | String} A DOM node or XML string to test. 
     222     * expected - {DOMElement | String} The expected DOM node or XML string. 
     223     * msg - {String} A message to print with test output. 
     224     * options - {Object} Optional object for configuring test options.  Set 
     225     *     'prefix' property to true in order to compare element and attribute 
     226     *     prefixes (namespace uri always tested).  By default, prefixes 
     227     *     are not tested. 
     228     */ 
     229    var proto = Test.AnotherWay._test_object_t.prototype; 
     230    proto.xml_eq = function(got, expected, msg, options) { 
    242231        // convert arguments to nodes if string 
    243232        if(typeof got == "string") { 
     
    245234                got = createNode(got); 
    246235            } catch(err) { 
    247                 this.fail("got argument could not be converted to an XML node: " + err); 
     236                this.fail(msg + ": got argument could not be converted to an XML node: " + err); 
    248237                return; 
    249238            } 
     
    253242                expected = createNode(expected); 
    254243            } catch(err) { 
    255                 this.fail("expected argument could not be converted to an XML node: " + err); 
     244                this.fail(msg + ": expected argument could not be converted to an XML node: " + err); 
    256245                return; 
    257246            } 
     
    260249        // test nodes for equivalence 
    261250        try { 
    262             assertElementNodesEqual(got, expected); 
     251            assertElementNodesEqual(got, expected, options); 
    263252            this.ok(true, msg); 
    264253        } catch(err) { 
    265             this.fail(err); 
     254            this.fail(msg + ": " + err); 
    266255        } 
    267256    }