Răsfoiți Sursa

Added automated tests for autoformat and autoformatengine.

Maksymilian Barnaś 9 ani în urmă
părinte
comite
a29e981cb8

+ 106 - 0
packages/ckeditor5-autoformat/tests/autoformat.js

@@ -0,0 +1,106 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+import Autoformat from '/ckeditor5/autoformat/autoformat.js';
+import Paragraph from '/ckeditor5/paragraph/paragraph.js';
+import VirtualTestEditor from '/tests/core/_utils/virtualtesteditor.js';
+import Enter from '/ckeditor5/enter/enter.js';
+import { setData, getData } from '/ckeditor5/engine/dev-utils/model.js';
+import testUtils from '/tests/core/_utils/utils.js';
+
+testUtils.createSinonSandbox();
+
+describe( 'Autoformat', () => {
+	let editor, doc, batch;
+
+	beforeEach( () => {
+		return VirtualTestEditor.create( {
+			features: [ Enter, Paragraph, Autoformat ]
+		} )
+		.then( newEditor => {
+			editor = newEditor;
+			doc = editor.document;
+			batch = doc.batch();
+		} );
+	} );
+
+	describe( 'Bulleted list', () => {
+		it( 'should replace asterisk with bulleted list item', () => {
+			setData( doc, '<paragraph>*[]</paragraph>' );
+			doc.enqueueChanges( () => {
+				batch.insert( doc.selection.getFirstPosition(), ' a' );
+			} );
+
+			expect( getData( doc ) ).to.equal( '<listItem indent="0" type="bulleted">a[]</listItem>' );
+		} );
+
+		it( 'should replace minus character with bulleted list item', () => {
+			setData( doc, '<paragraph>-[]</paragraph>' );
+			doc.enqueueChanges( () => {
+				batch.insert( doc.selection.getFirstPosition(), ' a' );
+			} );
+
+			expect( getData( doc ) ).to.equal( '<listItem indent="0" type="bulleted">a[]</listItem>' );
+		} );
+
+		it( 'should not replace minus character when inside bulleted list item', () => {
+			setData( doc, '<listItem indent="0" type="bulleted">-[]</listItem>' );
+			doc.enqueueChanges( () => {
+				batch.insert( doc.selection.getFirstPosition(), ' a' );
+			} );
+
+			expect( getData( doc ) ).to.equal( '<listItem indent="0" type="bulleted">- a[]</listItem>' );
+		} );
+	} );
+
+	describe( 'Numbered list', () => {
+		it( 'should replace digit with numbered list item', () => {
+			setData( doc, '<paragraph>1.[]</paragraph>' );
+			doc.enqueueChanges( () => {
+				batch.insert( doc.selection.getFirstPosition(), ' a' );
+			} );
+
+			expect( getData( doc ) ).to.equal( '<listItem indent="0" type="numbered">a[]</listItem>' );
+		} );
+
+		it( 'should not replace digit character when inside numbered list item', () => {
+			setData( doc, '<listItem indent="0" type="numbered">1.[]</listItem>' );
+			doc.enqueueChanges( () => {
+				batch.insert( doc.selection.getFirstPosition(), ' a' );
+			} );
+
+			expect( getData( doc ) ).to.equal( '<listItem indent="0" type="numbered">1. a[]</listItem>' );
+		} );
+	} );
+
+	describe( 'Heading', () => {
+		it( 'should replace hash character with heading', () => {
+			setData( doc, '<paragraph>#[]</paragraph>' );
+			doc.enqueueChanges( () => {
+				batch.insert( doc.selection.getFirstPosition(), ' a' );
+			} );
+
+			expect( getData( doc ) ).to.equal( '<heading1>a[]</heading1>' );
+		} );
+
+		it( 'should replace two hash characters with heading level 2', () => {
+			setData( doc, '<paragraph>##[]</paragraph>' );
+			doc.enqueueChanges( () => {
+				batch.insert( doc.selection.getFirstPosition(), ' a' );
+			} );
+
+			expect( getData( doc ) ).to.equal( '<heading2>a[]</heading2>' );
+		} );
+
+		it( 'should not replace minus character when inside heading', () => {
+			setData( doc, '<heading1>#[]</heading1>' );
+			doc.enqueueChanges( () => {
+				batch.insert( doc.selection.getFirstPosition(), ' a' );
+			} );
+
+			expect( getData( doc ) ).to.equal( '<heading1># a[]</heading1>' );
+		} );
+	} );
+} );

