Skip to content
This repository has been archived by the owner on Jul 2, 2024. It is now read-only.

Commit

Permalink
Merge pull request #41 from inloop/feature/textstyle_tests
Browse files Browse the repository at this point in the history
FEAT: Textstyle properties tests
  • Loading branch information
radimhalfar authored Feb 27, 2018
2 parents b8182c6 + 5259266 commit baea478
Show file tree
Hide file tree
Showing 9 changed files with 387 additions and 236 deletions.
6 changes: 3 additions & 3 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
PODS:
- Styles (0.14.6)
- Styles/Tests (0.14.6)
- Styles (0.14.7)
- Styles/Tests (0.14.7)

DEPENDENCIES:
- Styles (from `../`)
Expand All @@ -11,7 +11,7 @@ EXTERNAL SOURCES:
:path: ../

SPEC CHECKSUMS:
Styles: bb74d11de1bff44374559fde605ed8c198f7ae8b
Styles: 60935d033b02a851acc9857695deb4035ed8ce85

PODFILE CHECKSUM: f5341d1bc44d76e5ca1e0d50940004053970d8dc

Expand Down
4 changes: 2 additions & 2 deletions Example/Pods/Local Podspecs/Styles.podspec.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Example/Pods/Manifest.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

410 changes: 207 additions & 203 deletions Example/Pods/Pods.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Example/Pods/Target Support Files/Styles/Info.plist

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
shouldUseLaunchSchemeArgsEnv = "YES">
shouldUseLaunchSchemeArgsEnv = "YES"
codeCoverageEnabled = "YES">
<Testables>
<TestableReference
skipped = "NO">
Expand Down
2 changes: 1 addition & 1 deletion Styles.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'Styles'
s.version = '0.14.6'
s.version = '0.14.7'
s.summary = 'UI Elements rapid styling'
s.description = <<-DESC
UIElements styling made easy, declarative and rapid.
Expand Down
137 changes: 137 additions & 0 deletions Styles/Tests/TextStyleTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
// Copyright © 2018 Inloop, s.r.o. All rights reserved.

import XCTest
@testable import Styles

final class TextStyleTests: XCTestCase {
func testFont() {
let expected = UIFont.systemFont(ofSize: 12)
let style = TextStyle(.font(expected))

let actual = style.attributes[.font] as? UIFont

XCTAssertNotNil(actual)
XCTAssertEqual(actual, expected)
}

func testForegroundColor() {
let style = TextStyle(.foregroundColor(.red))

let actual = style.attributes[.foregroundColor] as? UIColor

XCTAssertNotNil(actual)
XCTAssertEqual(actual, .red)
}

func testBackgroundColor() {
let style = TextStyle(.backgroundColor(.red))

let actual = style.attributes[.backgroundColor] as? UIColor

XCTAssertNotNil(actual)
XCTAssertEqual(actual, .red)
}

func testParagraphStyle() {
let style = TextStyle(.paragraphStyle([.alignment(.center), .lineHeight(104)]))
let actual = style.attributes[.paragraphStyle] as? NSParagraphStyle

XCTAssertNotNil(actual)
XCTAssertEqual(actual?.alignment, .center)
XCTAssertEqual(actual?.lineSpacing, 104)
}

func testLetterSpacing() {
let style = TextStyle(.letterSpacing(104))
let actual = style.attributes[.kern] as? CGFloat

XCTAssertNotNil(actual)
XCTAssertEqual(actual, CGFloat(104))
}

func testStrikethrought() {
_testDecoration(styleKey: .strikethroughStyle, colorKey: .strikethroughColor)
}

func testUnderline() {
_testDecoration(styleKey: .underlineStyle, colorKey: .underlineColor)
}

func _testDecoration(styleKey: NSAttributedStringKey, colorKey: NSAttributedStringKey) {
let expected = TextDecoration(style: .thick, pattern: .dashDotDot, byWord: true, color: .red)
let property: TextStyle.Property
if styleKey == .strikethroughStyle {
property = .strikethrought(expected)
} else {
property = .underline(expected)
}
let style = TextStyle(property)
let actualStyle = style.attributes[styleKey] as? Int
let actualColor = style.attributes[colorKey] as? UIColor

XCTAssertNotNil(actualStyle)
XCTAssertEqual(actualStyle, expected.value)

XCTAssertNotNil(actualColor)
XCTAssertEqual(actualColor, expected.color)
}

func testObliqueness() {
let style = TextStyle(.obliqueness(104))
let actual = style.attributes[.obliqueness] as? Double

XCTAssertNotNil(actual)
XCTAssertEqual(actual, 104.0)
}

func testShadow() {
let expected = NSShadow()
expected.shadowColor = UIColor.red
expected.shadowOffset = CGSize(width: 10, height: 4)
expected.shadowBlurRadius = 10.4
let style = TextStyle(.shadow(expected))
let actual = style.attributes[.shadow] as? NSShadow

XCTAssertNotNil(actual)
XCTAssertEqual(actual, expected)
}

func testWritingDirectionOverrides() {
let overrides = [
TextStyle.WritingDirectionOverride.leftToRightOverride
]
let expected = overrides.map { $0.rawValue }
let style = TextStyle(.writingDirectionOverrides(overrides))
let actual = style.attributes[.writingDirection] as? [Int]

XCTAssertNotNil(actual)
XCTAssertEqual(actual!, expected)
}

func testBaselineOffset() {
let style = TextStyle(.baselineOffset(10.4))
let actual = style.attributes[.baselineOffset] as? Double

XCTAssertNotNil(actual)
XCTAssertEqual(actual, 10.4)
}

func testCombining() {
let style = TextStyle(.baselineOffset(10.4)) + TextStyle(.backgroundColor(.red))
let actualColor = style.attributes[.backgroundColor] as? UIColor
let actualOffset = style.attributes[.baselineOffset] as? Double

XCTAssertNotNil(actualColor)
XCTAssertNotNil(actualOffset)
XCTAssertEqual(actualColor, .red)
XCTAssertEqual(actualOffset, 10.4)
}

func testUpdating() {
let style = TextStyle(.baselineOffset(10.3)).updating(.baselineOffset(10.4))
let actual = style.attributes[.baselineOffset] as? Double

XCTAssertNotNil(actual)
XCTAssertEqual(actual, 10.4)
}
}
53 changes: 31 additions & 22 deletions Styles/Tests/ViewStyleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,66 +3,75 @@
import XCTest
@testable import Styles

