Skip to content

Commit

Permalink
Merge pull request #6 from Tiliqua/instanceof-fix
Browse files Browse the repository at this point in the history
Fixed instanceof assertion when expected argument is not a valid class
  • Loading branch information
norberttech committed May 7, 2016
2 parents 9b8c4b0 + cea6071 commit 48d4a25
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
10 changes: 6 additions & 4 deletions bin/JSAssert/Assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ var Assert = function () {

/**
* @param {object} objectValue
* @param {function} instance
* @param {function} expectedInstance
* @param {string} [message]
*/
value: function instanceOf(objectValue, instance) {
value: function instanceOf(objectValue, expectedInstance) {
var message = arguments.length <= 2 || arguments[2] === undefined ? "" : arguments[2];

this.string(message, "Custom error message passed to Assert.instanceOf needs to be a valid string.");
Expand All @@ -38,8 +38,10 @@ var Assert = function () {
throw _InvalidValueException2.default.expected("object", objectValue, message);
}

if (!(objectValue instanceof instance)) {
throw _InvalidValueException2.default.expected(instance.name, objectValue, message.length ? message : "Expected instance of \"${expected}\" but got \"${received}\".");
this.isFunction(expectedInstance, "Expected argument needs to be a valid class instead of that got \"${received}\". Most likely there is typo in class name or file with class is missing export declaration.");

if (!(objectValue instanceof expectedInstance)) {
throw _InvalidValueException2.default.expected(expectedInstance.name, objectValue, message.length ? message : "Expected instance of \"${expected}\" but got \"${received}\".");
}
}

Expand Down
6 changes: 6 additions & 0 deletions spec/Doubles/ObjectWithNoExportStub.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';

class ObjectWithNoExportStub
{

}
11 changes: 9 additions & 2 deletions spec/JSAssert/AssertSpec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import Assert from '../../src/JSAssert/Assert';

import ObjectWithNoExportStub from './../Doubles/ObjectWithNoExportStub';

describe("Assert", () => {
it("compares instance of", () => {
Assert.instanceOf(new String("string"), String);
});

it("compares instance of when object has no export", () => {
expect(() => { Assert.instanceOf(new String("test"), ObjectWithNoExportStub); })
.toThrow(`Expected argument needs to be a valid class instead of that got "object[{}]". Most likely there is typo in class name or file with class is missing export declaration.`);
});

it ("throws error when asserting instance of non object", () => {
expect(() => {Assert.instanceOf(1, String)}).toThrow('Expected object but got "int[1]".');
expect(() => {Assert.instanceOf(new Number(2), String)}).toThrow('Expected instance of "String" but got "Number:int[2]".');
Expand Down Expand Up @@ -209,11 +216,11 @@ describe("Assert", () => {
expect(() => {Assert.notEmpty("")}).toThrow('Expected not empty value but got "string[""]".');
expect(() => {Assert.notEmpty("", 'custom message')}).toThrow('custom message');
});

it ("asserts json string", () => {
Assert.jsonString('{"key":"value"}');
});

it ("throws error when expected json string is not valid", () => {
expect(() => {Assert.jsonString('{"key":value"}')}).toThrow('Expected json string but got "string["{"key":value"}"]".');
expect(() => {Assert.jsonString('{"key":value"}', "custom message")}).toThrow('custom message');
Expand Down
15 changes: 10 additions & 5 deletions src/JSAssert/Assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,25 @@ export default class Assert
{
/**
* @param {object} objectValue
* @param {function} instance
* @param {function} expectedInstance
* @param {string} [message]
*/
static instanceOf(objectValue, instance, message = "")
static instanceOf(objectValue, expectedInstance, message = "")
{
this.string(message, "Custom error message passed to Assert.instanceOf needs to be a valid string.");

if (typeof objectValue !== 'object') {
throw InvalidValueException.expected("object", objectValue, message);
}

if (!(objectValue instanceof instance)) {
this.isFunction(
expectedInstance,
"Expected argument needs to be a valid class instead of that got \"${received}\". Most likely there is typo in class name or file with class is missing export declaration."
);

if (!(objectValue instanceof expectedInstance)) {
throw InvalidValueException.expected(
instance.name,
expectedInstance.name,
objectValue,
message.length ? message : "Expected instance of \"${expected}\" but got \"${received}\"."
);
Expand Down Expand Up @@ -279,7 +284,7 @@ export default class Assert
throw InvalidValueException.expected("even number", integerValue, message);
}
}

/**
* @param {string} stringValue
* @param {string} [message]
Expand Down

0 comments on commit 48d4a25

Please sign in to comment.