Skip to content

Commit

Permalink
Merge pull request #4 from ricocrescenzio95/3-add-uitests
Browse files Browse the repository at this point in the history
Setup UITests
  • Loading branch information
ricocrescenzio95 authored Apr 14, 2022
2 parents 1cd65cc + 7115e8a commit 4362299
Show file tree
Hide file tree
Showing 34 changed files with 256 additions and 24 deletions.
13 changes: 2 additions & 11 deletions .swiftpm/xcode/xcshareddata/xcschemes/SwiftUITextField.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,8 @@
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "SwiftUITextFieldTests"
BuildableName = "SwiftUITextFieldTests"
BlueprintName = "SwiftUITextFieldTests"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
<TestableReference
skipped = "NO">
skipped = "NO"
testExecutionOrdering = "random">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "SUITextFieldTests"
Expand Down
29 changes: 29 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Welcome to SUITextField contribution

Thank you for investing your time in contributing to our project!

## Issues

### Create a new issue

Open an [issue](https://github.com/ricocrescenzio95/SUITextField/issues/new?assignees=ricocrescenzio95&labels=enhancement&template=feature_request.md&title=%5BNEW%5D) or a [bug](https://github.com/ricocrescenzio95/SUITextField/issues/new?assignees=ricocrescenzio95&labels=enhancement&template=feature_request.md&title=%5BNEW%5D).

### Solve an issue

Create a branch from an existing issue (or first create an issue) and work on it 🥳

Just few things:

- Try to follow Swift code-style, indenting 4 spaces and 120 char per line
- Use documentation comments as much as you can. Update DocC documentation as well.
- Write some snapshot tests (if you're not experienced, let [me](https://github.com/ricocrescenzio95) know, I might take care of those ✌🏻).

## Pull Request

Open a PR and describe your solution, any hidden implementation or workaround (if any).
Don't forget to link the PR to the issue (Github should help you). Assign [me](https://github.com/ricocrescenzio95) as
reviewer and we'll discuss about your solution.

## Your PR is merged!

Congratulations :tada::tada: The solution looks good and we can merge it :sparkles:.
15 changes: 12 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,28 @@ let package = Package(
name: "SwiftUITextField",
platforms: [.iOS(.v13), .macCatalyst(.v13)],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "SwiftUITextField",
targets: ["SwiftUITextField"]),
],
dependencies: [
.package(
url: "https://github.com/pointfreeco/swift-snapshot-testing.git",
from: "1.9.0"
)
],
targets: [
.target(
name: "SwiftUITextField",
dependencies: []),
dependencies: []
),
.testTarget(
name: "SUITextFieldTests",
dependencies: ["SwiftUITextField"]),
dependencies: [
"SwiftUITextField",
.product(name: "SnapshotTesting", package: "swift-snapshot-testing"),
],
exclude: ["__Snapshots__"]
),
]
)
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
### A SwiftUI wrapper of UITextField that allows more customization

