[foozzie] Properly mock out Date
The old proxy only mocked out constructor calls and didn't intercept function application. It also kept the original constructor property, through which non-mocked dates could be constructed again. BUG=chromium:697870 NOTRY=true R=mstarzinger@chromium.org,yangguo@chromium.org Change-Id: Icb4ef22342424f95463a7a9c57fa0bb8d910ac19 Reviewed-on: https://chromium-review.googlesource.com/448564 Reviewed-by: Yang Guo <yangguo@chromium.org> Commit-Queue: Michael Achenbach <machenbach@chromium.org> Cr-Commit-Position: refs/heads/master@{#43569}
This commit is contained in:
parent
4f426e104d
commit
bf84d924c3
@ -32,14 +32,25 @@ var __PrettyPrint = function __PrettyPrint(msg) { print(msg); };
|
||||
}
|
||||
|
||||
var origDate = Date;
|
||||
var constructDate = function(args) {
|
||||
if (args.length > 0) {
|
||||
var result = new (
|
||||
Function.prototype.bind.apply(origDate, [null].concat(args)));
|
||||
} else {
|
||||
var result = new origDate(mockDateNow());
|
||||
}
|
||||
result.constructor = function(...args) { return constructDate(args); }
|
||||
Object.defineProperty(
|
||||
result, "constructor", { configurable: false, writable: false });
|
||||
return result
|
||||
}
|
||||
|
||||
var handler = {
|
||||
construct: function(target, args, newTarget) {
|
||||
if (args.length > 0) {
|
||||
return new (
|
||||
Function.prototype.bind.apply(origDate, [null].concat(args)));
|
||||
} else {
|
||||
return new origDate(mockDateNow());
|
||||
}
|
||||
apply: function (target, thisArg, args) {
|
||||
return constructDate(args)
|
||||
},
|
||||
construct: function (target, args, newTarget) {
|
||||
return constructDate(args)
|
||||
},
|
||||
get: function(target, property, receiver) {
|
||||
if (property == "now") {
|
||||
|
Loading…
Reference in New Issue
Block a user