summaryrefslogtreecommitdiff
path: root/misc/openlayers/tests/Test.AnotherWay.xml_eq.js
blob: 8c245667d92287da69d0385cd30dd15a44277e7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/**
 * File: Test.AnotherWay.xml_eq.js 
 * Adds a xml_eq method to AnotherWay test objects.
 *
 */

(function() {

    /**
     * Function: createNode
     * Given a string, try to create an XML DOM node.  Throws string messages
     *     on failure.
     * 
     * Parameters:
     * text - {String} An XML string.
     *
     * Returns:
     * {DOMElement} An element node.
     */
    function createNode(text) {
        
        var index = text.indexOf('<');
        if(index > 0) {
            text = text.substring(index);
        }
        
        var doc;
        if(window.ActiveXObject && !this.xmldom) {
            doc = new ActiveXObject("Microsoft.XMLDOM");
            try {
                doc.loadXML(text);
            } catch(err) {
                throw "ActiveXObject loadXML failed: " + err;
            }
        } else if(window.DOMParser) {
            try {
                doc = new DOMParser().parseFromString(text, 'text/xml');
            } catch(err) {
                throw "DOMParser.parseFromString failed";
            }
            if(doc.documentElement && doc.documentElement.nodeName == "parsererror") {
                throw "DOMParser.parseFromString returned parsererror";
            }
        } else {
            var req = new XMLHttpRequest();
            req.open("GET", "data:text/xml;charset=utf-8," +
                     encodeURIComponent(text), false);
            if(req.overrideMimeType) {
                req.overrideMimeType("text/xml");
            }
            req.send(null);
            doc = req.responseXML;
        }
        
        var root = doc.documentElement;
        if(!root) {
            throw "no documentElement";
        }
        return root;
    }
    
    /**
     * Function assertEqual
     * Test two objects for equivalence (based on ==).  Throw an exception
     *     if not equivalent.
     * 
     * Parameters:
     * got - {Object}
     * expected - {Object}
     * msg - {String} The message to be thrown.  This message will be appended
     *     with ": got {got} but expected {expected}" where got and expected are
     *     replaced with string representations of the above arguments.
     */
    function assertEqual(got, expected, msg) {
        if(got === undefined) {
            got = "undefined";
        } else if (got === null) {
            got = "null";
        }
        if(expected === undefined) {
            expected = "undefined";
        } else if (expected === null) {
            expected = "null";
        }
        if(got != expected) {
            throw msg + ": got '" + got + "' but expected '" + expected + "'";
        }
    }
    
    /**
     * Function assertElementNodesEqual
     * Test two element nodes for equivalence.  Nodes are considered equivalent
     *     if they are of the same type, have the same name, have the same
     *     namespace prefix and uri, and if all child nodes are equivalent.
     *     Throws a message as exception if not equivalent.
     * 
     * Parameters:
     * got - {DOMElement}
     * expected - {DOMElement}
     * options - {Object} Optional object for configuring test options.
     *
     * Valid options:
     * prefix - {Boolean} Compare element and attribute
     *     prefixes (namespace uri always tested).  Default is false.
     * includeWhiteSpace - {Boolean} Include whitespace only nodes when
     *     comparing child nodes.  Default is false.
     */
    function assertElementNodesEqual(got, expected, options) {
        var testPrefix = (options && options.prefix === true);
        
        // compare types
        assertEqual(got.nodeType, expected.nodeType, "Node type mismatch");
        
        // compare names
        var gotName = testPrefix ?
            got.nodeName : got.nodeName.split(":").pop();
        var expName = testPrefix ?
            expected.nodeName : expected.nodeName.split(":").pop();
        assertEqual(gotName, expName, "Node name mismatch");
        
        // for text nodes compare value
        if(got.nodeType == 3) {
            assertEqual(
                got.nodeValue, expected.nodeValue, "Node value mismatch"
            );
        }
        // for element type nodes compare namespace, attributes, and children
        else if(got.nodeType == 1) {
            
            // test namespace alias and uri
            if(got.prefix || expected.prefix) {
                if(testPrefix) {
                    assertEqual(
                        got.prefix, expected.prefix,
                        "Bad prefix for " + got.nodeName
                    );
                }
            }
            if(got.namespaceURI || expected.namespaceURI) {
                assertEqual(
                    got.namespaceURI, expected.namespaceURI,
                    "Bad namespaceURI for " + got.nodeName
                );
            }
            
            // compare attributes - disregard xmlns given namespace handling above
            var gotAttrLen = 0;
            var gotAttr = {};
            var expAttrLen = 0;
            var expAttr = {};
            var ga, ea, gn, en;
            for(var i=0; i<got.attributes.length; ++i) {
                ga = got.attributes[i];
                if(ga.specified === undefined || ga.specified === true) {
                    if(ga.name.split(":").shift() != "xmlns") {
                        gn = testPrefix ? ga.name : ga.name.split(":").pop();
                        gotAttr[gn] = ga;
                        ++gotAttrLen;
                    }
                }
            }
            for(var i=0; i<expected.attributes.length; ++i) {
                ea = expected.attributes[i];
                if(ea.specified === undefined || ea.specified === true) {
                    if(ea.name.split(":").shift() != "xmlns") {
                        en = testPrefix ? ea.name : ea.name.split(":").pop();
                        expAttr[en] = ea;
                        ++expAttrLen;
                    }
                }
            }
            assertEqual(
                gotAttrLen, expAttrLen,
                "Attributes length mismatch for " + got.nodeName
            );
            var gv, ev;
            for(var name in gotAttr) {
                if(expAttr[name] == undefined) {
                    throw "Attribute name " + gotAttr[name].name + " expected for element " + got.nodeName;
                }
                // test attribute namespace
                assertEqual(
                    gotAttr[name].namespaceURI, expAttr[name].namespaceURI,
                    "Attribute namespace mismatch for element " +
                    got.nodeName + " attribute name " + gotAttr[name].name
                );
                // test attribute value
                assertEqual(
                    gotAttr[name].value, expAttr[name].value,
                    "Attribute value mismatch for element " + got.nodeName +
                    " attribute name " + gotAttr[name].name
                );
            }
            
            // compare children
            var gotChildNodes = getChildNodes(got, options);
            var expChildNodes = getChildNodes(expected, options);

            assertEqual(
                gotChildNodes.length, expChildNodes.length,
                "Children length mismatch for " + got.nodeName
            );
            for(var j=0; j<gotChildNodes.length; ++j) {
                try {
                    assertElementNodesEqual(
                        gotChildNodes[j], expChildNodes[j], options
                    );
                } catch(err) {
                    throw "Bad child " + j + " for element " + got.nodeName + ": " + err;
                }
            }
        }
        return true;
    }

    /**
     * Function getChildNodes
     * Returns the child nodes of the specified nodes. By default this method
     *     will ignore child text nodes which are made up of whitespace content.
     *     The 'includeWhiteSpace' option is used to control this behaviour.
     * 
     * Parameters:
     * node - {DOMElement}
     * options - {Object} Optional object for test configuration.
     * 
     * Valid options:
     * includeWhiteSpace - {Boolean} Include whitespace only nodes when
     *     comparing child nodes.  Default is false.
     * 
     * Returns:
     * {Array} of {DOMElement}
     */
    function getChildNodes(node, options) {
        //check whitespace
        if (options && options.includeWhiteSpace) {
            return node.childNodes;
        }
        else {
           nodes = [];
           for (var i = 0; i < node.childNodes.length; i++ ) {
              var child = node.childNodes[i];
              if (child.nodeType == 1) {
                 //element node, add it 
                 nodes.push(child);
              }
              else if (child.nodeType == 3) {
                 //text node, add if non empty
                 if (child.nodeValue && 
                       child.nodeValue.replace(/^\s*(.*?)\s*$/, "$1") != "" ) { 

                    nodes.push(child);
                 }
              }
           }
  
           return nodes;
        }
    } 
    
    /**
     * Function: Test.AnotherWay._test_object_t.xml_eq
     * Test if two XML nodes are equivalent.  Tests for same node types, same
     *     node names, same namespace URI, same attributes, and recursively
     *     tests child nodes for same criteria.
     *
     * (code)
     * t.xml_eq(got, expected, message);
     * (end)
     * 
     * Parameters:
     * got - {DOMElement | String} A DOM node or XML string to test.
     * expected - {DOMElement | String} The expected DOM node or XML string.
     * msg - {String} A message to print with test output.
     * options - {Object} Optional object for configuring test.
     *
     * Valid options:
     * prefix - {Boolean} Compare element and attribute
     *     prefixes (namespace uri always tested).  Default is false.
     * includeWhiteSpace - {Boolean} Include whitespace only nodes when
     *     comparing child nodes.  Default is false.
     */
    var proto = Test.AnotherWay._test_object_t.prototype;
    proto.xml_eq = function(got, expected, msg, options) {
        // convert arguments to nodes if string
        if(typeof got == "string") {
            try {
                got = createNode(got);
            } catch(err) {
                this.fail(msg + ": got argument could not be converted to an XML node: " + err);
                return;
            }
        }
        if(typeof expected == "string") {
            try {
                expected = createNode(expected);
            } catch(err) {
                this.fail(msg + ": expected argument could not be converted to an XML node: " + err);
                return;
            }
        }
        
        // test nodes for equivalence
        try {
            assertElementNodesEqual(got, expected, options);
            this.ok(true, msg);
        } catch(err) {
            this.fail(msg + ": " + err);
        }
    }
    
})();