<p>
<a href="https://app.bitrise.io/app/e9993c1a127d5a26#">
<img src="https://app.bitrise.io/app/e9993c1a127d5a26/status.svg?token=WZGzlJfxkVPPq7MernCdVg&branch=master">
</a>
<a href="https://swiftpackageindex.com/ricocrescenzio95/SUITextField">
<img src="https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fricocrescenzio95%2FSUITextField%2Fbadge%3Ftype%3Dswift-versions">
</a>
Expand Down Expand Up @@ -135,6 +138,8 @@ If you found a bug, you can open an issue as a bug [here](https://github.com/ric

Want a new feature? Open an issue [here](https://github.com/ricocrescenzio95/SUITextField/issues/new?assignees=ricocrescenzio95&labels=enhancement&template=feature_request.md&title=%5BNEW%5D)

### Yu can also open your own PR and contribute to the project! [Contributing](CONTRIBUTING.md) 🤝

## License

This software is provided under the [MIT](LICENSE.md) license
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"pins" : [
{
"identity" : "swift-snapshot-testing",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-snapshot-testing.git",
"state" : {
"revision" : "f8a9c997c3c1dab4e216a8ec9014e23144cbab37",
"version" : "1.9.0"
}
}
],
"version" : 2
}
52 changes: 52 additions & 0 deletions Tests/SUITextFieldTests/CoreTestCase.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//
// CoreTestCase.swift
//
//
// Created by Rico Crescenzio on 14/04/22.
//

import XCTest
import SnapshotTesting
import SwiftUI

class CoreTestCase: XCTestCase {

override func setUp() {
super.setUp()

XCTAssertEqual(UIDevice.current.name, "iPhone 11 Pro")
}

func assertSnapshot<V>(
matching view: V,
asLayer: Bool = false,
named name: String,
record recording: Bool = false,
timeout: TimeInterval = 5,
file: StaticString = #file,
testName: String = #function,
line: UInt = #line
) where V: View {
let view = UIHostingController(rootView: view).view!
SnapshotTesting.assertSnapshot(
matching: view,
as: .image(precision: 0.8, size: view.intrinsicContentSize),
named: name,
record: recording,
file: file,
testName: testName,
line: line
)
let darkMode = UITraitCollection(userInterfaceStyle: .dark)
SnapshotTesting.assertSnapshot(
matching: view,
as: .image(precision: 0.8, size: view.intrinsicContentSize, traits: darkMode),
named: name + "-dark",
record: recording,
file: file,
testName: testName,
line: line
)
}

}
10 changes: 0 additions & 10 deletions Tests/SUITextFieldTests/SUITextFieldTests.swift

This file was deleted.

142 changes: 142 additions & 0 deletions Tests/SUITextFieldTests/StylingTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
//
// StylingTests.swift
//
//
// Created by Rico Crescenzio on 14/04/22.
//

import XCTest
@testable import SwiftUITextField
import SnapshotTesting
import SwiftUI

final class StylingTests: CoreTestCase {

private func testBorder(_ border: UITextField.BorderStyle, testName: String = #function) {
let view = SUITextField(text: .constant("test"))
.onUpdate {
XCTAssertEqual($0.borderStyle, border)
}
.uiTextFieldBorderStyle(border)
assertSnapshot(matching: view,
named: "textField",
testName: testName)
}

private func testAlignment(_ alignment: NSTextAlignment, testName: String = #function) {
let view = SUITextField(text: .constant("test"))
.onUpdate {
XCTAssertEqual($0.textAlignment, alignment)
}
.frame(width: 200)
.uiTextFieldTextAlignment(alignment)
assertSnapshot(matching: view,
named: "textField",
testName: testName)
}

func testInitialState() {
let view = SUITextField(text: .constant("test"))
.onUpdate {
XCTAssertEqual($0.borderStyle, .none)
}
.uiTextFieldBorderStyle(.none)
assertSnapshot(matching: view, named: "textField")
assertSnapshot(matching: SUITextField(text: .constant("test")), named: "textField")
}

func testRoundedRectBorder() {
testBorder(.roundedRect)
}

func testBezelBorder() {
testBorder(.bezel)
}

func testLineBorder() {
testBorder(.line)
}

func testTextColor() {
let view = SUITextField(text: .constant("test"))
.onUpdate {
XCTAssertEqual($0.textColor, .systemBlue)
}
.uiTextFieldTextColor(.systemBlue)
assertSnapshot(matching: view, named: "textField")
}

func testLeftAlignment() {
testAlignment(.left)
}

func testRightAlignment() {
testAlignment(.right)
}

func testCenterAlignment() {
testAlignment(.center)
}

func testNaturalAlignment() {
testAlignment(.natural)
}

func testJustifiedAlignment() {
testAlignment(.justified)
}

func testDefaultTextAttributesRewriteAll() {
let attributes: [NSAttributedString.Key: Any] = [
.foregroundColor: UIColor.systemRed,
.font: UIFont.systemFont(ofSize: 12, weight: .bold),
.paragraphStyle: {
let paragraph = NSMutableParagraphStyle()
paragraph.alignment = .right
return paragraph
}(),
.kern: 10,
.backgroundColor: UIColor.systemYellow
]
let view = SUITextField(text: .constant("test"))
.uiTextFieldDefaultTextAttributes(attributes)
.frame(width: 200)
assertSnapshot(matching: view, named: "textField")
}

func testDefaultTextAttributesKeepNew() {
let attributes: [NSAttributedString.Key: Any] = [
.foregroundColor: UIColor.systemRed,
.kern: 5
]
let newAttributes: [NSAttributedString.Key: Any] = [
.foregroundColor: UIColor.systemBlue,
]
let view = SUITextField(text: .constant("test"))
.uiTextFieldDefaultTextAttributes(newAttributes, mergePolicy: .keepNew)
.uiTextFieldDefaultTextAttributes(attributes)
.frame(width: 200)
assertSnapshot(matching: view, named: "textField")
}

func testDefaultTextAttributesKeepOld() {
let attributes: [NSAttributedString.Key: Any] = [
.foregroundColor: UIColor.systemRed,
.kern: 5
]
let newAttributes: [NSAttributedString.Key: Any] = [
.foregroundColor: UIColor.systemBlue,
.paragraphStyle: {
let paragraph = NSMutableParagraphStyle()
paragraph.alignment = .right
return paragraph
}()
]
let view = SUITextField(text: .constant("test"))
.uiTextFieldDefaultTextAttributes(newAttributes, mergePolicy: .keepOld)
.uiTextFieldDefaultTextAttributes(attributes)
.frame(width: 200)
assertSnapshot(matching: view, named: "textField")
}

}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 4362299

Please sign in to comment.