Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
protocol ViewModelBinding {
associatedtype Key
associatedtype Data
func updateNotify(changed: @escaping (Key, Data)->())
}
class ForecastImageViewModel : ViewModelBinding {
typealias Key = [UIImage]?
typealias Data = (duration: Double, firstImage: UIImage?)
private var images : Key = nil {
didSet {
changedHandler(images, (duration, firstImage))
}
}
private var changedHandler : (Key, Data)->()
var duration : Double {
return 0.25 * Double(self.images?.count ?? 0)
}
var firstImage : UIImage? {
return self.images?.first
}
init(with images: [UIImage], changed handler: @escaping (Key, Data) -> () = { (_, _) in } ) {
self.changedHandler = handler
self.images = images
changedHandler(images, (duration, firstImage))
}
func append(image: UIImage) {
self.images?.append(image)
}
func updateNotify(changed: @escaping (Key, Data) -> ()) {
self.changedHandler = changed
}
}
class ForecastImageViewModelTests: XCTestCase {
override func setUpWithError() throws {
}
override func tearDownWithError() throws {
}
func test_이미지없을때_생성확인() throws {
let expectation = XCTestExpectation(description: "뷰모델 생성")
_ = ForecastImageViewModel(with: [], changed: { (images, _) in
XCTAssertNotNil(images)
XCTAssertEqual(images?.count, 0)
expectation.fulfill()
})
wait(for: [expectation], timeout: 4.0)
}
func test_이미지한개_생성확인() throws {
let expectation = XCTestExpectation(description: "뷰모델 생성")
let dummy = UIImage()
let forecastViewModel = ForecastImageViewModel(with: [dummy], changed: { (images, data) in
XCTAssertNotNil(images)
XCTAssertEqual(images?.count, 1)
XCTAssertEqual(data.duration, 0.25)
XCTAssertEqual(data.firstImage, dummy)
expectation.fulfill()
})
wait(for: [expectation], timeout: 4.0)
}
func test_생성이후_이미지한개_업데이트() throws {
let expectation = XCTestExpectation(description: "업데이트에 대한 알림 확인")
let dummy = UIImage()
let forecastViewModel = ForecastImageViewModel(with: [])
forecastViewModel.updateNotify(changed: { (images, data) in
XCTAssertNotNil(images)
XCTAssertEqual(images?.count, 1)
XCTAssertEqual(data.duration, 0.25)
XCTAssertEqual(data.firstImage, dummy)
expectation.fulfill()
})
forecastViewModel.append(image: dummy)
wait(for: [expectation], timeout: 4.0)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment