Skip to content

Commit fbcae0d

Browse files
committed
Add tests for EngineCommand and EngineResponse
1 parent 49c5224 commit fbcae0d

9 files changed

Lines changed: 473 additions & 17 deletions

File tree

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Scheme
3+
LastUpgradeVersion = "1430"
4+
version = "1.7">
5+
<BuildAction
6+
parallelizeBuildables = "YES"
7+
buildImplicitDependencies = "YES">
8+
<BuildActionEntries>
9+
<BuildActionEntry
10+
buildForTesting = "YES"
11+
buildForRunning = "YES"
12+
buildForProfiling = "YES"
13+
buildForArchiving = "YES"
14+
buildForAnalyzing = "YES">
15+
<BuildableReference
16+
BuildableIdentifier = "primary"
17+
BlueprintIdentifier = "ChessKitEngine"
18+
BuildableName = "ChessKitEngine"
19+
BlueprintName = "ChessKitEngine"
20+
ReferencedContainer = "container:">
21+
</BuildableReference>
22+
</BuildActionEntry>
23+
</BuildActionEntries>
24+
</BuildAction>
25+
<TestAction
26+
buildConfiguration = "Debug"
27+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
28+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
29+
shouldUseLaunchSchemeArgsEnv = "YES"
30+
shouldAutocreateTestPlan = "YES">
31+
</TestAction>
32+
<LaunchAction
33+
buildConfiguration = "Debug"
34+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
35+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
36+
launchStyle = "0"
37+
useCustomWorkingDirectory = "NO"
38+
ignoresPersistentStateOnLaunch = "NO"
39+
debugDocumentVersioning = "YES"
40+
debugServiceExtension = "internal"
41+
allowLocationSimulation = "YES">
42+
</LaunchAction>
43+
<ProfileAction
44+
buildConfiguration = "Release"
45+
shouldUseLaunchSchemeArgsEnv = "YES"
46+
savedToolIdentifier = ""
47+
useCustomWorkingDirectory = "NO"
48+
debugDocumentVersioning = "YES">
49+
<MacroExpansion>
50+
<BuildableReference
51+
BuildableIdentifier = "primary"
52+
BlueprintIdentifier = "ChessKitEngine"
53+
BuildableName = "ChessKitEngine"
54+
BlueprintName = "ChessKitEngine"
55+
ReferencedContainer = "container:">
56+
</BuildableReference>
57+
</MacroExpansion>
58+
</ProfileAction>
59+
<AnalyzeAction
60+
buildConfiguration = "Debug">
61+
</AnalyzeAction>
62+
<ArchiveAction
63+
buildConfiguration = "Release"
64+
revealArchiveInOrganizer = "YES">
65+
</ArchiveAction>
66+
</Scheme>
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Scheme
3+
LastUpgradeVersion = "1430"
4+
version = "1.7">
5+
<BuildAction
6+
parallelizeBuildables = "YES"
7+
buildImplicitDependencies = "YES">
8+
<BuildActionEntries>
9+
<BuildActionEntry
10+
buildForTesting = "YES"
11+
buildForRunning = "YES"
12+
buildForProfiling = "YES"
13+
buildForArchiving = "YES"
14+
buildForAnalyzing = "YES">
15+
<BuildableReference
16+
BuildableIdentifier = "primary"
17+
BlueprintIdentifier = "ChessKitEngine"
18+
BuildableName = "ChessKitEngine"
19+
BlueprintName = "ChessKitEngine"
20+
ReferencedContainer = "container:">
21+
</BuildableReference>
22+
</BuildActionEntry>
23+
<BuildActionEntry
24+
buildForTesting = "YES"
25+
buildForRunning = "YES"
26+
buildForProfiling = "YES"
27+
buildForArchiving = "YES"
28+
buildForAnalyzing = "YES">
29+
<BuildableReference
30+
BuildableIdentifier = "primary"
31+
BlueprintIdentifier = "ChessKitEngineCore"
32+
BuildableName = "ChessKitEngineCore"
33+
BlueprintName = "ChessKitEngineCore"
34+
ReferencedContainer = "container:">
35+
</BuildableReference>
36+
</BuildActionEntry>
37+
<BuildActionEntry
38+
buildForTesting = "YES"
39+
buildForRunning = "YES"
40+
buildForProfiling = "NO"
41+
buildForArchiving = "NO"
42+
buildForAnalyzing = "YES">
43+
<BuildableReference
44+
BuildableIdentifier = "primary"
45+
BlueprintIdentifier = "ChessKitEngineTests"
46+
BuildableName = "ChessKitEngineTests"
47+
BlueprintName = "ChessKitEngineTests"
48+
ReferencedContainer = "container:">
49+
</BuildableReference>
50+
</BuildActionEntry>
51+
</BuildActionEntries>
52+
</BuildAction>
53+
<TestAction
54+
buildConfiguration = "Debug"
55+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
56+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
57+
shouldUseLaunchSchemeArgsEnv = "YES"
58+
shouldAutocreateTestPlan = "YES">
59+
<Testables>
60+
<TestableReference
61+
skipped = "NO">
62+
<BuildableReference
63+
BuildableIdentifier = "primary"
64+
BlueprintIdentifier = "ChessKitEngineTests"
65+
BuildableName = "ChessKitEngineTests"
66+
BlueprintName = "ChessKitEngineTests"
67+
ReferencedContainer = "container:">
68+
</BuildableReference>
69+
</TestableReference>
70+
</Testables>
71+
</TestAction>
72+
<LaunchAction
73+
buildConfiguration = "Debug"
74+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
75+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
76+
launchStyle = "0"
77+
useCustomWorkingDirectory = "NO"
78+
ignoresPersistentStateOnLaunch = "NO"
79+
debugDocumentVersioning = "YES"
80+
debugServiceExtension = "internal"
81+
allowLocationSimulation = "YES">
82+
</LaunchAction>
83+
<ProfileAction
84+
buildConfiguration = "Release"
85+
shouldUseLaunchSchemeArgsEnv = "YES"
86+
savedToolIdentifier = ""
87+
useCustomWorkingDirectory = "NO"
88+
debugDocumentVersioning = "YES">
89+
<MacroExpansion>
90+
<BuildableReference
91+
BuildableIdentifier = "primary"
92+
BlueprintIdentifier = "ChessKitEngine"
93+
BuildableName = "ChessKitEngine"
94+
BlueprintName = "ChessKitEngine"
95+
ReferencedContainer = "container:">
96+
</BuildableReference>
97+
</MacroExpansion>
98+
</ProfileAction>
99+
<AnalyzeAction
100+
buildConfiguration = "Debug">
101+
</AnalyzeAction>
102+
<ArchiveAction
103+
buildConfiguration = "Release"
104+
revealArchiveInOrganizer = "YES">
105+
</ArchiveAction>
106+
</Scheme>

