Skip to content

Commit 4362299

Browse files
Merge pull request #4 from ricocrescenzio95/3-add-uitests
Setup UITests
2 parents 1cd65cc + 7115e8a commit 4362299

34 files changed

+256
-24
lines changed

.swiftpm/xcode/xcshareddata/xcschemes/SwiftUITextField.xcscheme

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,8 @@
4343
shouldUseLaunchSchemeArgsEnv = "YES">
4444
<Testables>
4545
<TestableReference
46-
skipped = "NO">
47-
<BuildableReference
48-
BuildableIdentifier = "primary"
49-
BlueprintIdentifier = "SwiftUITextFieldTests"
50-
BuildableName = "SwiftUITextFieldTests"
51-
BlueprintName = "SwiftUITextFieldTests"
52-
ReferencedContainer = "container:">
53-
</BuildableReference>
54-
</TestableReference>
55-
<TestableReference
56-
skipped = "NO">
46+
skipped = "NO"
47+
testExecutionOrdering = "random">
5748
<BuildableReference
5849
BuildableIdentifier = "primary"
5950
BlueprintIdentifier = "SUITextFieldTests"

CONTRIBUTING.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Welcome to SUITextField contribution
2+
3+
Thank you for investing your time in contributing to our project!
4+
5+
## Issues
6+
7+
### Create a new issue
8+
9+
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).
10+
11+
### Solve an issue
12+
13+
Create a branch from an existing issue (or first create an issue) and work on it 🥳
14+
15+
Just few things:
16+
17+
- Try to follow Swift code-style, indenting 4 spaces and 120 char per line
18+
- Use documentation comments as much as you can. Update DocC documentation as well.
19+
- Write some snapshot tests (if you're not experienced, let [me](https://github.com/ricocrescenzio95) know, I might take care of those ✌🏻).
20+
21+
## Pull Request
22+
23+
Open a PR and describe your solution, any hidden implementation or workaround (if any).
24+
Don't forget to link the PR to the issue (Github should help you). Assign [me](https://github.com/ricocrescenzio95) as
25+
reviewer and we'll discuss about your solution.
26+
27+
## Your PR is merged!
28+
29+
Congratulations :tada::tada: The solution looks good and we can merge it :sparkles:.

Package.swift

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,28 @@ let package = Package(
77
name: "SwiftUITextField",
88
platforms: [.iOS(.v13), .macCatalyst(.v13)],
99
products: [
10-
// Products define the executables and libraries a package produces, and make them visible to other packages.
1110
.library(
1211
name: "SwiftUITextField",
1312
targets: ["SwiftUITextField"]),
1413
],
1514
dependencies: [
15+
.package(
16+
url: "https://github.com/pointfreeco/swift-snapshot-testing.git",
17+
from: "1.9.0"
18+
)
1619
],
1720
targets: [
1821
.target(
1922
name: "SwiftUITextField",
20-
dependencies: []),
23+
dependencies: []
24+
),
2125
.testTarget(
2226
name: "SUITextFieldTests",
23-
dependencies: ["SwiftUITextField"]),
27+
dependencies: [
28+
"SwiftUITextField",
29+
.product(name: "SnapshotTesting", package: "swift-snapshot-testing"),
30+
],
31+
exclude: ["__Snapshots__"]
32+
),
2433
]
2534
)

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
### A SwiftUI wrapper of UITextField that allows more customization
99

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

136139
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)
137140

141+
### Yu can also open your own PR and contribute to the project! [Contributing](CONTRIBUTING.md) 🤝
142+
138143
## License
139144

140145
This software is provided under the [MIT](LICENSE.md) license

SUITextFieldExample/SUITextFieldExample.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//
2+
// CoreTestCase.swift
3+
//
4+
//
5+
// Created by Rico Crescenzio on 14/04/22.
6+
//
7+
8+
import XCTest
9+
import SnapshotTesting
10+
import SwiftUI
11+
12+
class CoreTestCase: XCTestCase {
13+
14+
override func setUp() {
15+
super.setUp()
16+
17+
XCTAssertEqual(UIDevice.current.name, "iPhone 11 Pro")
18+
}
19+
20+
func assertSnapshot<V>(
21+
matching view: V,
22+
asLayer: Bool = false,
23+
named name: String,
24+
record recording: Bool = false,
25+
timeout: TimeInterval = 5,
26+
file: StaticString = #file,
27+
testName: String = #function,
28+
line: UInt = #line
29+
) where V: View {
30+
let view = UIHostingController(rootView: view).view!
31+
SnapshotTesting.assertSnapshot(
32+
matching: view,
33+
as: .image(precision: 0.8, size: view.intrinsicContentSize),
34+
named: name,
35+
record: recording,
36+
file: file,
37+
testName: testName,
38+
line: line
39+
)
40+
let darkMode = UITraitCollection(userInterfaceStyle: .dark)
41+
SnapshotTesting.assertSnapshot(
42+
matching: view,
43+
as: .image(precision: 0.8, size: view.intrinsicContentSize, traits: darkMode),
44+
named: name + "-dark",
45+
record: recording,
46+
file: file,
47+
testName: testName,
48+
line: line
49+
)
50+
}
51+
52+
}

