extensions.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* jshint node: false, browser: true, globalstrict: true, varstmt: false */
  6. /* globals bender, requirejs */
  7. 'use strict';
  8. ( function() {
  9. // This seems to be the only way to force Require.JS to load modules starting with '/' from a different path.
  10. var load = requirejs.load;
  11. requirejs.load = function( context, moduleId, url ) {
  12. var basePath = bender.getCKEditorModuleBasePath().replace( /\/$/, '' );
  13. if ( moduleId[ 0 ] == '/' ) {
  14. url = basePath + url;
  15. }
  16. return load( context, moduleId, url );
  17. };
  18. require( [ '/ckeditor5/utils/ckeditorerror.js', 'chai' ], function( CKEditorError, chai ) {
  19. CKEditorError = CKEditorError.default;
  20. chai.use( function( _chai, utils ) {
  21. var Assertion = chai.Assertion;
  22. Assertion.addMethod( 'throwCKEditorError', assertThrows );
  23. function assertThrows( errMsg ) {
  24. var obj = utils.flag( this, 'object' ),
  25. actuallyGot = '' ,
  26. expectedThrown,
  27. thrown = false ,
  28. name = null ,
  29. thrownError = null,
  30. message;
  31. if ( arguments.length === 0 ) {
  32. errMsg = null;
  33. }
  34. try {
  35. obj();
  36. } catch ( err ) {
  37. // next, check constructor
  38. if (constructor) {
  39. this.assert(
  40. CKEditorError.isCKEditorError( err ),
  41. 'expected #{this} to throw #{exp} but #{act} was thrown',
  42. 'expected #{this} to not throw #{exp} but #{act} was thrown',
  43. 'CKEditorError',
  44. (err instanceof Error ? err.toString() : err)
  45. );
  46. if ( !errMsg ) {
  47. utils.flag( this, 'object', err );
  48. return this;
  49. }
  50. }
  51. // next, check message
  52. message = utils.type( err ) === 'object' && 'message' in err ?
  53. err.message
  54. : '' + err;
  55. if ( ( message != null ) && errMsg && errMsg instanceof RegExp ) {
  56. this.assert(
  57. errMsg.exec( message ),
  58. 'expected #{this} to throw error matching #{exp} but got #{act}',
  59. 'expected #{this} to throw error not matching #{exp}',
  60. errMsg,
  61. message
  62. );
  63. utils.flag( this, 'object', err );
  64. return this;
  65. } else if ( ( message != null ) && errMsg && typeof errMsg == 'string' ) {
  66. this.assert(
  67. ~message.indexOf( errMsg ),
  68. 'expected #{this} to throw error including #{exp} but got #{act}',
  69. 'expected #{this} to throw error not including #{act}',
  70. errMsg,
  71. message
  72. );
  73. utils.flag( this, 'object', err );
  74. return this;
  75. } else {
  76. thrown = true;
  77. thrownError = err;
  78. }
  79. }
  80. expectedThrown = 'an error';
  81. if ( thrown ) {
  82. actuallyGot = ' but #{act} was thrown'
  83. }
  84. this.assert(
  85. thrown === true ,
  86. 'expected #{this} to throw ' + expectedThrown + actuallyGot ,
  87. 'expected #{this} to not throw ' + expectedThrown + actuallyGot ,
  88. 'CKEditorError',
  89. ( thrownError instanceof Error ? thrownError.toString() : thrownError )
  90. );
  91. utils.flag( this, 'object', thrownError );
  92. };
  93. } );
  94. chai.expect( function() {
  95. throw new CKEditorError();
  96. } ).to.throwCKEditorError();
  97. chai.expect( function() {
  98. throw new Error();
  99. } ).to.not.throwCKEditorError();
  100. chai.expect( function() {
  101. throw new CKEditorError( 'custom message' );
  102. } ).to.throwCKEditorError( 'message' );
  103. chai.expect( function() {
  104. throw new CKEditorError( 'another msg' );
  105. } ).to.throwCKEditorError( /msg/ );
  106. } );
  107. // Reported: https://github.com/benderjs/benderjs/issues/248
  108. // Ugh... make some paths cleanup, because we need to combine these fragments and we don't want to
  109. // duplicate '/'. BTW. if you want to touch this make sure you haven't broken Bender jobs which
  110. // have different bender.basePaths (no trailing '/', unnecessary 'tests/' fragment).
  111. bender.getCKEditorModuleBasePath = function() {
  112. var appBasePath = bender.basePath;
  113. var ckeditorBasePath = bender.config.applications.ckeditor.basePath;
  114. var modulebasePath;
  115. modulebasePath = appBasePath
  116. .split( '/' )
  117. .filter( nonEmpty )
  118. // When running a job we need to drop the last parth of the base path, which is "tests".
  119. .slice( 0, -1 )
  120. .concat(
  121. ckeditorBasePath.split( '/' ).filter( nonEmpty )
  122. )
  123. .join( '/' );
  124. return '/' + modulebasePath;
  125. };
  126. function nonEmpty( str ) {
  127. return !!str.length;
  128. }
  129. } )();