-
-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
62de97c
commit d09cbca
Showing
47 changed files
with
769 additions
and
811 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,124 +1,122 @@ | ||
"use strict"; | ||
'use strict' | ||
|
||
var assert = require('assert') | ||
, stats = require('./samples/stats') | ||
, proxyquire = require('../..') | ||
, file = '/some/path/test.ext' | ||
, foo | ||
, fooCut | ||
, fooWild | ||
, cutBarStub = { bar: function () { return 'barber'; } } | ||
, wildBarStub = { bar: function () { return 'barbar'; } } | ||
; | ||
|
||
foo = proxyquire('./samples/foo', { }); | ||
fooCut = proxyquire('./samples/foo', { './bar': cutBarStub }); | ||
fooWild = proxyquire('./samples/foo', { './bar': wildBarStub }); | ||
|
||
assert.equal(stats.fooRequires(), 3); | ||
|
||
assert.equal(foo.bigBar() , 'BAR'); | ||
assert.equal(fooCut.bigBar() , 'BARBER'); | ||
assert.equal(fooWild.bigBar() , 'BARBAR'); | ||
var stats = require('./samples/stats') | ||
var proxyquire = require('../..') | ||
var file = '/some/path/test.ext' | ||
var foo | ||
var fooCut | ||
var fooWild | ||
var cutBarStub = { bar: function () { return 'barber' } } | ||
var wildBarStub = { bar: function () { return 'barbar' } } | ||
|
||
foo = proxyquire('./samples/foo', { }) | ||
fooCut = proxyquire('./samples/foo', { './bar': cutBarStub }) | ||
fooWild = proxyquire('./samples/foo', { './bar': wildBarStub }) | ||
|
||
assert.equal(stats.fooRequires(), 3) | ||
|
||
assert.equal(foo.bigBar(), 'BAR') | ||
assert.equal(fooCut.bigBar(), 'BARBER') | ||
assert.equal(fooWild.bigBar(), 'BARBAR') | ||
|
||
// non overriden keys call thru by default | ||
assert.equal(foo.bigRab() , 'RAB'); | ||
assert.equal(fooCut.bigRab() , 'RAB'); | ||
assert.equal(foo.bigRab(), 'RAB') | ||
assert.equal(fooCut.bigRab(), 'RAB') | ||
|
||
// non overridden module path untouched | ||
assert.equal(foo.bigExt(file) , '.EXT'); | ||
assert.equal(fooCut.bigExt(file) , '.EXT'); | ||
assert.equal(fooWild.bigExt(file) , '.EXT'); | ||
assert.equal(foo.bigBas(file) , 'TEST.EXT'); | ||
assert.equal(fooCut.bigBas(file) , 'TEST.EXT'); | ||
assert.equal(fooWild.bigBas(file) , 'TEST.EXT'); | ||
assert.equal(foo.bigExt(file), '.EXT') | ||
assert.equal(fooCut.bigExt(file), '.EXT') | ||
assert.equal(fooWild.bigExt(file), '.EXT') | ||
assert.equal(foo.bigBas(file), 'TEST.EXT') | ||
assert.equal(fooCut.bigBas(file), 'TEST.EXT') | ||
assert.equal(fooWild.bigBas(file), 'TEST.EXT') | ||
|
||
// overriding keys after require works for both inline and non inline requires | ||
cutBarStub.bar = function () { return 'friseur'; }; | ||
cutBarStub.rab = function () { return 'rabarber'; }; | ||
cutBarStub.bar = function () { return 'friseur' } | ||
cutBarStub.rab = function () { return 'rabarber' } | ||
|
||
assert.equal(fooCut.bigBar(), 'FRISEUR'); | ||
assert.equal(fooCut.bigRab(), 'RABARBER'); | ||
assert.equal(fooCut.bigBar(), 'FRISEUR') | ||
assert.equal(fooCut.bigRab(), 'RABARBER') | ||
|
||
// autofilling keys on delete only works for inline requires | ||
cutBarStub.bar = undefined; | ||
assert.equal(fooCut.bigBar(), 'BAR'); | ||
|
||
cutBarStub.rab = undefined; | ||
assert.throws(fooCut.bigRab); | ||
cutBarStub.bar = undefined | ||
assert.equal(fooCut.bigBar(), 'BAR') | ||
|
||
cutBarStub.rab = undefined | ||
assert.throws(fooCut.bigRab) | ||
|
||
// turn off callThru feature via noCallThru | ||
// not turned off | ||
foo = proxyquire('./samples/foo', { | ||
path: { | ||
extname: function (file) { return 'Exterminate, exterminate the ' + file; } | ||
} | ||
}); | ||
foo = proxyquire('./samples/foo', { | ||
path: { | ||
extname: function (file) { return 'Exterminate, exterminate the ' + file } | ||
} | ||
}) | ||
|
||
assert.equal(foo.bigExt(file), 'EXTERMINATE, EXTERMINATE THE /SOME/PATH/TEST.EXT'); | ||
assert.equal(foo.bigBas(file), 'TEST.EXT'); | ||
assert.equal(foo.bigExt(file), 'EXTERMINATE, EXTERMINATE THE /SOME/PATH/TEST.EXT') | ||
assert.equal(foo.bigBas(file), 'TEST.EXT') | ||
|
||
// turned off | ||
foo = proxyquire('./samples/foo', { | ||
path: { | ||
extname: function (file) { return 'Exterminate, exterminate the ' + file; } | ||
, '@noCallThru': true | ||
} | ||
}); | ||
foo = proxyquire('./samples/foo', { | ||
path: { | ||
extname: function (file) { return 'Exterminate, exterminate the ' + file }, | ||
'@noCallThru': true | ||
} | ||
}) | ||
|
||
assert.equal(foo.bigExt(file), 'EXTERMINATE, EXTERMINATE THE /SOME/PATH/TEST.EXT'); | ||
assert.throws(foo.bigBas); | ||
assert.equal(foo.bigExt(file), 'EXTERMINATE, EXTERMINATE THE /SOME/PATH/TEST.EXT') | ||
assert.throws(foo.bigBas) | ||
|
||
// turned off globally | ||
// not turned back on per module | ||
|
||
foo = proxyquire | ||
.noCallThru() | ||
.load('./samples/foo', { | ||
path: { | ||
extname: function (file) { return 'Exterminate, exterminate the ' + file; } | ||
} | ||
}); | ||
.load('./samples/foo', { | ||
path: { | ||
extname: function (file) { return 'Exterminate, exterminate the ' + file } | ||
} | ||
}) | ||
|
||
assert.throws(foo.bigBas); | ||
assert.throws(foo.bigBas) | ||
|
||
// turned back on per module | ||
|
||
foo = proxyquire | ||
.noCallThru() | ||
.load('./samples/foo', { | ||
path: { | ||
extname: function (file) { return 'Exterminate, exterminate the ' + file; } | ||
, '@noCallThru': false | ||
} | ||
}); | ||
.load('./samples/foo', { | ||
path: { | ||
extname: function (file) { return 'Exterminate, exterminate the ' + file }, | ||
'@noCallThru': false | ||
} | ||
}) | ||
|
||
assert.equal(foo.bigBas(file), 'TEST.EXT'); | ||
assert.equal(foo.bigBas(file), 'TEST.EXT') | ||
|
||
// turned back on globally | ||
|
||
foo = proxyquire | ||
.callThru() | ||
.load('./samples/foo', { | ||
path: { | ||
extname: function (file) { return 'Exterminate, exterminate the ' + file; } | ||
} | ||
}); | ||
.load('./samples/foo', { | ||
path: { | ||
extname: function (file) { return 'Exterminate, exterminate the ' + file } | ||
} | ||
}) | ||
|
||
assert.equal(foo.bigBas(file), 'TEST.EXT'); | ||
assert.equal(foo.bigBas(file), 'TEST.EXT') | ||
|
||
// turned back off per module | ||
|
||
foo = proxyquire | ||
.callThru() | ||
.load('./samples/foo', { | ||
path: { | ||
extname: function (file) { return 'Exterminate, exterminate the ' + file; } | ||
, '@noCallThru': true | ||
} | ||
}); | ||
.load('./samples/foo', { | ||
path: { | ||
extname: function (file) { return 'Exterminate, exterminate the ' + file }, | ||
'@noCallThru': true | ||
} | ||
}) | ||
|
||
assert.throws(foo.bigBas); | ||
assert.throws(foo.bigBas) | ||
|
||
console.log('*** All Asserts passed ***'); | ||
console.log('*** All Asserts passed ***') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,9 @@ | ||
function bar () { | ||
return 'bar'; | ||
return 'bar' | ||
} | ||
|
||
function rab () { | ||
return 'rab'; | ||
return 'rab' | ||
} | ||
|
||
module.exports = { bar : bar, rab: rab }; | ||
|
||
module.exports = { bar: bar, rab: rab } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,30 @@ | ||
var stats = require('./stats') | ||
, bar = require('./bar') | ||
, path = require('path') | ||
; | ||
var bar = require('./bar') | ||
var path = require('path') | ||
|
||
stats.incFooRequires(); | ||
stats.incFooRequires() | ||
|
||
function bigBar () { | ||
function bigBar () { | ||
// inline require | ||
return require('./bar').bar().toUpperCase(); | ||
return require('./bar').bar().toUpperCase() | ||
} | ||
|
||
function bigRab () { | ||
// module wide require | ||
return bar.rab().toUpperCase(); | ||
return bar.rab().toUpperCase() | ||
} | ||
|
||
function bigExt (file) { | ||
return path.extname(file).toUpperCase(); | ||
return path.extname(file).toUpperCase() | ||
} | ||
|
||
function bigBas (file) { | ||
return path.basename(file).toUpperCase(); | ||
return path.basename(file).toUpperCase() | ||
} | ||
|
||
module.exports = { | ||
bigBar: bigBar | ||
, bigRab: bigRab | ||
, bigExt: bigExt | ||
, bigBas: bigBas | ||
}; | ||
bigBar: bigBar, | ||
bigRab: bigRab, | ||
bigExt: bigExt, | ||
bigBas: bigBas | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
var fooRequires = 0; | ||
var fooRequires = 0 | ||
module.exports = { | ||
fooRequires: function () { return fooRequires; } | ||
, incFooRequires: function () { fooRequires++; } | ||
}; | ||
fooRequires: function () { return fooRequires }, | ||
incFooRequires: function () { fooRequires++ } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,44 @@ | ||
"use strict"; | ||
'use strict' | ||
|
||
require('../example-utils').listModuleAndTests(__dirname + '/foo.js', __filename); | ||
var path = require('path') | ||
|
||
require('../example-utils').listModuleAndTests(path.resolve(__dirname, '/foo.js'), __filename) | ||
|
||
// Overriding callbacks that would normally be async will cause them to call back immediately | ||
// Thus allowing you to run synchronous tests against async APIs. | ||
|
||
var proxyquire = require('../..') | ||
, assert = require('assert') | ||
, readdirError = new Error('some error') | ||
, fsStub = { } | ||
, calledBack | ||
; | ||
var proxyquire = require('../..') | ||
var assert = require('assert') | ||
var readdirError = new Error('some error') | ||
var fsStub = { } | ||
var calledBack | ||
|
||
var foo = proxyquire('./foo', { fs: fsStub }); | ||
var foo = proxyquire('./foo', { fs: fsStub }) | ||
|
||
/* | ||
* Test caps locking of returned files | ||
*/ | ||
fsStub.readdir = function (dir, cb) { cb(null, [ 'file1', 'file2' ]); }; | ||
fsStub.readdir = function (dir, cb) { cb(null, [ 'file1', 'file2' ]) } | ||
|
||
calledBack = false; | ||
calledBack = false | ||
foo.filesAllCaps('./somedir', function (err, files) { | ||
assert.equal(err, null); | ||
assert.equal(files[0], 'FILE1'); | ||
assert.equal(files[1], 'FILE2'); | ||
calledBack = true; | ||
}); | ||
assert.equal(err, null) | ||
assert.equal(files[0], 'FILE1') | ||
assert.equal(files[1], 'FILE2') | ||
|
||
calledBack = true | ||
}) | ||
|
||
// fs.readdir and thus filesAllCaps calls back before we get here, which means the code ran synchronously | ||
assert(calledBack); | ||
assert(calledBack) | ||
|
||
/* | ||
* Test error propagation | ||
*/ | ||
fsStub.readdir = function (dir, cb) { cb(readdirError); }; | ||
fsStub.readdir = function (dir, cb) { cb(readdirError) } | ||
|
||
foo.filesAllCaps('./somedir', function (err, files) { | ||
assert.equal(err, readdirError); | ||
}); | ||
assert.equal(err, readdirError) | ||
}) | ||
|
||
console.log('*** All asserts passed ***'); | ||
console.log('*** All asserts passed ***') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
var fs = require('fs'); | ||
var fs = require('fs') | ||
|
||
module.exports.filesAllCaps = function (dir, cb) { | ||
module.exports.filesAllCaps = function (dir, cb) { | ||
fs.readdir(dir, function (err, files) { | ||
if (err) cb(err); | ||
else cb ( | ||
if (err) cb(err) | ||
else { | ||
cb( | ||
null | ||
, files.map(function (f) { return f.toUpperCase(); }) | ||
); | ||
}); | ||
}; | ||
, files.map(function (f) { return f.toUpperCase() }) | ||
) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,17 @@ | ||
var fs = require('fs'); | ||
var fs = require('fs') | ||
|
||
module.exports.listModuleAndTests = function (module, tests) { | ||
|
||
console.log( | ||
'\n**********\n' + | ||
'* Module:*\n' + | ||
'* Module:*\n' + | ||
'**********\n\n' + | ||
fs.readFileSync(module).toString() | ||
); | ||
) | ||
|
||
console.log( | ||
'**********\n' + | ||
'* Tests: *\n' + | ||
'* Tests: *\n' + | ||
'**********\n' + | ||
fs.readFileSync(tests).toString() | ||
); | ||
|
||
}; | ||
fs.readFileSync(tests).toString() | ||
) | ||
} |
Oops, something went wrong.