Skip to content

Commit c0734d2

Browse files
committed
Added Tests
1 parent a063bba commit c0734d2

File tree

4 files changed

+140
-0
lines changed

4 files changed

+140
-0
lines changed

Package.swift

+1
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ let package = Package(
2020
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
2121
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
2222
.target(name: "PublishedObject", dependencies: []),
23+
.testTarget(name: "PublishedObjectTests", dependencies: ["PublishedObject"])
2324
]
2425
)

Tests/LinuxMain.swift

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import XCTest
2+
3+
import PublishedObjectTests
4+
5+
var tests = [XCTestCaseEntry]()
6+
tests += PublishedObjectTests.allTests()
7+
XCTMain(tests)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
import XCTest
2+
import Combine
3+
@testable import PublishedObject
4+
5+
class Outer: ObservableObject {
6+
@PublishedObject var innerPublishedObject: Inner
7+
@Published var innerPublished: Inner
8+
9+
init(_ value: Int) {
10+
self.innerPublishedObject = Inner(value)
11+
self.innerPublished = Inner(value)
12+
}
13+
}
14+
15+
class Inner: ObservableObject {
16+
@Published var value: Int
17+
18+
init(_ int: Int) {
19+
self.value = int
20+
}
21+
}
22+
23+
final class PublishedObjectTests: XCTestCase {
24+
var cancellables: Set<AnyCancellable> = []
25+
26+
func testObjectWillChange() throws {
27+
let outer = Outer(1)
28+
29+
// Setting property on Outer (This will send an update with either @Published or @PublishedObject)
30+
31+
let exp1 = XCTestExpectation(description: "outer.objectWillChange will be called")
32+
outer.objectWillChange.first().sink { exp1.fulfill() } .store(in: &cancellables)
33+
outer.innerPublishedObject = Inner(2)
34+
wait(for: [exp1], timeout: 0.1)
35+
36+
let exp2 = XCTestExpectation(description: "outer.objectWillChange will be called")
37+
outer.objectWillChange.first().sink { exp2.fulfill() } .store(in: &cancellables)
38+
outer.innerPublished = Inner(2)
39+
wait(for: [exp2], timeout: 0.1)
40+
41+
// Setting property on Inner (This will only send an update when using @PublishedObject)
42+
43+
let exp3 = XCTestExpectation(description: "outer.objectWillChange will be called")
44+
outer.objectWillChange.first().sink { exp3.fulfill() } .store(in: &cancellables)
45+
outer.innerPublishedObject.value = 3
46+
wait(for: [exp3], timeout: 0.1)
47+
48+
let exp4 = XCTestExpectation(description: "outer.objectWillChange will NOT be called")
49+
exp4.isInverted = true
50+
outer.objectWillChange.first().sink { exp4.fulfill() } .store(in: &cancellables)
51+
outer.innerPublished.value = 3
52+
wait(for: [exp4], timeout: 0.1)
53+
}
54+
55+
func testProjectedValue() throws {
56+
let outer = Outer(1)
57+
58+
// Initial value
59+
60+
let exp0 = XCTestExpectation(description: "projectedValue will be called")
61+
outer.$innerPublishedObject.first().sink { inner in
62+
if inner.value == 1 {
63+
exp0.fulfill()
64+
}
65+
}.store(in: &cancellables)
66+
wait(for: [exp0], timeout: 0.1)
67+
68+
let exp1 = XCTestExpectation(description: "projectedValue will be called")
69+
outer.$innerPublished.first().sink { inner in
70+
if inner.value == 1 {
71+
exp1.fulfill()
72+
}
73+
}.store(in: &cancellables)
74+
wait(for: [exp1], timeout: 0.1)
75+
76+
77+
// Setting property on Outer (This will send an update with either @Published or @PublishedObject)
78+
79+
let exp2 = XCTestExpectation(description: "projectedValue will be called")
80+
outer.$innerPublishedObject.dropFirst().first().sink { inner in
81+
if inner.value == 2 {
82+
exp2.fulfill()
83+
}
84+
}.store(in: &cancellables)
85+
outer.innerPublishedObject = Inner(2)
86+
wait(for: [exp2], timeout: 0.1)
87+
88+
let exp3 = XCTestExpectation(description: "projectedValue will be called")
89+
outer.$innerPublished.dropFirst().first().sink { inner in
90+
if inner.value == 2 {
91+
exp3.fulfill()
92+
}
93+
}.store(in: &cancellables)
94+
outer.innerPublished = Inner(2)
95+
wait(for: [exp3], timeout: 0.1)
96+
97+
// Setting property on Inner (This will only send an update when using @PublishedObject)
98+
99+
let exp4 = XCTestExpectation(description: "projectedValue will be called")
100+
outer.$innerPublishedObject.dropFirst().first().sink { inner in
101+
if inner.value == 3 {
102+
exp4.fulfill()
103+
}
104+
}.store(in: &cancellables)
105+
outer.innerPublishedObject.value = 3
106+
wait(for: [exp4], timeout: 0.1)
107+
108+
let exp5 = XCTestExpectation(description: "projectedValue will NOT be called")
109+
exp5.isInverted = true
110+
outer.$innerPublished.dropFirst().first().sink { inner in
111+
if inner.value == 3 {
112+
exp5.fulfill()
113+
}
114+
}.store(in: &cancellables)
115+
outer.innerPublished.value = 3
116+
wait(for: [exp5], timeout: 0.1)
117+
}
118+
119+
static var allTests = [
120+
("testObjectWillChange", testObjectWillChange),
121+
("testProjectedValue", testProjectedValue),
122+
]
123+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import XCTest
2+
3+
#if !canImport(ObjectiveC)
4+
public func allTests() -> [XCTestCaseEntry] {
5+
return [
6+
testCase(PublishedObjectTests.allTests),
7+
]
8+
}
9+
#endif

0 commit comments

Comments
 (0)