CHANGELOG.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
1-
# [Unreleased]
1+
# ChessKitEngine 0.1.3
2+
Released Saturday, April 15, 2023.
23

3-
* Simplify internal Obj-C and C++ targets
4+
#### Improvements
5+
* Add default `nil` value for `value` parameter in `EngineCommand.setoption(id:value:)`
6+
7+
#### Bug Fixes
48
* Fix `loggingEnabled` default value to match documentation
9+
* Fix `EngineCommand.PositionString(rawValue:)` when passing a FEN string
10+
11+
#### Technical Improvements
12+
* Simplify internal Obj-C and C++ targets
13+
* Increase test coverage
514

615
# ChessKitEngine 0.1.2
716
Released Friday, April 14, 2023.

Sources/ChessKitEngine/EngineCommand/EngineCommand.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public enum EngineCommand {
3030
///
3131
/// See [UCI protocol documentation](https://backscattering.de/chess/uci/2006-04.txt)
3232
/// for more information.
33-
case setoption(id: String, value: String?)
33+
case setoption(id: String, value: String? = nil)
3434

3535
/// `"ucinewgame"`
3636
///

Sources/ChessKitEngine/EngineCommand/EngineCommandPosition.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public extension EngineCommand {
77

88
/// Possible positions that can be passed to
99
/// `EngineCommand.position`.
10-
enum PositionString: RawRepresentable {
10+
enum PositionString: Equatable, RawRepresentable {
1111
/// Any FEN position, given in the provided `String`.
1212
case fen(String)
1313
/// The starting position.
@@ -27,8 +27,9 @@ public extension EngineCommand {
2727
self = .startpos
2828
} else if rawValue.starts(with: "fen") {
2929
let tokens = rawValue.components(separatedBy: " ")
30-
guard tokens.count == 2 else { return nil }
31-
self = .fen(tokens[1])
30+
// FEN strings must have 6 components
31+
guard tokens.count == 7 else { return nil }
32+
self = .fen(tokens[1...6].joined(separator: " "))
3233
} else {
3334
return nil
3435
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//
2+
// EngineCommandPositionTests.swift
3+
// ChessKitEngineTests
4+
//
5+
6+
import XCTest
7+
@testable import ChessKitEngine
8+
9+
class EngineCommandPositionTests: XCTestCase {
10+
11+
func testPositionStringRawValue() {
12+
let p = EngineCommand.PositionString.startpos
13+
XCTAssertEqual(p.rawValue, "startpos")
14+
15+
let fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
16+
let f = EngineCommand.PositionString.fen(fen)
17+
XCTAssertEqual(f.rawValue, "fen \(fen)")
18+
}
19+
20+
func testPositionStringRawValueInit() {
21+
XCTAssertEqual(
22+
EngineCommand.PositionString(rawValue: "startpos"),
23+
.startpos
24+
)
25+
26+
let fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
27+
XCTAssertEqual(
28+
EngineCommand.PositionString(rawValue: "fen \(fen)"),
29+
.fen(fen)
30+
)
31+
}
32+
33+
34+
func testInvalidFENPositionStrings() {
35+
let fen1 = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR"
36+
XCTAssertNil(EngineCommand.PositionString(rawValue: "fen \(fen1)"))
37+
38+
let fen2 = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w"
39+
XCTAssertNil(EngineCommand.PositionString(rawValue: "fen \(fen2)"))
40+
41+
let fen3 = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq"
42+
XCTAssertNil(EngineCommand.PositionString(rawValue: "fen \(fen3)"))
43+
44+
let fen4 = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq -"
45+
XCTAssertNil(EngineCommand.PositionString(rawValue: "fen \(fen4)"))
46+
47+
let fen5 = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0"
48+
XCTAssertNil(EngineCommand.PositionString(rawValue: "fen \(fen5)"))
49+
}
50+
51+
func testInvalidPositionString() {
52+
XCTAssertNil(EngineCommand.PositionString(rawValue: "invalid"))
53+
XCTAssertNil(EngineCommand.PositionString(rawValue: ""))
54+
}
55+
56+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
//
2+
// EngineCommandTests.swift
3+
// ChessKitEngineTests
4+
//
5+
6+
import XCTest
7+
@testable import ChessKitEngine
8+
9+
class EngineCommandTests: XCTestCase {
10+
11+
func testDebugCommand() {
12+
let debugOn = EngineCommand.debug(on: true)
13+
XCTAssertEqual(debugOn.rawValue, "debug on")
14+
15+
let debugOff = EngineCommand.debug(on: false)
16+
XCTAssertEqual(debugOff.rawValue, "debug off")
17+
}
18+
19+
func testUciCommand() {
20+
XCTAssertEqual(EngineCommand.uci.rawValue, "uci")
21+
}
22+
23+
func testIsReadyCommand() {
24+
XCTAssertEqual(EngineCommand.isready.rawValue, "isready")
25+
}
26+
27+
func testSetOptionCommand() {
28+
let setOption1 = EngineCommand.setoption(id: "name", value: "val")
29+
XCTAssertEqual(
30+
setOption1.rawValue,
31+
"setoption name name value val"
32+
)
33+
34+
let setOption2 = EngineCommand.setoption(id: "name")
35+
XCTAssertEqual(
36+
setOption2.rawValue,
37+
"setoption name name"
38+
)
39+
}
40+
41+
func testUciNewGameCommand() {
42+
XCTAssertEqual(EngineCommand.ucinewgame.rawValue, "ucinewgame")
43+
}
44+
45+
func testPositionCommand() {
46+
XCTAssertEqual(
47+
EngineCommand.position(
48+
.startpos,
49+
moves: ["e2e4", "c7c6"]
50+
).rawValue,
51+
"position startpos moves e2e4 c7c6"
52+
)
53+
54+
XCTAssertEqual(
55+
EngineCommand.position(
56+
.fen("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")
57+
).rawValue,
58+
"position fen rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
59+
)
60+
61+
XCTAssertEqual(
62+
EngineCommand.position(
63+
.fen("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"),
64+
moves: ["e2e4", "c7c6"]
65+
).rawValue,
66+
"position fen rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1 moves e2e4 c7c6"
67+
)
68+
}
69+
70+
func testGoCommand() {
71+
let go = EngineCommand.go(
72+
searchmoves: ["e2e4", "c7c6"],
73+
ponder: true,
74+
wtime: 5,
75+
btime: 5,
76+
winc: 2,
77+
binc: 2,
78+
movestogo: 10,
79+
depth: 15,
80+
nodes: 100,
81+
mate: 2,
82+
movetime: 3,
83+
infinite: false
84+
)
85+
86+
XCTAssertEqual(go.rawValue, "go searchmoves e2e4 c7c6 ponder wtime 5 btime 5 winc 2 binc 2 movestogo 10 depth 15 nodes 100 mate 2 movetime 3")
87+
88+
let goInfinite = EngineCommand.go(infinite: true)
89+
XCTAssertEqual(goInfinite.rawValue, "go infinite")
90+
}
91+
92+
func testStopCommand() {
93+
XCTAssertEqual(EngineCommand.stop.rawValue, "stop")
94+
}
95+
96+
func testPonderhitCommand() {
97+
XCTAssertEqual(EngineCommand.ponderhit.rawValue, "ponderhit")
98+
}
99+
100+
func testQuitCommand() {
101+
XCTAssertEqual(EngineCommand.quit.rawValue, "quit")
102+
}
103+
104+
}

0 commit comments

Comments
 (0)