Skip to content

Commit 4470103

Browse files
Tests: Migrate WATTests to use swift-testing
1 parent 4c51bae commit 4470103

File tree

4 files changed

+187
-202
lines changed

4 files changed

+187
-202
lines changed

Tests/WATTests/EncoderTests.swift

Lines changed: 61 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,34 @@
1+
#if canImport(Testing)
2+
import Testing
13
import Foundation
24
import WasmParser
3-
import XCTest
45

56
@testable import WAT
67

7-
class EncoderTests: XCTestCase {
8+
@Suite
9+
struct EncoderTests {
810

911
struct CompatibilityTestStats {
1012
var run: Int = 0
1113
var failed: Set<String> = []
1214
}
1315

1416
func checkWabtCompatibility(
15-
wast: URL, json: URL, stats parentStats: inout CompatibilityTestStats
17+
wast: URL, json: URL, stats parentStats: inout CompatibilityTestStats,
18+
sourceLocation: SourceLocation = #_sourceLocation
1619
) throws {
1720
var stats = parentStats
1821
defer { parentStats = stats }
1922
func recordFail() {
2023
stats.failed.insert(wast.lastPathComponent)
2124
}
22-
func assertEqual<T: Equatable>(_ lhs: T, _ rhs: T, file: StaticString = #file, line: UInt = #line) {
23-
XCTAssertEqual(lhs, rhs, file: file, line: line)
25+
func assertEqual<T: Equatable>(_ lhs: T, _ rhs: T, sourceLocation: SourceLocation = #_sourceLocation) {
26+
#expect(lhs == rhs, sourceLocation: sourceLocation)
2427
if lhs != rhs {
2528
recordFail()
2629
}
2730
}
2831

29-
print("Checking\n wast: \(wast.path)\n json: \(json.path)")
3032
var parser = WastParser(try String(contentsOf: wast), features: Spectest.deriveFeatureSet(wast: wast))
3133
var watModules: [ModuleDirective] = []
3234

@@ -35,23 +37,21 @@ class EncoderTests: XCTestCase {
3537
case .module(let moduleDirective):
3638
watModules.append(moduleDirective)
3739
case .assertMalformed(let module, let message):
38-
let diagnostic = {
40+
let diagnostic: () -> Comment = {
3941
let (line, column) = module.location.computeLineAndColumn()
4042
return "\(wast.path):\(line):\(column) should be malformed: \(message)"
4143
}
4244
switch module.source {
4345
case .text(var wat):
44-
XCTAssertThrowsError(
45-
try {
46-
_ = try wat.encode()
47-
recordFail()
48-
}(), diagnostic())
46+
#expect(throws: (any Error).self, diagnostic(), sourceLocation: sourceLocation) {
47+
_ = try wat.encode()
48+
recordFail()
49+
}
4950
case .quote(let bytes):
50-
XCTAssertThrowsError(
51-
try {
52-
_ = try wat2wasm(String(decoding: bytes, as: UTF8.self))
53-
recordFail()
54-
}(), diagnostic())
51+
#expect(throws: (any Error).self, diagnostic(), sourceLocation: sourceLocation) {
52+
_ = try wat2wasm(String(decoding: bytes, as: UTF8.self))
53+
recordFail()
54+
}
5555
case .binary: break
5656
}
5757
default: break
@@ -65,8 +65,8 @@ class EncoderTests: XCTestCase {
6565
assertEqual(watModules.count, moduleBinaryFiles.count)
6666

6767
for (watModule, (moduleBinaryFile, expectedName)) in zip(watModules, moduleBinaryFiles) {
68-
func assertEqual<T: Equatable>(_ lhs: T, _ rhs: T, file: StaticString = #file, line: UInt = #line) {
69-
XCTAssertEqual(lhs, rhs, moduleBinaryFile.path, file: file, line: line)
68+
func assertEqual<T: Equatable>(_ lhs: T, _ rhs: T, sourceLocation: SourceLocation = #_sourceLocation) {
69+
#expect(lhs == rhs, sourceLocation: sourceLocation)
7070
if lhs != rhs {
7171
recordFail()
7272
}
@@ -86,7 +86,7 @@ class EncoderTests: XCTestCase {
8686
}
8787
} catch {
8888
recordFail()
89-
XCTFail("Error while encoding \(moduleBinaryFile.lastPathComponent): \(error)")
89+
#expect((false), "Error while encoding \(moduleBinaryFile.lastPathComponent): \(error)")
9090
return
9191
}
9292
if moduleBytes != expectedBytes {
@@ -99,49 +99,48 @@ class EncoderTests: XCTestCase {
9999
}
100100
}
101101

102-
func testSpectest() throws {
103-
#if os(iOS) || os(watchOS) || os(tvOS) || os(visionOS)
104-
throw XCTSkip("Spectest compatibility test requires Foundation.Process")
105-
#else
106-
guard let wast2json = TestSupport.lookupExecutable("wast2json") else {
107-
throw XCTSkip("wast2json not found in PATH")
108-
}
102+
#if !(os(iOS) || os(watchOS) || os(tvOS) || os(visionOS))
103+
@Test(
104+
arguments: Spectest.wastFiles(include: [], exclude: [])
105+
)
106+
func spectest(wastFile: URL) throws {
107+
guard let wast2json = TestSupport.lookupExecutable("wast2json") else {
108+
return // Skip the test if wast2json is not found in PATH
109+
}
109110

110-
var stats = CompatibilityTestStats()
111-
let excluded: [String] = []
112-
for wastFile in Spectest.wastFiles(include: [], exclude: excluded) {
113-
try TestSupport.withTemporaryDirectory { tempDir, shouldRetain in
114-
let jsonFileName = wastFile.deletingPathExtension().lastPathComponent + ".json"
115-
let json = URL(fileURLWithPath: tempDir).appendingPathComponent(jsonFileName)
111+
var stats = CompatibilityTestStats()
112+
try TestSupport.withTemporaryDirectory { tempDir, shouldRetain in
113+
let jsonFileName = wastFile.deletingPathExtension().lastPathComponent + ".json"
114+
let json = URL(fileURLWithPath: tempDir).appendingPathComponent(jsonFileName)
116115

117-
let wast2jsonProcess = try Process.run(
118-
wast2json,
119-
arguments: [
120-
wastFile.path,
121-
"--enable-memory64",
122-
"--enable-tail-call",
123-
"-o", json.path,
124-
]
125-
)
126-
wast2jsonProcess.waitUntilExit()
116+
let wast2jsonProcess = try Process.run(
117+
wast2json,
118+
arguments: [
119+
wastFile.path,
120+
"--enable-memory64",
121+
"--enable-tail-call",
122+
"-o", json.path,
123+
]
124+
)
125+
wast2jsonProcess.waitUntilExit()
127126

128-
do {
129-
try checkWabtCompatibility(wast: wastFile, json: json, stats: &stats)
130-
} catch {
131-
stats.failed.insert(wastFile.lastPathComponent)
132-
shouldRetain = true
133-
XCTFail("Error while checking compatibility between \(wastFile) and \(json.path): \(error)")
134-
}
135-
}
136-
}
137-
print("Spectest compatibility: \(stats.run - stats.failed.count) / \(stats.run)")
138-
if !stats.failed.isEmpty {
139-
print("Failed test cases: \(stats.failed.sorted())")
127+
do {
128+
try checkWabtCompatibility(wast: wastFile, json: json, stats: &stats)
129+
} catch {
130+
stats.failed.insert(wastFile.lastPathComponent)
131+
shouldRetain = true
132+
#expect((false), "Error while checking compatibility between \(wastFile) and \(json.path): \(error)")
140133
}
141-
#endif
134+
}
135+
136+
if !stats.failed.isEmpty {
137+
#expect((false), "Failed test cases: \(stats.failed.sorted())")
138+
}
142139
}
140+
#endif
143141

144-
func testEncodeNameSection() throws {
142+
@Test
143+
func encodeNameSection() throws {
145144
let bytes = try wat2wasm(
146145
"""
147146
(module
@@ -166,11 +165,12 @@ class EncoderTests: XCTestCase {
166165
stream: StaticByteStream(bytes: nameSection?.bytes ?? [])
167166
)
168167
let names = try nameParser.parseAll()
169-
XCTAssertEqual(names.count, 1)
170-
guard case .functions(let functionNames) = try XCTUnwrap(names.first) else {
171-
XCTFail()
168+
#expect(names.count == 1)
169+
guard case .functions(let functionNames) = try #require(names.first) else {
170+
#expect((false), "Expected functions name section")
172171
return
173172
}
174-
XCTAssertEqual(functionNames, [0: "foo", 2: "bar"])
173+
#expect(functionNames == [0: "foo", 2: "bar"])
175174
}
176175
}
176+
#endif

Tests/WATTests/LexerTests.swift

Lines changed: 66 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
#if canImport(Testing)
2+
import Testing
13
import Foundation
24
import WasmParser
3-
import XCTest
45

56
@testable import WAT
67

7-
class LexerTests: XCTestCase {
8+
@Suite
9+
struct LexerTests {
810
func collectToken(_ source: String) throws -> [TokenKind] {
911
var lexer = Lexer(input: source)
1012
var tokens: [TokenKind] = []
@@ -14,94 +16,89 @@ class LexerTests: XCTestCase {
1416
return tokens
1517
}
1618

17-
func testLexBasics() {
18-
try XCTAssertEqual(collectToken(""), [])
19-
try XCTAssertEqual(collectToken("(module"), [.leftParen, .keyword])
20-
try XCTAssertEqual(collectToken("( module"), [.leftParen, .keyword])
21-
try XCTAssertEqual(collectToken("(\tmodule"), [.leftParen, .keyword])
22-
try XCTAssertEqual(collectToken("(\nmodule"), [.leftParen, .keyword])
23-
try XCTAssertEqual(collectToken("(module)"), [.leftParen, .keyword, .rightParen])
19+
@Test
20+
func lexBasics() throws {
21+
#expect(try collectToken("") == [])
22+
#expect(try collectToken("(module") == [.leftParen, .keyword])
23+
#expect(try collectToken("( module") == [.leftParen, .keyword])
24+
#expect(try collectToken("(\tmodule") == [.leftParen, .keyword])
25+
#expect(try collectToken("(\nmodule") == [.leftParen, .keyword])
26+
#expect(try collectToken("(module)") == [.leftParen, .keyword, .rightParen])
2427

2528
}
2629

27-
func testLexComment() {
28-
XCTAssertEqual(try collectToken("(; foo ;)"), [.blockComment])
29-
try XCTAssertEqual(
30-
collectToken(
30+
@Test
31+
func testLexComment() throws {
32+
#expect(try collectToken("(; foo ;)") == [.blockComment])
33+
#expect(
34+
try collectToken(
3135
"""
3236
(;
3337
multi-line comment
3438
;)
3539
"""
36-
),
37-
[.blockComment]
40+
) == [.blockComment]
3841
)
39-
try XCTAssertEqual(collectToken(";; foo"), [.lineComment])
40-
try XCTAssertEqual(collectToken(";; foo\n(bar"), [.lineComment, .leftParen, .keyword])
42+
#expect(try collectToken(";; foo") == [.lineComment])
43+
#expect(try collectToken(";; foo\n(bar") == [.lineComment, .leftParen, .keyword])
4144

4245
}
4346

44-
func testLexBrokenComment() {
45-
XCTAssertThrowsError(try collectToken("(;)"))
46-
XCTAssertThrowsError(try collectToken("(; foo )"))
47-
XCTAssertThrowsError(try collectToken(";)"))
47+
@Test
48+
func lexBrokenComment() throws {
49+
#expect(throws: (any Error).self) { try collectToken("(;)") }
50+
#expect(throws: (any Error).self) { try collectToken("(; foo )") }
51+
#expect(throws: (any Error).self) { try collectToken(";)") }
4852
}
4953

50-
func testLexIdAndString() throws {
51-
try XCTAssertEqual(collectToken("$foo"), [.id])
52-
try XCTAssertEqual(collectToken("\"foo\""), [.string(Array("foo".utf8))])
53-
try XCTAssertEqual(collectToken("\"\\t\\n\\r\\\"\\\\\""), [.string(Array("\t\n\r\"\\".utf8))])
54-
try XCTAssertEqual(collectToken("\"\\u{1F600}\""), [.string(Array("😀".utf8))])
55-
try XCTAssertEqual(collectToken("$\"foo\""), [.id])
56-
try XCTAssertEqual(collectToken("0$x"), [.unknown])
54+
@Test
55+
func lexIdAndString() throws {
56+
#expect(try collectToken("$foo") == [.id])
57+
#expect(try collectToken("\"foo\"") == [.string(Array("foo".utf8))])
58+
#expect(try collectToken("\"\\t\\n\\r\\\"\\\\\"") == [.string(Array("\t\n\r\"\\".utf8))])
59+
#expect(try collectToken("\"\\u{1F600}\"") == [.string(Array("😀".utf8))])
60+
#expect(try collectToken("$\"foo\"") == [.id])
61+
#expect(try collectToken("0$x") == [.unknown])
5762
}
5863

59-
func testLexInteger() throws {
60-
try XCTAssertEqual(collectToken("inf"), [.float(nil, .inf)])
61-
try XCTAssertEqual(collectToken("+inf"), [.float(.plus, .inf)])
62-
try XCTAssertEqual(collectToken("-inf"), [.float(.minus, .inf)])
63-
try XCTAssertEqual(collectToken("nan"), [.float(nil, .nan(hexPattern: nil))])
64-
try XCTAssertEqual(collectToken("+nan"), [.float(.plus, .nan(hexPattern: nil))])
65-
try XCTAssertEqual(collectToken("-nan"), [.float(.minus, .nan(hexPattern: nil))])
66-
try XCTAssertEqual(collectToken("nan:0x7f_ffff"), [.float(nil, .nan(hexPattern: "7fffff"))])
67-
try XCTAssertEqual(collectToken("3.14"), [.float(nil, .decimalPattern("3.14"))])
68-
try XCTAssertEqual(collectToken("1e+07"), [.float(nil, .decimalPattern("1e+07"))])
69-
try XCTAssertEqual(collectToken("1E+07"), [.float(nil, .decimalPattern("1E+07"))])
70-
try XCTAssertEqual(collectToken("0xff"), [.integer(nil, .hexPattern("ff"))])
71-
try XCTAssertEqual(collectToken("8_128"), [.integer(nil, .decimalPattern("8128"))])
72-
try XCTAssertEqual(collectToken("1.e10"), [.float(nil, .decimalPattern("1.e10"))])
64+
@Test
65+
func lexInteger() throws {
66+
#expect(try collectToken("inf") == [.float(nil, .inf)])
67+
#expect(try collectToken("+inf") == [.float(.plus, .inf)])
68+
#expect(try collectToken("-inf") == [.float(.minus, .inf)])
69+
#expect(try collectToken("nan") == [.float(nil, .nan(hexPattern: nil))])
70+
#expect(try collectToken("+nan") == [.float(.plus, .nan(hexPattern: nil))])
71+
#expect(try collectToken("-nan") == [.float(.minus, .nan(hexPattern: nil))])
72+
#expect(try collectToken("nan:0x7f_ffff") == [.float(nil, .nan(hexPattern: "7fffff"))])
73+
#expect(try collectToken("3.14") == [.float(nil, .decimalPattern("3.14"))])
74+
#expect(try collectToken("1e+07") == [.float(nil, .decimalPattern("1e+07"))])
75+
#expect(try collectToken("1E+07") == [.float(nil, .decimalPattern("1E+07"))])
76+
#expect(try collectToken("0xff") == [.integer(nil, .hexPattern("ff"))])
77+
#expect(try collectToken("8_128") == [.integer(nil, .decimalPattern("8128"))])
78+
#expect(try collectToken("1.e10") == [.float(nil, .decimalPattern("1.e10"))])
7379
}
7480

75-
func testLexFloatLiteral() throws {
76-
try XCTAssertEqual(collectToken("nan:canonical"), [.keyword])
77-
try XCTAssertEqual(collectToken("0x1.921fb6p+2"), [.float(nil, .hexPattern("1.921fb6p+2"))])
81+
@Test
82+
func lexFloatLiteral() throws {
83+
#expect(try collectToken("nan:canonical") == [.keyword])
84+
#expect(try collectToken("0x1.921fb6p+2") == [.float(nil, .hexPattern("1.921fb6p+2"))])
7885
}
7986

80-
func testLexMemory() throws {
81-
try XCTAssertEqual(collectToken("(module (memory 1))"), [.leftParen, .keyword, .leftParen, .keyword, .integer(nil, .decimalPattern("1")), .rightParen, .rightParen])
87+
@Test
88+
func lexMemory() throws {
89+
#expect(try collectToken("(module (memory 1))") == [.leftParen, .keyword, .leftParen, .keyword, .integer(nil, .decimalPattern("1")), .rightParen, .rightParen])
8290
}
8391

84-
func testLexSpectest() throws {
85-
// NOTE: We do the same check as a part of the EncoderTests, so it's
86-
// usually redundant and time-wasting to run this test every time.
87-
// Keeping it here just for local unit testing purposes.
88-
try XCTSkipIf(
89-
ProcessInfo.processInfo.environment["WASMKIT_LEXER_SPECTEST"] != "1"
90-
)
91-
var failureCount = 0
92-
for filePath in Spectest.wastFiles() {
93-
print("Lexing \(filePath.path)...")
94-
let source = try String(contentsOf: filePath)
95-
do {
96-
_ = try collectToken(source)
97-
} catch {
98-
failureCount += 1
99-
XCTFail("Failed to lex \(filePath.path):\(error)")
100-
}
101-
}
102-
103-
if failureCount > 0 {
104-
XCTFail("Failed to lex \(failureCount) files")
105-
}
92+
// NOTE: We do the same check as a part of the EncoderTests, so it's
93+
// usually redundant and time-wasting to run this test every time.
94+
// Keeping it here just for local unit testing purposes.
95+
@Test(
96+
.enabled(if: ProcessInfo.processInfo.environment["WASMKIT_PARSER_SPECTEST"] == "1"),
97+
arguments: Spectest.wastFiles(include: [])
98+
)
99+
func lexSpectest(wastFile: URL) throws {
100+
let source = try String(contentsOf: wastFile)
101+
_ = try collectToken(source)
106102
}
107103
}
104+
#endif

0 commit comments

Comments
 (0)