final class UIViewColorStyleTests: XCTestCase {
final class ViewStyleTests: XCTestCase {
let colorStyle = ViewStyle(.backgroundColor(.red))
let tintStyle = ViewStyle(.tintColor(.blue))
let borderColorStyle = ViewStyle(.borderColor(.magenta))
let borderWidthStyle = ViewStyle(.borderWidth(10))
let opacityStyle = ViewStyle(.opacity(0.3))
let roundCornersStyle = ViewStyle(.roundCorners(.allCorners, radius: 10))

func testVeiewHasProperBackgoundColor() {
let view = UIView()
XCTAssertTrue(view.backgroundColor == nil)
var view: UIView!

override func setUp() {
super.setUp()
view = UIView()
}

func testBackgoundColor() {
view.viewStyle = colorStyle
XCTAssertTrue(view.backgroundColor != nil)

XCTAssertTrue(view.backgroundColor == .red)
}

func testViewHasTintIfStyleApplied() {
let view = UIView()
func testTint() {
view.viewStyle = tintStyle

XCTAssertTrue(view.tintColor == .blue)
}

func testHasBorderColorIfStyleApplied() {
let view = UIView()
func testBorderColor() {
view.viewStyle = borderColorStyle

XCTAssertTrue(view.layer.borderColor == UIColor.magenta.cgColor)
}

func testHasBorderWidthIfStyleApplied() {
let view = UIView()
func testBorderWidth() {
view.viewStyle = borderWidthStyle

XCTAssertTrue(view.layer.borderWidth == 10)
}

func testHasProperOpacityIfStyleApplied() {
let view = UIView()
func testOpacity() {
view.viewStyle = opacityStyle

XCTAssertTrue(view.layer.opacity == 0.3)
}

func testHasCornerRadiusesIfStyleApplied() {
let view = UIView()
func testCornerRadius() {
view.viewStyle = roundCornersStyle
let fullMask: CACornerMask = [
.layerMinXMinYCorner,
.layerMaxXMinYCorner,
.layerMinXMaxYCorner,
.layerMaxXMaxYCorner
]

if #available(iOS 11.0, *) {
XCTAssertTrue(view.clipsToBounds)
XCTAssertTrue(view.layer.cornerRadius == 10)
XCTAssertTrue(view.layer.maskedCorners == fullMask)
XCTAssertTrue(view.layer.maskedCorners == UIRectCorner.allCorners.maskedCorners)
} else {
XCTAssertTrue(view.layer.mask != nil)
}
}

func testCombining() {
let combinedStyle = colorStyle + tintStyle
view.viewStyle = combinedStyle

XCTAssert(view.backgroundColor == .red)
XCTAssert(view.tintColor == .blue)
}

func testUpdating() {
let updatedStyle = colorStyle.updating(.backgroundColor(.green))
view.viewStyle = updatedStyle

XCTAssert(view.backgroundColor == .green)
}
}

0 comments on commit baea478

Please sign in to comment.