|
|
@@ -7,12 +7,9 @@
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
-import coreTestUtils from '/tests/core/_utils/utils.js';
|
|
|
import AttributeList from '/ckeditor5/core/treemodel/attributelist.js';
|
|
|
import Attribute from '/ckeditor5/core/treemodel/attribute.js';
|
|
|
|
|
|
-const getIteratorCount = coreTestUtils.getIteratorCount;
|
|
|
-
|
|
|
describe( 'AttributeList', () => {
|
|
|
let list, attrFooBar;
|
|
|
|
|
|
@@ -21,129 +18,173 @@ describe( 'AttributeList', () => {
|
|
|
attrFooBar = new Attribute( 'foo', 'bar' );
|
|
|
} );
|
|
|
|
|
|
- describe( 'setAttr', () => {
|
|
|
- it( 'should insert an attribute', () => {
|
|
|
- list.setAttr( attrFooBar );
|
|
|
+ it( 'should extend Map', () => {
|
|
|
+ expect( list ).to.be.instanceof( Map );
|
|
|
+ } );
|
|
|
|
|
|
- expect( getIteratorCount( list.getAttrs() ) ).to.equal( 1 );
|
|
|
- expect( list.getAttr( attrFooBar.key ) ).to.equal( attrFooBar.value );
|
|
|
+ describe( 'constructor', () => {
|
|
|
+ it( 'should initialize list with passed attributes', () => {
|
|
|
+ list = new AttributeList( [ attrFooBar ] );
|
|
|
+ expect( list.size ).to.equal( 1 );
|
|
|
+ expect( list.has( 'foo' ) ).to.be.true;
|
|
|
+ expect( list.get( 'foo' ).value ).to.equal( 'bar' );
|
|
|
} );
|
|
|
|
|
|
- it( 'should overwrite attribute with the same key', () => {
|
|
|
- list.setAttr( attrFooBar );
|
|
|
+ it( 'should copy passed AttributeList', () => {
|
|
|
+ list = new AttributeList( [ attrFooBar ] );
|
|
|
+ let copy = new AttributeList( list );
|
|
|
|
|
|
- expect( getIteratorCount( list.getAttrs() ) ).to.equal( 1 );
|
|
|
- expect( list.getAttr( 'foo' ) ).to.equal( 'bar' );
|
|
|
+ expect( copy.size ).to.equal( 1 );
|
|
|
+ expect( copy.has( 'foo' ) ).to.be.true;
|
|
|
+ expect( copy.get( 'foo' ).value ).to.equal( 'bar' );
|
|
|
+ } );
|
|
|
+ } );
|
|
|
|
|
|
- let attrFooXyz = new Attribute( 'foo', 'xyz' );
|
|
|
+ describe( 'iterator', () => {
|
|
|
+ it( 'should iterate over all added attributes', () => {
|
|
|
+ let attrAbcXyz = new Attribute( 'abc', 'xyz' );
|
|
|
+ let attrTestTrue = new Attribute( 'test', true );
|
|
|
+
|
|
|
+ list = new AttributeList( [ attrFooBar, attrAbcXyz, attrTestTrue ] );
|
|
|
+ list.delete( 'test' );
|
|
|
+
|
|
|
+ let it = list[ Symbol.iterator ]();
|
|
|
+
|
|
|
+ let step = it.next();
|
|
|
|
|
|
- list.setAttr( attrFooXyz );
|
|
|
+ expect( step.done ).to.be.false;
|
|
|
+ expect( step.value ).to.equal( attrFooBar );
|
|
|
|
|
|
- expect( getIteratorCount( list.getAttrs() ) ).to.equal( 1 );
|
|
|
- expect( list.getAttr( 'foo' ) ).to.equal( 'xyz' );
|
|
|
+ step = it.next();
|
|
|
+
|
|
|
+ expect( step.done ).to.be.false;
|
|
|
+ expect( step.value ).to.equal( attrAbcXyz );
|
|
|
+
|
|
|
+ step = it.next();
|
|
|
+
|
|
|
+ expect( step.done ).to.be.true;
|
|
|
} );
|
|
|
} );
|
|
|
|
|
|
- describe( 'setAttrsTo', () => {
|
|
|
- it( 'should remove all attributes and set passed ones', () => {
|
|
|
- list.setAttr( attrFooBar );
|
|
|
+ describe( 'getValue', () => {
|
|
|
+ it( 'should return value of set attribute for given key', () => {
|
|
|
+ list.set( attrFooBar );
|
|
|
+ expect( list.getValue( 'foo' ) ).to.equal( 'bar' );
|
|
|
+ } );
|
|
|
|
|
|
- let attrs = [ new Attribute( 'abc', true ), new Attribute( 'xyz', false ) ];
|
|
|
+ it( 'should return null if attribute with given key is not set', () => {
|
|
|
+ expect( list.getValue( 'foo' ) ).to.be.null;
|
|
|
+ } );
|
|
|
+ } );
|
|
|
|
|
|
- list.setAttrsTo( attrs );
|
|
|
+ describe( 'set', () => {
|
|
|
+ it( 'should insert given attribute', () => {
|
|
|
+ list.set( attrFooBar );
|
|
|
|
|
|
- expect( getIteratorCount( list.getAttrs() ) ).to.equal( 2 );
|
|
|
- expect( list.getAttr( 'foo' ) ).to.be.null;
|
|
|
- expect( list.getAttr( 'abc' ) ).to.be.true;
|
|
|
- expect( list.getAttr( 'xyz' ) ).to.be.false;
|
|
|
+ expect( list.size ).to.equal( 1 );
|
|
|
+ expect( list.getValue( 'foo' ) ).to.equal( 'bar' );
|
|
|
} );
|
|
|
|
|
|
- it( 'should copy attributes array, not pass by reference', () => {
|
|
|
- let attrs = [ new Attribute( 'attr', true ) ];
|
|
|
+ it( 'should overwrite attribute with the same key', () => {
|
|
|
+ list.set( attrFooBar );
|
|
|
+
|
|
|
+ expect( list.size ).to.equal( 1 );
|
|
|
+ expect( list.getValue( 'foo' ) ).to.equal( 'bar' );
|
|
|
|
|
|
- list.setAttrsTo( attrs );
|
|
|
+ let attrFooXyz = new Attribute( 'foo', 'xyz' );
|
|
|
|
|
|
- attrs.pop();
|
|
|
+ list.set( attrFooXyz );
|
|
|
|
|
|
- expect( getIteratorCount( list.getAttrs() ) ).to.equal( 1 );
|
|
|
+ expect( list.size ).to.equal( 1 );
|
|
|
+ expect( list.getValue( 'foo' ) ).to.equal( 'xyz' );
|
|
|
} );
|
|
|
} );
|
|
|
|
|
|
- describe( 'getAttr', () => {
|
|
|
- beforeEach( () => {
|
|
|
- list.setAttr( attrFooBar );
|
|
|
- } );
|
|
|
+ describe( 'setTo', () => {
|
|
|
+ it( 'should remove all attributes from the list and set given ones', () => {
|
|
|
+ list.set( attrFooBar );
|
|
|
+ list.setTo( [ new Attribute( 'abc', true ), new Attribute( 'bar', false ) ] );
|
|
|
|
|
|
- it( 'should return attribute value if key of previously set attribute has been passed', () => {
|
|
|
- expect( list.getAttr( 'foo' ) ).to.equal( attrFooBar.value );
|
|
|
+ expect( list.has( 'foo' ) ).to.be.false;
|
|
|
+ expect( list.getValue( 'abc' ) ).to.be.true;
|
|
|
+ expect( list.getValue( 'bar' ) ).to.be.false;
|
|
|
} );
|
|
|
+ } );
|
|
|
|
|
|
- it( 'should return null if attribute with given key has not been found', () => {
|
|
|
- expect( list.getAttr( 'bar' ) ).to.be.null;
|
|
|
+ describe( 'has', () => {
|
|
|
+ it( 'should return true if list contains given attribute (same key and value)', () => {
|
|
|
+ list.set( attrFooBar );
|
|
|
+
|
|
|
+ expect( list.has( attrFooBar ) ).to.be.true;
|
|
|
} );
|
|
|
- } );
|
|
|
|
|
|
- describe( 'removeAttr', () => {
|
|
|
- it( 'should remove an attribute', () => {
|
|
|
- let attrA = new Attribute( 'a', 'A' );
|
|
|
- let attrB = new Attribute( 'b', 'B' );
|
|
|
- let attrC = new Attribute( 'c', 'C' );
|
|
|
+ it( 'should return true if list contains an attribute with given key', () => {
|
|
|
+ list.set( attrFooBar );
|
|
|
|
|
|
- list.setAttr( attrA );
|
|
|
- list.setAttr( attrB );
|
|
|
- list.setAttr( attrC );
|
|
|
+ expect( list.has( 'foo' ) ).to.be.true;
|
|
|
+ } );
|
|
|
|
|
|
- list.removeAttr( attrB.key );
|
|
|
+ it( 'should return false if list does not contain given attribute', () => {
|
|
|
+ list.set( attrFooBar );
|
|
|
|
|
|
- expect( getIteratorCount( list.getAttrs() ) ).to.equal( 2 );
|
|
|
- expect( list.getAttr( attrA.key ) ).to.equal( attrA.value );
|
|
|
- expect( list.getAttr( attrC.key ) ).to.equal( attrC.value );
|
|
|
- expect( list.getAttr( attrB.key ) ).to.be.null;
|
|
|
+ expect( list.has( new Attribute( 'abc', true ) ) ).to.be.false;
|
|
|
} );
|
|
|
- } );
|
|
|
|
|
|
- describe( 'hasAttr', () => {
|
|
|
- it( 'should check attribute by key', () => {
|
|
|
- list.setAttr( attrFooBar );
|
|
|
- expect( list.hasAttr( 'foo' ) ).to.be.true;
|
|
|
+ it( 'should return false if list contains given attribute but value differs', () => {
|
|
|
+ list.set( attrFooBar );
|
|
|
+
|
|
|
+ expect( list.has( new Attribute( 'foo', 'foo' ) ) ).to.be.false;
|
|
|
} );
|
|
|
|
|
|
- it( 'should return false if attribute was not found by key', () => {
|
|
|
- expect( list.hasAttr( 'bar' ) ).to.be.false;
|
|
|
+ it( 'should return false if list does not contain an attribute with given key', () => {
|
|
|
+ list.set( attrFooBar );
|
|
|
+
|
|
|
+ expect( list.has( 'abc' ) ).to.be.false;
|
|
|
} );
|
|
|
+ } );
|
|
|
+
|
|
|
+ describe( 'isEqual', () => {
|
|
|
+ it( 'should return false if lists have different size', () => {
|
|
|
+ let attrAbcXyz = new Attribute( 'abc', 'xyz' );
|
|
|
+ list.setTo( [ attrFooBar, attrAbcXyz ] );
|
|
|
|
|
|
- it( 'should check attribute by object', () => {
|
|
|
- list.setAttr( attrFooBar );
|
|
|
- expect( list.hasAttr( attrFooBar ) ).to.be.true;
|
|
|
+ let other = new AttributeList( [ attrFooBar ] );
|
|
|
+
|
|
|
+ expect( list.isEqual( other ) ).to.be.false;
|
|
|
+ expect( other.isEqual( list ) ).to.be.false;
|
|
|
} );
|
|
|
|
|
|
- it( 'should return false if attribute was not found by object', () => {
|
|
|
- expect( list.hasAttr( attrFooBar ) ).to.be.false;
|
|
|
+ it( 'should return false if lists have different attributes', () => {
|
|
|
+ let attrAbcXyz = new Attribute( 'abc', 'xyz' );
|
|
|
+ list.setTo( [ attrFooBar ] );
|
|
|
+
|
|
|
+ let other = new AttributeList( [ attrAbcXyz ] );
|
|
|
+
|
|
|
+ expect( list.isEqual( other ) ).to.be.false;
|
|
|
+ expect( other.isEqual( list ) ).to.be.false;
|
|
|
} );
|
|
|
- } );
|
|
|
|
|
|
- describe( 'getAttrs', () => {
|
|
|
- it( 'should return all set attributes', () => {
|
|
|
- let attrA = new Attribute( 'a', 'A' );
|
|
|
- let attrB = new Attribute( 'b', 'B' );
|
|
|
- let attrC = new Attribute( 'c', 'C' );
|
|
|
+ it( 'should return false if lists have same attributes but different values for them', () => {
|
|
|
+ let attrAbcXyz = new Attribute( 'abc', 'xyz' );
|
|
|
+ let attrFooTrue = new Attribute( 'foo', true );
|
|
|
|
|
|
- list.setAttrsTo( [
|
|
|
- attrA,
|
|
|
- attrB,
|
|
|
- attrC
|
|
|
- ] );
|
|
|
+ list.setTo( [ attrFooBar, attrAbcXyz ] );
|
|
|
|
|
|
- list.removeAttr( attrB.key );
|
|
|
+ let other = new AttributeList( [ attrFooTrue, attrAbcXyz ] );
|
|
|
+
|
|
|
+ expect( list.isEqual( other ) ).to.be.false;
|
|
|
+ expect( other.isEqual( list ) ).to.be.false;
|
|
|
+ } );
|
|
|
|
|
|
- let attrsIt = list.getAttrs();
|
|
|
- let attrs = [];
|
|
|
+ it( 'should return true if lists have same attributes and same values for them', () => {
|
|
|
+ let attrAbcXyz = new Attribute( 'abc', 'xyz' );
|
|
|
+ list.setTo( [ attrFooBar, attrAbcXyz ] );
|
|
|
|
|
|
- for ( let attr of attrsIt ) {
|
|
|
- attrs.push( attr );
|
|
|
- }
|
|
|
+ // Note different order of attributes.
|
|
|
+ let other = new AttributeList( [ attrAbcXyz, attrFooBar ] );
|
|
|
|
|
|
- expect( [ attrA, attrC ] ).to.deep.equal( attrs );
|
|
|
+ expect( list.isEqual( other ) ).to.be.true;
|
|
|
+ expect( other.isEqual( list ) ).to.be.true;
|
|
|
} );
|
|
|
} );
|
|
|
} );
|