summaryrefslogtreecommitdiff
path: root/tests/xmlreader.tests.js
blob: 842e300e6be6807bd962f70a1a9a643836f1239c (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
var assert = require("assert");

var xmlreader = require("../lib/xmlreader");
var test = require("./testing").test;


describe('xmlreader.read', function() {
    test('should read self-closing element', function() {
        return xmlreader.read("<body/>").then(function(result) {
            assert.deepEqual({type: "element", name: "body", attributes: {}, children: []}, result.root);
        });
    });
    
    test('should read empty element with separate closing tag', function() {
        return xmlreader.read("<body></body>").then(function(result) {
            assert.deepEqual({type: "element", name: "body", attributes: {}, children: []}, result.root);
        });
    });
    
    test('should read attributes of tags', function() {
        return xmlreader.read('<body name="bob"/>').then(function(result) {
            assert.deepEqual({name: "bob"}, result.root.attributes);
        });
    });
    
    test('can read text element', function() {
        return xmlreader.read('<body>Hello!</body>').then(function(result) {
            assert.deepEqual({type: "text", value: "Hello!"}, result.root.children[0]);
        });
    });
    
    test('should read element with children', function() {
        return xmlreader.read("<body><a/><b/></body>").then(function(result) {
            var root = result.root;
            assert.equal(2, root.children.length);
            assert.equal("a", root.children[0].name);
            assert.equal("b", root.children[1].name);
        });
    });

    test('unmapped namespaces URIs are included in braces as prefix', function() {
        return xmlreader.read('<w:body xmlns:w="word"/>').then(function(result) {
            assert.deepEqual(result.root.name, "{word}:body");
        });
    });

    test('mapped namespaces URIs are translated using map', function() {
        var namespaceMap = {
            "word": "x"
        };
        
        return xmlreader.read('<w:body xmlns:w="word"/>', namespaceMap).then(function(result) {
            assert.deepEqual(result.root.name, "x:body");
        });
    });

    test('namespace of attributes is mapped to prefix', function() {
        var namespaceMap = {
            "word": "x"
        };
        var xmlString = '<w:body xmlns:w="word" w:val="Hello!"/>';
        return xmlreader.read(xmlString, namespaceMap).then(function(result) {
            assert.deepEqual(result.root.attributes["x:val"], "Hello!");
        });
    });
    
    test('can find first element with name', function() {
        return xmlreader.read('<body><a/><b index="1"/><b index="2"/></body>').then(function(result) {
            var first = result.root.first("b");
            assert.equal("1", first.attributes.index);
        });
    });
    
    test('whitespace between xml declaration and root tag is ignored', function() {
        return xmlreader.read('<?xml version="1.0" ?>\n<body/>').then(function(result) {
            assert.deepEqual("body", result.root.name);
        });
    });
    
    test('error if XML is badly formed', function() {
        return xmlreader.read("<bo").then(function(result) {
            throw new Error("Expected failure");
        }, function(error) {
            assert.ok(error);
            return 1;
        });
    });
});