1 // Copyright 2018-present 650 Industries. All rights reserved.
2 
3 import Foundation
4 @testable import ExpoClipboard
5 
6 /**
7  A  mocked `NSAttributedString+utilities` for unit tests
8  `NSAttributedString` HTML processor requires main thread and it is slow. It may break unit test occasionally.
9  This mocked version just returns the raw string.
10  */
11 class MockNSAttributedString: NSAttributedString {
12   convenience init(htmlString: String) throws {
13     self.init(string: htmlString)
14   }
15 
16   override var rtfData: Data? {
17     return try? self.data(from: NSRange(location: 0, length: self.length))
18   }
19 
20   override var htmlString: String? {
21     return self.string
22   }
23 }
24 
swizzleNSAttributedStringnull25 func swizzleNSAttributedString() {
26   // swiftlint:disable force_unwrapping
27   method_exchangeImplementations(
28     class_getInstanceMethod(NSAttributedString.self, #selector(NSAttributedString.init(htmlString:)))!,
29     class_getInstanceMethod(MockNSAttributedString.self, #selector(MockNSAttributedString.init(htmlString:)))!
30   )
31   method_exchangeImplementations(
32     class_getInstanceMethod(NSAttributedString.self, #selector(getter: NSAttributedString.rtfData))!,
33     class_getInstanceMethod(MockNSAttributedString.self, #selector(getter: MockNSAttributedString.rtfData))!
34   )
35   method_exchangeImplementations(
36     class_getInstanceMethod(NSAttributedString.self, #selector(getter: NSAttributedString.htmlString))!,
37     class_getInstanceMethod(MockNSAttributedString.self, #selector(getter: MockNSAttributedString.htmlString))!
38   )
39   // swiftlint:enable force_unwrapping
40 }
41