diff --git a/Package.swift b/Package.swift index 52bdc1ef..0e7d2bc8 100644 --- a/Package.swift +++ b/Package.swift @@ -175,6 +175,7 @@ let package = Package( name: "D2ScriptTests", dependencies: [ .product(name: "Utils", package: "swift-utils"), + .target(name: "D2TestUtils"), .target(name: "D2Script"), ] ), diff --git a/Tests/D2ScriptTests/D2ScriptParserTests.swift b/Tests/D2ScriptTests/D2ScriptParserTests.swift index 695d2dc1..f6328b9d 100644 --- a/Tests/D2ScriptTests/D2ScriptParserTests.swift +++ b/Tests/D2ScriptTests/D2ScriptParserTests.swift @@ -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 @@ -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, @@ -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 @@ -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..