|
| 1 | +// |
| 2 | +// UIView+constrainSizeTests.swift |
| 3 | +// YCoreUITests |
| 4 | +// |
| 5 | +// Created by Dharmik Ghelani on 17/10/22. |
| 6 | +// Copyright © 2022 Y Media Labs. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import XCTest |
| 10 | +@testable import YCoreUI |
| 11 | + |
| 12 | +final class UIViewConstrainSizeTests: XCTestCase { |
| 13 | + private lazy var randomSize = { |
| 14 | + CGSize( |
| 15 | + width: self.randomFloat, |
| 16 | + height: self.randomFloat |
| 17 | + ) |
| 18 | + }() |
| 19 | + |
| 20 | + private lazy var randomDimension: CGFloat = { |
| 21 | + randomFloat |
| 22 | + }() |
| 23 | + |
| 24 | + func testSize() { |
| 25 | + let sut = makeSUT() |
| 26 | + |
| 27 | + XCTAssert(sut.translatesAutoresizingMaskIntoConstraints) |
| 28 | + |
| 29 | + let sizeAttributes: [ |
| 30 | + (NSLayoutConstraint.Attribute, CGFloat) |
| 31 | + ] = [ |
| 32 | + (.width, randomSize.width), |
| 33 | + (.height, randomSize.height) |
| 34 | + ] |
| 35 | + |
| 36 | + let constraints = sut.constrainSize(randomSize) |
| 37 | + |
| 38 | + sizeAttributes.forEach { |
| 39 | + guard let constraint = constraints[$0.0] else { |
| 40 | + XCTFail("Expected to have constraint for give attribute") |
| 41 | + return |
| 42 | + } |
| 43 | + |
| 44 | + XCTAssertEqual(constraint.firstItem as? UIView, sut) |
| 45 | + XCTAssertEqual(constraint.firstAttribute, $0.0) |
| 46 | + XCTAssertNil(constraint.secondItem) |
| 47 | + XCTAssertEqual(constraint.secondAttribute, .notAnAttribute) |
| 48 | + XCTAssertEqual(constraint.multiplier, 1) |
| 49 | + XCTAssertEqual(constraint.constant, $0.1) |
| 50 | + XCTAssertEqual(constraint.priority, .required) |
| 51 | + XCTAssert(constraint.isActive) |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + func testWidthAndHeight() { |
| 56 | + let sut = makeSUT() |
| 57 | + |
| 58 | + XCTAssert(sut.translatesAutoresizingMaskIntoConstraints) |
| 59 | + |
| 60 | + let sizeAttributes: [ |
| 61 | + (NSLayoutConstraint.Attribute, CGFloat) |
| 62 | + ] = [ |
| 63 | + (.width, randomSize.width), |
| 64 | + (.height, randomSize.height) |
| 65 | + ] |
| 66 | + |
| 67 | + let constraints = sut.constrainSize(width: randomSize.width, height: randomSize.height) |
| 68 | + |
| 69 | + sizeAttributes.forEach { |
| 70 | + guard let constraint = constraints[$0.0] else { |
| 71 | + XCTFail("Expected to have constraint for give attribute") |
| 72 | + return |
| 73 | + } |
| 74 | + |
| 75 | + XCTAssertEqual(constraint.firstItem as? UIView, sut) |
| 76 | + XCTAssertEqual(constraint.firstAttribute, $0.0) |
| 77 | + XCTAssertNil(constraint.secondItem) |
| 78 | + XCTAssertEqual(constraint.secondAttribute, .notAnAttribute) |
| 79 | + XCTAssertEqual(constraint.multiplier, 1) |
| 80 | + XCTAssertEqual(constraint.constant, $0.1) |
| 81 | + XCTAssertEqual(constraint.priority, .required) |
| 82 | + XCTAssert(constraint.isActive) |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + func testDimension() { |
| 87 | + let sut = makeSUT() |
| 88 | + |
| 89 | + XCTAssert(sut.translatesAutoresizingMaskIntoConstraints) |
| 90 | + |
| 91 | + let randomDimension = randomFloat |
| 92 | + |
| 93 | + let sizeAttributes: [ |
| 94 | + (NSLayoutConstraint.Attribute, CGFloat) |
| 95 | + ] = [ |
| 96 | + (.width, randomDimension), |
| 97 | + (.height, randomDimension) |
| 98 | + ] |
| 99 | + |
| 100 | + let constraints = sut.constrainSize(randomDimension) |
| 101 | + |
| 102 | + sizeAttributes.forEach { |
| 103 | + guard let constraint = constraints[$0.0] else { |
| 104 | + XCTFail("Expected to have constraint for give attribute") |
| 105 | + return |
| 106 | + } |
| 107 | + |
| 108 | + XCTAssertEqual(constraint.firstItem as? UIView, sut) |
| 109 | + XCTAssertEqual(constraint.firstAttribute, $0.0) |
| 110 | + XCTAssertNil(constraint.secondItem) |
| 111 | + XCTAssertEqual(constraint.secondAttribute, .notAnAttribute) |
| 112 | + XCTAssertEqual(constraint.multiplier, 1) |
| 113 | + XCTAssertEqual(constraint.constant, $0.1) |
| 114 | + XCTAssertEqual(constraint.priority, .required) |
| 115 | + XCTAssert(constraint.isActive) |
| 116 | + } |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +extension UIViewConstrainSizeTests { |
| 121 | + func makeSUT( |
| 122 | + file: StaticString = #filePath, |
| 123 | + line: UInt = #line |
| 124 | + ) -> UIView { |
| 125 | + let sut = UIView() |
| 126 | + |
| 127 | + trackForMemoryLeak(sut, file: file, line: line) |
| 128 | + return sut |
| 129 | + } |
| 130 | + |
| 131 | + var randomFloat: CGFloat { |
| 132 | + CGFloat.random(in: 0...500) |
| 133 | + } |
| 134 | +} |
0 commit comments