Browse Source

Add setStubs/unsetStubs to domstubs to support testing

Do not directly export to global. Instead, export all stubs in domstubs.js and
add a method setStubs to assign all exported stubs to a namespace. Then replace
the import domstubs with an explicit call to this setStubs method.  Also added
unsetStubs for undoing the changes. This is done to allow unit testing of the
SVG backend without namespace pollution.
Rob Wu 8 years ago
parent
commit
9caaaf3a91
  1. 25
      examples/node/domstubs.js
  2. 2
      examples/node/pdf2svg.js

25
examples/node/domstubs.js

@ -41,7 +41,7 @@ function xmlEncode(s){
return buf; return buf;
} }
global.btoa = function btoa(chars) { function btoa(chars) {
var digits = var digits =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
var buffer = ''; var buffer = '';
@ -57,7 +57,7 @@ global.btoa = function btoa(chars) {
digits.charAt(d3) + digits.charAt(d4)); digits.charAt(d3) + digits.charAt(d4));
} }
return buffer; return buffer;
}; }
function DOMElement(name) { function DOMElement(name) {
this.nodeName = name; this.nodeName = name;
@ -127,7 +127,7 @@ DOMElement.prototype = {
}, },
} }
global.document = { const document = {
childNodes : [], childNodes : [],
get currentScript() { get currentScript() {
@ -171,4 +171,21 @@ Image.prototype = {
} }
} }
global.Image = Image; exports.btoa = btoa;
exports.document = document;
exports.Image = Image;
var exported_symbols = Object.keys(exports);
exports.setStubs = function(namespace) {
exported_symbols.forEach(function(key) {
console.assert(!(key in namespace), 'property should not be set: ' + key);
namespace[key] = exports[key];
});
};
exports.unsetStubs = function(namespace) {
exported_symbols.forEach(function(key) {
console.assert(key in namespace, 'property should be set: ' + key);
delete namespace[key];
});
};

2
examples/node/pdf2svg.js

@ -8,7 +8,7 @@
var fs = require('fs'); var fs = require('fs');
// HACK few hacks to let PDF.js be loaded not as a module in global space. // HACK few hacks to let PDF.js be loaded not as a module in global space.
require('./domstubs.js'); require('./domstubs.js').setStubs(global);
// Run `gulp dist-install` to generate 'pdfjs-dist' npm package files. // Run `gulp dist-install` to generate 'pdfjs-dist' npm package files.
var pdfjsLib = require('pdfjs-dist'); var pdfjsLib = require('pdfjs-dist');

Loading…
Cancel
Save