model.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import { stringify, parse, getData, setData } from '../../src/dev-utils/model';
  6. import Model from '../../src/model/model';
  7. import DocumentFragment from '../../src/model/documentfragment';
  8. import Element from '../../src/model/element';
  9. import Text from '../../src/model/text';
  10. import Range from '../../src/model/range';
  11. import Position from '../../src/model/position';
  12. import count from '@ckeditor/ckeditor5-utils/src/count';
  13. describe( 'model test utils', () => {
  14. let model, document, root, selection, sandbox;
  15. beforeEach( () => {
  16. model = new Model();
  17. document = model.document;
  18. root = document.createRoot();
  19. selection = document.selection;
  20. sandbox = sinon.sandbox.create();
  21. model.change( writer => {
  22. writer.setSelection( null );
  23. } );
  24. model.schema.register( 'a', {
  25. allowWhere: '$text',
  26. allowIn: '$root',
  27. allowAttributes: [ 'bar', 'car', 'foo' ]
  28. } );
  29. model.schema.register( 'b', {
  30. allowWhere: '$text',
  31. allowIn: '$root',
  32. allowAttributes: [ 'barFoo', 'fooBar', 'x' ]
  33. } );
  34. model.schema.register( 'c', {
  35. allowWhere: '$text',
  36. allowIn: [ '$root', 'b' ]
  37. } );
  38. model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  39. model.schema.extend( '$text', {
  40. allowIn: [ '$root', 'a', 'b' ]
  41. } );
  42. } );
  43. afterEach( () => {
  44. sandbox.restore();
  45. } );
  46. describe( 'getData', () => {
  47. it( 'should use stringify method', () => {
  48. const stringifySpy = sandbox.spy( getData, '_stringify' );
  49. root.appendChildren( new Element( 'b', null, new Text( 'btext' ) ) );
  50. expect( getData( model, { withoutSelection: true } ) ).to.equal( '<b>btext</b>' );
  51. sinon.assert.calledOnce( stringifySpy );
  52. sinon.assert.calledWithExactly( stringifySpy, root );
  53. } );
  54. it( 'should use stringify method with selection', () => {
  55. const stringifySpy = sandbox.spy( getData, '_stringify' );
  56. root.appendChildren( new Element( 'b', null, new Text( 'btext' ) ) );
  57. model.change( writer => {
  58. writer.setSelection( Range.createFromParentsAndOffsets( root, 0, root, 1 ) );
  59. } );
  60. expect( getData( model ) ).to.equal( '[<b>btext</b>]' );
  61. sinon.assert.calledOnce( stringifySpy );
  62. sinon.assert.calledWithExactly( stringifySpy, root, document.selection );
  63. } );
  64. it( 'should throw an error when passing invalid document', () => {
  65. expect( () => {
  66. getData( { invalid: 'document' } );
  67. } ).to.throw( TypeError, 'Model needs to be an instance of module:engine/model/model~Model.' );
  68. } );
  69. } );
  70. describe( 'setData', () => {
  71. it( 'should use parse method', () => {
  72. const parseSpy = sandbox.spy( setData, '_parse' );
  73. const options = {};
  74. const data = '<b>btext</b>text';
  75. setData( model, data, options );
  76. expect( getData( model, { withoutSelection: true } ) ).to.equal( data );
  77. sinon.assert.calledOnce( parseSpy );
  78. const args = parseSpy.firstCall.args;
  79. expect( args[ 0 ] ).to.equal( data );
  80. } );
  81. it( 'should use parse method with selection', () => {
  82. const parseSpy = sandbox.spy( setData, '_parse' );
  83. const options = {};
  84. const data = '[<b>btext</b>]';
  85. setData( model, data, options );
  86. expect( getData( model ) ).to.equal( data );
  87. sinon.assert.calledOnce( parseSpy );
  88. const args = parseSpy.firstCall.args;
  89. expect( args[ 0 ] ).to.equal( data );
  90. } );
  91. it( 'should insert text', () => {
  92. test( 'this is test text', '[]this is test text' );
  93. } );
  94. it( 'should insert text with selection around', () => {
  95. test( '[this is test text]' );
  96. } );
  97. it( 'should insert text with selection inside #1', () => {
  98. test( 'this [is test] text' );
  99. } );
  100. it( 'should insert text with selection inside #2', () => {
  101. test( '[this is test] text' );
  102. } );
  103. it( 'should insert text with selection inside #3', () => {
  104. test( 'this is [test text]' );
  105. } );
  106. it( 'should insert unicode text with selection', () => {
  107. test( 'நி[லைக்]கு' );
  108. } );
  109. it( 'should insert element', () => {
  110. test( '<b>foo bar</b>', '[]<b>foo bar</b>' );
  111. } );
  112. it( 'should insert element with selection inside #1', () => {
  113. test( '<b>[foo ]bar</b>' );
  114. } );
  115. it( 'should insert element with selection inside #2', () => {
  116. test( '[<b>foo ]bar</b>' );
  117. } );
  118. it( 'should insert element with selection inside #3', () => {
  119. test( '<b>[foo bar</b>]' );
  120. } );
  121. it( 'should insert backward selection', () => {
  122. setData( model, '<b>[foo bar</b>]', { lastRangeBackward: true } );
  123. expect( getData( model ) ).to.equal( '<b>[foo bar</b>]' );
  124. expect( document.selection.isBackward ).to.true;
  125. } );
  126. it( 'should throw an error when passing invalid document', () => {
  127. expect( () => {
  128. setData( { invalid: 'document' } );
  129. } ).to.throw( TypeError, 'Model needs to be an instance of module:engine/model/model~Model.' );
  130. } );
  131. it( 'should set attributes to the selection', () => {
  132. setData( model, '<b>[foo bar]</b>', { selectionAttributes: { foo: 'bar' } } );
  133. expect( document.selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
  134. } );
  135. // #815.
  136. it( 'should work in a special root', () => {
  137. const model = new Model();
  138. model.schema.register( 'textOnly' );
  139. model.schema.extend( '$text', { allowIn: 'textOnly' } );
  140. model.document.createRoot( 'textOnly', 'textOnly' );
  141. setData( model, 'a[b]c', { rootName: 'textOnly' } );
  142. expect( getData( model, { rootName: 'textOnly' } ) ).to.equal( 'a[b]c' );
  143. } );
  144. function test( data, expected ) {
  145. expected = expected || data;
  146. setData( model, data );
  147. expect( getData( model ) ).to.equal( expected );
  148. }
  149. } );
  150. describe( 'stringify', () => {
  151. it( 'should stringify text', () => {
  152. const text = new Text( 'text', { underline: true, bold: true } );
  153. expect( stringify( text ) ).to.equal( '<$text bold="true" underline="true">text</$text>' );
  154. } );
  155. it( 'should stringify element', () => {
  156. const element = new Element( 'a', null, [
  157. new Element( 'b', null, new Text( 'btext' ) ),
  158. new Text( 'atext' )
  159. ] );
  160. expect( stringify( element ) ).to.equal( '<a><b>btext</b>atext</a>' );
  161. } );
  162. it( 'should stringify document fragment', () => {
  163. const fragment = new DocumentFragment( [
  164. new Element( 'b', null, new Text( 'btext' ) ),
  165. new Text( 'atext' )
  166. ] );
  167. expect( stringify( fragment ) ).to.equal( '<b>btext</b>atext' );
  168. } );
  169. it( 'writes elements and texts', () => {
  170. root.appendChildren( [
  171. new Element( 'a', null, new Text( 'atext' ) ),
  172. new Element( 'b', null, [
  173. new Element( 'c1' ),
  174. new Text( 'ctext' ),
  175. new Element( 'c2' )
  176. ] ),
  177. new Element( 'd' )
  178. ] );
  179. expect( stringify( root ) ).to.equal(
  180. '<a>atext</a><b><c1></c1>ctext<c2></c2></b><d></d>'
  181. );
  182. } );
  183. it( 'writes element attributes', () => {
  184. root.appendChildren(
  185. new Element( 'a', { foo: true, bar: 1, car: false }, [
  186. new Element( 'b', { fooBar: 'x y', barFoo: { x: 1, y: 2 } } )
  187. ] )
  188. );
  189. // Note: attributes are written in a very simplistic way, because they are not to be parsed. They are just
  190. // to be compared in the tests with some patterns.
  191. expect( stringify( root ) ).to.equal(
  192. '<a bar="1" car="false" foo="true"><b barFoo="{"x":1,"y":2}" fooBar="x y"></b></a>'
  193. );
  194. } );
  195. it( 'writes text attributes', () => {
  196. root.appendChildren( [
  197. new Text( 'foo', { bold: true } ),
  198. new Text( 'bar' ),
  199. new Text( 'bom', { bold: true, italic: true } ),
  200. new Element( 'a', null, [
  201. new Text( 'pom', { underline: true, bold: true } )
  202. ] )
  203. ] );
  204. expect( stringify( root ) ).to.equal(
  205. '<$text bold="true">foo</$text>bar<$text bold="true" italic="true">bom</$text>' +
  206. '<a><$text bold="true" underline="true">pom</$text></a>'
  207. );
  208. } );
  209. it( 'writes unicode text', () => {
  210. root.appendChildren( new Text( 'நிலைக்கு' ) );
  211. expect( stringify( root ) ).to.equal( 'நிலைக்கு' );
  212. } );
  213. describe( 'selection', () => {
  214. let elA, elB;
  215. beforeEach( () => {
  216. elA = new Element( 'a' );
  217. elB = new Element( 'b' );
  218. root.appendChildren( [
  219. elA,
  220. new Text( 'foo' ),
  221. new Text( 'bar', { bold: true } ),
  222. elB
  223. ] );
  224. } );
  225. it( 'writes selection in an empty root', () => {
  226. const root = document.createRoot( '$root', 'empty' );
  227. model.change( writer => {
  228. writer.setSelection( root );
  229. } );
  230. expect( stringify( root, selection ) ).to.equal(
  231. '[]'
  232. );
  233. } );
  234. it( 'writes selection collapsed in an element', () => {
  235. model.change( writer => {
  236. writer.setSelection( root );
  237. } );
  238. expect( stringify( root, selection ) ).to.equal(
  239. '[]<a></a>foo<$text bold="true">bar</$text><b></b>'
  240. );
  241. } );
  242. it( 'writes selection collapsed in a text', () => {
  243. model.change( writer => {
  244. writer.setSelection( root, 3 );
  245. } );
  246. expect( stringify( root, selection ) ).to.equal(
  247. '<a></a>fo[]o<$text bold="true">bar</$text><b></b>'
  248. );
  249. } );
  250. it( 'writes selection collapsed at the text left boundary', () => {
  251. model.change( writer => {
  252. writer.setSelection( elA, 'after' );
  253. } );
  254. expect( stringify( root, selection ) ).to.equal(
  255. '<a></a>[]foo<$text bold="true">bar</$text><b></b>'
  256. );
  257. } );
  258. it( 'writes selection collapsed at the text right boundary', () => {
  259. model.change( writer => {
  260. writer.setSelection( elB, 'before' );
  261. } );
  262. expect( stringify( root, selection ) ).to.equal(
  263. '<a></a>foo<$text bold="true">bar[]</$text><b></b>'
  264. );
  265. } );
  266. it( 'writes selection collapsed at the end of the root', () => {
  267. model.change( writer => {
  268. writer.setSelection( root, 'end' );
  269. // Needed due to https://github.com/ckeditor/ckeditor5-engine/issues/320.
  270. writer.removeSelectionAttribute( model.document.selection.getAttributeKeys() );
  271. } );
  272. expect( stringify( root, selection ) ).to.equal(
  273. '<a></a>foo<$text bold="true">bar</$text><b></b>[]'
  274. );
  275. } );
  276. it( 'writes selection collapsed selection in a text with attributes', () => {
  277. model.change( writer => {
  278. writer.setSelection( root, 5 );
  279. } );
  280. expect( stringify( root, selection ) ).to.equal(
  281. '<a></a>foo<$text bold="true">b[]ar</$text><b></b>'
  282. );
  283. } );
  284. it( 'writes flat selection containing couple of nodes', () => {
  285. model.change( writer => {
  286. writer.setSelection( Range.createFromParentsAndOffsets( root, 0, root, 4 ) );
  287. } );
  288. expect( stringify( root, selection ) ).to.equal(
  289. '[<a></a>foo]<$text bold="true">bar</$text><b></b>'
  290. );
  291. } );
  292. it( 'writes flat selection within text', () => {
  293. model.change( writer => {
  294. writer.setSelection( Range.createFromParentsAndOffsets( root, 2, root, 3 ) );
  295. } );
  296. expect( stringify( root, selection ) ).to.equal(
  297. '<a></a>f[o]o<$text bold="true">bar</$text><b></b>'
  298. );
  299. } );
  300. it( 'writes multi-level selection', () => {
  301. model.change( writer => {
  302. writer.setSelection( Range.createFromParentsAndOffsets( elA, 0, elB, 0 ) );
  303. } );
  304. expect( stringify( root, selection ) ).to.equal(
  305. '<a>[</a>foo<$text bold="true">bar</$text><b>]</b>'
  306. );
  307. } );
  308. it( 'writes selection when is backward', () => {
  309. model.change( writer => {
  310. writer.setSelection( Range.createFromParentsAndOffsets( elA, 0, elB, 0 ), true );
  311. } );
  312. expect( stringify( root, selection ) ).to.equal(
  313. '<a>[</a>foo<$text bold="true">bar</$text><b>]</b>'
  314. );
  315. } );
  316. it( 'writes selection in unicode text', () => {
  317. const root = document.createRoot( '$root', 'empty' );
  318. root.appendChildren( new Text( 'நிலைக்கு' ) );
  319. model.change( writer => {
  320. writer.setSelection( Range.createFromParentsAndOffsets( root, 2, root, 6 ) );
  321. } );
  322. expect( stringify( root, selection ) ).to.equal( 'நி[லைக்]கு' );
  323. } );
  324. it( 'uses range and coverts it to selection', () => {
  325. const range = Range.createFromParentsAndOffsets( elA, 0, elB, 0 );
  326. expect( stringify( root, range ) ).to.equal(
  327. '<a>[</a>foo<$text bold="true">bar</$text><b>]</b>'
  328. );
  329. } );
  330. it( 'uses position and converts it to collapsed selection', () => {
  331. const position = new Position( root, [ 0 ] );
  332. expect( stringify( root, position ) ).to.equal(
  333. '[]<a></a>foo<$text bold="true">bar</$text><b></b>'
  334. );
  335. } );
  336. } );
  337. } );
  338. describe( 'parse', () => {
  339. test( 'creates empty DocumentFragment from empty string', {
  340. data: '',
  341. check( fragment ) {
  342. expect( fragment ).to.be.instanceOf( DocumentFragment );
  343. }
  344. } );
  345. test( 'creates empty DocumentFragment with selection', {
  346. data: '[]',
  347. check( fragment, selection ) {
  348. expect( fragment ).to.be.instanceOf( DocumentFragment );
  349. expect( fragment.childCount ).to.equal( 0 );
  350. expect( selection.rangeCount ).to.equal( 1 );
  351. expect( selection.getFirstRange().isEqual( Range.createFromParentsAndOffsets( fragment, 0, fragment, 0 ) ) ).to.be.true;
  352. }
  353. } );
  354. test( 'returns Element if range is around single element', {
  355. data: '[<a></a>]',
  356. check( el, selection ) {
  357. const fragment = el.parent;
  358. expect( el ).to.be.instanceOf( Element );
  359. expect( fragment ).to.be.instanceOf( DocumentFragment );
  360. expect( selection.rangeCount ).to.equal( 1 );
  361. expect( selection.getFirstRange().isEqual( Range.createFromParentsAndOffsets( fragment, 0, fragment, 1 ) ) ).to.be.true;
  362. }
  363. } );
  364. test( 'returns DocumentFragment when multiple elements on root', {
  365. data: '<a></a><b></b>',
  366. check( fragment ) {
  367. expect( fragment ).to.be.instanceOf( DocumentFragment );
  368. expect( fragment.childCount ).to.equal( 2 );
  369. }
  370. } );
  371. test( 'creates elements', {
  372. data: '<a></a><b><c></c></b>'
  373. } );
  374. test( 'creates text nodes', {
  375. data: 'foo<a>bar</a>bom'
  376. } );
  377. test( 'creates text nodes with unicode text', {
  378. data: 'நிலைக்கு'
  379. } );
  380. test( 'sets elements attributes', {
  381. data: '<a bar="true" car="x y" foo="1"></a><b x="y"></b>',
  382. output: '<a bar="true" car="x y" foo="1"></a><b x="y"></b>',
  383. check( root ) {
  384. expect( root.getChild( 0 ).getAttribute( 'bar' ) ).to.equal( true );
  385. expect( root.getChild( 0 ).getAttribute( 'car' ) ).to.equal( 'x y' );
  386. expect( root.getChild( 0 ).getAttribute( 'foo' ) ).to.equal( 1 );
  387. expect( root.getChild( 1 ).getAttribute( 'x' ) ).to.equal( 'y' );
  388. }
  389. } );
  390. test( 'sets text attributes', {
  391. data: '<$text bar="true" car="x y" foo="1">foo</$text><$text x="y">bar</$text>bom',
  392. check( root ) {
  393. expect( root.childCount ).to.equal( 3 );
  394. expect( root.maxOffset ).to.equal( 9 );
  395. expect( root.getChild( 0 ).getAttribute( 'bar' ) ).to.equal( true );
  396. expect( root.getChild( 0 ).getAttribute( 'car' ) ).to.equal( 'x y' );
  397. expect( root.getChild( 0 ).getAttribute( 'foo' ) ).to.equal( 1 );
  398. expect( root.getChild( 1 ).getAttribute( 'x' ) ).to.equal( 'y' );
  399. expect( count( root.getChild( 2 ).getAttributes() ) ).to.equal( 0 );
  400. }
  401. } );
  402. test( 'creates element with complex attributes', {
  403. data: '<a foo=\'{"x":1,"y":2}\'></a>',
  404. output: '<a foo="{"x":1,"y":2}"></a>',
  405. check( a ) {
  406. expect( count( a.getAttributes() ) ).to.equal( 1 );
  407. expect( a.getAttribute( 'foo' ) ).to.have.property( 'x', 1 );
  408. expect( a.getAttribute( 'foo' ) ).to.have.property( 'y', 2 );
  409. }
  410. } );
  411. test( 'returns single parsed element', {
  412. data: '<paragraph></paragraph>',
  413. check( p ) {
  414. expect( p instanceof Element ).to.be.true;
  415. }
  416. } );
  417. test( 'returns DocumentFragment for multiple parsed elements', {
  418. data: '<paragraph></paragraph><paragraph></paragraph>',
  419. check( fragment ) {
  420. expect( fragment instanceof DocumentFragment ).to.be.true;
  421. }
  422. } );
  423. it( 'throws when invalid XML', () => {
  424. expect( () => {
  425. parse( '<a><b></a></b>', model.schema );
  426. } ).to.throw( Error, /Parse error/ );
  427. } );
  428. it( 'throws when try to set element not registered in schema', () => {
  429. expect( () => {
  430. parse( '<xyz></xyz>', model.schema );
  431. } ).to.throw( Error, 'Element \'xyz\' was not allowed in context ["$root"].' );
  432. } );
  433. it( 'throws when try to set text directly to $root without registering it', () => {
  434. const model = new Model();
  435. expect( () => {
  436. parse( 'text', model.schema );
  437. } ).to.throw( Error, 'Text was not allowed in context ["$root"].' );
  438. } );
  439. it( 'converts data in the specified context', () => {
  440. const model = new Model();
  441. model.schema.register( 'foo' );
  442. model.schema.extend( '$text', { allowIn: 'foo' } );
  443. expect( () => {
  444. parse( 'text', model.schema, { context: [ 'foo' ] } );
  445. } ).to.not.throw();
  446. } );
  447. describe( 'selection', () => {
  448. test( 'sets collapsed selection in an element', {
  449. data: '<a>[]</a>',
  450. check( root, selection ) {
  451. expect( selection.getFirstPosition().parent ).to.have.property( 'name', 'a' );
  452. }
  453. } );
  454. test( 'sets collapsed selection between elements', {
  455. data: '<a></a>[]<b></b>'
  456. } );
  457. test( 'sets collapsed selection before a text', {
  458. data: '<a></a>[]foo'
  459. } );
  460. test( 'sets collapsed selection after a text', {
  461. data: 'foo[]'
  462. } );
  463. test( 'sets collapsed selection within a text', {
  464. data: 'foo[]bar',
  465. check( text, selection ) {
  466. expect( text.offsetSize ).to.equal( 6 );
  467. expect( text.getPath() ).to.deep.equal( [ 0 ] );
  468. expect( selection.getFirstRange().start.path ).to.deep.equal( [ 3 ] );
  469. expect( selection.getFirstRange().end.path ).to.deep.equal( [ 3 ] );
  470. }
  471. } );
  472. it( 'sets selection attributes', () => {
  473. const result = parse( 'foo[]bar', model.schema, {
  474. selectionAttributes: {
  475. bold: true,
  476. italic: true
  477. }
  478. } );
  479. expect( stringify( result.model, result.selection ) ).to.equal( 'foo<$text bold="true" italic="true">[]</$text>bar' );
  480. } );
  481. test( 'sets collapsed selection between text and text with attributes', {
  482. data: 'foo[]<$text bold="true">bar</$text>',
  483. check( root, selection ) {
  484. expect( root.maxOffset ).to.equal( 6 );
  485. expect( selection.getAttribute( 'bold' ) ).to.be.undefined;
  486. }
  487. } );
  488. test( 'sets selection containing an element', {
  489. data: 'x[<a></a>]'
  490. } );
  491. it( 'sets selection with attribute containing an element', () => {
  492. const result = parse( 'x[<a></a>]', model.schema, {
  493. selectionAttributes: {
  494. bold: true
  495. }
  496. } );
  497. expect( stringify( result.model, result.selection ) ).to.equal( 'x[<a></a>]' );
  498. expect( result.selection.getAttribute( 'bold' ) ).to.be.true;
  499. } );
  500. it( 'sets a backward selection containing an element', () => {
  501. const result = parse( 'x[<a></a>]', model.schema, {
  502. lastRangeBackward: true
  503. } );
  504. expect( stringify( result.model, result.selection ) ).to.equal( 'x[<a></a>]' );
  505. expect( result.selection.isBackward ).to.true;
  506. } );
  507. test( 'sets selection within a text', {
  508. data: 'x[y]z'
  509. } );
  510. it( 'sets selection within a text with different attributes', () => {
  511. const result = parse( '<$text bold="true">fo[o</$text>ba]r', model.schema, {
  512. selectionAttributes: { bold: true }
  513. } );
  514. expect( stringify( result.model, result.selection ) ).to.equal( '<$text bold="true">fo[o</$text>ba]r' );
  515. expect( result.selection.getAttribute( 'bold' ) ).to.true;
  516. } );
  517. it( 'throws when missing selection start', () => {
  518. expect( () => {
  519. parse( 'foo]', model.schema );
  520. } ).to.throw( Error, /^Parse error/ );
  521. } );
  522. it( 'throws when missing selection end', () => {
  523. expect( () => {
  524. parse( '[foo', model.schema );
  525. } ).to.throw( Error, /^Parse error/ );
  526. } );
  527. } );
  528. function test( title, options ) {
  529. it( title, () => {
  530. const output = options.output || options.data;
  531. const data = parse( options.data, model.schema );
  532. let converted, selection;
  533. if ( data.selection && data.model ) {
  534. converted = data.model;
  535. selection = data.selection;
  536. } else {
  537. converted = data;
  538. }
  539. expect( stringify( converted, selection ) ).to.equal( output );
  540. if ( options.check ) {
  541. options.check( converted, selection );
  542. }
  543. } );
  544. }
  545. } );
  546. } );