Skip to content

Commit

Permalink
More unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
juyan committed Dec 26, 2024
1 parent effbcb8 commit eb8f599
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
9 changes: 7 additions & 2 deletions Sources/FadeInText/FadeInTextController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class FadeInTextController: ObservableObject {
@objc
private func onFrameUpdate(_ displayLink: CADisplayLink) {
guard let startTime, !self.chunks.isEmpty else {
self.tearDownDisplayLink()
return
}
let time = CACurrentMediaTime() - startTime
Expand All @@ -52,8 +53,12 @@ public class FadeInTextController: ObservableObject {
}
self.text = updatedString
if newResult.shouldAnimationFinish {
self.displayLink?.invalidate()
self.displayLink = nil
self.tearDownDisplayLink()
}
}

private func tearDownDisplayLink() {
self.displayLink?.invalidate()
self.displayLink = nil
}
}
2 changes: 1 addition & 1 deletion Sources/FadeInText/Interpolator/LinearInterpolator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public final class LinearInterpolator: Interpolator {

var newOpacities = [Double]()
for i in 0..<numberOfChunks {
let startTime = Double(i) * (config.appearanceDuration / Double(numberOfChunks))
let startTime = Double(i) * config.appearanceDuration / Double(numberOfChunks)
let newOpacity = max(min(1.0, (currentTime - startTime) / config.fadeInDuration), 0.0)
newOpacities.append(newOpacity)
}
Expand Down
27 changes: 27 additions & 0 deletions Tests/FadeInTextTests/LinearInterpolatorTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// File.swift
//
//
// Created by Jun Yan on 12/26/24.
//

import Foundation
import XCTest
@testable import FadeInText

class LinearInterpolatorTests: XCTestCase {

func test_interpolation() {
let interpolator = LinearInterpolator(config: .init(fadeInDuration: 0.4, appearanceDuration: 3))

var result = interpolator.interpolate(currentTime: 0, numberOfChunks: 5)
XCTAssertFalse(result.shouldAnimationFinish)
XCTAssertTrue(result.opacities.allSatisfy({ $0 == 0.0 }))

result = interpolator.interpolate(currentTime: 0.3, numberOfChunks: 5)
XCTAssertFalse(result.shouldAnimationFinish)
XCTAssertTrue(result.opacities.first! > 0)
XCTAssertTrue(result.opacities.filter { $0 == 0.0 }.count == 4)

}
}

0 comments on commit eb8f599

Please sign in to comment.