1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
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 = {
__filename: "some/file.js",
myNumber: 0,
myObj: {}
};
vm.runInNewContext(
"__get__ = " + __get__.toString() + "; " +
"setNumber = function (value) { myNumber = value; }; " +
"setObj = function (value) { myObj = value; }; ",
moduleFake,
__filename
);
});
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);
});
});
|