Repository: ReactiveX/RxSwift
File: Tests/RxCocoaTests/UITextField+RxTests.swift (Swift)

Code before:
import UIKit
import RxSwift
import RxCocoa
import XCTest

// UITextField
final class UITextFieldTests : RxTest {
    
    func test_TextCompletesOnDealloc() {
        ensurePropertyDeallocated({ UITextField() }, "a", comparer: { $0 == $1 }) { (view: UITextField) in view.rx.text }
    }
    func test_ValueCompletesOnDealloc() {
        ensurePropertyDeallocated({ UITextField() }, "a", comparer: { $0 == $1 }) { (view: UITextField) in view.rx.value }
    }
    
    func testSettingTextDoesntClearMarkedText() {
        let textField = UITextFieldSubclass(frame: CGRect.zero)
        textField.text = "Text1"
        textField.isSettedText = false
        textField.rx.text.on(.next("Text1"))
        XCTAssertTrue(!textField.isSettedText)
        textField.rx.text.on(.next("Text2"))
        XCTAssertTrue(textField.isSettedText)
    }
    
    func testLabel_attributedTextObserver() {
        let textField = UITextField()
        XCTAssertEqual(textField.attributedText, "".textFieldAttributedString)
        let attributedText = "Hello!".textFieldAttributedString
        _ = Observable.just(attributedText).bind(to: textField.rx.attributedText)
        XCTAssertEqual(textField.attributedText!, attributedText)
    }
}

private extension String {
    var textFieldAttributedString: NSAttributedString {
        let tf = UITextField()
        tf.attributedText = NSAttributedString(string: self)
        return tf.attributedText!
    }
}

final class UITextFieldSubclass : UITextField {
    var isSettedText = false
    
    override var text: String? {
        get {
            return super.text
        }
        set {
            isSettedText = true

Review comment (suggestion):
Test title says `testLabel`. It should probably be just `test` because there is no label that we are testing inside this test.

Code after:
import UIKit
import RxSwift
import RxCocoa
import XCTest

// UITextField
final class UITextFieldTests : RxTest {
    
    func test_completesOnDealloc() {
        ensurePropertyDeallocated({ UITextField() }, "a", comparer: { $0 == $1 }) { (view: UITextField) in view.rx.text }
        ensurePropertyDeallocated({ UITextField() }, "a", comparer: { $0 == $1 }) { (view: UITextField) in view.rx.value }
        ensurePropertyDeallocated({ UITextField() }, "a".enrichedWithTextFieldAttributes, comparer: { $0 == $1 }) { (view: UITextField) in view.rx.attributedText }
    }
    
    func test_settingTextDoesntClearMarkedText() {
        let textField = UITextFieldSubclass(frame: CGRect.zero)
        textField.text = "Text1"
        textField.didSetText = false
        textField.rx.text.on(.next("Text1"))
        XCTAssertTrue(!textField.didSetText)
        textField.rx.text.on(.next("Text2"))
        XCTAssertTrue(textField.didSetText)
    }
    
    func test_attributedTextObserver() {
        let textField = UITextField()
        XCTAssertEqual(textField.attributedText, "".enrichedWithTextFieldAttributes)
        let attributedText = "Hello!".enrichedWithTextFieldAttributes
        textField.rx.attributedText.onNext(attributedText)
        XCTAssertEqual(textField.attributedText!, attributedText)
    }
}

private extension String {
    var enrichedWithTextFieldAttributes: NSAttributedString {
        let tf = UITextField()
        tf.attributedText = NSAttributedString(string: self)
        return tf.attributedText!
    }
}

final class UITextFieldSubclass : UITextField {
    var didSetText = false
    
    override var text: String? {
        get {
            return super.text
        }
        set {
            didSetText = true
            super.text = newValue