|
|
@@ -21,114 +21,37 @@
|
|
|
return load( context, moduleId, url );
|
|
|
};
|
|
|
|
|
|
+ // Extend ChaiJS with custom exception check.
|
|
|
require( [ '/ckeditor5/utils/ckeditorerror.js', 'chai' ], function( CKEditorError, chai ) {
|
|
|
CKEditorError = CKEditorError.default;
|
|
|
|
|
|
- chai.use( function( _chai, utils ) {
|
|
|
- var Assertion = chai.Assertion;
|
|
|
+ // Load extension and bind class used to check for inheritance.
|
|
|
+ chai.use( addThrowsCKEditorError.bind( null, CKEditorError ) );
|
|
|
|
|
|
- Assertion.addMethod( 'throwCKEditorError', assertThrows );
|
|
|
-
|
|
|
- function assertThrows( errMsg ) {
|
|
|
- var obj = utils.flag( this, 'object' ),
|
|
|
- actuallyGot = '' ,
|
|
|
- expectedThrown,
|
|
|
- thrown = false ,
|
|
|
- name = null ,
|
|
|
- thrownError = null,
|
|
|
- message;
|
|
|
-
|
|
|
- if ( arguments.length === 0 ) {
|
|
|
- errMsg = null;
|
|
|
- }
|
|
|
-
|
|
|
- try {
|
|
|
- obj();
|
|
|
- } catch ( err ) {
|
|
|
- // next, check constructor
|
|
|
- if (constructor) {
|
|
|
- this.assert(
|
|
|
- CKEditorError.isCKEditorError( err ),
|
|
|
- 'expected #{this} to throw #{exp} but #{act} was thrown',
|
|
|
- 'expected #{this} to not throw #{exp} but #{act} was thrown',
|
|
|
- 'CKEditorError',
|
|
|
- (err instanceof Error ? err.toString() : err)
|
|
|
- );
|
|
|
-
|
|
|
- if ( !errMsg ) {
|
|
|
- utils.flag( this, 'object', err );
|
|
|
- return this;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // next, check message
|
|
|
- message = utils.type( err ) === 'object' && 'message' in err ?
|
|
|
- err.message
|
|
|
- : '' + err;
|
|
|
-
|
|
|
- if ( ( message != null ) && errMsg && errMsg instanceof RegExp ) {
|
|
|
- this.assert(
|
|
|
- errMsg.exec( message ),
|
|
|
- 'expected #{this} to throw error matching #{exp} but got #{act}',
|
|
|
- 'expected #{this} to throw error not matching #{exp}',
|
|
|
- errMsg,
|
|
|
- message
|
|
|
- );
|
|
|
-
|
|
|
- utils.flag( this, 'object', err );
|
|
|
-
|
|
|
- return this;
|
|
|
- } else if ( ( message != null ) && errMsg && typeof errMsg == 'string' ) {
|
|
|
- this.assert(
|
|
|
- ~message.indexOf( errMsg ),
|
|
|
- 'expected #{this} to throw error including #{exp} but got #{act}',
|
|
|
- 'expected #{this} to throw error not including #{act}',
|
|
|
- errMsg,
|
|
|
- message
|
|
|
- );
|
|
|
-
|
|
|
- utils.flag( this, 'object', err );
|
|
|
-
|
|
|
- return this;
|
|
|
- } else {
|
|
|
- thrown = true;
|
|
|
- thrownError = err;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- expectedThrown = 'an error';
|
|
|
-
|
|
|
- if ( thrown ) {
|
|
|
- actuallyGot = ' but #{act} was thrown'
|
|
|
- }
|
|
|
-
|
|
|
- this.assert(
|
|
|
- thrown === true ,
|
|
|
- 'expected #{this} to throw ' + expectedThrown + actuallyGot ,
|
|
|
- 'expected #{this} to not throw ' + expectedThrown + actuallyGot ,
|
|
|
- 'CKEditorError',
|
|
|
- ( thrownError instanceof Error ? thrownError.toString() : thrownError )
|
|
|
- );
|
|
|
-
|
|
|
- utils.flag( this, 'object', thrownError );
|
|
|
- };
|
|
|
- } );
|
|
|
-
|
|
|
- chai.expect( function() {
|
|
|
+ // Self-test of the extension. Function names will help find those tests in case of regression.
|
|
|
+ chai.expect( function throwCKEditorErrorSelfTest() {
|
|
|
throw new CKEditorError();
|
|
|
} ).to.throwCKEditorError();
|
|
|
|
|
|
- chai.expect( function() {
|
|
|
+ chai.expect( function throwCKEditorErrorSelfTest() {
|
|
|
throw new Error();
|
|
|
} ).to.not.throwCKEditorError();
|
|
|
|
|
|
- chai.expect( function() {
|
|
|
+ chai.expect( function throwCKEditorErrorSelfTest() {
|
|
|
throw new CKEditorError( 'custom message' );
|
|
|
} ).to.throwCKEditorError( 'message' );
|
|
|
|
|
|
- chai.expect( function() {
|
|
|
+ chai.expect( function throwCKEditorErrorSelfTest() {
|
|
|
throw new CKEditorError( 'another msg' );
|
|
|
} ).to.throwCKEditorError( /msg/ );
|
|
|
+
|
|
|
+ chai.expect( function throwCKEditorErrorSelfTest() {
|
|
|
+ throw new CKEditorError( 'another msg' );
|
|
|
+ } ).to.throwCKEditorError( /^another/ );
|
|
|
+
|
|
|
+ chai.expect( function throwCKEditorErrorSelfTest() {
|
|
|
+ throw new CKEditorError( 'another msg' );
|
|
|
+ } ).to.throwCKEditorError( /msg$/ );
|
|
|
} );
|
|
|
|
|
|
// Reported: https://github.com/benderjs/benderjs/issues/248
|
|
|
@@ -156,4 +79,98 @@
|
|
|
function nonEmpty( str ) {
|
|
|
return !!str.length;
|
|
|
}
|
|
|
+
|
|
|
+ // Add `throwCKEditorError` chainable method to ChaiJS.
|
|
|
+ //
|
|
|
+ // @param {Object} CKEditorError Constructor of class checking for CKEditorError membership.
|
|
|
+ // @param {Object} _chai Chai instance.
|
|
|
+ // @param {Object} utils Chai extension utils.
|
|
|
+ // @returns {Object} Assertion
|
|
|
+ function addThrowsCKEditorError( CKEditorError, _chai, utils ) {
|
|
|
+ var Assertion = _chai.Assertion;
|
|
|
+
|
|
|
+ Assertion.addMethod( 'throwCKEditorError', assertThrowCKEditorError );
|
|
|
+
|
|
|
+ function assertThrowCKEditorError( errMsg ) {
|
|
|
+ // jshint validthis: true
|
|
|
+ // jscs:disable disallowMultipleVarDecl
|
|
|
+
|
|
|
+ var obj = utils.flag( this, 'object' ),
|
|
|
+ actuallyGot = '' ,
|
|
|
+ thrown = false ,
|
|
|
+ thrownError = null,
|
|
|
+ message;
|
|
|
+
|
|
|
+ if ( arguments.length === 0 ) {
|
|
|
+ errMsg = null;
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ obj();
|
|
|
+ } catch ( err ) {
|
|
|
+ this.assert(
|
|
|
+ CKEditorError.isCKEditorError( err ),
|
|
|
+ 'expected #{this} to throw #{exp} but #{act} was thrown',
|
|
|
+ 'expected #{this} to not throw #{exp} but #{act} was thrown',
|
|
|
+ 'CKEditorError',
|
|
|
+ ( err instanceof Error ? err.toString() : err )
|
|
|
+ );
|
|
|
+
|
|
|
+ // Set subject of assertion.
|
|
|
+ if ( !errMsg ) {
|
|
|
+ utils.flag( this, 'object', err );
|
|
|
+
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ // Check message of error.
|
|
|
+ message = utils.type( err ) === 'object' && 'message' in err ? err.message : err.toString();
|
|
|
+
|
|
|
+ if ( ( message !== null && message !== undefined ) && errMsg && errMsg instanceof RegExp ) {
|
|
|
+ this.assert(
|
|
|
+ errMsg.exec( message ),
|
|
|
+ 'expected #{this} to throw error matching #{exp} but got #{act}',
|
|
|
+ 'expected #{this} to throw error not matching #{exp}',
|
|
|
+ errMsg,
|
|
|
+ message
|
|
|
+ );
|
|
|
+
|
|
|
+ utils.flag( this, 'object', err );
|
|
|
+
|
|
|
+ return this;
|
|
|
+ } else if ( ( message !== null && message !== undefined ) && errMsg && typeof errMsg == 'string' ) {
|
|
|
+ this.assert(
|
|
|
+ message.indexOf( errMsg ) !== -1,
|
|
|
+ 'expected #{this} to throw error including #{exp} but got #{act}',
|
|
|
+ 'expected #{this} to throw error not including #{act}',
|
|
|
+ errMsg,
|
|
|
+ message
|
|
|
+ );
|
|
|
+
|
|
|
+ utils.flag( this, 'object', err );
|
|
|
+
|
|
|
+ return this;
|
|
|
+ } else {
|
|
|
+ thrown = true;
|
|
|
+ thrownError = err;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if ( thrown ) {
|
|
|
+ actuallyGot = ' but #{act} was thrown';
|
|
|
+ }
|
|
|
+
|
|
|
+ this.assert(
|
|
|
+ thrown === true ,
|
|
|
+ 'expected #{this} to throw an error' + actuallyGot ,
|
|
|
+ 'expected #{this} to not throw an error' + actuallyGot ,
|
|
|
+ 'CKEditorError',
|
|
|
+ ( thrownError instanceof Error ? thrownError.toString() : thrownError )
|
|
|
+ );
|
|
|
+
|
|
|
+ utils.flag( this, 'object', thrownError );
|
|
|
+
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+ }
|
|
|
} )();
|