Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/twbs/rewire.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohannes <mail@johannesewald.de>2012-06-03 23:01:29 +0400
committerJohannes <mail@johannesewald.de>2012-06-03 23:01:29 +0400
commitd4eea33555f4e67be674c046ff1d1a9a460af94a (patch)
tree60672c8764687cd532f219dbb08e8c6d6f239548
parent07496ad0283cf5cc2184ecdf57b7d80cdc8348eb (diff)
changed injection behaviour
-rw-r--r--lib/getInjectionSrc.js28
-rw-r--r--lib/getMonkeyPatchSrc.js33
-rw-r--r--lib/trick.js4
-rw-r--r--test/getInjectionSrc.test.js24
-rw-r--r--test/getMonkeyPatchSrc.test.js30
-rw-r--r--test/trick.test.js5
6 files changed, 57 insertions, 67 deletions
diff --git a/lib/getInjectionSrc.js b/lib/getInjectionSrc.js
new file mode 100644
index 0000000..67c15ec
--- /dev/null
+++ b/lib/getInjectionSrc.js
@@ -0,0 +1,28 @@
+"use strict"; // run code in ES5 strict mode
+
+var toSrc = require("toSrc");
+
+function getMonkeyPatchSrc(obj) {
+ function walkObj(obj, level) {
+ var key,
+ value,
+ src = "";
+
+ for (key in obj) {
+ if (obj.hasOwnProperty(key)) {
+ value = obj[key];
+ if (level === 0) {
+ src += "var "; // on the top level, we need a var statement to override variables
+ }
+ src += key + "=" + toSrc(value, 9999) + ";";
+ }
+ }
+
+
+ return src;
+ }
+
+ return walkObj(obj, 0);
+}
+
+module.exports = getMonkeyPatchSrc; \ No newline at end of file
diff --git a/lib/getMonkeyPatchSrc.js b/lib/getMonkeyPatchSrc.js
deleted file mode 100644
index 408b6ee..0000000
--- a/lib/getMonkeyPatchSrc.js
+++ /dev/null
@@ -1,33 +0,0 @@
-"use strict"; // run code in ES5 strict mode
-
-var toSrc = require("toSrc");
-
-function getMonkeyPatchSrc(obj) {
- function walkObj(obj, level) {
- var key,
- value,
- src = "";
-
- for (key in obj) {
- if (obj.hasOwnProperty(key)) {
- value = obj[key];
- if (typeof value === "object" && Array.isArray(value) === false) {
- src += key + ".";
- src += walkObj(value, level + 1);
- } else {
- if (level === 0) {
- src += "var "; // in the top level, we need a var statement to override variables
- }
- src += key + "=" + toSrc(value, 9999) + ";";
- }
- }
- }
-
-
- return src;
- }
-
- return walkObj(obj, 0);
-}
-
-module.exports = getMonkeyPatchSrc; \ No newline at end of file
diff --git a/lib/trick.js b/lib/trick.js
index 36644be..89cbe32 100644
--- a/lib/trick.js
+++ b/lib/trick.js
@@ -4,7 +4,7 @@ var Module = require("module"),
nodeWrapper0 = Module.wrapper[0], // caching original wrapper
nodeWrapper1 = Module.wrapper[1],
getLeakingSrc = require("./getLeakingSrc.js"),
- getMonkeyPatchSrc = require("./getMonkeyPatchSrc.js");
+ getInjectionSrc = require("./getInjectionSrc.js");
function restoreOriginalWrappers() {
Module.wrapper[0] = nodeWrapper0;
@@ -37,7 +37,7 @@ function trick(parentModule, filename, mocks, injections, leaks, cache) {
// Prepare module for injection
if (typeof injections === "object") {
- Module.wrapper[0] = nodeWrapper0 + getMonkeyPatchSrc(injections);
+ Module.wrapper[0] = nodeWrapper0 + getInjectionSrc(injections);
} else if (typeof injections === "string") {
Module.wrapper[0] = nodeWrapper0 + injections;
}
diff --git a/test/getInjectionSrc.test.js b/test/getInjectionSrc.test.js
new file mode 100644
index 0000000..0ec9a49
--- /dev/null
+++ b/test/getInjectionSrc.test.js
@@ -0,0 +1,24 @@
+"use strict"; // run code in ES5 strict mode
+
+var expect = require("expect.js"),
+ getInjectionSrc = require("../lib/getInjectionSrc.js");
+
+describe("#getMonkeyPatchSrc", function () {
+ it("should return ''", function () {
+ var expectedSrc = "",
+ subject = {};
+
+ expect(getInjectionSrc(subject)).to.be(expectedSrc);
+ });
+ it("should return 'var process={\"argv\": [\"myArg1\", \"myArg2\"]};var console=456;'", function () {
+ var expectedSrc = "var process={\"argv\": [\"myArg1\", \"myArg2\"]};var console=456;",
+ subject = {
+ process: {
+ argv: ["myArg1", "myArg2"]
+ },
+ console: 456
+ };
+
+ expect(getInjectionSrc(subject)).to.be(expectedSrc);
+ });
+}); \ No newline at end of file
diff --git a/test/getMonkeyPatchSrc.test.js b/test/getMonkeyPatchSrc.test.js
deleted file mode 100644
index d7cecaa..0000000
--- a/test/getMonkeyPatchSrc.test.js
+++ /dev/null
@@ -1,30 +0,0 @@
-"use strict"; // run code in ES5 strict mode
-
-var expect = require("expect.js"),
- getMonkeyPatchSrc = require("../lib/getMonkeyPatchSrc.js");
-
-describe("#getMonkeyPatchSrc", function () {
- it("should return ''", function () {
- var expectedSrc = "",
- subject = {};
-
- expect(getMonkeyPatchSrc(subject)).to.be(expectedSrc);
- });
- it("should return 'process.argv=[\"myArg1\", \"myArg2\"];var console=456;'", function () {
- var expectedSrc = "process.argv=[\"myArg1\", \"myArg2\"];var console=456;",
- subject = {
- process: {
- argv: ["myArg1", "myArg2"]
- },
- console: 456
- };
-
- expect(getMonkeyPatchSrc(subject)).to.be(expectedSrc);
- });
- it("should return 'level1.level2.level3.level4.level5=true;", function () {
- var expectedSrc = "level1.level2.level3.level4.level5=true;",
- subject = {level1: {level2: {level3: {level4: {level5: true}}}}};
-
- expect(getMonkeyPatchSrc(subject)).to.be(expectedSrc);
- });
-}); \ No newline at end of file
diff --git a/test/trick.test.js b/test/trick.test.js
index bb6633b..92055bc 100644
--- a/test/trick.test.js
+++ b/test/trick.test.js
@@ -57,8 +57,9 @@ describe("#trick", function () {
};
tricked = trick("./testModules/A/moduleA.js", null, injections);
- expect(tricked.process).to.be(process); // the process object itself should not be changed
- expect(tricked.process.argv).to.be.eql(injections.process.argv);
+ expect(tricked.process).not.to.be(process);
+ expect(process.argv).not.to.eql(injections.process.argv);
+ expect(tricked.process).to.eql(injections.process);
expect(tricked.console).to.be(123);
});
it("should inject custom scripts", function () {