Tests/SUITextFieldTests/SUITextFieldTests.swift

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
//
2+
// StylingTests.swift
3+
//
4+
//
5+
// Created by Rico Crescenzio on 14/04/22.
6+
//
7+
8+
import XCTest
9+
@testable import SwiftUITextField
10+
import SnapshotTesting
11+
import SwiftUI
12+
13+
final class StylingTests: CoreTestCase {
14+
15+
private func testBorder(_ border: UITextField.BorderStyle, testName: String = #function) {
16+
let view = SUITextField(text: .constant("test"))
17+
.onUpdate {
18+
XCTAssertEqual($0.borderStyle, border)
19+
}
20+
.uiTextFieldBorderStyle(border)
21+
assertSnapshot(matching: view,
22+
named: "textField",
23+
testName: testName)
24+
}
25+
26+
private func testAlignment(_ alignment: NSTextAlignment, testName: String = #function) {
27+
let view = SUITextField(text: .constant("test"))
28+
.onUpdate {
29+
XCTAssertEqual($0.textAlignment, alignment)
30+
}
31+
.frame(width: 200)
32+
.uiTextFieldTextAlignment(alignment)
33+
assertSnapshot(matching: view,
34+
named: "textField",
35+
testName: testName)
36+
}
37+
38+
func testInitialState() {
39+
let view = SUITextField(text: .constant("test"))
40+
.onUpdate {
41+
XCTAssertEqual($0.borderStyle, .none)
42+
}
43+
.uiTextFieldBorderStyle(.none)
44+
assertSnapshot(matching: view, named: "textField")
45+
assertSnapshot(matching: SUITextField(text: .constant("test")), named: "textField")
46+
}
47+
48+
func testRoundedRectBorder() {
49+
testBorder(.roundedRect)
50+
}
51+
52+
func testBezelBorder() {
53+
testBorder(.bezel)
54+
}
55+
56+
func testLineBorder() {
57+
testBorder(.line)
58+
}
59+
60+
func testTextColor() {
61+
let view = SUITextField(text: .constant("test"))
62+
.onUpdate {
63+
XCTAssertEqual($0.textColor, .systemBlue)
64+
}
65+
.uiTextFieldTextColor(.systemBlue)
66+
assertSnapshot(matching: view, named: "textField")
67+
}
68+
69+
func testLeftAlignment() {
70+
testAlignment(.left)
71+
}
72+
73+
func testRightAlignment() {
74+
testAlignment(.right)
75+
}
76+
77+
func testCenterAlignment() {
78+
testAlignment(.center)
79+
}
80+
81+
func testNaturalAlignment() {
82+
testAlignment(.natural)
83+
}
84+
85+
func testJustifiedAlignment() {
86+
testAlignment(.justified)
87+
}
88+
89+
func testDefaultTextAttributesRewriteAll() {
90+
let attributes: [NSAttributedString.Key: Any] = [
91+
.foregroundColor: UIColor.systemRed,
92+
.font: UIFont.systemFont(ofSize: 12, weight: .bold),
93+
.paragraphStyle: {
94+
let paragraph = NSMutableParagraphStyle()
95+
paragraph.alignment = .right
96+
return paragraph
97+
}(),
98+
.kern: 10,
99+
.backgroundColor: UIColor.systemYellow
100+
]
101+
let view = SUITextField(text: .constant("test"))
102+
.uiTextFieldDefaultTextAttributes(attributes)
103+
.frame(width: 200)
104+
assertSnapshot(matching: view, named: "textField")
105+
}
106+
107+
func testDefaultTextAttributesKeepNew() {
108+
let attributes: [NSAttributedString.Key: Any] = [
109+
.foregroundColor: UIColor.systemRed,
110+
.kern: 5
111+
]
112+
let newAttributes: [NSAttributedString.Key: Any] = [
113+
.foregroundColor: UIColor.systemBlue,
114+
]
115+
let view = SUITextField(text: .constant("test"))
116+
.uiTextFieldDefaultTextAttributes(newAttributes, mergePolicy: .keepNew)
117+
.uiTextFieldDefaultTextAttributes(attributes)
118+
.frame(width: 200)
119+
assertSnapshot(matching: view, named: "textField")
120+
}
121+
122+
func testDefaultTextAttributesKeepOld() {
123+
let attributes: [NSAttributedString.Key: Any] = [
124+
.foregroundColor: UIColor.systemRed,
125+
.kern: 5
126+
]
127+
let newAttributes: [NSAttributedString.Key: Any] = [
128+
.foregroundColor: UIColor.systemBlue,
129+
.paragraphStyle: {
130+
let paragraph = NSMutableParagraphStyle()
131+
paragraph.alignment = .right
132+
return paragraph
133+
}()
134+
]
135+
let view = SUITextField(text: .constant("test"))
136+
.uiTextFieldDefaultTextAttributes(newAttributes, mergePolicy: .keepOld)
137+
.uiTextFieldDefaultTextAttributes(attributes)
138+
.frame(width: 200)
139+
assertSnapshot(matching: view, named: "textField")
140+
}
141+
142+
}
2.69 KB
Loading
2.79 KB
Loading

0 commit comments

Comments
 (0)