Skip to content

Commit af3c078

Browse files
committed
Swift Testing
1 parent f674571 commit af3c078

5 files changed

+375
-74
lines changed

Tests/AllocatedLock+Unsafe.swift

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//
2+
// AllocatedLock+Unsafe.swift
3+
// swift-mutex
4+
//
5+
// Created by Simon Whitty on 11/02/2025.
6+
// Copyright 2025 Simon Whitty
7+
//
8+
// Distributed under the permissive MIT license
9+
// Get the latest version from here:
10+
//
11+
// https://github.com/swhitty/swift-mutex
12+
//
13+
// Permission is hereby granted, free of charge, to any person obtaining a copy
14+
// of this software and associated documentation files (the "Software"), to deal
15+
// in the Software without restriction, including without limitation the rights
16+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17+
// copies of the Software, and to permit persons to whom the Software is
18+
// furnished to do so, subject to the following conditions:
19+
//
20+
// The above copyright notice and this permission notice shall be included in all
21+
// copies or substantial portions of the Software.
22+
//
23+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29+
// SOFTWARE.
30+
//
31+
32+
@testable import Mutex
33+
34+
// sidestep warning: unavailable from asynchronous contexts
35+
extension AllocatedLock {
36+
func unsafeLock() { storage.lock() }
37+
func unsafeUnlock() { storage.unlock() }
38+
}

Tests/AllocatedLockTests.swift

+52-58
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@
2929
// SOFTWARE.
3030
//
3131

32+
#if canImport(Testing)
3233
@testable import Mutex
33-
import XCTest
34+
import Testing
3435

