Skip to content

Commit

Permalink
Implement mockable mainModule.
Browse files Browse the repository at this point in the history
Closes #185.
  • Loading branch information
flatheadmill committed Jul 31, 2016
1 parent 0dffcd8 commit 1f7b315
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
6 changes: 3 additions & 3 deletions arguable.js
Expand Up @@ -62,16 +62,16 @@ module.exports = function () {
var ee = options.events
if (ee == null) {
ee = new events.EventEmitter
ee.mainModule == options.mainModule || process.mainModule
ee.connected == ('connected' in options) ? options.connected : true
ee.mainModule = options.mainModule || process.mainModule
ee.connected = ('connected' in options) ? options.connected : true
ee.disconnect = function () { this.connected = false }
}
var program = new Program(usage, parameters, {
module: module,
stdout: createStream(options.stdout),
stdin: createStream(options.stdin),
stderr: createStream(options.stderr),
process: ee,
events: ee,
send: send || null,
env: options.env || {}
})
Expand Down
6 changes: 5 additions & 1 deletion program.js
Expand Up @@ -244,6 +244,10 @@ Program.prototype.__defineGetter__('connected', function () {
return this._process.connected
})

Program.prototype.__defineGetter__('mainModule', function () {
return this._process.mainModule
})

// Format a message using the string tables provided in the usage message.
Program.prototype.format = function (key) {
return this._usage.format(this.lang, key, slice.call(arguments, 1))
Expand Down Expand Up @@ -336,7 +340,7 @@ Program.prototype.delegate = cadence(function (async, format, argv) {
env: this.env,
stdin: this.stdin,
stderr: this.stderr,
events: this,
process: this,
send: this.send
}, async())
})
Expand Down
3 changes: 3 additions & 0 deletions t/fixtures/main.js
@@ -0,0 +1,3 @@
require('../..')(module, function (program, callback) {
callback(null, program.mainModule)
})
9 changes: 8 additions & 1 deletion t/index.t.js
@@ -1,10 +1,11 @@
require('proof')(9, require('cadence')(prove))
require('proof')(11, require('cadence')(prove))

function prove (async, assert) {
var echo1 = require('./fixtures/echo-1')
var echo2 = require('./fixtures/echo-2')
var send = require('./fixtures/send')
var parameters = require('./fixtures/parameters')
var main = require('./fixtures/main')
var stream = require('stream')
var events = require('events')
var stdout
Expand Down Expand Up @@ -39,5 +40,11 @@ function prove (async, assert) {
parameters([[{ three: 3 }]], {}, async())
}, function (result) {
assert(result, { one: 1, two: 2, three: 3 }, 'nested array argument')
main([], {}, async())
}, function (result) {
assert(result === process.mainModule, 'main module')
main([], { mainModule: main }, async())
}, function (result) {
assert(result === main, 'main module')
})
}

0 comments on commit 1f7b315

Please sign in to comment.