Skip to content

Commit 1d62745

Browse files
committed
Add didCheckKing delegate method to unit tests
1 parent 0344dcb commit 1d62745

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

Sources/ChessKit/Board.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
/// as pawn promotions and end results.
99
public protocol BoardDelegate: AnyObject, Sendable {
1010
func didPromote(with move: Move)
11-
func didCheckKing(ofColor: Piece.Color)
11+
func didCheckKing(ofColor color: Piece.Color)
1212
func didEnd(with result: Board.EndResult)
1313
}
1414

@@ -233,7 +233,7 @@ public struct Board: Sendable {
233233
} else if positionHashCounts[position.hashValue] == 3 {
234234
delegate?.didEnd(with: .draw(.repetition))
235235
} else if checkState == .check {
236-
delegate?.didCheckKing(ofColor: processedMove.piece.color)
236+
delegate?.didCheckKing(ofColor: processedMove.piece.color.opposite)
237237
}
238238

239239
// pawn promotion

Tests/ChessKitTests/BoardTests.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,23 @@ final class BoardTests: XCTestCase {
352352

353353
func testCheckMove() {
354354
var board = Board(position: .init(fen: "k7/7R/8/8/8/8/K7/8 w - - 0 1")!)
355+
356+
nonisolated(unsafe) var expectation: XCTestExpectation? = self.expectation(description: "Board returns check result")
357+
358+
let delegate = MockBoardDelegate(didCheckKing: { color in
359+
if color == .black {
360+
expectation?.fulfill()
361+
expectation = nil
362+
} else {
363+
XCTFail()
364+
}
365+
})
366+
367+
board.delegate = delegate
355368
let move = board.move(pieceAt: .h7, to: .h8)
356369
XCTAssertEqual(move?.checkState, .check)
370+
371+
waitForExpectations(timeout: 1.0)
357372
}
358373

359374
func testCheckmateMove() {

Tests/ChessKitTests/Utilities/MockBoardDelegate.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,25 @@
77

88
final class MockBoardDelegate: BoardDelegate {
99
private let didPromote: (@Sendable (Move) -> Void)?
10+
private let didCheckKing: (@Sendable (Piece.Color) -> Void)?
1011
private let didEnd: (@Sendable (Board.EndResult) -> Void)?
1112

1213
init(
1314
didPromote: (@Sendable (Move) -> Void)? = nil,
15+
didCheckKing: (@Sendable (Piece.Color) -> Void)? = nil,
1416
didEnd: (@Sendable (Board.EndResult) -> Void)? = nil
1517
) {
1618
self.didPromote = didPromote
19+
self.didCheckKing = didCheckKing
1720
self.didEnd = didEnd
1821
}
1922

2023
func didPromote(with move: Move) {
2124
didPromote?(move)
2225
}
2326

24-
func didCheckKing(ofColor: Piece.Color) {
25-
27+
func didCheckKing(ofColor color: Piece.Color) {
28+
didCheckKing?(color)
2629
}
2730

2831
func didEnd(with result: Board.EndResult) {

0 commit comments

Comments
 (0)