Skip to content

Commit 6543ee7

Browse files
authored
Refactor the test code to use Testing instead of XCTest (#115)
1 parent 2a4449d commit 6543ee7

File tree

8 files changed

+322
-290
lines changed

8 files changed

+322
-290
lines changed

Tests/OneWayTestingTests/TestingTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ private struct TestReducer: Reducer {
8484
return .none
8585
case .delayedIncrement:
8686
return .single {
87-
try! await Task.sleep(nanoseconds: NSEC_PER_MSEC * 100)
87+
try! await Task.sleep(for: .milliseconds(100))
8888
return .increment
8989
}
9090
case let .setName(name):

Tests/OneWayTestingTests/XCTestTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ private struct TestReducer: Reducer {
8181
return .none
8282
case .delayedIncrement:
8383
return .single {
84-
try! await Task.sleep(nanoseconds: NSEC_PER_MSEC * 100)
84+
try! await Task.sleep(for: .milliseconds(100))
8585
return .increment
8686
}
8787
case let .setName(name):

Tests/OneWayTests/EffectTests.swift

Lines changed: 101 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -5,53 +5,57 @@
55
// Copyright (c) 2022-2025 SeungYeop Yeom ( https://github.com/DevYeom ).
66
//
77

8+
import Testing
89
import Clocks
910
import OneWay
10-
import XCTest
1111

12-
final class EffectTests: XCTestCase {
13-
enum Action: Sendable {
12+
struct EffectTests {
13+
enum Action: Sendable, Equatable {
1414
case first
1515
case second
1616
case third
1717
case fourth
1818
case fifth
1919
}
2020

21-
func test_just() async {
21+
@Test
22+
func just() async {
2223
let values = Effects.Just(Action.first).values
2324

2425
var result: [Action] = []
2526
for await value in values {
2627
result.append(value)
2728
}
2829

29-
XCTAssertEqual(result, [.first])
30+
#expect(result == [.first])
3031
}
3132

32-
func test_cancel() async {
33+
@Test
34+
func cancel() async {
3335
let cancel = AnyEffect<Action>.cancel("100")
3436
let method = cancel.method
3537

3638
if case .cancel(let id) = method {
37-
XCTAssertEqual(id as! String, "100")
39+
#expect(id as? String == "100")
3840
} else {
39-
XCTFail()
41+
Issue.record("method should be .cancel")
4042
}
4143
}
4244

43-
func test_cancellable() async {
45+
@Test
46+
func cancellable() async {
4447
let effect = Effects.Just("").eraseToAnyEffect().cancellable("100")
4548
let method = effect.method
4649

4750
if case let .register(id, _) = method {
48-
XCTAssertEqual(id as! String, "100")
51+
#expect(id as? String == "100")
4952
} else {
50-
XCTFail()
53+
Issue.record("method should be .register")
5154
}
5255
}
5356

54-
func test_single() async {
57+
@Test
58+
func single() async {
5559
let clock = TestClock()
5660

5761
let values = Effects.Single {
@@ -65,10 +69,11 @@ final class EffectTests: XCTestCase {
6569
result.append(value)
6670
}
6771

68-
XCTAssertEqual(result, [.first])
72+
#expect(result == [.first])
6973
}
7074

71-
func test_sequence() async {
75+
@Test
76+
func sequence() async {
7277
let stream = AsyncStream { continuation in
7378
for number in 1 ... 5 {
7479
continuation.yield(number)
@@ -87,7 +92,7 @@ final class EffectTests: XCTestCase {
8792
case 5: action = .fifth
8893
default:
8994
action = .first
90-
XCTFail()
95+
Issue.record("should not be reached")
9196
}
9297
send(action)
9398
}
@@ -98,19 +103,18 @@ final class EffectTests: XCTestCase {
98103
result.append(value)
99104
}
100105

101-
XCTAssertEqual(
102-
result,
103-
[
104-
.first,
105-
.second,
106-
.third,
107-
.fourth,
108-
.fifth,
109-
]
110-
)
106+
let expectation: [Action] = [
107+
.first,
108+
.second,
109+
.third,
110+
.fourth,
111+
.fifth,
112+
]
113+
#expect(result == expectation)
111114
}
112115

113-
func test_concat() async {
116+
@Test
117+
func concat() async {
114118
let clock = TestClock()
115119

116120
let first = Effects.Just(Action.first).eraseToAnyEffect()
@@ -142,20 +146,18 @@ final class EffectTests: XCTestCase {
142146
result.append(value)
143147
}
144148

145-
146-
XCTAssertEqual(
147-
result,
148-
[
149-
.first,
150-
.second,
151-
.third,
152-
.fourth,
153-
.fifth,
154-
]
155-
)
149+
let expectation: [Action] = [
150+
.first,
151+
.second,
152+
.third,
153+
.fourth,
154+
.fifth,
155+
]
156+
#expect(result == expectation)
156157
}
157158

158-
func test_concatIncludingMerge() async {
159+
@Test
160+
func concatIncludingMerge() async {
159161
let clock = TestClock()
160162

161163
let first = Effects.Single {
@@ -191,19 +193,18 @@ final class EffectTests: XCTestCase {
191193
result.append(value)
192194
}
193195

194-
XCTAssertEqual(
195-
result,
196-
[
197-
.first,
198-
.second,
199-
.third,
200-
.fourth,
201-
.fifth,
202-
]
203-
)
196+
let expectation: [Action] = [
197+
.first,
198+
.second,
199+
.third,
200+
.fourth,
201+
.fifth,
202+
]
203+
#expect(result == expectation)
204204
}
205205

206-
func test_merge() async {
206+
@Test
207+
func merge() async {
207208
let clock = TestClock()
208209

209210
let first = Effects.Single {
@@ -241,19 +242,18 @@ final class EffectTests: XCTestCase {
241242
result.append(value)
242243
}
243244

244-
XCTAssertEqual(
245-
result,
246-
[
247-
.first,
248-
.second,
249-
.third,
250-
.fourth,
251-
.fifth,
252-
]
253-
)
245+
let expectation: [Action] = [
246+
.first,
247+
.second,
248+
.third,
249+
.fourth,
250+
.fifth,
251+
]
252+
#expect(result == expectation)
254253
}
255254

256-
func test_mergeIncludingConcat() async {
255+
@Test
256+
func mergeIncludingConcat() async {
257257
let clock = TestClock()
258258

259259
let first = Effects.Single {
@@ -289,19 +289,18 @@ final class EffectTests: XCTestCase {
289289
result.append(value)
290290
}
291291

292-
XCTAssertEqual(
293-
result,
294-
[
295-
.first,
296-
.second,
297-
.third,
298-
.fourth,
299-
.fifth,
300-
]
301-
)
292+
let expectation: [Action] = [
293+
.first,
294+
.second,
295+
.third,
296+
.fourth,
297+
.fifth,
298+
]
299+
#expect(result == expectation)
302300
}
303301

304-
func test_createSynchronously() async {
302+
@Test
303+
func createSynchronously() async {
305304
let values = Effects.Create { continuation in
306305
continuation.yield(Action.first)
307306
continuation.yield(Action.second)
@@ -316,19 +315,18 @@ final class EffectTests: XCTestCase {
316315
result.append(value)
317316
}
318317

319-
XCTAssertEqual(
320-
result,
321-
[
322-
.first,
323-
.second,
324-
.third,
325-
.fourth,
326-
.fifth,
327-
]
328-
)
318+
let expectation: [Action] = [
319+
.first,
320+
.second,
321+
.third,
322+
.fourth,
323+
.fifth,
324+
]
325+
#expect(result == expectation)
329326
}
330327

331-
func test_createAsynchronously() async {
328+
@Test
329+
func createAsynchronously() async {
332330
let clock = TestClock()
333331

334332
let values = Effects.Create { continuation in
@@ -355,19 +353,18 @@ final class EffectTests: XCTestCase {
355353
result.append(value)
356354
}
357355

358-
XCTAssertEqual(
359-
result,
360-
[
361-
.first,
362-
.second,
363-
.third,
364-
.fourth,
365-
.fifth,
366-
]
367-
)
356+
let expectation: [Action] = [
357+
.first,
358+
.second,
359+
.third,
360+
.fourth,
361+
.fifth,
362+
]
363+
#expect(result == expectation)
368364
}
369365

370-
func test_createAsynchronouslyWithCompletionHandler() async {
366+
@Test
367+
func createAsynchronouslyWithCompletionHandler() async {
371368
let values = Effects.Create { continuation in
372369
perform { action in
373370
continuation.yield(action)
@@ -382,23 +379,22 @@ final class EffectTests: XCTestCase {
382379
result.append(value)
383380
}
384381

385-
XCTAssertEqual(
386-
result,
387-
[
388-
.first,
389-
.second,
390-
.third,
391-
.fourth,
392-
.fifth,
393-
]
394-
)
382+
let expectation: [Action] = [
383+
.first,
384+
.second,
385+
.third,
386+
.fourth,
387+
.fifth,
388+
]
389+
#expect(result == expectation)
395390

396391
func perform(completionHandler: @Sendable @escaping (Action) -> Void) {
397-
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
392+
Task {
398393
completionHandler(.first)
399394
completionHandler(.second)
400395
}
401-
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
396+
Task {
397+
try await Task.sleep(for: .milliseconds(100))
402398
completionHandler(.third)
403399
completionHandler(.fourth)
404400
completionHandler(.fifth)

0 commit comments

Comments
 (0)