Skip to content

Commit

Permalink
improved test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurdapaz committed Sep 10, 2023
1 parent 00825e6 commit a605e12
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 20 deletions.
4 changes: 2 additions & 2 deletions Sources/VoirBuilder/VoirBuilder+Traits.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ public struct VoirTraits {
}
}

private extension UIView {
extension UIView {
private static var orientedConstraintsKey: UInt8 = 0
private static var orientationKey: UInt8 = 0

var orientedConstraints: [VoirTraits.Orientation: [NSLayoutConstraint]] {
fileprivate var orientedConstraints: [VoirTraits.Orientation: [NSLayoutConstraint]] {
get { objc_getAssociatedObject(self, &Self.orientedConstraintsKey) as? [VoirTraits.Orientation: [NSLayoutConstraint]] ?? [:] }
set { objc_setAssociatedObject(self, &Self.orientedConstraintsKey, newValue, .OBJC_ASSOCIATION_RETAIN) }
}
Expand Down
9 changes: 1 addition & 8 deletions Sources/VoirHooker/VoirHooker.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,7 @@ + (void)swizzleMethod:(SEL)originalSelector withMethod:(SEL)swizzledSelector {
Class class = [self class];
Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);

BOOL didAddMethod = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));

if (didAddMethod) {
class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
method_exchangeImplementations(originalMethod, swizzledMethod);
}
@end

Expand Down
16 changes: 16 additions & 0 deletions Tests/VoirBuilderTests/Doubles/MockUIView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import UIKit
@testable import VoirBuilder

final class MockUIView: UIView {
var mockTraitCollection: UITraitCollection?

override var traitCollection: UITraitCollection {
mockTraitCollection ?? super.traitCollection
}

private(set) var notifyOrientationCalled = false
override func notifyOrientation() {
super.notifyOrientation()
notifyOrientationCalled = true
}
}
11 changes: 11 additions & 0 deletions Tests/VoirBuilderTests/Doubles/MockUIViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import UIKit
@testable import VoirBuilder

final class MockUIViewController: UIViewController {

private(set) var notifyOrientationCalled = false
override func notifyOrientation() {
super.notifyOrientation()
notifyOrientationCalled = true
}
}
53 changes: 53 additions & 0 deletions Tests/VoirBuilderTests/VoirBuilderBindTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import UIKit
import XCTest
@testable import VoirBuilder

final class VoirBuilderBindTests: XCTestCase {
func testUISlider_Binding() throws {
let control = UISlider()
control => { }
XCTAssertEqual(control.allTargets.count, 1)
}

func testUIButton_Binding() throws {
let control = UIButton()
control => { }
XCTAssertEqual(control.allTargets.count, 1)
}

func testUISwitch_Binding() throws {
let control = UISwitch()
control => { }
XCTAssertEqual(control.allTargets.count, 1)
}

func testUISegmentedControl_Binding() throws {
let control = UISegmentedControl()
control => { }
XCTAssertEqual(control.allTargets.count, 1)
}

func testUIStepper_Binding() throws {
let control = UIStepper()
control => { }
XCTAssertEqual(control.allTargets.count, 1)
}

func testUITextField_Binding() throws {
let control = UITextField()
control => { }
XCTAssertEqual(control.allTargets.count, 1)
}

func testUIDatePicker_Binding() throws {
let control = UIDatePicker()
control => { }
XCTAssertEqual(control.allTargets.count, 1)
}

func testUIPageControl_Binding() throws {
let control = UIPageControl()
control => { }
XCTAssertEqual(control.allTargets.count, 1)
}
}
27 changes: 17 additions & 10 deletions Tests/VoirBuilderTests/VoirBuilderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ final class VoirBuilderTests: XCTestCase {
}

func testActivateConstraints_WhenDeviceIsPortrait() {
let view = UIViewMock()
let view = MockUIView()

let constraint1 = view.widthAnchor.constraint(equalToConstant: 50)
let constraint2 = view.heightAnchor.constraint(equalToConstant: 50)
Expand All @@ -85,7 +85,7 @@ final class VoirBuilderTests: XCTestCase {
}

func testActivateConstraints_WhenDeviceIsLandscpae() {
let view = UIViewMock()
let view = MockUIView()

let constraint1 = view.widthAnchor.constraint(equalToConstant: 50)
let constraint2 = view.heightAnchor.constraint(equalToConstant: 50)
Expand All @@ -105,6 +105,21 @@ final class VoirBuilderTests: XCTestCase {
XCTAssertTrue(view.constraints.contains(constraint2))
}

func testActivateConstraints_ForAllTraits() {
let view = MockUIView()

let constraint1 = view.widthAnchor.constraint(equalToConstant: 50)
let constraint2 = view.heightAnchor.constraint(equalToConstant: 50)

view.activate {
constraint1
constraint2
}.when(.always)

XCTAssertTrue(view.constraints.contains(constraint1))
XCTAssertTrue(view.constraints.contains(constraint2))
}

// Test UIControl elements easy customization
func testIsThis() {
let element = UIView().is {
Expand All @@ -117,11 +132,3 @@ final class VoirBuilderTests: XCTestCase {
XCTAssertEqual(element.tag, 1000)
}
}

private class UIViewMock: UIView {
public var mockTraitCollection: UITraitCollection?

override var traitCollection: UITraitCollection {
mockTraitCollection ?? super.traitCollection
}
}
20 changes: 20 additions & 0 deletions Tests/VoirBuilderTests/VoirHookerTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import XCTest
@testable import VoirBuilder

final class VoirHookerTests: XCTestCase {
func testUIView_whenDidChangeTraitCollection() {
let view = MockUIView()

view.traitCollectionDidChange(nil)

XCTAssertTrue(view.notifyOrientationCalled)
}

func testUIViewController_whenViewDidLoad() {
let viewController = MockUIViewController()

viewController.viewDidLoad()

XCTAssertTrue(viewController.notifyOrientationCalled)
}
}
7 changes: 7 additions & 0 deletions Tests/VoirTests/VoirControllerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ final class VoirControllerTests: XCTestCase {
sut.loadView()
}
}

func testVoirComponent_ViewController() {
let (sut, _) = makeSut()
sut.loadView()

XCTAssertNotNil(sut.component.viewController)
}
}

// MARK: - SUT
Expand Down

0 comments on commit a605e12

Please sign in to comment.