| 1 |
var test = require('tape'); |
| 2 |
var concat = require('concat-stream'); |
| 3 |
var quote = require('quote-stream'); |
| 4 |
var staticModule = require('../'); |
| 5 |
var fs = require('fs'); |
| 6 |
var path = require('path'); |
| 7 |
|
| 8 |
test('multi-vars', function (t) { |
| 9 |
t.plan(2); |
| 10 |
|
| 11 |
var expected = [ 'beep boop!' ]; |
| 12 |
var sm = staticModule({ |
| 13 |
fs: { |
| 14 |
readFileSync: function (file, enc) { |
| 15 |
return fs.createReadStream(file).pipe(quote()); |
| 16 |
} |
| 17 |
} |
| 18 |
}, { vars: { __dirname: path.join(__dirname, 'vars') } }); |
| 19 |
|
| 20 |
readStream('source.js').pipe(sm).pipe(concat(function (body) { |
| 21 |
t.equal( |
| 22 |
body.toString('utf8').replace(/;/g,''), |
| 23 |
'var html = "beep boop",\n x = \'!\'\nconsole.log(html + x)\n' |
| 24 |
); |
| 25 |
Function(['console'],body)({ log: log }); |
| 26 |
function log (msg) { t.equal(msg, expected.shift()) } |
| 27 |
})); |
| 28 |
}); |
| 29 |
|
| 30 |
test('2-var', function (t) { |
| 31 |
t.plan(2); |
| 32 |
|
| 33 |
var expected = [ 'beep boop' ]; |
| 34 |
var sm = staticModule({ |
| 35 |
fs: { |
| 36 |
readFileSync: function (file, enc) { |
| 37 |
return fs.createReadStream(file).pipe(quote()); |
| 38 |
} |
| 39 |
} |
| 40 |
}, { vars: { __dirname: path.join(__dirname, 'vars') } }); |
| 41 |
|
| 42 |
readStream('one.js').pipe(sm).pipe(concat(function (body) { |
| 43 |
t.equal( |
| 44 |
body.toString('utf8').replace(/;/g,''), |
| 45 |
'var html = "beep boop"\nconsole.log(html)\n' |
| 46 |
); |
| 47 |
Function(['console'],body)({ log: log }); |
| 48 |
function log (msg) { t.equal(msg, expected.shift()) } |
| 49 |
})); |
| 50 |
}); |
| 51 |
|
| 52 |
test('5-var', function (t) { |
| 53 |
t.plan(1); |
| 54 |
|
| 55 |
var expected = [ 'beep boop123' ]; |
| 56 |
var sm = staticModule({ |
| 57 |
fs: { |
| 58 |
readFileSync: function (file, enc) { |
| 59 |
return fs.createReadStream(file).pipe(quote()); |
| 60 |
} |
| 61 |
} |
| 62 |
}, { vars: { __dirname: path.join(__dirname, 'vars') } }); |
| 63 |
|
| 64 |
readStream('five.js').pipe(sm).pipe(concat(function (body) { |
| 65 |
Function(['console'],body)({ log: log }); |
| 66 |
function log (msg) { t.equal(msg, expected.shift()) } |
| 67 |
})); |
| 68 |
}); |
| 69 |
|
| 70 |
test('multi consecutive require vars', function (t) { |
| 71 |
t.plan(1); |
| 72 |
|
| 73 |
var expected = [ 'beep boop' ]; |
| 74 |
var sm = staticModule({ |
| 75 |
fs: { |
| 76 |
readFileSync: function (file, enc) { |
| 77 |
return fs.createReadStream(file).pipe(quote()); |
| 78 |
} |
| 79 |
} |
| 80 |
}, { |
| 81 |
vars: { __dirname: path.join(__dirname, 'vars') }, |
| 82 |
sourceMap: true // Make sure source maps work when replacing 0 length ranges. |
| 83 |
}); |
| 84 |
|
| 85 |
readStream('multi-require.js').pipe(sm).pipe(concat(function (body) { |
| 86 |
Function(['console','require'],body)({ log: log }, function() { return {} }); |
| 87 |
function log (msg) { t.equal(msg, expected.shift()) } |
| 88 |
})); |
| 89 |
}); |
| 90 |
|
| 91 |
function readStream (file) { |
| 92 |
return fs.createReadStream(path.join(__dirname, 'vars', file)); |
| 93 |
} |