1- import Foundation
2- import WasmParser
3- import XCTest
1+ #if canImport(Testing)
2+ import Testing
3+ import Foundation
4+ import WasmParser
45
5- @testable import WAT
6+ @testable import WAT
67
7- class EncoderTests : XCTestCase {
8+ @Suite
9+ struct EncoderTests {
810
9- struct CompatibilityTestStats {
10- var run : Int = 0
11- var failed : Set < String > = [ ]
12- }
13-
14- func checkWabtCompatibility(
15- wast: URL , json: URL , stats parentStats: inout CompatibilityTestStats
16- ) throws {
17- var stats = parentStats
18- defer { parentStats = stats }
19- func recordFail( ) {
20- stats. failed. insert ( wast. lastPathComponent)
11+ struct CompatibilityTestStats {
12+ var run : Int = 0
13+ var failed : Set < String > = [ ]
2114 }
22- func assertEqual< T: Equatable > ( _ lhs: T , _ rhs: T , file: StaticString = #file, line: UInt = #line) {
23- XCTAssertEqual ( lhs, rhs, file: file, line: line)
24- if lhs != rhs {
25- recordFail ( )
15+
16+ func checkWabtCompatibility(
17+ wast: URL , json: URL , stats parentStats: inout CompatibilityTestStats ,
18+ sourceLocation: SourceLocation = #_sourceLocation
19+ ) throws {
20+ var stats = parentStats
21+ defer { parentStats = stats }
22+ func recordFail( ) {
23+ stats. failed. insert ( wast. lastPathComponent)
24+ }
25+ func assertEqual< T: Equatable > ( _ lhs: T , _ rhs: T , sourceLocation: SourceLocation = #_sourceLocation) {
26+ #expect( lhs == rhs, sourceLocation: sourceLocation)
27+ if lhs != rhs {
28+ recordFail ( )
29+ }
2630 }
27- }
2831
29- print ( " Checking \n wast: \( wast. path) \n json: \( json. path) " )
30- var parser = WastParser ( try String ( contentsOf: wast) , features: Spectest . deriveFeatureSet ( wast: wast) )
31- var watModules : [ ModuleDirective ] = [ ]
32+ var parser = WastParser ( try String ( contentsOf: wast) , features: Spectest . deriveFeatureSet ( wast: wast) )
33+ var watModules: [ ModuleDirective] = [ ]
3234
33- while let directive = try parser. nextDirective ( ) {
34- switch directive {
35- case . module( let moduleDirective) :
36- watModules. append ( moduleDirective)
37- case . assertMalformed( let module, let message) :
38- let diagnostic = {
39- let ( line, column) = module. location. computeLineAndColumn ( )
40- return " \( wast. path) : \( line) : \( column) should be malformed: \( message) "
41- }
42- switch module. source {
43- case . text( var wat) :
44- XCTAssertThrowsError (
45- try {
35+ while let directive = try parser. nextDirective ( ) {
36+ switch directive {
37+ case . module( let moduleDirective) :
38+ watModules. append ( moduleDirective)
39+ case . assertMalformed( let module, let message) :
40+ let diagnostic : ( ) -> Comment = {
41+ let ( line, column) = module. location. computeLineAndColumn ( )
42+ return " \( wast. path) : \( line) : \( column) should be malformed: \( message) "
43+ }
44+ switch module. source {
45+ case . text( var wat) :
46+ #expect( throws: ( any Error ) . self, diagnostic ( ) , sourceLocation: sourceLocation) {
4647 _ = try wat. encode ( )
4748 recordFail ( )
48- } ( ) , diagnostic ( ) )
49- case . quote( let bytes) :
50- XCTAssertThrowsError (
51- try {
49+ }
50+ case . quote( let bytes) :
51+ #expect( throws: ( any Error ) . self, diagnostic ( ) , sourceLocation: sourceLocation) {
5252 _ = try wat2wasm ( String ( decoding: bytes, as: UTF8 . self) )
5353 recordFail ( )
54- } ( ) , diagnostic ( ) )
55- case . binary: break
54+ }
55+ case . binary: break
56+ }
57+ default : break
5658 }
57- default : break
5859 }
59- }
60- guard FileManager . default. fileExists ( atPath: json. path) else {
61- print ( " Skipping binary comparison because the oracle file ( \( json. path) ) does not exist. " )
62- return
63- }
64- let moduleBinaryFiles = try Spectest . moduleFiles ( json: json)
65- assertEqual ( watModules. count, moduleBinaryFiles. count)
60+ guard FileManager. default. fileExists ( atPath: json. path) else {
61+ print ( " Skipping binary comparison because the oracle file ( \( json. path) ) does not exist. " )
62+ return
63+ }
64+ let moduleBinaryFiles = try Spectest . moduleFiles ( json: json)
65+ assertEqual( watModules. count, moduleBinaryFiles. count)
6666
67- 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)
70- if lhs != rhs {
67+ for ( watModule, ( moduleBinaryFile, expectedName) ) in zip( watModules, moduleBinaryFiles) {
68+ func assertEqual< T: Equatable > ( _ lhs: T , _ rhs: T , sourceLocation: SourceLocation = #_sourceLocation) {
69+ #expect( lhs == rhs, sourceLocation: sourceLocation)
70+ if lhs != rhs {
71+ recordFail ( )
72+ }
73+ }
74+ stats. run += 1
75+ let moduleBytes: [ UInt8]
76+ let expectedBytes = try Array ( Data ( contentsOf: moduleBinaryFile) )
77+ do {
78+ assertEqual ( watModule. id, expectedName)
79+ switch watModule. source {
80+ case . text( var watModule) :
81+ moduleBytes = try encode ( module: & watModule, options: . default)
82+ case . binary( let bytes) :
83+ moduleBytes = bytes
84+ case . quote( let watText) :
85+ moduleBytes = try wat2wasm ( String ( decoding: watText, as: UTF8 . self) )
86+ }
87+ } catch {
7188 recordFail ( )
89+ #expect( ( false ) , " Error while encoding \( moduleBinaryFile. lastPathComponent) : \( error) " )
90+ return
7291 }
73- }
74- stats. run += 1
75- let moduleBytes : [ UInt8 ]
76- let expectedBytes = try Array ( Data ( contentsOf: moduleBinaryFile) )
77- do {
78- assertEqual ( watModule. id, expectedName)
79- switch watModule. source {
80- case . text( var watModule) :
81- moduleBytes = try encode ( module: & watModule, options: . default)
82- case . binary( let bytes) :
83- moduleBytes = bytes
84- case . quote( let watText) :
85- moduleBytes = try wat2wasm ( String ( decoding: watText, as: UTF8 . self) )
92+ if moduleBytes != expectedBytes {
93+ recordFail ( )
94+ }
95+ assertEqual( moduleBytes. count, expectedBytes. count)
96+ if moduleBytes. count == expectedBytes. count {
97+ assertEqual ( moduleBytes, expectedBytes)
8698 }
87- } catch {
88- recordFail ( )
89- XCTFail ( " Error while encoding \( moduleBinaryFile. lastPathComponent) : \( error) " )
90- return
91- }
92- if moduleBytes != expectedBytes {
93- recordFail ( )
94- }
95- assertEqual ( moduleBytes. count, expectedBytes. count)
96- if moduleBytes. count == expectedBytes. count {
97- assertEqual ( moduleBytes, expectedBytes)
9899 }
99100 }
100- }
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) {
111+ var stats = CompatibilityTestStats ( )
113112 try TestSupport . withTemporaryDirectory { tempDir, shouldRetain in
114113 let jsonFileName = wastFile. deletingPathExtension ( ) . lastPathComponent + " .json "
115114 let json = URL ( fileURLWithPath: tempDir) . appendingPathComponent ( jsonFileName)
@@ -130,47 +129,48 @@ class EncoderTests: XCTestCase {
130129 } catch {
131130 stats. failed. insert ( wastFile. lastPathComponent)
132131 shouldRetain = true
133- XCTFail ( " Error while checking compatibility between \( wastFile) and \( json. path) : \( error) " )
132+ #expect ( ( false ) , " Error while checking compatibility between \( wastFile) and \( json. path) : \( error) " )
134133 }
135134 }
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 ( ) ) " )
135+
136+ if ! stats. failed. isEmpty {
137+ #expect ( ( false ) , " Failed test cases: \( stats. failed. sorted ( ) ) " )
138+ }
140139 }
141140 #endif
142- }
143141
144- func testEncodeNameSection( ) throws {
145- let bytes = try wat2wasm (
146- """
147- (module
148- (func $foo)
149- (func)
150- (func $bar)
142+ @Test
143+ func encodeNameSection( ) throws {
144+ let bytes = try wat2wasm (
145+ """
146+ (module
147+ (func $foo)
148+ (func)
149+ (func $bar)
150+ )
151+ """ ,
152+ options: EncodeOptions ( nameSection: true )
151153 )
152- """ ,
153- options: EncodeOptions ( nameSection: true )
154- )
155154
156- var parser = WasmParser . Parser ( bytes: bytes)
157- var customSections : [ CustomSection ] = [ ]
158- while let payload = try parser. parseNext ( ) {
159- guard case . customSection( let section) = payload else {
160- continue
155+ var parser = WasmParser . Parser ( bytes: bytes)
156+ var customSections : [ CustomSection ] = [ ]
157+ while let payload = try parser. parseNext ( ) {
158+ guard case . customSection( let section) = payload else {
159+ continue
160+ }
161+ customSections. append ( section)
161162 }
162- customSections. append ( section )
163- }
164- let nameSection = customSections . first ( where : { $0 . name == " name " } )
165- let nameParser = NameSectionParser (
166- stream : StaticByteStream ( bytes : nameSection ? . bytes ?? [ ] )
167- )
168- let names = try nameParser . parseAll ( )
169- XCTAssertEqual ( names . count , 1 )
170- guard case . functions ( let functionNames ) = try XCTUnwrap ( names . first ) else {
171- XCTFail ( )
172- return
163+ let nameSection = customSections. first ( where : { $0 . name == " name " } )
164+ let nameParser = NameSectionParser (
165+ stream : StaticByteStream ( bytes : nameSection ? . bytes ?? [ ] )
166+ )
167+ let names = try nameParser . parseAll ( )
168+ #expect ( names . count == 1 )
169+ guard case . functions ( let functionNames ) = try #require ( names . first ) else {
170+ #expect ( ( false ) , " Expected functions name section " )
171+ return
172+ }
173+ #expect ( functionNames == [ 0 : " foo " , 2 : " bar " ] )
173174 }
174- XCTAssertEqual ( functionNames, [ 0 : " foo " , 2 : " bar " ] )
175175 }
176- }
176+ #endif
0 commit comments