Skip to content

Commit

Permalink
Added tests to ensure that functions throw if target has wrong type
Browse files Browse the repository at this point in the history
  • Loading branch information
Victorystick committed Jan 4, 2015
1 parent a4a7d6e commit 1c9a82e
Showing 1 changed file with 59 additions and 16 deletions.
75 changes: 59 additions & 16 deletions test/reflect.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ describe('Reflect', function () {
}
});

var testCallableThrow = function (func) {
[null, undefined, 1, 'string', true, [], {}].forEach(function (item) {
expect(func.bind(null, item)).to.throw(TypeError);
});
};

var testPrimitiveThrow = function (func) {
[null, undefined, 1, 'string', true].forEach(function (item) {
expect(func.bind(null, item)).to.throw(TypeError);
});
};

it('is on the exported object', function () {
expect(exported.Reflect).to.equal(Reflect);
});
Expand All @@ -35,6 +47,12 @@ describe('Reflect', function () {
expect(typeof Reflect.apply).to.equal('function');
});

it('throws if target isn\'t callable', function () {
testCallableThrow(function (item) {
return Reflect.apply(item, null, []);
});
});

expect(Reflect.apply(Array.prototype.push, [1, 2], [3, 4, 5])).to.equal(5);
});

Expand Down Expand Up @@ -80,6 +98,12 @@ describe('Reflect', function () {
expect(typeof Reflect.deleteProperty).to.equal('function');
});

it('throws if the target isn\'t an object', function () {
testPrimitiveThrow(function (item) {
return Reflect.deleteProperty(item, 'prop');
});
});

it('returns true for success and false for failure', function () {
var o = { a: 1 };

Expand Down Expand Up @@ -213,15 +237,22 @@ describe('Reflect', function () {
expect(typeof Reflect.getOwnPropertyDescriptor).to.equal('function');
});

it('throws if the target isn\'t an object', function () {
testPrimitiveThrow(function (item) {
return Reflect.getOwnPropertyDescriptor(item, 'prop');
});
});
});

describe('Reflect.getPrototypeOf()', function () {
it('is a function', function () {
expect(typeof Reflect.getPrototypeOf).to.equal('function');
});

it('can get prototypes', function () {

it('throws if the target isn\'t an object', function () {
testPrimitiveThrow(function (item) {
return Reflect.getPrototypeOf(item);
});
});
});

Expand All @@ -230,11 +261,9 @@ describe('Reflect', function () {
expect(typeof Reflect.has).to.equal('function');
});

it('throws on primitives', function () {
[null, undefined, 1, 'string', true].forEach(function (item) {
expect(function () {
return Reflect.has(item, 'property', 'value');
}).to.throw(TypeError);
it('throws if the target isn\'t an object', function () {
testPrimitiveThrow(function (item) {
return Reflect.has(item, 'prop');
});
});

Expand Down Expand Up @@ -279,13 +308,25 @@ describe('Reflect', function () {
expect(Reflect.isExtensible({})).to.equal(true);
expect(Reflect.isExtensible(Object.preventExtensions({}))).to.equal(false);
});

it('throws if the target isn\'t an object', function () {
testPrimitiveThrow(function (item) {
return Reflect.isExtensible(item);
});
});
});

describe('Reflect.ownKeys()', function () {
it('is a function', function () {
expect(typeof Reflect.ownKeys).to.equal('function');
});

it('throws if the target isn\'t an object', function () {
testPrimitiveThrow(function (item) {
return Reflect.ownKeys(item);
});
});

it('should return the same result as Object.keys()', function () {
var obj = { foo: 1, bar: 2};

Expand All @@ -298,6 +339,12 @@ describe('Reflect', function () {
expect(typeof Reflect.preventExtensions).to.equal('function');
});

it('throws if the target isn\'t an object', function () {
testPrimitiveThrow(function (item) {
return Reflect.preventExtensions(item);
});
});

it('prevents extensions on objects', function () {
var obj = {};
Reflect.preventExtensions(obj);
Expand All @@ -310,11 +357,9 @@ describe('Reflect', function () {
expect(typeof Reflect.set).to.equal('function');
});

it('throws on null and undefined', function () {
[null, undefined].forEach(function (item) {
expect(function () {
return Reflect.set(item, 'property', 'value');
}).to.throw(TypeError);
it('throws if the target isn\'t an object', function () {
testPrimitiveThrow(function (item) {
return Reflect.set(item, 'prop', 'value');
});
});
});
Expand All @@ -325,10 +370,8 @@ describe('Reflect', function () {
});

it('throws if the target isn\'t an object', function () {
[null, undefined, 1, 'string', true].forEach(function (item) {
expect(function () {
return Reflect.setPrototypeOf(item, null);
}).to.throw(TypeError);
testPrimitiveThrow(function (item) {
return Reflect.setPrototypeOf(item, null);
});
});

Expand Down

0 comments on commit 1c9a82e

Please sign in to comment.