35-
final class AllocatedLockTests: XCTestCase {
36+
struct AllocatedLockTests {
3637

37-
func testLockState_IsProtected() async {
38+
@Test
39+
func lockState_IsProtected() async {
3840
let state = AllocatedLock<Int>(initialState: 0)
3941

4042
let total = await withTaskGroup(of: Void.self) { group in
@@ -47,17 +49,18 @@ final class AllocatedLockTests: XCTestCase {
4749
return state.withLock { $0 }
4850
}
4951

50-
XCTAssertEqual(total, 500500)
52+
#expect(total == 500500)
5153
}
5254

53-
func testLock_ReturnsValue() async {
55+
@Test
56+
func lock_ReturnsValue() async {
5457
let lock = AllocatedLock()
5558
let value = lock.withLock { true }
56-
XCTAssertTrue(value)
59+
#expect(value)
5760
}
5861

59-
60-
func testLock_Blocks() async {
62+
@Test
63+
func lock_Blocks() async {
6164
let lock = AllocatedLock()
6265
await MainActor.run {
6366
lock.unsafeLock()
@@ -82,99 +85,95 @@ final class AllocatedLockTests: XCTestCase {
8285
let second = await group.next()!
8386
return [first, second]
8487
}
85-
XCTAssertEqual(results, [true, false])
88+
#expect(results == [true, false])
8689
}
8790

88-
func testTryLock() {
91+
@Test
92+
func tryLock() {
8993
let lock = AllocatedLock()
9094
let value = lock.withLock { true }
91-
XCTAssertTrue(value)
95+
#expect(value)
9296
}
9397

94-
func testIfAvailable() {
98+
@Test
99+
func ifAvailable() {
95100
let lock = AllocatedLock(uncheckedState: 5)
96-
XCTAssertEqual(
97-
lock.withLock { _ in "fish" },
98-
"fish"
101+
#expect(
102+
lock.withLock { _ in "fish" } == "fish"
99103
)
100104

101105
lock.unsafeLock()
102-
XCTAssertEqual(
103-
lock.withLockIfAvailable { _ in "fish" },
104-
String?.none
106+
#expect(
107+
lock.withLockIfAvailable { _ in "fish" } == nil
105108
)
106109

107110
lock.unsafeUnlock()
108-
XCTAssertEqual(
109-
lock.withLockIfAvailable { _ in "fish" },
110-
"fish"
111+
#expect(
112+
lock.withLockIfAvailable { _ in "fish" } == "fish"
111113
)
112114
}
113115

114-
func testIfAvailableUnchecked() {
116+
@Test
117+
func ifAvailableUnchecked() {
115118
let lock = AllocatedLock(uncheckedState: NonSendable("fish"))
116-
XCTAssertEqual(
117-
lock.withLockUnchecked { $0 }.name,
118-
"fish"
119+
#expect(
120+
lock.withLockUnchecked { $0 }.name == "fish"
119121
)
120122

121123
lock.unsafeLock()
122-
XCTAssertNil(
123-
lock.withLockIfAvailableUnchecked { $0 }?.name
124+
#expect(
125+
lock.withLockIfAvailableUnchecked { $0 }?.name == nil
124126
)
125127

126128
lock.unsafeUnlock()
127-
XCTAssertEqual(
128-
lock.withLockIfAvailableUnchecked { $0 }?.name,
129-
"fish"
129+
#expect(
130+
lock.withLockIfAvailableUnchecked { $0 }?.name == "fish"
130131
)
131132
}
132133

133-
func testVoidIfAvailable() {
134+
@Test
135+
func voidIfAvailable() {
134136
let lock = AllocatedLock()
135-
XCTAssertEqual(
136-
lock.withLock { "fish" },
137-
"fish"
137+
#expect(
138+
lock.withLock { "fish" } == "fish"
138139
)
139140

140141
lock.unsafeLock()
141-
XCTAssertEqual(
142-
lock.withLockIfAvailable { "fish" },
143-
String?.none
142+
#expect(
143+
lock.withLockIfAvailable { "fish" } == nil
144144
)
145145

146146
lock.unsafeUnlock()
147-
XCTAssertEqual(
148-
lock.withLockIfAvailable { "fish" },
149-
"fish"
147+
#expect(
148+
lock.withLockIfAvailable { "fish" } == "fish"
150149
)
151150
}
152151

153-
func testVoidIfAvailableUnchecked() {
152+
@Test
153+
func voidIfAvailableUnchecked() {
154154
let lock = AllocatedLock()
155-
XCTAssertEqual(
156-
lock.withLockUnchecked { NonSendable("fish") }.name,
157-
"fish"
155+
#expect(
156+
lock.withLockUnchecked { NonSendable("fish") }.name == "fish"
158157
)
159158

160159
lock.lock()
161-
XCTAssertNil(
162-
lock.withLockIfAvailableUnchecked { NonSendable("fish") }
160+
#expect(
161+
lock.withLockIfAvailableUnchecked { NonSendable("fish") } == nil
163162
)
164163

165164
lock.unlock()
166-
XCTAssertEqual(
167-
lock.withLockIfAvailableUnchecked { NonSendable("chips") }?.name,
168-
"chips"
165+
#expect(
166+
lock.withLockIfAvailableUnchecked { NonSendable("chips") }?.name == "chips"
169167
)
170168
}
171169

172-
func testVoidLock() {
170+
@Test
171+
func voidLock() {
173172
let lock = AllocatedLock()
174173
lock.lock()
175-
XCTAssertFalse(lock.lockIfAvailable())
174+
#expect(lock.lockIfAvailable() == false)
176175
lock.unlock()
177-
XCTAssertTrue(lock.lockIfAvailable())
176+
#expect(lock.lockIfAvailable())
178177
lock.unlock()
179178
}
180179
}
@@ -187,9 +186,4 @@ public final class NonSendable {
187186
self.name = name
188187
}
189188
}
190-
191-
// sidestep warning: unavailable from asynchronous contexts
192-
extension AllocatedLock {
193-
func unsafeLock() { storage.lock() }
194-
func unsafeUnlock() { storage.unlock() }
195-
}
189+
#endif

0 commit comments

Comments
 (0)