-
-
Notifications
You must be signed in to change notification settings - Fork 771
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor; introduce sinon.wrapMethod to handle wrapping of methods on…
… object with a restore() option
- Loading branch information
Showing
3 changed files
with
38 additions
and
19 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,3 +1,30 @@ | ||
var sinon = (function () { | ||
return {}; | ||
function wrapMethod (object, property, method) { | ||
if (!object) { | ||
throw new TypeError("Should wrap property of object"); | ||
} | ||
|
||
if (typeof method != "function") { | ||
throw new TypeError("Method wrapper should be function"); | ||
} | ||
|
||
var wrappedMethod = object[property]; | ||
var type = typeof wrappedMethod; | ||
|
||
if (!!wrappedMethod && type != "function") { | ||
throw new TypeError("Attempted to wrap " + type + " as function"); | ||
} | ||
|
||
object[property] = method; | ||
|
||
method.restore = function () { | ||
object[property] = wrappedMethod; | ||
}; | ||
|
||
return method; | ||
} | ||
|
||
return { | ||
wrapMethod: wrapMethod | ||
}; | ||
}()); |
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
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