damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

UserSearchAppendTests.swift (2037B)


      1 //
      2 //  Created by Jericho Hasselbush on 9/9/23.
      3 //
      4 
      5 
      6 // Test fix for https://github.com/damus-io/damus/issues/1525
      7 // Only change in damus source is in UserSearch.swift
      8 // UserSearch.appendUserTag
      9 
     10 import XCTest
     11 @testable import damus
     12 
     13 final class UserSearchAppendTests: XCTestCase {
     14     func testCursorShouldBeAtEndOfEmoji() throws {
     15         let simpleTag = NSMutableAttributedString("@JB55")
     16         let emojiTag = NSMutableAttributedString("@BTCapsule 🏴🧡")
     17         let post = NSMutableAttributedString("A Post")
     18 
     19         var cursorIndex: Int = 0
     20         appendUserTag(withTag: simpleTag, post: post, word_range: .init(location: 0, length: 0), newCursorIndex: &cursorIndex, spy: simulatedCursor )
     21         XCTAssertEqual(cursorIndex, simpleTag.length + 1) // +1 for past end of tag
     22         cursorIndex = 0
     23         appendUserTag(withTag: emojiTag, post: post, word_range: .init(location: 0, length: 0), newCursorIndex: &cursorIndex, spy: simulatedCursor)
     24         XCTAssertEqual(cursorIndex, emojiTag.length + 1) // +1 for past end of tag
     25     }
     26 }
     27 
     28 typealias CursorSpy = (Int, NSMutableAttributedString) -> Void
     29 
     30 var simulatedCursor: CursorSpy = { cursorIndex, tag in
     31     let tagWithSimulatedCursor = NSMutableAttributedString(attributedString: tag)
     32     if tagWithSimulatedCursor.length < cursorIndex {
     33         tagWithSimulatedCursor.append(.init(string: "|"))
     34     } else {
     35         tagWithSimulatedCursor.insert(.init(string: "|"), at: cursorIndex)
     36     }
     37     print(tagWithSimulatedCursor.string)
     38 }
     39 
     40 func appendUserTag(withTag tag: NSMutableAttributedString,
     41                    post: NSMutableAttributedString,
     42                    word_range: NSRange,
     43                    newCursorIndex: inout Int,
     44                    spy: CursorSpy = { _, _ in }) {
     45     let appended = append_user_tag(tag: tag, post: post, word_range: word_range)
     46 
     47     // faulty call
     48 //     newCursorIndex = word_range.location + appended.tag.string.count
     49 
     50     // good call
     51     newCursorIndex = word_range.location + appended.tag.length
     52 
     53     spy(newCursorIndex, tag)
     54 }