1
- import XCTest
1
+ import Testing
2
+ import D2TestUtils
2
3
@testable import D2Script
3
4
4
- final class D2ScriptParserTests : XCTestCase {
5
- private let eps = 0.01
5
+ struct D2ScriptParserTests {
6
6
private let parser = D2ScriptParser ( )
7
7
8
- func testTokenization ( ) throws {
9
- assertTokensEqual ( try parser. tokenize ( " 4.645 " ) , [ . numberLiteral( 4.645 ) ] )
10
- assertTokensEqual ( try parser. tokenize ( " \" \" " ) , [ . stringLiteral( " " ) ] )
11
- assertTokensEqual ( try parser. tokenize ( " (({}, {}), ()) " ) , [
8
+ @ Test func tokenization ( ) throws {
9
+ expectTokensEqual ( try parser. tokenize ( " 4.645 " ) , [ . numberLiteral( 4.645 ) ] )
10
+ expectTokensEqual ( try parser. tokenize ( " \" \" " ) , [ . stringLiteral( " " ) ] )
11
+ expectTokensEqual ( try parser. tokenize ( " (({}, {}), ()) " ) , [
12
12
. leftParenthesis, . leftParenthesis, . leftCurlyBracket, . rightCurlyBracket, . comma,
13
13
. leftCurlyBracket, . rightCurlyBracket, . rightParenthesis, . comma,
14
14
. leftParenthesis, . rightParenthesis, . rightParenthesis
@@ -22,7 +22,7 @@ final class D2ScriptParserTests: XCTestCase {
22
22
}
23
23
"""
24
24
let tokens = try parser. tokenize ( simpleProgram)
25
- assertTokensEqual ( tokens, [
25
+ expectTokensEqual ( tokens, [
26
26
. keyword( " command " ) , . identifier( " test " ) , . leftCurlyBracket, . linebreak,
27
27
. identifier( " a " ) , . anyOperator( " = " ) , . numberLiteral( 4.3 ) , . linebreak,
28
28
. identifier( " b " ) , . anyOperator( " = " ) , . stringLiteral( " This is a string literal 12345 " ) , . linebreak,
@@ -39,24 +39,24 @@ final class D2ScriptParserTests: XCTestCase {
39
39
}
40
40
"""
41
41
let ast = try parser. parse ( simpleProgram1)
42
- XCTAssertEqual ( ast. topLevelNodes. count, 1 , " Expected exactly one top-level declaration: The command " )
42
+ #expect ( ast. topLevelNodes. count == 1 , " Expected exactly one top-level declaration: The command " )
43
43
44
44
let command = ast. topLevelNodes. first as? D2ScriptCommandDeclaration
45
- XCTAssertEqual ( command? . commandName, " simpleProgram " , " Expected a command labelled 'simpleProgram' " )
45
+ #expect ( command? . commandName == " simpleProgram " , " Expected a command labelled 'simpleProgram' " )
46
46
47
47
let statements = command? . statementList. statements
48
- XCTAssertEqual ( statements? . count, 2 , " Expected two statements " )
48
+ #expect ( statements? . count == 2 , " Expected two statements " )
49
49
50
50
let firstPrint = ( statements ? [ 0 ] as? D2ScriptExpressionStatement ) ? . expression as? D2ScriptFunctionCall
51
51
let secondCall = ( statements ? [ 1 ] as? D2ScriptExpressionStatement ) ? . expression as? D2ScriptFunctionCall
52
- XCTAssertEqual ( firstPrint? . functionName, " print " )
53
- XCTAssertEqual ( secondCall? . functionName, " someFunction " )
52
+ #expect ( firstPrint? . functionName == " print " )
53
+ #expect ( secondCall? . functionName == " someFunction " )
54
54
55
- assertExpressionsEqual ( firstPrint!. arguments, [ D2ScriptValue . string ( " Hello world! " ) ] )
56
- assertExpressionsEqual ( secondCall!. arguments, [ D2ScriptValue . string ( " Test argument " ) , D2ScriptValue . string ( " ABC " ) ] )
55
+ expectExpressionsEqual ( firstPrint!. arguments, [ D2ScriptValue . string ( " Hello world! " ) ] )
56
+ expectExpressionsEqual ( secondCall!. arguments, [ D2ScriptValue . string ( " Test argument " ) , D2ScriptValue . string ( " ABC " ) ] )
57
57
}
58
58
59
- func testIdentifierExpressions ( ) throws {
59
+ @ Test func identifierExpressions ( ) throws {
60
60
let simpleProgram2 = """
61
61
command simpleProgram {
62
62
someVariable = 4.5
@@ -71,31 +71,31 @@ final class D2ScriptParserTests: XCTestCase {
71
71
72
72
let assignment1 = statements ? [ 0 ] as? D2ScriptAssignment
73
73
let assignment2 = statements ? [ 1 ] as? D2ScriptAssignment
74
- XCTAssertEqual ( assignment1? . identifier, " someVariable " )
75
- XCTAssertEqual ( assignment2? . identifier, " anotherVariable " )
76
- assertExpressionsEqual ( [ assignment2? . expression] . compactMap { $0 } , [ D2ScriptValue . string ( " abc " ) ] )
74
+ #expect ( assignment1? . identifier == " someVariable " )
75
+ #expect ( assignment2? . identifier == " anotherVariable " )
76
+ expectExpressionsEqual ( [ assignment2? . expression] . compactMap { $0 } , [ D2ScriptValue . string ( " abc " ) ] )
77
77
78
78
let call1 = ( statements ? [ 2 ] as? D2ScriptExpressionStatement ) ? . expression as? D2ScriptFunctionCall
79
79
let call2 = ( statements ? [ 3 ] as? D2ScriptExpressionStatement ) ? . expression as? D2ScriptFunctionCall
80
- XCTAssertEqual ( call1? . functionName, " someFunction " )
81
- XCTAssertEqual ( call2? . functionName, " anotherFunction " )
82
- assertExpressionsEqual ( call1!. arguments, [ D2ScriptIdentifierExpression ( name: " someVariable " ) ] )
83
- assertExpressionsEqual ( call2!. arguments, [ D2ScriptIdentifierExpression ( name: " someVariable " ) , D2ScriptIdentifierExpression ( name: " anotherVariable " ) ] )
80
+ #expect ( call1? . functionName == " someFunction " )
81
+ #expect ( call2? . functionName == " anotherFunction " )
82
+ expectExpressionsEqual ( call1!. arguments, [ D2ScriptIdentifierExpression ( name: " someVariable " ) ] )
83
+ expectExpressionsEqual ( call2!. arguments, [ D2ScriptIdentifierExpression ( name: " someVariable " ) , D2ScriptIdentifierExpression ( name: " anotherVariable " ) ] )
84
84
}
85
85
86
- private func assertExpressionsEqual ( _ actual: [ D2ScriptExpression ] , _ expected: [ D2ScriptExpression ] ) {
86
+ func expectExpressionsEqual ( _ actual: [ D2ScriptExpression ] , _ expected: [ D2ScriptExpression ] ) {
87
87
guard actual. count == expected. count else {
88
- XCTFail ( " The actual expression count \( actual. count) ( \( format ( actual) ) ) does not match the expected token count \( expected. count) ( \( format ( expected) ) ) " )
88
+ Issue . record ( " The actual expression count \( actual. count) ( \( format ( actual) ) ) does not match the expected token count \( expected. count) ( \( format ( expected) ) ) " )
89
89
return
90
90
}
91
91
for i in 0 ..< actual. count {
92
- XCTAssert ( actual [ i] . isEqual ( to: expected [ i] ) , " Expressions mismatched first at index \( i) " )
92
+ #expect ( actual [ i] . isEqual ( to: expected [ i] ) , " Expressions mismatched first at index \( i) " )
93
93
}
94
94
}
95
95
96
- private func assertTokensEqual ( _ actual: [ D2ScriptToken ] , _ expected: [ D2ScriptToken ] ) {
96
+ private func expectTokensEqual ( _ actual: [ D2ScriptToken ] , _ expected: [ D2ScriptToken ] ) {
97
97
guard actual. count == expected. count else {
98
- XCTFail ( " The actual token count \( actual. count) ( \( format ( actual) ) ) does not match the expected token count \( expected. count) ( \( format ( expected) ) ) " )
98
+ Issue . record ( " The actual token count \( actual. count) ( \( format ( actual) ) ) does not match the expected token count \( expected. count) ( \( format ( expected) ) ) " )
99
99
return
100
100
}
101
101
let count = actual. count
@@ -105,14 +105,14 @@ final class D2ScriptParserTests: XCTestCase {
105
105
106
106
if case let . numberLiteral( expectedValue) = expectedToken {
107
107
guard case let . numberLiteral( actualValue) = actualToken else {
108
- XCTFail ( " Expected the number token \( expectedToken) , but was \( actualToken) " )
108
+ Issue . record ( " Expected the number token \( expectedToken) , but was \( actualToken) " )
109
109
return
110
110
}
111
111
// Ensure that numeric tokens are not compared directly
112
112
// since that could result in floating point inaccuracies
113
- XCTAssertEqual ( expectedValue, actualValue , accuracy : eps )
113
+ #expect ( expectedValue. isApproximatelyEqual ( to : actualValue ) )
114
114
} else {
115
- XCTAssertEqual ( actualToken, expectedToken)
115
+ #expect ( actualToken == expectedToken)
116
116
}
117
117
}
118
118
}
0 commit comments