-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #175 from fwcd/swift-testing
Migrate unit tests to swift-testing
- Loading branch information
Showing
26 changed files
with
350 additions
and
403 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
public extension Double { | ||
func isApproximatelyEqual(to other: Self, accuracy: Double = 0.0001) -> Bool { | ||
abs(self - other) < accuracy | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 39 additions & 39 deletions
78
Tests/D2CommandTests/Game/Chess/Notation/ChessNotationParserTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,60 @@ | ||
import XCTest | ||
import Testing | ||
@testable import D2Commands | ||
|
||
final class ChessNotationParserTests: XCTestCase { | ||
func testShortAlgebraicNotation() throws { | ||
struct ChessNotationParserTests { | ||
@Test func shortAlgebraicNotation() { | ||
let parser = ShortAlgebraicNotationParser() | ||
|
||
let bishopMove = parser.parse("Lc4") | ||
XCTAssertEqual(bishopMove?.pieceType, ChessPieceType.bishop) | ||
XCTAssertEqual(bishopMove?.destinationX, 2) | ||
XCTAssertEqual(bishopMove?.destinationY, 4) | ||
XCTAssertEqual(bishopMove?.isCapture, false) | ||
#expect(bishopMove?.pieceType == ChessPieceType.bishop) | ||
#expect(bishopMove?.destinationX == 2) | ||
#expect(bishopMove?.destinationY == 4) | ||
#expect(bishopMove?.isCapture == false) | ||
|
||
let captureMove = parser.parse("Bxb5") | ||
XCTAssertEqual(captureMove?.pieceType, ChessPieceType.bishop) | ||
XCTAssertEqual(captureMove?.isCapture, true) | ||
XCTAssertEqual(captureMove?.destinationX, 1) | ||
XCTAssertEqual(captureMove?.destinationY, 3) | ||
#expect(captureMove?.pieceType == ChessPieceType.bishop) | ||
#expect(captureMove?.isCapture == true) | ||
#expect(captureMove?.destinationX == 1) | ||
#expect(captureMove?.destinationY == 3) | ||
|
||
let pawnMove = parser.parse("g3") | ||
XCTAssertEqual(pawnMove?.pieceType, ChessPieceType.pawn) | ||
XCTAssertEqual(pawnMove?.isCapture, false) | ||
XCTAssertEqual(pawnMove?.isEnPassant, false) | ||
XCTAssertEqual(pawnMove?.destinationX, 6) | ||
XCTAssertEqual(pawnMove?.destinationY, 5) | ||
#expect(pawnMove?.pieceType == ChessPieceType.pawn) | ||
#expect(pawnMove?.isCapture == false) | ||
#expect(pawnMove?.isEnPassant == false) | ||
#expect(pawnMove?.destinationX == 6) | ||
#expect(pawnMove?.destinationY == 5) | ||
|
||
let enPassantMove = parser.parse("fxg6 e. p.") | ||
XCTAssertEqual(enPassantMove?.pieceType, ChessPieceType.pawn) | ||
XCTAssertEqual(enPassantMove?.isCapture, true) | ||
XCTAssertEqual(enPassantMove?.isEnPassant, true) | ||
XCTAssertEqual(enPassantMove?.originX, 5) | ||
XCTAssertEqual(enPassantMove?.destinationX, 6) | ||
XCTAssertEqual(enPassantMove?.destinationY, 2) | ||
#expect(enPassantMove?.pieceType == ChessPieceType.pawn) | ||
#expect(enPassantMove?.isCapture == true) | ||
#expect(enPassantMove?.isEnPassant == true) | ||
#expect(enPassantMove?.originX == 5) | ||
#expect(enPassantMove?.destinationX == 6) | ||
#expect(enPassantMove?.destinationY == 2) | ||
|
||
let knightMove1 = parser.parse("Sac7") | ||
XCTAssertEqual(knightMove1?.pieceType, ChessPieceType.knight) | ||
XCTAssertEqual(knightMove1?.originX, 0) | ||
XCTAssertEqual(knightMove1?.destinationX, 2) | ||
XCTAssertEqual(knightMove1?.destinationY, 1) | ||
#expect(knightMove1?.pieceType == ChessPieceType.knight) | ||
#expect(knightMove1?.originX == 0) | ||
#expect(knightMove1?.destinationX == 2) | ||
#expect(knightMove1?.destinationY == 1) | ||
|
||
let knightMove2 = parser.parse("Ne1xc4") | ||
XCTAssertEqual(knightMove2?.pieceType, ChessPieceType.knight) | ||
XCTAssertEqual(knightMove2?.originX, 4) | ||
XCTAssertEqual(knightMove2?.originY, 7) | ||
XCTAssertEqual(knightMove2?.isCapture, true) | ||
XCTAssertEqual(knightMove2?.destinationX, 2) | ||
XCTAssertEqual(knightMove2?.destinationY, 4) | ||
#expect(knightMove2?.pieceType == ChessPieceType.knight) | ||
#expect(knightMove2?.originX == 4) | ||
#expect(knightMove2?.originY == 7) | ||
#expect(knightMove2?.isCapture == true) | ||
#expect(knightMove2?.destinationX == 2) | ||
#expect(knightMove2?.destinationY == 4) | ||
|
||
let rookMove1 = parser.parse("R1c7") | ||
XCTAssertEqual(rookMove1?.pieceType, ChessPieceType.rook) | ||
XCTAssertEqual(rookMove1?.originY, 7) | ||
XCTAssertEqual(rookMove1?.destinationX, 2) | ||
XCTAssertEqual(rookMove1?.destinationY, 1) | ||
#expect(rookMove1?.pieceType == ChessPieceType.rook) | ||
#expect(rookMove1?.originY == 7) | ||
#expect(rookMove1?.destinationX == 2) | ||
#expect(rookMove1?.destinationY == 1) | ||
|
||
let rookMove2 = parser.parse("Th2") | ||
XCTAssertEqual(rookMove2?.pieceType, ChessPieceType.rook) | ||
XCTAssertEqual(rookMove2?.destinationX, 7) | ||
XCTAssertEqual(rookMove2?.destinationY, 6) | ||
#expect(rookMove2?.pieceType == ChessPieceType.rook) | ||
#expect(rookMove2?.destinationX == 7) | ||
#expect(rookMove2?.destinationY == 6) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,17 @@ | ||
import XCTest | ||
import Testing | ||
@testable import D2Commands | ||
|
||
final class WordleBoardTests: XCTestCase { | ||
func testClues() throws { | ||
XCTAssertEqual(WordleBoard.Clues(fromArray: [.here]).count, 1) | ||
XCTAssertEqual(WordleBoard.Clues(fromArray: [.nowhere, .somewhere]).count, 2) | ||
|
||
testCodingRoundtrip(for: []) | ||
testCodingRoundtrip(for: [.somewhere, .unknown]) | ||
testCodingRoundtrip(for: [.nowhere, .somewhere, .somewhere, .here, .nowhere]) | ||
struct WordleBoardTests { | ||
@Test func clues() { | ||
#expect(WordleBoard.Clues(fromArray: [.here]).count == 1) | ||
#expect(WordleBoard.Clues(fromArray: [.nowhere, .somewhere]).count == 2) | ||
} | ||
|
||
private func testCodingRoundtrip(for clues: [WordleBoard.Clue]) { | ||
XCTAssertEqual(Array(WordleBoard.Clues(fromArray: clues)), clues) | ||
@Test(arguments: [ | ||
[WordleBoard.Clue](), | ||
[.somewhere, .unknown], | ||
[.nowhere, .somewhere, .somewhere, .here, .nowhere], | ||
]) func codingRoundtrip(clues: [WordleBoard.Clue]) { | ||
#expect(Array(WordleBoard.Clues(fromArray: clues)) == clues) | ||
} | ||
} |
47 changes: 23 additions & 24 deletions
47
Tests/D2CommandTests/Math/Parse/ExpressionParserTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,36 @@ | ||
import XCTest | ||
import Testing | ||
import D2TestUtils | ||
@testable import D2Commands | ||
|
||
fileprivate let eps = 0.00001 | ||
|
||
final class ExpressionParserTests: XCTestCase { | ||
func testRPNExpressionParser() throws { | ||
struct ExpressionParserTests { | ||
@Test func rpnExpressionParser() throws { | ||
let parser = RPNExpressionParser() | ||
|
||
let rawProduct = "3 4 *" | ||
guard let product = try parser.parse(rawProduct) as? ProductNode else { XCTFail("\(rawProduct) should be a product node"); return } | ||
XCTAssertEqual(try product.lhs.evaluate(), 3.0, accuracy: eps) | ||
XCTAssertEqual(try product.rhs.evaluate(), 4.0, accuracy: eps) | ||
guard let product = try parser.parse(rawProduct) as? ProductNode else { Issue.record("\(rawProduct) should be a product node"); return } | ||
#expect(try product.lhs.evaluate().isApproximatelyEqual(to: 3.0)) | ||
#expect(try product.rhs.evaluate().isApproximatelyEqual(to: 4.0)) | ||
|
||
let rawQuotient = "2.1 -51.09 pi + 1 - /" | ||
guard let quotient = try parser.parse(rawQuotient) as? QuotientNode else { XCTFail("\(rawQuotient) should be a quotient node"); return } | ||
guard let quotientLeft = quotient.lhs as? ConstantNode else { XCTFail("Left-hand side of quotient should be a constant"); return } | ||
guard let quotientRight = quotient.rhs as? DifferenceNode else { XCTFail("Right-hand side of quotient should be a difference"); return } | ||
guard let differenceLeft = quotientRight.lhs as? SumNode else { XCTFail("Left-hand side of difference should be a sum"); return } | ||
guard let differenceRight = quotientRight.rhs as? ConstantNode else { XCTFail("Right-hand side of quotient should be a constant"); return } | ||
guard let sumLeft = differenceLeft.lhs as? ConstantNode else { XCTFail("Left-hand side of sum should be a constant"); return } | ||
guard let sumRight = differenceLeft.rhs as? ConstantNode else { XCTFail("Right-hand side of sum should be a constant"); return } | ||
XCTAssertEqual(try quotientLeft.evaluate(), 2.1, accuracy: eps) | ||
XCTAssertEqual(try sumLeft.evaluate(), -51.09, accuracy: eps) | ||
XCTAssertEqual(try sumRight.evaluate(), Double.pi, accuracy: eps) | ||
XCTAssertEqual(try differenceRight.evaluate(), 1.0, accuracy: eps) | ||
guard let quotient = try parser.parse(rawQuotient) as? QuotientNode else { Issue.record("\(rawQuotient) should be a quotient node"); return } | ||
guard let quotientLeft = quotient.lhs as? ConstantNode else { Issue.record("Left-hand side of quotient should be a constant"); return } | ||
guard let quotientRight = quotient.rhs as? DifferenceNode else { Issue.record("Right-hand side of quotient should be a difference"); return } | ||
guard let differenceLeft = quotientRight.lhs as? SumNode else { Issue.record("Left-hand side of difference should be a sum"); return } | ||
guard let differenceRight = quotientRight.rhs as? ConstantNode else { Issue.record("Right-hand side of quotient should be a constant"); return } | ||
guard let sumLeft = differenceLeft.lhs as? ConstantNode else { Issue.record("Left-hand side of sum should be a constant"); return } | ||
guard let sumRight = differenceLeft.rhs as? ConstantNode else { Issue.record("Right-hand side of sum should be a constant"); return } | ||
#expect(try quotientLeft.evaluate().isApproximatelyEqual(to: 2.1)) | ||
#expect(try sumLeft.evaluate().isApproximatelyEqual(to: -51.09)) | ||
#expect(try sumRight.evaluate().isApproximatelyEqual(to: Double.pi)) | ||
#expect(try differenceRight.evaluate().isApproximatelyEqual(to: 1.0)) | ||
} | ||
|
||
func testInfixExpressionParser() throws { | ||
@Test func infixExpressionParser() throws { | ||
let parser = InfixExpressionParser() | ||
|
||
XCTAssertTrue(try parser.parse("3 * 4").isEqual(to: ProductNode(lhs: ConstantNode(value: 3), rhs: ConstantNode(value: 4)))) | ||
XCTAssertTrue(try parser.parse("3 + 4").isEqual(to: SumNode(lhs: ConstantNode(value: 3), rhs: ConstantNode(value: 4)))) | ||
XCTAssertTrue(try parser.parse("4 - 9 * 8").isEqual(to: DifferenceNode(lhs: ConstantNode(value: 4), rhs: ProductNode(lhs: ConstantNode(value: 9), rhs: ConstantNode(value: 8))))) | ||
XCTAssertTrue(try parser.parse("(4 - 9) * 8").isEqual(to: ProductNode(lhs: DifferenceNode(lhs: ConstantNode(value: 4), rhs: ConstantNode(value: 9)), rhs: ConstantNode(value: 8)))) | ||
#expect(try parser.parse("3 * 4").isEqual(to: ProductNode(lhs: ConstantNode(value: 3), rhs: ConstantNode(value: 4)))) | ||
#expect(try parser.parse("3 + 4").isEqual(to: SumNode(lhs: ConstantNode(value: 3), rhs: ConstantNode(value: 4)))) | ||
#expect(try parser.parse("4 - 9 * 8").isEqual(to: DifferenceNode(lhs: ConstantNode(value: 4), rhs: ProductNode(lhs: ConstantNode(value: 9), rhs: ConstantNode(value: 8))))) | ||
#expect(try parser.parse("(4 - 9) * 8").isEqual(to: ProductNode(lhs: DifferenceNode(lhs: ConstantNode(value: 4), rhs: ConstantNode(value: 9)), rhs: ConstantNode(value: 8)))) | ||
} | ||
} |
Oops, something went wrong.