Renetik iOS - 이벤트 및 속성

https://github.com/renetik/renetik-ios-event



선적 서류 비치



읽기 쉬운 코드를 작성하면서 애플리케이션 개발을 즐기고, 개선하고, 속도를 높일 수 있는 프레임워크입니다.
많은 프로젝트에서 라이브러리로 사용되며 새로운 프로젝트를 개발하면서 개선합니다.
Hire 또는 내 모바일 앱 음악 제작 및 공연 프로젝트 Renetik Instrumentswww.renetik.com에 투자할 준비가 되어 있습니다.

설치



최신 릴리스 버전 또는 마스터를 사용하여 현재 swift 패키지 관리자를 사용하여 설치할 수 있습니다.




/**
 * Simple event use cases
 */
final class EventTests: XCTestCase {

    func testListen() throws {
        let event = event()
        var count = 0
        event.listen { count += 1 }
        event.fire()
        event.fire()
        XCTAssertEqual(count, 2)
    }

    func testArgListen() throws {
        let event: CSEvent<Int> = event()
        var count = 0
        event.listen { count += $0 }
        event.fire(2)
        event.fire(3)
        XCTAssertEqual(count, 5)
    }

    func testListenOnce() throws {
        let event = event()
        var count = 0
        event.listenOnce { count += 1 }
        event.fire()
        event.fire()
        XCTAssertEqual(count, 1)
    }

    func testArgListenOnce() throws {
        let event = event()
        var count = 0
        event.listenOnce { count += 1 }
        event.fire()
        event.fire()
        XCTAssertEqual(count, 1)
    }

    func testEventCancel() throws {
        let event = event()
        var count = 0
        event.listen { registration, _ in
            count += 1
            if count == 2 { registration.cancel() }
        }
        event.fire()
        event.fire()
        event.fire()
        XCTAssertEqual(count, 2)
    }

    func testStringEventCancel() throws {
        let event: CSEvent<String> = event()
        var value: String? = nil
        event.listen {
            $0.cancel()
            value = $1
        }
        event.fire("first")
        XCTAssertEqual("first", value)
        event.fire("second")
    }

    func testEventPause() throws {
        let event = event()
        var count = 0
        let registration = event.listen { count += 1 }
        registration.pause { event.fire() }
        XCTAssertEqual(count, 0)
        event.fire()
        XCTAssertEqual(count, 1)
    }
}



/**
 * Simple event property use cases
 */
class EventPropertyTests: XCTestCase {

    func testOnChange() throws {
        let property = property("initial")
        var count = 0
        property.onChange { count += 1 }
        property.value = "second"
        property.value = "third"
        XCTAssertEqual(count, 2)
        XCTAssertEqual("third", property.value)
    }

    func testOnApply() throws {
        var count = 0
        let property = property("initial") { _ in count += 1 }.apply()
        property.value = "second"
        property.value = "third"
        XCTAssertEqual(count, 3)
        XCTAssertEqual("third", property.value)
    }

    func testArgListen() throws {
        var count = 0
        let property = property(0) { count += 1 }
        property.value += 2
        property.value += 3
        XCTAssertEqual(5, property.value)
        XCTAssertEqual(2, count)
    }

    func testEquals() throws {
        var count = 0
        let property = property(""){ count += 1 }
        property.value = "second"
        property.value = "second"
        XCTAssertEqual(count, 1)
        XCTAssertEqual("second", property.value)
    }

    func testOnChangeOnce() throws {
        var count = 0
        let property = property("")
        property.onChangeOnce { count += 1 }
        property.value = "second"
        property.value = "third"
        XCTAssertEqual(count, 1)
        XCTAssertEqual("third", property.value)
    }

    func testEventCancel() throws {
        var count = 0
        let property = property(0)
        property.onChange { registration, value in
            count += value
            if count > 2 { registration.cancel() }
        }
        property.value = 1
        property.value = 2
        property.value = 3
        XCTAssertEqual(count, 3)
    }

    func testEventPause() throws {
        var count = 0
        let property = property(0)
        let registration = property.onChange { count += $1 }
        registration.pause { property.value = 1 }
        XCTAssertEqual(count, 0)
        property.value = 2
        XCTAssertEqual(count, 2)
    }
}



/**
 * Event unregister after owner niled
 */
final class EventOwnerEventTest: XCTestCase {

    func testUnregisteredAfterNilled() throws {
        var owner: CSEventOwner? = CSEventOwnerBase()
        let event = event()
        var count = 0
        owner!.register(event.listen { count += 1 })
        event.fire()
        event.fire()
        XCTAssertEqual(count, 2)
        owner = nil
        event.fire()
        XCTAssertEqual(count, 2)
    }
}



/**
 * Event property unregister after owner niled
 */
class EventOwnerPropertyTest: XCTestCase {
    class SomeClass: CSEventOwnerBase {
        let prop = property("initial value")
        init(arugment: SomeClass? = nil) {
            super.init()
            arugment?.also {
                register($0.prop.onChange { [unowned self] in prop.value = $0 })
            }
        }
    }
    func testUnregisteredAfterNilled() throws {
        let class1 = SomeClass()
        var class2: SomeClass? = SomeClass(arugment: class1)
        let class3 = SomeClass(arugment: class2)
        XCTAssertEqual(class3.prop.value, "initial value")
        class1.prop.value = "first value"
        XCTAssertEqual(class3.prop.value, "first value")
        class2 = nil
        class1.prop.value = "second value"
        XCTAssertEqual(class3.prop.value, "first value")
    }
}

좋은 웹페이지 즐겨찾기