2023-08-19 19:04:18 +00:00
//
// P o s t V i e w T e s t s . s w i f t
// d a m u s T e s t s
//
// C r e a t e d b y D a n i e l D ’ A q u i n o o n 2 0 2 3 - 0 8 - 1 9 .
//
import Foundation
import XCTest
2023-10-07 01:21:53 +00:00
import SnapshotTesting
import SwiftUI
2023-08-19 19:04:18 +00:00
@ testable import damus
import SwiftUI
final class PostViewTests : XCTestCase {
override func setUpWithError ( ) throws {
// P u t s e t u p c o d e h e r e . T h i s m e t h o d i s c a l l e d b e f o r e t h e i n v o c a t i o n o f e a c h t e s t m e t h o d i n t h e c l a s s .
}
override func tearDownWithError ( ) throws {
// P u t t e a r d o w n c o d e h e r e . T h i s m e t h o d i s c a l l e d a f t e r t h e i n v o c a t i o n o f e a c h t e s t m e t h o d i n t h e c l a s s .
}
2023-11-14 00:19:54 +00:00
/*
2023-10-07 01:21:53 +00:00
func testTextWrapperViewWillWrapText ( ) {
// S e t u p t e s t v a r i a b l e s t o b e p a s s e d i n t o t h e T e x t V i e w W r a p p e r
let tagModel : TagModel = TagModel ( )
var textHeight : CGFloat ? = nil
let textHeightBinding : Binding < CGFloat ? > = Binding ( get : {
return textHeight
} , set : { newValue in
textHeight = newValue
} )
// S e t u p t h e t e s t v i e w
let textEditorView = TextViewWrapper (
attributedText : . constant ( NSMutableAttributedString ( string : " Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. " ) ) ,
textHeight : textHeightBinding ,
2023-10-20 18:15:58 +00:00
initialTextSuffix : nil ,
2023-10-07 01:21:53 +00:00
cursorIndex : 9 ,
updateCursorPosition : { _ in return }
) . environmentObject ( tagModel )
let hostView = UIHostingController ( rootView : textEditorView )
// R u n s n a p s h o t c h e c k
assertSnapshot ( matching : hostView , as : . image ( on : . iPhoneSe ( . portrait ) ) )
}
2023-11-14 00:19:54 +00:00
*/
2023-08-19 19:04:18 +00:00
// / B a s e d o n h t t p s : / / g i t h u b . c o m / d a m u s - i o / d a m u s / i s s u e s / 1 3 7 5
// / T e s t s w h e t h e r t h e e d i t o r p r o p e r l y h a n d l e s m e n t i o n l i n k s a f t e r t h e y h a v e b e e n a d d e d , t o a v o i d m a n u a l e d i t i n g o f a t t r i b u t e d l i n k s
func testMentionLinkEditorHandling ( ) throws {
var content : NSMutableAttributedString
// T e s t n o r m a l i n s e r t i o n
checkMentionLinkEditorHandling ( content : NSMutableAttributedString ( string : " Hello " ) , replacementText : " @ " , replacementRange : NSRange ( location : 0 , length : 0 ) , shouldBeAbleToChangeAutomatically : true )
checkMentionLinkEditorHandling ( content : NSMutableAttributedString ( string : " Hello " ) , replacementText : " @ " , replacementRange : NSRange ( location : 6 , length : 0 ) , shouldBeAbleToChangeAutomatically : true )
checkMentionLinkEditorHandling ( content : NSMutableAttributedString ( string : " Helo " ) , replacementText : " l " , replacementRange : NSRange ( location : 3 , length : 0 ) , shouldBeAbleToChangeAutomatically : true )
// T e s t n o r m a l b a c k s p a c i n g
checkMentionLinkEditorHandling ( content : NSMutableAttributedString ( string : " Hello " ) , replacementText : " " , replacementRange : NSRange ( location : 5 , length : 1 ) , shouldBeAbleToChangeAutomatically : true )
checkMentionLinkEditorHandling ( content : NSMutableAttributedString ( string : " Hello " ) , replacementText : " " , replacementRange : NSRange ( location : 6 , length : 1 ) , shouldBeAbleToChangeAutomatically : true )
checkMentionLinkEditorHandling ( content : NSMutableAttributedString ( string : " Helo " ) , replacementText : " " , replacementRange : NSRange ( location : 3 , length : 1 ) , shouldBeAbleToChangeAutomatically : true )
// T e s t n o r m a l i n s e r t i o n a f t e r m e n t i o n l i n k
content = NSMutableAttributedString ( string : " Hello @user " )
content . addAttribute ( . link , value : " damus:1234 " , range : NSRange ( location : 6 , length : 5 ) )
checkMentionLinkEditorHandling ( content : content , replacementText : " a " , replacementRange : NSRange ( location : 12 , length : 0 ) , shouldBeAbleToChangeAutomatically : true )
// T e s t i n s e r t i o n r i g h t a t t h e e n d o f a m e n t i o n l i n k , a t t h e e n d o f t h e t e x t
content = NSMutableAttributedString ( string : " Hello @user " )
content . addAttribute ( . link , value : " damus:1234 " , range : NSRange ( location : 6 , length : 5 ) )
checkMentionLinkEditorHandling ( content : content , replacementText : " , " , replacementRange : NSRange ( location : 11 , length : 0 ) , shouldBeAbleToChangeAutomatically : false , expectedNewCursorIndex : 12 , handleNewContent : { newManuallyEditedContent in
XCTAssertEqual ( newManuallyEditedContent . string , " Hello @user, " )
XCTAssertNil ( newManuallyEditedContent . attribute ( . link , at : 11 , effectiveRange : nil ) )
} )
// T e s t i n s e r t i o n r i g h t a t t h e e n d o f a m e n t i o n l i n k , i n t h e m i d d l e o f t h e t e x t
content = NSMutableAttributedString ( string : " Hello @user how are you? " )
content . addAttribute ( . link , value : " damus:1234 " , range : NSRange ( location : 6 , length : 5 ) )
checkMentionLinkEditorHandling ( content : content , replacementText : " , " , replacementRange : NSRange ( location : 11 , length : 0 ) , shouldBeAbleToChangeAutomatically : false , expectedNewCursorIndex : 12 , handleNewContent : { newManuallyEditedContent in
XCTAssertEqual ( newManuallyEditedContent . string , " Hello @user, how are you? " )
XCTAssertNil ( newManuallyEditedContent . attribute ( . link , at : 11 , effectiveRange : nil ) )
} )
// T e s t i n s e r t i o n i n t h e m i d d l e o f a m e n t i o n l i n k t o c h e c k i f t h e l i n k i s r e m o v e d
content = NSMutableAttributedString ( string : " Hello @user how are you? " )
content . addAttribute ( . link , value : " damus:1234 " , range : NSRange ( location : 6 , length : 5 ) )
checkMentionLinkEditorHandling ( content : content , replacementText : " a " , replacementRange : NSRange ( location : 8 , length : 0 ) , shouldBeAbleToChangeAutomatically : false , expectedNewCursorIndex : 9 , handleNewContent : { newManuallyEditedContent in
XCTAssertEqual ( newManuallyEditedContent . string , " Hello @uaser how are you? " )
XCTAssertNil ( newManuallyEditedContent . attribute ( . link , at : 8 , effectiveRange : nil ) )
} )
// T e s t i n s e r t i o n i n t h e m i d d l e o f a m e n t i o n l i n k t o c h e c k i f t h e l i n k i s r e m o v e d , a t t h e e n d o f t h e t e x t
content = NSMutableAttributedString ( string : " Hello @user " )
content . addAttribute ( . link , value : " damus:1234 " , range : NSRange ( location : 6 , length : 5 ) )
checkMentionLinkEditorHandling ( content : content , replacementText : " a " , replacementRange : NSRange ( location : 8 , length : 0 ) , shouldBeAbleToChangeAutomatically : false , expectedNewCursorIndex : 9 , handleNewContent : { newManuallyEditedContent in
XCTAssertEqual ( newManuallyEditedContent . string , " Hello @uaser " )
XCTAssertNil ( newManuallyEditedContent . attribute ( . link , at : 8 , effectiveRange : nil ) )
} )
// T e s t b a c k s p a c i n g r i g h t a t t h e e n d o f a m e n t i o n l i n k , a t t h e e n d o f t h e t e x t
content = NSMutableAttributedString ( string : " Hello @user " )
content . addAttribute ( . link , value : " damus:1234 " , range : NSRange ( location : 6 , length : 5 ) )
checkMentionLinkEditorHandling ( content : content , replacementText : " " , replacementRange : NSRange ( location : 10 , length : 1 ) , shouldBeAbleToChangeAutomatically : false , expectedNewCursorIndex : 10 , handleNewContent : { newManuallyEditedContent in
XCTAssertEqual ( newManuallyEditedContent . string , " Hello @use " )
XCTAssertNil ( newManuallyEditedContent . attribute ( . link , at : 6 , effectiveRange : nil ) )
} )
// T e s t a d d i n g t e x t r i g h t a t t h e s t a r t o f a m e n t i o n l i n k , t o c h e c k t h a t t h e l i n k i s r e m o v e d
content = NSMutableAttributedString ( string : " Hello @user " )
content . addAttribute ( . link , value : " damus:1234 " , range : NSRange ( location : 6 , length : 5 ) )
checkMentionLinkEditorHandling ( content : content , replacementText : " a " , replacementRange : NSRange ( location : 6 , length : 0 ) , shouldBeAbleToChangeAutomatically : false , expectedNewCursorIndex : 7 , handleNewContent : { newManuallyEditedContent in
XCTAssertEqual ( newManuallyEditedContent . string , " Hello a@user " )
XCTAssertNil ( newManuallyEditedContent . attribute ( . link , at : 7 , effectiveRange : nil ) )
} )
// T e s t t h a t r e m o v i n g o n e l i n k d o e s n o t a f f e c t t h e o t h e r
content = NSMutableAttributedString ( string : " Hello @user1 @user2 " )
content . addAttribute ( . link , value : " damus:1234 " , range : NSRange ( location : 6 , length : 6 ) )
content . addAttribute ( . link , value : " damus:5678 " , range : NSRange ( location : 13 , length : 6 ) )
checkMentionLinkEditorHandling ( content : content , replacementText : " " , replacementRange : NSRange ( location : 18 , length : 1 ) , shouldBeAbleToChangeAutomatically : false , expectedNewCursorIndex : 18 , handleNewContent : { newManuallyEditedContent in
XCTAssertEqual ( newManuallyEditedContent . string , " Hello @user1 @user " )
XCTAssertNil ( newManuallyEditedContent . attribute ( . link , at : 13 , effectiveRange : nil ) )
XCTAssertNotNil ( newManuallyEditedContent . attribute ( . link , at : 6 , effectiveRange : nil ) )
} )
// T e s t t h a t r e p l a c i n g a w h o l e r a n g e i n t e r s e c t i n g w i t h t w o l i n k s r e m o v e s b o t h l i n k s
content = NSMutableAttributedString ( string : " Hello @user1 @user2 " )
content . addAttribute ( . link , value : " damus:1234 " , range : NSRange ( location : 6 , length : 6 ) )
content . addAttribute ( . link , value : " damus:5678 " , range : NSRange ( location : 13 , length : 6 ) )
checkMentionLinkEditorHandling ( content : content , replacementText : " a " , replacementRange : NSRange ( location : 10 , length : 4 ) , shouldBeAbleToChangeAutomatically : false , expectedNewCursorIndex : 11 , handleNewContent : { newManuallyEditedContent in
XCTAssertEqual ( newManuallyEditedContent . string , " Hello @useauser2 " )
XCTAssertNil ( newManuallyEditedContent . attribute ( . link , at : 6 , effectiveRange : nil ) )
XCTAssertNil ( newManuallyEditedContent . attribute ( . link , at : 11 , effectiveRange : nil ) )
} )
// T e s t t h a t r e p l a c i n g a w h o l e r a n g e i n c l u d i n g t w o l i n k s r e m o v e s b o t h l i n k s n a t u r a l l y
content = NSMutableAttributedString ( string : " Hello @user1 @user2, how are you? " )
content . addAttribute ( . link , value : " damus:1234 " , range : NSRange ( location : 6 , length : 6 ) )
content . addAttribute ( . link , value : " damus:5678 " , range : NSRange ( location : 13 , length : 6 ) )
checkMentionLinkEditorHandling ( content : content , replacementText : " " , replacementRange : NSRange ( location : 5 , length : 28 ) , shouldBeAbleToChangeAutomatically : true )
}
2024-01-18 22:56:32 +00:00
func testMentionLinkEditorHandling_noWhitespaceAfterLink1_addsWhitespace ( ) {
var content : NSMutableAttributedString
content = NSMutableAttributedString ( string : " Hello @user " )
content . addAttribute ( . link , value : " damus:1234 " , range : NSRange ( location : 6 , length : 5 ) )
checkMentionLinkEditorHandling ( content : content , replacementText : " up " , replacementRange : NSRange ( location : 11 , length : 1 ) , shouldBeAbleToChangeAutomatically : true , expectedNewCursorIndex : 13 , handleNewContent : { newManuallyEditedContent in
XCTAssertEqual ( newManuallyEditedContent . string , " Hello @user up " )
XCTAssertNil ( newManuallyEditedContent . attribute ( . link , at : 11 , effectiveRange : nil ) )
} )
}
func testMentionLinkEditorHandling_noWhitespaceAfterLink2_addsWhitespace ( ) {
var content : NSMutableAttributedString
content = NSMutableAttributedString ( string : " Hello @user test " )
content . addAttribute ( . link , value : " damus:1234 " , range : NSRange ( location : 6 , length : 5 ) )
checkMentionLinkEditorHandling ( content : content , replacementText : " up " , replacementRange : NSRange ( location : 11 , length : 1 ) , shouldBeAbleToChangeAutomatically : true , expectedNewCursorIndex : 13 , handleNewContent : { newManuallyEditedContent in
XCTAssertEqual ( newManuallyEditedContent . string , " Hello @user uptest " )
XCTAssertNil ( newManuallyEditedContent . attribute ( . link , at : 11 , effectiveRange : nil ) )
} )
}
2023-08-19 19:04:18 +00:00
}
func checkMentionLinkEditorHandling (
content : NSMutableAttributedString ,
replacementText : String ,
replacementRange : NSRange ,
shouldBeAbleToChangeAutomatically : Bool ,
expectedNewCursorIndex : Int ? = nil ,
handleNewContent : ( ( NSMutableAttributedString ) -> Void ) ? = nil ) {
let bindingContent : Binding < NSMutableAttributedString > = Binding ( get : {
return content
} , set : { newValue in
handleNewContent ? ( newValue )
} )
let coordinator : TextViewWrapper . Coordinator = TextViewWrapper . Coordinator ( attributedText : bindingContent , getFocusWordForMention : nil , updateCursorPosition : { newCursorIndex in
if let expectedNewCursorIndex {
XCTAssertEqual ( newCursorIndex , expectedNewCursorIndex )
}
2023-10-20 18:15:58 +00:00
} , initialTextSuffix : nil )
2023-08-19 19:04:18 +00:00
let textView = UITextView ( )
textView . attributedText = content
XCTAssertEqual ( coordinator . textView ( textView , shouldChangeTextIn : replacementRange , replacementText : replacementText ) , shouldBeAbleToChangeAutomatically , " Expected shouldChangeTextIn to return \( shouldBeAbleToChangeAutomatically ) , but was \( ! shouldBeAbleToChangeAutomatically ) " )
}