+ 122 - 0
packages/ckeditor5-autoformat/tests/autoformatengine.js

@@ -0,0 +1,122 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+import AutoformatEngine from '/ckeditor5/autoformat/autoformatengine.js';
+import Paragraph from '/ckeditor5/paragraph/paragraph.js';
+import VirtualTestEditor from '/tests/core/_utils/virtualtesteditor.js';
+import Enter from '/ckeditor5/enter/enter.js';
+import { setData, getData } from '/ckeditor5/engine/dev-utils/model.js';
+import testUtils from '/tests/core/_utils/utils.js';
+import Command from '/ckeditor5/core/command/command.js';
+
+testUtils.createSinonSandbox();
+
+describe( 'AutoformatEngine', () => {
+	let editor, doc, batch;
+
+	beforeEach( () => {
+		return VirtualTestEditor.create( {
+			features: [ Enter, Paragraph ]
+		} )
+		.then( newEditor => {
+			editor = newEditor;
+			doc = editor.document;
+			batch = doc.batch();
+		} );
+	} );
+
+	describe( 'Command name', () => {
+		it( 'should run a command when the pattern is matched', () => {
+			const spy = testUtils.sinon.spy();
+			editor.commands.set( 'testCommand', new TestCommand( editor, spy ) );
+			new AutoformatEngine( editor, /^[\*]\s$/, 'testCommand' );
+
+			setData( doc, '<paragraph>*[]</paragraph>' );
+			doc.enqueueChanges( () => {
+				batch.insert( doc.selection.getFirstPosition(), ' a' );
+			} );
+
+			sinon.assert.calledOnce( spy );
+		} );
+
+		it( 'should remove found pattern', () => {
+			const spy = testUtils.sinon.spy();
+			editor.commands.set( 'testCommand', new TestCommand( editor, spy ) );
+			new AutoformatEngine( editor, /^[\*]\s$/, 'testCommand' );
+
+			setData( doc, '<paragraph>*[]</paragraph>' );
+			doc.enqueueChanges( () => {
+				batch.insert( doc.selection.getFirstPosition(), ' a' );
+			} );
+
+			sinon.assert.calledOnce( spy );
+			expect( getData( doc ) ).to.equal( '<paragraph>a[]</paragraph>' );
+		} );
+	} );
+
+	describe( 'Callback', () => {
+		it( 'should run callback when the pattern is matched', () => {
+			const spy = testUtils.sinon.spy();
+			new AutoformatEngine( editor, /^[\*]\s$/, spy );
+
+			setData( doc, '<paragraph>*[]</paragraph>' );
+			doc.enqueueChanges( () => {
+				batch.insert( doc.selection.getFirstPosition(), ' a' );
+			} );
+
+			sinon.assert.calledOnce( spy );
+		} );
+
+		it( 'should ignore other delta operations', () => {
+			const spy = testUtils.sinon.spy();
+			new AutoformatEngine( editor, /^[\*]\s/, spy );
+
+			setData( doc, '<paragraph>*[]</paragraph>' );
+			doc.enqueueChanges( () => {
+				batch.remove( doc.selection.getFirstRange() );
+			} );
+
+			sinon.assert.notCalled( spy );
+		} );
+
+		it( 'should stop if there is no text to run matching on', () => {
+			const spy = testUtils.sinon.spy();
+			new AutoformatEngine( editor, /^[\*]\s/, spy );
+
+			setData( doc, '<paragraph>[]</paragraph>' );
+			doc.enqueueChanges( () => {
+				batch.insert( doc.selection.getFirstPosition(), '' );
+			} );
+
+			sinon.assert.notCalled( spy );
+		} );
+	} );
+} );
+
+/**
+ * Dummy command to execute.
+ */
+class TestCommand extends Command {
+	/**
+	 * Creates an instance of the command.
+	 *
+	 * @param {core.editor.Editor} editor Editor instance.
+	 * @param {Function} onExecuteCallback _doExecute call hook
+	 */
+	constructor( editor, onExecuteCallback ) {
+		super( editor );
+
+		this.onExecute = onExecuteCallback;
+	}
+
+	/**
+	 * Executes command.
+	 *
+	 * @protected
+	 */
+	_doExecute() {
+		this.onExecute();
+	}
+}