@@ -716,6 +716,77 @@ final class ParagraphTextStorageTests: XCTestCase {
716
716
" ParagraphTextStorage paragraph ranges should match the delegate ranges " )
717
717
}
718
718
719
+ func testParagraphTextStorage_EditParagraphWithSameLengthEdit( ) {
720
+ let string = " First paragraph \n Second paragraph "
721
+ let editString = " additions "
722
+
723
+ textStorage. beginEditing ( )
724
+ textStorage. replaceCharacters ( in: NSRange ( location: 0 , length: 0 ) , with: string)
725
+ textStorage. endEditing ( )
726
+
727
+ textStorage. beginEditing ( )
728
+ textStorage. replaceCharacters ( in: NSRange ( location: 6 , length: 9 ) , with: editString)
729
+ textStorage. endEditing ( )
730
+
731
+ XCTAssertTrue ( delegate. insertions. count == 1 && delegate. editions. count == 2 && delegate. removals. count == 0 ,
732
+ " ParagraphTextStorage paragraph delegate should be notified of exact changes " )
733
+
734
+ XCTAssertTrue ( delegate. insertions [ 0 ] == 1 )
735
+ XCTAssertTrue ( delegate. editions [ 0 ] == 0 && delegate. editions [ 1 ] == 0 )
736
+
737
+ let endString = " First additions \n Second paragraph "
738
+
739
+ XCTAssertTrue ( textStorage. paragraphRanges. count == 2 ,
740
+ " ParagraphTextStorage should now have 2 paragraphs " )
741
+
742
+ let firstRange = NSRange ( location: 0 , length: endString. paragraphs [ 0 ] . length)
743
+ let secondRange = NSRange ( location: NSMaxRange ( firstRange) , length: endString. paragraphs [ 1 ] . length)
744
+
745
+ XCTAssertTrue ( textStorage. paragraphRanges [ 0 ] == firstRange &&
746
+ textStorage. paragraphRanges [ 1 ] == secondRange,
747
+ " ParagraphTextStorage paragraph ranges should be correct " )
748
+
749
+ XCTAssertEqual ( textStorage. paragraphRanges, delegate. ranges,
750
+ " ParagraphTextStorage paragraph ranges should match the delegate ranges " )
751
+ }
752
+
753
+ func testParagraphTextStorage_EditMultipleParagraphWithSameLengthEdit( ) {
754
+ let string = " First paragraph \n Second paragraph \n Third paragraph "
755
+ let editString = " First paragraph \n Second paragraph \n "
756
+
757
+ textStorage. beginEditing ( )
758
+ textStorage. replaceCharacters ( in: NSRange ( location: 0 , length: 0 ) , with: string)
759
+ textStorage. endEditing ( )
760
+
761
+ textStorage. beginEditing ( )
762
+ textStorage. replaceCharacters ( in: NSRange ( location: 0 , length: 33 ) , with: editString)
763
+ textStorage. endEditing ( )
764
+
765
+ XCTAssertTrue ( delegate. insertions. count == 2 && delegate. editions. count == 4 && delegate. removals. count == 0 ,
766
+ " ParagraphTextStorage paragraph delegate should be notified of exact changes " )
767
+
768
+ XCTAssertTrue ( delegate. insertions [ 0 ] == 1 && delegate. insertions [ 1 ] == 2 )
769
+ XCTAssertTrue ( delegate. editions [ 0 ] == 0 && delegate. editions [ 1 ] == 0 && delegate. editions [ 2 ] == 1 && delegate. editions [ 3 ] == 2 )
770
+
771
+ let endString = " First paragraph \n Second paragraph \n Third paragraph "
772
+
773
+ XCTAssertTrue ( textStorage. paragraphRanges. count == 3 ,
774
+ " ParagraphTextStorage should now have 3 paragraphs " )
775
+
776
+ let firstRange = NSRange ( location: 0 , length: endString. paragraphs [ 0 ] . length)
777
+ let secondRange = NSRange ( location: NSMaxRange ( firstRange) , length: endString. paragraphs [ 1 ] . length)
778
+ let thirdRange = NSRange ( location: NSMaxRange ( secondRange) , length: endString. paragraphs [ 2 ] . length)
779
+
780
+ XCTAssertTrue ( textStorage. paragraphRanges [ 0 ] == firstRange &&
781
+ textStorage. paragraphRanges [ 1 ] == secondRange &&
782
+ textStorage. paragraphRanges [ 2 ] == thirdRange,
783
+ " ParagraphTextStorage paragraph ranges should be correct " )
784
+
785
+ XCTAssertEqual ( textStorage. paragraphRanges, delegate. ranges,
786
+ " ParagraphTextStorage paragraph ranges should match the delegate ranges " )
787
+ }
788
+
789
+
719
790
func testParagraphTextStorage_EditParagraphAtEnd( ) {
720
791
let string = " First paragraph \n Second paragraph "
721
792
let editString = " addition "
0 commit comments