Created
April 3, 2020 03:06
-
-
Save godrm/6914218e567de542bb653b0c4b99f1da to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
Verify Github on Galxe. gid:dgYyGhJrLMDMpHUG2UgQ3U