Debian Codegraph @

extensions/VisualEditor/lib/ve/tests/dm/ve.dm.SourceConverter.test.js

raw symbols SCIP
1 /*!
2 * VisualEditor DataModel SourceConverter tests.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 QUnit.module( 've.dm.SourceConverter' );
8
9 /* Tests */
10
11 QUnit.test( 'conversion', ( assert ) => {
12 const cases = [
13 {
14 msg: 'Simple text with language options',
15 text: 'foo',
16 textFromDataRange: 'foo\n',
17 data: [
18 { type: 'paragraph' },
19 ...'foo',
20 { type: '/paragraph' }
21 ],
22 options: {
23 lang: 'he',
24 dir: 'rtl'
25 }
26 },
27 {
28 msg: 'Multiple lines and whitespace',
29 text: 'foo \n\n\tbar\n',
30 textFromDataRange: 'foo \n\n\tbar\n\n',
31 data: [
32 { type: 'paragraph' },
33 ...'foo ',
34 { type: '/paragraph' },
35 { type: 'paragraph' },
36 { type: '/paragraph' },
37 { type: 'paragraph' },
38 ...'\tbar',
39 { type: '/paragraph' },
40 { type: 'paragraph' },
41 { type: '/paragraph' }
42 ]
43 },
44 {
45 msg: 'Different newlines are all converted to paragraphs (CR/LF/CRLF)',
46 text: 'asd\nasd\rasd\r\nasd',
47 textFromDataRange: 'asd\nasd\nasd\nasd\n',
48 data: [
49 { type: 'paragraph' },
50 ...'asd',
51 { type: '/paragraph' },
52 { type: 'paragraph' },
53 ...'asd',
54 { type: '/paragraph' },
55 { type: 'paragraph' },
56 ...'asd',
57 { type: '/paragraph' },
58 { type: 'paragraph' },
59 ...'asd',
60 { type: '/paragraph' }
61 ],
62 textRoundtrip: 'asd\nasd\nasd\nasd'
63 }
64 ];
65
66 cases.forEach( ( caseItem ) => {
67 const doc = ve.dm.sourceConverter.getModelFromSourceText( caseItem.text, caseItem.options );
68 assert.deepEqual(
69 doc.data.data,
70 [ ...caseItem.data, { type: 'internalList' }, { type: '/internalList' } ],
71 caseItem.msg + ': getModelFromSourceText (data)'
72 );
73 if ( caseItem.options ) {
74 assert.deepEqual(
75 { lang: doc.getLang(), dir: doc.getDir() },
76 caseItem.options,
77 caseItem.msg + ': getModelFromSourceText (language options)'
78 );
79 }
80 assert.deepEqual(
81 ve.dm.sourceConverter.getSourceTextFromModel( doc ),
82 caseItem.textRoundtrip || caseItem.text,
83 caseItem.msg + ': getSourceTextFromModel'
84 );
85 assert.deepEqual(
86 ve.dm.sourceConverter.getDataFromSourceText( caseItem.text ),
87 caseItem.data,
88 caseItem.msg + ': getDataFromSourceText'
89 );
90 assert.deepEqual(
91 ve.dm.sourceConverter.getDataFromSourceText( caseItem.text, true ),
92 caseItem.data.slice( 1, -1 ),
93 caseItem.msg + ': getDataFromSourceText (inline)'
94 );
95 assert.strictEqual(
96 ve.dm.sourceConverter.getSourceTextFromDataRange( caseItem.data ),
97 caseItem.textFromDataRange || caseItem.textRoundtrip || caseItem.text,
98 caseItem.msg + ': getSourceTextFromDataRange'
99 );
100 // getSourceTextFromDataRange with a range argument is currently tested by ElementLinearData#getSourceText tests
101 } );
102 } );