mirror of
https://github.com/musix-org/musix-oss
synced 2025-06-17 01:16:00 +00:00
Modules
This commit is contained in:
35
node_modules/stubs/index.js
generated
vendored
Normal file
35
node_modules/stubs/index.js
generated
vendored
Normal file
@ -0,0 +1,35 @@
|
||||
'use strict'
|
||||
|
||||
module.exports = function stubs(obj, method, cfg, stub) {
|
||||
if (!obj || !method || !obj[method])
|
||||
throw new Error('You must provide an object and a key for an existing method')
|
||||
|
||||
if (!stub) {
|
||||
stub = cfg
|
||||
cfg = {}
|
||||
}
|
||||
|
||||
stub = stub || function() {}
|
||||
|
||||
cfg.callthrough = cfg.callthrough || false
|
||||
cfg.calls = cfg.calls || 0
|
||||
|
||||
var norevert = cfg.calls === 0
|
||||
|
||||
var cached = obj[method].bind(obj)
|
||||
|
||||
obj[method] = function() {
|
||||
var args = [].slice.call(arguments)
|
||||
var returnVal
|
||||
|
||||
if (cfg.callthrough)
|
||||
returnVal = cached.apply(obj, args)
|
||||
|
||||
returnVal = stub.apply(obj, args) || returnVal
|
||||
|
||||
if (!norevert && --cfg.calls === 0)
|
||||
obj[method] = cached
|
||||
|
||||
return returnVal
|
||||
}
|
||||
}
|
56
node_modules/stubs/package.json
generated
vendored
Normal file
56
node_modules/stubs/package.json
generated
vendored
Normal file
@ -0,0 +1,56 @@
|
||||
{
|
||||
"_args": [
|
||||
[
|
||||
"stubs@3.0.0",
|
||||
"C:\\Users\\matia\\Musix"
|
||||
]
|
||||
],
|
||||
"_from": "stubs@3.0.0",
|
||||
"_id": "stubs@3.0.0",
|
||||
"_inBundle": false,
|
||||
"_integrity": "sha1-6NK6H6nJBXAwPAMLaQD31fiavls=",
|
||||
"_location": "/stubs",
|
||||
"_optional": true,
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"type": "version",
|
||||
"registry": true,
|
||||
"raw": "stubs@3.0.0",
|
||||
"name": "stubs",
|
||||
"escapedName": "stubs",
|
||||
"rawSpec": "3.0.0",
|
||||
"saveSpec": null,
|
||||
"fetchSpec": "3.0.0"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"/stream-events"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/stubs/-/stubs-3.0.0.tgz",
|
||||
"_spec": "3.0.0",
|
||||
"_where": "C:\\Users\\matia\\Musix",
|
||||
"author": {
|
||||
"name": "Stephen Sawchuk"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/stephenplusplus/stubs/issues"
|
||||
},
|
||||
"description": "Easy method stubber.",
|
||||
"devDependencies": {
|
||||
"tess": "^1.0.0"
|
||||
},
|
||||
"homepage": "https://github.com/stephenplusplus/stubs",
|
||||
"keywords": [
|
||||
"stubs"
|
||||
],
|
||||
"license": "MIT",
|
||||
"main": "index.js",
|
||||
"name": "stubs",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/stephenplusplus/stubs.git"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "node ./test"
|
||||
},
|
||||
"version": "3.0.0"
|
||||
}
|
73
node_modules/stubs/readme.md
generated
vendored
Normal file
73
node_modules/stubs/readme.md
generated
vendored
Normal file
@ -0,0 +1,73 @@
|
||||
# stubs
|
||||
|
||||
> It's a simple stubber.
|
||||
|
||||
## About
|
||||
|
||||
For when you don't want to write the same thing over and over to cache a method and call an override, then revert it, and blah blah.
|
||||
|
||||
|
||||
## Use
|
||||
```sh
|
||||
$ npm install --save-dev stubs
|
||||
```
|
||||
```js
|
||||
var mylib = require('./lib/index.js')
|
||||
var stubs = require('stubs')
|
||||
|
||||
// make it a noop
|
||||
stubs(mylib, 'create')
|
||||
|
||||
// stub it out
|
||||
stubs(mylib, 'create', function() {
|
||||
// calls this instead
|
||||
})
|
||||
|
||||
// stub it out, but call the original first
|
||||
stubs(mylib, 'create', { callthrough: true }, function() {
|
||||
// call original method, then call this
|
||||
})
|
||||
|
||||
// use the stub for a while, then revert
|
||||
stubs(mylib, 'create', { calls: 3 }, function() {
|
||||
// call this 3 times, then use the original method
|
||||
})
|
||||
```
|
||||
|
||||
|
||||
## API
|
||||
|
||||
### stubs(object, method[[, opts], stub])
|
||||
|
||||
#### object
|
||||
- Type: Object
|
||||
|
||||
#### method
|
||||
- Type: String
|
||||
|
||||
Name of the method to stub.
|
||||
|
||||
#### opts
|
||||
- (optional)
|
||||
- Type: Object
|
||||
|
||||
##### opts.callthrough
|
||||
- (optional)
|
||||
- Type: Boolean
|
||||
- Default: `false`
|
||||
|
||||
Call the original method as well as the stub (if a stub is provided).
|
||||
|
||||
##### opts.calls
|
||||
- (optional)
|
||||
- Type: Number
|
||||
- Default: `0` (never revert)
|
||||
|
||||
Number of calls to allow the stub to receive until reverting to the original.
|
||||
|
||||
#### stub
|
||||
- (optional)
|
||||
- Type: Function
|
||||
- Default: `function() {}`
|
||||
|
||||
This method is called in place of the original method. If `opts.callthrough` is `true`, this method is called *after* the original method is called as well.
|
172
node_modules/stubs/test.js
generated
vendored
Normal file
172
node_modules/stubs/test.js
generated
vendored
Normal file
@ -0,0 +1,172 @@
|
||||
'use strict'
|
||||
|
||||
var assert = require('assert')
|
||||
var stubs = require('./')
|
||||
var tess = require('tess')
|
||||
|
||||
tess('init', function(it) {
|
||||
it('is a function', function() {
|
||||
assert.equal(typeof stubs, 'function')
|
||||
})
|
||||
|
||||
it('throws without obj || method', function() {
|
||||
assert.throws(function() {
|
||||
stubs()
|
||||
}, /must provide/)
|
||||
|
||||
assert.throws(function() {
|
||||
stubs({})
|
||||
}, /must provide/)
|
||||
})
|
||||
})
|
||||
|
||||
tess('stubs', function(it) {
|
||||
it('stubs a method with a noop', function() {
|
||||
var originalCalled = false
|
||||
|
||||
var obj = {}
|
||||
obj.method = function() {
|
||||
originalCalled = true
|
||||
}
|
||||
|
||||
stubs(obj, 'method')
|
||||
|
||||
obj.method()
|
||||
|
||||
assert(!originalCalled)
|
||||
})
|
||||
|
||||
it('accepts an override', function() {
|
||||
var originalCalled = false
|
||||
|
||||
var obj = {}
|
||||
obj.method = function() {
|
||||
originalCalled = true
|
||||
}
|
||||
|
||||
var replacementCalled = false
|
||||
stubs(obj, 'method', function() {
|
||||
replacementCalled = true
|
||||
})
|
||||
|
||||
obj.method()
|
||||
assert(!originalCalled)
|
||||
assert(replacementCalled)
|
||||
})
|
||||
|
||||
it('returns value of overridden method call', function() {
|
||||
var overriddenUniqueVal = {}
|
||||
|
||||
var obj = {}
|
||||
obj.method = function() {}
|
||||
|
||||
stubs(obj, 'method', { callthrough: true }, function() {
|
||||
return overriddenUniqueVal
|
||||
})
|
||||
|
||||
assert.strictEqual(obj.method(), overriddenUniqueVal)
|
||||
})
|
||||
|
||||
it('calls through to original method', function() {
|
||||
var originalCalled = false
|
||||
|
||||
var obj = {}
|
||||
obj.method = function() {
|
||||
originalCalled = true
|
||||
}
|
||||
|
||||
var replacementCalled = false
|
||||
stubs(obj, 'method', { callthrough: true }, function() {
|
||||
replacementCalled = true
|
||||
})
|
||||
|
||||
obj.method()
|
||||
assert(originalCalled)
|
||||
assert(replacementCalled)
|
||||
})
|
||||
|
||||
it('returns value of original method call', function() {
|
||||
var uniqueVal = Date.now()
|
||||
|
||||
var obj = {}
|
||||
obj.method = function() {
|
||||
return uniqueVal
|
||||
}
|
||||
|
||||
stubs(obj, 'method', { callthrough: true }, function() {})
|
||||
|
||||
assert.equal(obj.method(), uniqueVal)
|
||||
})
|
||||
|
||||
it('returns calls override and returns original value', function() {
|
||||
var uniqueVal = {}
|
||||
var overrideWasCalled = false
|
||||
|
||||
var obj = {}
|
||||
obj.method = function() {
|
||||
return uniqueVal
|
||||
}
|
||||
|
||||
stubs(obj, 'method', { callthrough: true }, function() {
|
||||
// does not return anything
|
||||
overrideWasCalled = true
|
||||
})
|
||||
|
||||
assert.strictEqual(obj.method(), uniqueVal)
|
||||
assert.strictEqual(overrideWasCalled, true)
|
||||
})
|
||||
|
||||
it('returns value of overridden method call', function() {
|
||||
var uniqueVal = {}
|
||||
var overriddenUniqueVal = {}
|
||||
|
||||
var obj = {}
|
||||
obj.method = function() {
|
||||
return uniqueVal
|
||||
}
|
||||
|
||||
stubs(obj, 'method', { callthrough: true }, function() {
|
||||
return overriddenUniqueVal
|
||||
})
|
||||
|
||||
assert.strictEqual(obj.method(), overriddenUniqueVal)
|
||||
})
|
||||
|
||||
it('stops calling stub after n calls', function() {
|
||||
var timesToCall = 5
|
||||
var timesCalled = 0
|
||||
|
||||
var obj = {}
|
||||
obj.method = function() {
|
||||
assert.equal(timesCalled, timesToCall)
|
||||
}
|
||||
|
||||
stubs(obj, 'method', { calls: timesToCall }, function() {
|
||||
timesCalled++
|
||||
})
|
||||
|
||||
obj.method() // 1 (stub)
|
||||
obj.method() // 2 (stub)
|
||||
obj.method() // 3 (stub)
|
||||
obj.method() // 4 (stub)
|
||||
obj.method() // 5 (stub)
|
||||
obj.method() // 6 (original)
|
||||
})
|
||||
|
||||
it('calls stub in original context of obj', function() {
|
||||
var secret = 'brownies'
|
||||
|
||||
function Class() {
|
||||
this.method = function() {}
|
||||
this.secret = secret
|
||||
}
|
||||
|
||||
var cl = new Class()
|
||||
|
||||
stubs(cl, 'method', function() {
|
||||
assert.equal(this.secret, secret)
|
||||
})
|
||||
|
||||
cl.method()
|
||||
})
|
||||
})
|
Reference in New Issue
Block a user