Skip to content

Commit

Permalink
Migrate D2ScriptParserTests to swift-testing
Browse files Browse the repository at this point in the history
  • Loading branch information
fwcd committed Oct 20, 2024
1 parent 51ce173 commit aef5e6c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 31 deletions.
1 change: 1 addition & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ let package = Package(
name: "D2ScriptTests",
dependencies: [
.product(name: "Utils", package: "swift-utils"),
.target(name: "D2TestUtils"),
.target(name: "D2Script"),
]
),
Expand Down
62 changes: 31 additions & 31 deletions Tests/D2ScriptTests/D2ScriptParserTests.swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import XCTest
import Testing
import D2TestUtils
@testable import D2Script

final class D2ScriptParserTests: XCTestCase {
private let eps = 0.01
struct D2ScriptParserTests {
private let parser = D2ScriptParser()

func testTokenization() throws {
assertTokensEqual(try parser.tokenize("4.645"), [.numberLiteral(4.645)])
assertTokensEqual(try parser.tokenize("\"\""), [.stringLiteral("")])
assertTokensEqual(try parser.tokenize("(({}, {}), ())"), [
@Test func tokenization() throws {
expectTokensEqual(try parser.tokenize("4.645"), [.numberLiteral(4.645)])
expectTokensEqual(try parser.tokenize("\"\""), [.stringLiteral("")])
expectTokensEqual(try parser.tokenize("(({}, {}), ())"), [
.leftParenthesis, .leftParenthesis, .leftCurlyBracket, .rightCurlyBracket, .comma,
.leftCurlyBracket, .rightCurlyBracket, .rightParenthesis, .comma,
.leftParenthesis, .rightParenthesis, .rightParenthesis
Expand All @@ -22,7 +22,7 @@ final class D2ScriptParserTests: XCTestCase {
}
"""
let tokens = try parser.tokenize(simpleProgram)
assertTokensEqual(tokens, [
expectTokensEqual(tokens, [
.keyword("command"), .identifier("test"), .leftCurlyBracket, .linebreak,
.identifier("a"), .anyOperator("="), .numberLiteral(4.3), .linebreak,
.identifier("b"), .anyOperator("="), .stringLiteral("This is a string literal 12345"), .linebreak,
Expand All @@ -39,24 +39,24 @@ final class D2ScriptParserTests: XCTestCase {
}
"""
let ast = try parser.parse(simpleProgram1)
XCTAssertEqual(ast.topLevelNodes.count, 1, "Expected exactly one top-level declaration: The command")
#expect(ast.topLevelNodes.count == 1, "Expected exactly one top-level declaration: The command")

let command = ast.topLevelNodes.first as? D2ScriptCommandDeclaration
XCTAssertEqual(command?.commandName, "simpleProgram", "Expected a command labelled 'simpleProgram'")
#expect(command?.commandName == "simpleProgram", "Expected a command labelled 'simpleProgram'")

let statements = command?.statementList.statements
XCTAssertEqual(statements?.count, 2, "Expected two statements")
#expect(statements?.count == 2, "Expected two statements")

let firstPrint = (statements?[0] as? D2ScriptExpressionStatement)?.expression as? D2ScriptFunctionCall
let secondCall = (statements?[1] as? D2ScriptExpressionStatement)?.expression as? D2ScriptFunctionCall
XCTAssertEqual(firstPrint?.functionName, "print")
XCTAssertEqual(secondCall?.functionName, "someFunction")
#expect(firstPrint?.functionName == "print")
#expect(secondCall?.functionName == "someFunction")

assertExpressionsEqual(firstPrint!.arguments, [D2ScriptValue.string("Hello world!")])
assertExpressionsEqual(secondCall!.arguments, [D2ScriptValue.string("Test argument"), D2ScriptValue.string("ABC")])
expectExpressionsEqual(firstPrint!.arguments, [D2ScriptValue.string("Hello world!")])
expectExpressionsEqual(secondCall!.arguments, [D2ScriptValue.string("Test argument"), D2ScriptValue.string("ABC")])
}

func testIdentifierExpressions() throws {
@Test func identifierExpressions() throws {
let simpleProgram2 = """
command simpleProgram {
someVariable = 4.5
Expand All @@ -71,31 +71,31 @@ final class D2ScriptParserTests: XCTestCase {

let assignment1 = statements?[0] as? D2ScriptAssignment
let assignment2 = statements?[1] as? D2ScriptAssignment
XCTAssertEqual(assignment1?.identifier, "someVariable")
XCTAssertEqual(assignment2?.identifier, "anotherVariable")
assertExpressionsEqual([assignment2?.expression].compactMap { $0 }, [D2ScriptValue.string("abc")])
#expect(assignment1?.identifier == "someVariable")
#expect(assignment2?.identifier == "anotherVariable")
expectExpressionsEqual([assignment2?.expression].compactMap { $0 }, [D2ScriptValue.string("abc")])

let call1 = (statements?[2] as? D2ScriptExpressionStatement)?.expression as? D2ScriptFunctionCall
let call2 = (statements?[3] as? D2ScriptExpressionStatement)?.expression as? D2ScriptFunctionCall
XCTAssertEqual(call1?.functionName, "someFunction")
XCTAssertEqual(call2?.functionName, "anotherFunction")
assertExpressionsEqual(call1!.arguments, [D2ScriptIdentifierExpression(name: "someVariable")])
assertExpressionsEqual(call2!.arguments, [D2ScriptIdentifierExpression(name: "someVariable"), D2ScriptIdentifierExpression(name: "anotherVariable")])
#expect(call1?.functionName == "someFunction")
#expect(call2?.functionName == "anotherFunction")
expectExpressionsEqual(call1!.arguments, [D2ScriptIdentifierExpression(name: "someVariable")])
expectExpressionsEqual(call2!.arguments, [D2ScriptIdentifierExpression(name: "someVariable"), D2ScriptIdentifierExpression(name: "anotherVariable")])
}

private func assertExpressionsEqual(_ actual: [D2ScriptExpression], _ expected: [D2ScriptExpression]) {
func expectExpressionsEqual(_ actual: [D2ScriptExpression], _ expected: [D2ScriptExpression]) {
guard actual.count == expected.count else {
XCTFail("The actual expression count \(actual.count) (\(format(actual))) does not match the expected token count \(expected.count) (\(format(expected)))")
Issue.record("The actual expression count \(actual.count) (\(format(actual))) does not match the expected token count \(expected.count) (\(format(expected)))")
return
}
for i in 0..<actual.count {
XCTAssert(actual[i].isEqual(to: expected[i]), "Expressions mismatched first at index \(i)")
#expect(actual[i].isEqual(to: expected[i]), "Expressions mismatched first at index \(i)")
}
}

private func assertTokensEqual(_ actual: [D2ScriptToken], _ expected: [D2ScriptToken]) {
private func expectTokensEqual(_ actual: [D2ScriptToken], _ expected: [D2ScriptToken]) {
guard actual.count == expected.count else {
XCTFail("The actual token count \(actual.count) (\(format(actual))) does not match the expected token count \(expected.count) (\(format(expected)))")
Issue.record("The actual token count \(actual.count) (\(format(actual))) does not match the expected token count \(expected.count) (\(format(expected)))")
return
}
let count = actual.count
Expand All @@ -105,14 +105,14 @@ final class D2ScriptParserTests: XCTestCase {

if case let .numberLiteral(expectedValue) = expectedToken {
guard case let .numberLiteral(actualValue) = actualToken else {
XCTFail("Expected the number token \(expectedToken), but was \(actualToken)")
Issue.record("Expected the number token \(expectedToken), but was \(actualToken)")
return
}
// Ensure that numeric tokens are not compared directly
// since that could result in floating point inaccuracies
XCTAssertEqual(expectedValue, actualValue, accuracy: eps)
#expect(expectedValue.isApproximatelyEqual(to: actualValue))
} else {
XCTAssertEqual(actualToken, expectedToken)
#expect(actualToken == expectedToken)
}
}
}
Expand Down

0 comments on commit aef5e6c

Please sign in to comment.