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-12 01:29:10 +0400
committerJohannes <mail@johannesewald.de>2012-06-12 01:29:10 +0400
commit0af1dcb6b2ccbc7ec0ae757549688e666cf569a7 (patch)
tree4b3854b937ff1f05bf8677c8fc1547fa733e6feb /test/__get__.test.js
parent75f6a7765a09344dab54a4a2ca99ac12f547a75d (diff)
- changed APIv0.2.0
- introduced __set__ and __get__ to rewired modules
Diffstat (limited to 'test/__get__.test.js')
-rw-r--r--test/__get__.test.js79
1 files changed, 79 insertions, 0 deletions
diff --git a/test/__get__.test.js b/test/__get__.test.js
new file mode 100644
index 0000000..6506e84
--- /dev/null
+++ b/test/__get__.test.js
@@ -0,0 +1,79 @@
+"use strict"; // run code in ES5 strict mode
+
+var expect = require("expect.js"),
+ vm = require("vm"),
+ __get__ = require("../lib/__get__.js"),
+
+ expectReferenceError = expectError(ReferenceError),
+ expectTypeError = expectError(TypeError);
+
+function expectError(ErrConstructor) {
+ return function expectReferenceError(err) {
+ expect(err.constructor.name).to.be(ErrConstructor.name);
+ };
+}
+
+
+describe("__get__", function () {
+ var moduleFake;
+
+ beforeEach(function () {
+ moduleFake = {
+ myNumber: 0,
+ myObj: {}
+ };
+
+ vm.runInNewContext(
+ "__get__ = " + __get__.toString() + "; " +
+ "setNumber = function (value) { myNumber = value; }; " +
+ "setObj = function (value) { myObj = value; }; ",
+ moduleFake
+ );
+ });
+ it("should return the initial value", function () {
+ expect(moduleFake.__get__("myNumber")).to.be(0);
+ expect(moduleFake.__get__("myObj")).to.eql({});
+ });
+ it("should return the changed value of the number", function () {
+ var newObj = { hello: "hello" };
+
+ moduleFake.setNumber(2);
+ moduleFake.setObj(newObj);
+ expect(moduleFake.__get__("myNumber")).to.be(2);
+ expect(moduleFake.__get__("myObj")).to.be(newObj);
+ });
+ it("should throw a ReferenceError when getting not existing vars", function () {
+ expect(function () {
+ moduleFake.__get__("blabla");
+ }).to.throwException(expectReferenceError);
+ });
+ it("should throw a TypeError when passing misfitting params", function () {
+ expect(function () {
+ moduleFake.__get__();
+ }).to.throwException(expectTypeError);
+ expect(function () {
+ moduleFake.__get__(undefined);
+ }).to.throwException(expectTypeError);
+ expect(function () {
+ moduleFake.__get__(null);
+ }).to.throwException(expectTypeError);
+ expect(function () {
+ moduleFake.__get__(true);
+ }).to.throwException(expectTypeError);
+ expect(function () {
+ moduleFake.__get__(2);
+ }).to.throwException(expectTypeError);
+ expect(function () {
+ moduleFake.__get__("");
+ }).to.throwException(expectTypeError);
+ expect(function () {
+ moduleFake.__get__([]);
+ }).to.throwException(expectTypeError);
+ expect(function () {
+ moduleFake.__get__({});
+ }).to.throwException(expectTypeError);
+ expect(function () {
+ moduleFake.__get__(function () {});
+ }).to.throwException(expectTypeError);
+ });
+}); \ No newline at end of file