Optimization.
[redakcja.git] / src / wiki / tests / xslt / auto / auto_test.js
1 var jsdom = require("jsdom");
2 var $ = require('jquery')(new jsdom.JSDOM().window);
3 var fs = require('fs');
4 var exec = require('child_process').exec;
5 var pd = require('pretty-data').pd;
6 var ansidiff = require('ansidiff');
7
8
9 eval(fs.readFileSync(__dirname + '/../../../../redakcja/static/js/wiki/xslt.js') + '');
10
11
12 function assertNodesEqual(lhs, rhs, areHTMLNodes) {
13
14     function throwError() {
15         throw new Error(ansidiff.chars(pd.xml(lhs), pd.xml(rhs)));
16     }
17
18     var lhsNode = $(lhs)[0],
19         rhsNode = $(rhs)[0];
20
21     if(areHTMLNodes) {
22         var getMagicAttrsInfo = function(node) {
23             var valuePrefix = 'x-attr-value-';
24             var namePrefix = 'x-attr-name-';
25             var startsWith = function(str, prefix) { return str.substr(0, prefix.length) === prefix; }
26             var isNameAttribute = function(attr) { return startsWith(attr.name, namePrefix); }
27             var isValueAttribute = function(attr) { return startsWith(attr.name, valuePrefix); }
28             var extractId = function(attr) { return attr.name.split('-').pop(); }
29             var temp = {};
30             var toret = {map: {}, magicAttrsList: []};
31             
32             for(var i = 0; i < node.attributes.length; i++) {
33                 var attr = node.attributes[i];
34                 if(isNameAttribute(attr) || isValueAttribute(attr)) {
35                     var id = extractId(attr);
36                     temp[id] = typeof temp[id] === 'undefined' ? {} : temp[id];
37                     if(isNameAttribute(attr))
38                         temp[id]['name'] = attr.value;
39                     else
40                         temp[id]['value'] = attr.value;
41                     toret.magicAttrsList.push(attr.name);
42                 }
43             }
44
45             Object.keys(temp).forEach(function(id) {
46                 var pair = temp[id];
47                 toret.map[pair.name] = typeof toret.map[pair.name] === 'undefined' ? toret.map[pair.name] = [] : toret.map[pair.name];
48                 toret.map[pair.name].push(pair.value);
49             });
50             
51             return toret;
52         }
53     
54         var removeAttrs = function(node, attrsNames) {
55             attrsNames.forEach(function(name) {
56                 node.removeAttribute(name);
57             });
58         }
59         
60         var mapsEqual = function(map1, map2) {
61             if(Object.keys(map1).length != Object.keys(map2).length)
62                 return false;        
63             var arraysEqual = function(a1, a2) {return a1.slice().sort().join('') === a2.slice().sort().join('');}
64             Object.keys(map1).forEach(function(key) {
65                 if(Object.keys(map2).indexOf(key) === -1 || !arraysEqual(map2[key], map1[key]))
66                     return false;
67             });
68             return true;
69         }
70     
71         var lhsMagicAttrsInfo = getMagicAttrsInfo(lhsNode),
72             rhsMagicAttrsInfo = getMagicAttrsInfo(rhsNode);
73         
74         removeAttrs(lhsNode, lhsMagicAttrsInfo.magicAttrsList);
75         removeAttrs(rhsNode, rhsMagicAttrsInfo.magicAttrsList);
76         
77         if(!mapsEqual(lhsMagicAttrsInfo.map, rhsMagicAttrsInfo.map))
78             throwError();
79     }
80     
81     if(!lhsNode.isEqualNode(rhsNode))
82         throwError();
83 }
84
85 suite('wiki.tests.xslt.auto', function() {
86         
87         var tempFileName = '.temp.xml';
88         var xsltStyleSheetPath = __dirname + '/../../../../redakcja/static/xsl/wl2html_client.xsl';
89         
90         fs.readdirSync(__dirname + '/data/').forEach(function(fileName) {
91             
92             if(fileName === tempFileName)
93                 return;
94             
95             var ext = fileName.split('.').pop();
96             if(ext !== 'html' && ext !== 'xml')
97                 return;
98
99             var inputData = fs.readFileSync(__dirname + '/data/' + fileName) + '';
100             
101             if(ext === 'html') {
102                 test('[HTML->XML->HTML] ' + fileName, function(done) {
103                     var result = html2text({
104                         element: $(inputData)[0],
105                         stripOuter: false,
106                         success: function(generatedXML) {
107                             fs.writeFileSync(tempFileName, generatedXML);
108                             exec(['xsltproc', xsltStyleSheetPath, tempFileName].join(' ') , {}, 
109                                 function(error, stdout, stderr) {
110                                     fs.unlinkSync(tempFileName);
111                                     assertNodesEqual(inputData, stdout, true);
112                                     done();
113                             });
114                         },
115                         error: function(msg){throw msg;}
116                     });
117                 });
118             } else if(ext === 'xml') {
119                 test('[XML->HTML->XML] ' + fileName, function(done) {
120                     var originalXML = $(inputData);
121                     exec(['xsltproc', xsltStyleSheetPath, __dirname + '/data/' + fileName].join(' ') , {},
122                         function(error, stdout, stderr) {
123                             var generatedHTML = $(stdout);
124                             var result = html2text({
125                                 element: generatedHTML[0],
126                                 stripOuter: false,
127                                 success: function(xmltext) {
128                                     assertNodesEqual(inputData, xmltext);
129                                     done();
130                                 },
131                                 error: function(msg){throw msg;}
132                             });
133                     });
134                 });
135             }
136         });
137 });
138
139