| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
QUnit.module( 've.dm.AnnotationSet' ); |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
QUnit.test( 'Basic usage', ( assert ) => { |
| 12 |
const store = new ve.dm.HashValueStore(), |
| 13 |
bold = new ve.dm.BoldAnnotation(), |
| 14 |
italic = new ve.dm.ItalicAnnotation(), |
| 15 |
underline = new ve.dm.UnderlineAnnotation(), |
| 16 |
annotationSet = new ve.dm.AnnotationSet( store, store.hashAll( [ bold, italic ] ) ), |
| 17 |
emptySet = new ve.dm.AnnotationSet( store ); |
| 18 |
let annotationSet2 = new ve.dm.AnnotationSet( store, store.hashAll( [ italic, underline ] ) ); |
| 19 |
|
| 20 |
assert.strictEqual( annotationSet.getLength(), 2, 'getLength is 2' ); |
| 21 |
assert.strictEqual( annotationSet.isEmpty(), false, 'isEmpty is false' ); |
| 22 |
assert.deepEqual( annotationSet.get( 0 ), bold, 'get(0) is bold' ); |
| 23 |
assert.strictEqual( annotationSet.contains( italic ), true, 'contains italic' ); |
| 24 |
assert.strictEqual( annotationSet.contains( underline ), false, 'doesn\'t contain underline' ); |
| 25 |
assert.strictEqual( annotationSet.containsHash( store.hashOfValue( italic ) ), true, 'contains italic by hash' ); |
| 26 |
assert.strictEqual( annotationSet.containsHash( store.hashOfValue( underline ) ), false, 'doesn\'t contain underline by hash' ); |
| 27 |
assert.strictEqual( annotationSet.containsAnyOf( annotationSet2 ), true, 'containsAnyOf set2 is true' ); |
| 28 |
assert.strictEqual( annotationSet.containsAnyOf( emptySet ), false, 'containsAnyOf empty set is false' ); |
| 29 |
assert.strictEqual( annotationSet.containsAllOf( annotationSet2 ), false, 'containsAllOf set2 set is false' ); |
| 30 |
assert.strictEqual( annotationSet.containsAllOf( annotationSet ), true, 'containsAllOf self is true' ); |
| 31 |
assert.strictEqual( annotationSet.offsetOf( italic ), 1, 'offsetOf italic is 1' ); |
| 32 |
assert.strictEqual( annotationSet.offsetOf( underline ), -1, 'offsetOf underline is -1' ); |
| 33 |
assert.deepEqual( |
| 34 |
annotationSet.filter( ( annotation ) => annotation.name === 'textStyle/bold' ).get(), |
| 35 |
[ bold ], 'filter for name=textStyle/bold returns just bold annotation' |
| 36 |
); |
| 37 |
assert.strictEqual( annotationSet.hasAnnotationWithName( 'textStyle/bold' ), true, 'hasAnnotationWithName textStyle/bold is true' ); |
| 38 |
assert.strictEqual( annotationSet.hasAnnotationWithName( 'textStyle/underline' ), false, 'hasAnnotationWithName underline is false' ); |
| 39 |
|
| 40 |
annotationSet2.add( bold, 1 ); |
| 41 |
assert.strictEqual( annotationSet2.offsetOf( bold ), 1, 'set2 contains bold at 1 after add at 1' ); |
| 42 |
annotationSet2.remove( bold ); |
| 43 |
assert.strictEqual( annotationSet2.contains( bold ), false, 'set2 doesn\'t contain bold after remove' ); |
| 44 |
annotationSet2.add( bold, 0 ); |
| 45 |
assert.strictEqual( annotationSet2.offsetOf( bold ), 0, 'set2 contains bold at 0 after add at 0' ); |
| 46 |
annotationSet2.add( bold, 0 ); |
| 47 |
assert.strictEqual( annotationSet2.getLength(), 3, 'adding existing annotation doesn\'t change length' ); |
| 48 |
|
| 49 |
annotationSet2.removeAt( 2 ); |
| 50 |
assert.strictEqual( annotationSet2.contains( underline ), false, 'set2 doesn\'t contain underline after removeAt 2' ); |
| 51 |
annotationSet2.removeAll(); |
| 52 |
assert.strictEqual( annotationSet2.isEmpty(), true, 'set2 is empty after removeAll' ); |
| 53 |
annotationSet2.addSet( annotationSet ); |
| 54 |
assert.strictEqual( annotationSet.getLength(), 2, 'set2 has length 2 after addSet' ); |
| 55 |
annotationSet2.removeSet( annotationSet ); |
| 56 |
assert.strictEqual( annotationSet2.isEmpty(), true, 'set2 is empty after removeSet' ); |
| 57 |
annotationSet2.push( bold ); |
| 58 |
annotationSet2.push( italic ); |
| 59 |
assert.deepEqual( annotationSet2.get(), [ bold, italic ], 'set2 contains bold then italic after two pushes' ); |
| 60 |
|
| 61 |
annotationSet2 = new ve.dm.AnnotationSet( store, store.hashAll( [ italic, underline ] ) ); |
| 62 |
annotationSet2.removeNotInSet( annotationSet ); |
| 63 |
assert.strictEqual( annotationSet.contains( italic ) && !annotationSet.contains( underline ), true, 'contains italic not underline after removeNotInSet' ); |
| 64 |
annotationSet2.add( underline, 1 ); |
| 65 |
let annotationSet3 = annotationSet2.reversed(); |
| 66 |
assert.strictEqual( annotationSet3.offsetOf( underline ), 0, 'underline has offsetOf 0 after reverse' ); |
| 67 |
annotationSet3 = annotationSet.mergeWith( annotationSet2 ); |
| 68 |
assert.strictEqual( annotationSet3.getLength(), 3, 'set merged with set2 has length 3' ); |
| 69 |
annotationSet3 = annotationSet.diffWith( annotationSet2 ); |
| 70 |
assert.strictEqual( annotationSet3.getLength(), 1, 'set diffed with set2 has length 1' ); |
| 71 |
assert.strictEqual( annotationSet3.contains( bold ), true, 'set diffed with set2 contains bold' ); |
| 72 |
annotationSet3 = annotationSet.intersectWith( annotationSet2 ); |
| 73 |
assert.strictEqual( annotationSet3.getLength(), 1, 'set intersected with set2 has length 1' ); |
| 74 |
assert.strictEqual( annotationSet3.contains( italic ), true, 'set intersected with set2 contains italic' ); |
| 75 |
} ); |
| 76 |
|
| 77 |
QUnit.test( 'Comparable', ( assert ) => { |
| 78 |
const store = new ve.dm.HashValueStore(), |
| 79 |
bold = new ve.dm.BoldAnnotation(), |
| 80 |
italic = new ve.dm.ItalicAnnotation(), |
| 81 |
strong = new ve.dm.BoldAnnotation( { type: 'textStyle/bold', attributes: { nodeName: 'strong' } } ), |
| 82 |
underline = new ve.dm.UnderlineAnnotation(), |
| 83 |
annotationSet = new ve.dm.AnnotationSet( store, store.hashAll( [ bold, italic ] ) ), |
| 84 |
annotationSet2 = new ve.dm.AnnotationSet( store, store.hashAll( [ strong, underline ] ) ), |
| 85 |
emptySet = new ve.dm.AnnotationSet( store ); |
| 86 |
|
| 87 |
assert.strictEqual( annotationSet.containsComparable( strong ), true, '[b,i] contains comparable strong' ); |
| 88 |
assert.strictEqual( annotationSet.containsComparable( bold ), true, '[b,i] contains comparable b' ); |
| 89 |
assert.strictEqual( annotationSet.containsComparable( underline ), false, '[b,i] doesn\'t contain comparable u' ); |
| 90 |
|
| 91 |
let annotationSet3 = new ve.dm.AnnotationSet( store, store.hashAll( [ bold ] ) ); |
| 92 |
assert.deepEqual( annotationSet.getComparableAnnotations( strong ), annotationSet3, '[b,i] get comparable strong returns [b]' ); |
| 93 |
assert.deepEqual( annotationSet.getComparableAnnotations( underline ), emptySet, '[b,i] get comparable underline returns []' ); |
| 94 |
|
| 95 |
annotationSet3 = new ve.dm.AnnotationSet( store, store.hashAll( [ bold ] ) ); |
| 96 |
assert.deepEqual( annotationSet.getComparableAnnotationsFromSet( annotationSet2 ), annotationSet3, '[b,i] get comparable from set [strong,u] returns just [b]' ); |
| 97 |
|
| 98 |
annotationSet3 = new ve.dm.AnnotationSet( store, store.hashAll( [ italic, strong ] ) ); |
| 99 |
assert.strictEqual( annotationSet.compareTo( annotationSet3 ), true, '[b,i] compares to [i,strong]' ); |
| 100 |
|
| 101 |
} ); |