mirror of
https://github.com/google/flatbuffers.git
synced 2026-07-03 14:14:11 +00:00
[Swift] Migrate to use Swift Testing (#9076)
* Migrating from Xctests to swift testing This migrates to the new Swift testing framework, which would allow us to always use the latest tech from swift moving forward. * Updates flag to make sure that Wasm testing works
This commit is contained in:
@@ -16,17 +16,19 @@
|
||||
|
||||
import Common
|
||||
import FlexBuffers
|
||||
import XCTest
|
||||
import Foundation
|
||||
import Testing
|
||||
|
||||
final class FlexBuffersJSONTests: XCTestCase {
|
||||
struct FlexBuffersJSONTests {
|
||||
@Test
|
||||
func testEncodingJSON() throws {
|
||||
let buf: ByteBuffer = createProperBuffer().sizedByteBuffer
|
||||
let reference = try getRoot(buffer: buf)!
|
||||
|
||||
let json = reference.jsonString()
|
||||
// swiftformat:disable all
|
||||
XCTAssertEqual(
|
||||
json,
|
||||
#expect(
|
||||
json ==
|
||||
"{\"bar\": [1, 2, 3], \"bar3\": [1, 2, 3], \"bool\": true, \"bools\": [true, false, true, false], \"foo\": 100.0, \"mymap\": {\"foo\": \"Fred\"}, \"vec\": [-100, \"Fred\", 4.0, \"M\", false, 4.0]}"
|
||||
)
|
||||
// swiftformat:enable all
|
||||
@@ -37,15 +39,15 @@ final class FlexBuffersJSONTests: XCTestCase {
|
||||
with: data,
|
||||
options: []) as! [String: Any]
|
||||
|
||||
XCTAssertEqual(decodedData["bar"] as! [Int], [1, 2, 3])
|
||||
XCTAssertEqual(decodedData["bar3"] as! [Int], [1, 2, 3])
|
||||
#expect(decodedData["bar"] as! [Int] == [1, 2, 3])
|
||||
#expect(decodedData["bar3"] as! [Int] == [1, 2, 3])
|
||||
|
||||
let vec: [Any] = decodedData["vec"] as! [Any]
|
||||
XCTAssertEqual(vec[0] as! Int, -100)
|
||||
XCTAssertEqual(vec[1] as! String, "Fred")
|
||||
XCTAssertEqual(vec[2] as! Double, 4.0)
|
||||
XCTAssertEqual(vec[3] as! String, "M")
|
||||
XCTAssertEqual(vec[4] as! Bool, false)
|
||||
XCTAssertEqual(vec[5] as! Double, 4.0)
|
||||
#expect(vec[0] as! Int == -100)
|
||||
#expect(vec[1] as! String == "Fred")
|
||||
#expect(vec[2] as! Double == 4.0)
|
||||
#expect(vec[3] as! String == "M")
|
||||
#expect(vec[4] as! Bool == false)
|
||||
#expect(vec[5] as! Double == 4.0)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,22 +15,26 @@
|
||||
*/
|
||||
|
||||
import Common
|
||||
import XCTest
|
||||
import Foundation
|
||||
import Testing
|
||||
|
||||
@testable import FlexBuffers
|
||||
|
||||
final class FlexBuffersReaderTests: XCTestCase {
|
||||
struct FlexBuffersReaderTests {
|
||||
|
||||
@Test
|
||||
func testReadingProperBuffer() throws {
|
||||
let buf: ByteBuffer = createProperBuffer().byteBuffer
|
||||
try validate(buffer: buf)
|
||||
}
|
||||
|
||||
@Test
|
||||
func testReadingSizedBuffer() throws {
|
||||
let buf: ByteBuffer = createSizedBuffer()
|
||||
try validate(buffer: buf)
|
||||
}
|
||||
|
||||
@Test(.bug("https://github.com/google/flatbuffers/issues/8642"))
|
||||
func testReset() throws {
|
||||
var fbx = FlexBuffersWriter(
|
||||
initialSize: 8,
|
||||
@@ -38,87 +42,87 @@ final class FlexBuffersReaderTests: XCTestCase {
|
||||
write(fbx: &fbx)
|
||||
|
||||
try validate(buffer: ByteBuffer(bytes: fbx.sizedByteArray))
|
||||
XCTAssertEqual(fbx.capacity, 512)
|
||||
#expect(fbx.capacity == 512)
|
||||
fbx.reset()
|
||||
XCTAssertEqual(fbx.writerIndex, 0)
|
||||
XCTAssertEqual(fbx.capacity, 8)
|
||||
#expect(fbx.writerIndex == 0)
|
||||
#expect(fbx.capacity == 8)
|
||||
|
||||
write(fbx: &fbx)
|
||||
try validate(buffer: ByteBuffer(bytes: fbx.sizedByteArray))
|
||||
fbx.reset(keepingCapacity: true)
|
||||
XCTAssertEqual(fbx.writerIndex, 0)
|
||||
XCTAssertEqual(fbx.capacity, 512)
|
||||
#expect(fbx.writerIndex == 0)
|
||||
#expect(fbx.capacity == 512)
|
||||
|
||||
write(fbx: &fbx)
|
||||
try validate(buffer: ByteBuffer(bytes: fbx.sizedByteArray))
|
||||
XCTAssertEqual(fbx.capacity, 512)
|
||||
#expect(fbx.capacity == 512)
|
||||
}
|
||||
|
||||
private func validate(buffer buf: ByteBuffer) throws {
|
||||
let reference = try getRoot(buffer: buf)!
|
||||
XCTAssertEqual(reference.type, .map)
|
||||
#expect(reference.type == .map)
|
||||
let map = reference.map!
|
||||
XCTAssertEqual(map.count, 7)
|
||||
#expect(map.count == 7)
|
||||
let vecRef = map["vec"]!
|
||||
XCTAssertEqual(vecRef.type, .vector)
|
||||
#expect(vecRef.type == .vector)
|
||||
let vec = vecRef.vector!
|
||||
XCTAssertEqual(vec.count, 6)
|
||||
XCTAssertEqual(vec[0]?.type, .int)
|
||||
XCTAssertEqual(vec[0]?.int, -100)
|
||||
XCTAssertEqual(vec[1]?.type, .string)
|
||||
XCTAssertEqual(vec[1]?.cString, "Fred")
|
||||
XCTAssertNil(vec[1]?.int)
|
||||
XCTAssertEqual(vec[2]?.double, 4.0)
|
||||
XCTAssertTrue(vec[3]?.type == .blob)
|
||||
#expect(vec.count == 6)
|
||||
#expect(vec[0]?.type == .int)
|
||||
#expect(vec[0]?.int == -100)
|
||||
#expect(vec[1]?.type == .string)
|
||||
#expect(vec[1]?.cString == "Fred")
|
||||
#expect(vec[1]?.int == nil)
|
||||
#expect(vec[2]?.double == 4.0)
|
||||
#expect(vec[3]?.type == .blob)
|
||||
|
||||
let blob = vec[3]!.blob { pointer in
|
||||
Array(pointer)
|
||||
}
|
||||
|
||||
XCTAssertEqual(blob?.count, 1)
|
||||
XCTAssertEqual(blob?[0], 77)
|
||||
XCTAssertEqual(vec[4]?.type, .bool)
|
||||
XCTAssertEqual(vec[4]?.bool, false)
|
||||
XCTAssertEqual(vec[5]?.double, 4.0) // Shared with vec[2]
|
||||
#expect(blob?.count == 1)
|
||||
#expect(blob?[0] == 77)
|
||||
#expect(vec[4]?.type == .bool)
|
||||
#expect(vec[4]?.bool == false)
|
||||
#expect(vec[5]?.double == 4.0) // Shared with vec[2]
|
||||
|
||||
let barVec = map["bar"]!.typedVector!
|
||||
XCTAssertEqual(barVec.count, 3)
|
||||
XCTAssertEqual(barVec[2]?.int, 3)
|
||||
XCTAssertEqual(barVec[2]?.asInt(), UInt8(3))
|
||||
#expect(barVec.count == 3)
|
||||
#expect(barVec[2]?.int == 3)
|
||||
#expect(barVec[2]?.asInt() == UInt8(3))
|
||||
|
||||
let fixedVec = map["bar3"]!.fixedTypedVector!
|
||||
XCTAssertEqual(fixedVec.count, 3)
|
||||
XCTAssertEqual(fixedVec[2]?.int, 3)
|
||||
XCTAssertEqual(fixedVec[2]?.asInt(), UInt8(3))
|
||||
XCTAssertEqual(map["bool"]?.bool, true)
|
||||
#expect(fixedVec.count == 3)
|
||||
#expect(fixedVec[2]?.int == 3)
|
||||
#expect(fixedVec[2]?.asInt() == UInt8(3))
|
||||
#expect(map["bool"]?.bool == true)
|
||||
|
||||
let boolsVector = map["bools"]!.typedVector!
|
||||
XCTAssertEqual(boolsVector.type, .bool)
|
||||
XCTAssertEqual(boolsVector[0]?.bool, true)
|
||||
XCTAssertEqual(boolsVector[1]?.bool, false)
|
||||
#expect(boolsVector.type == .bool)
|
||||
#expect(boolsVector[0]?.bool == true)
|
||||
#expect(boolsVector[1]?.bool == false)
|
||||
|
||||
let bools = [true, false, true, false]
|
||||
boolsVector.withUnsafeRawBufferPointer { buff in
|
||||
for i in 0..<boolsVector.count {
|
||||
XCTAssertEqual(buff.load(fromByteOffset: i, as: Bool.self), bools[i])
|
||||
#expect(buff.load(fromByteOffset: i, as: Bool.self) == bools[i])
|
||||
}
|
||||
}
|
||||
XCTAssertEqual(map["foo"]?.double, 100)
|
||||
XCTAssertNil(map["unknown"])
|
||||
#expect(map["foo"]?.double == 100)
|
||||
#expect(map["unknown"] == nil)
|
||||
let mymap = map["mymap"]?.map
|
||||
|
||||
// Check if both addresses used are the same for keys and strings
|
||||
XCTAssertEqual(mymap?.keys[0]?.cString, map.keys[4]?.cString)
|
||||
#expect(mymap?.keys[0]?.cString == map.keys[4]?.cString)
|
||||
map.keys[4]?.withUnsafeRawPointer { pointer in
|
||||
mymap?.keys[0]?.withUnsafeRawPointer { mymapPointer in
|
||||
XCTAssertEqual(pointer, mymapPointer)
|
||||
#expect(pointer == mymapPointer)
|
||||
}
|
||||
}
|
||||
|
||||
XCTAssertEqual(mymap?.values[0]?.cString, vec[1]?.cString)
|
||||
#expect(mymap?.values[0]?.cString == vec[1]?.cString)
|
||||
vec[1]?.withUnsafeRawPointer { pointer in
|
||||
mymap?.values[0]?.withUnsafeRawPointer { mymapPointer in
|
||||
XCTAssertEqual(pointer, mymapPointer)
|
||||
#expect(pointer == mymapPointer)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -127,7 +131,7 @@ final class FlexBuffersReaderTests: XCTestCase {
|
||||
#if os(macOS)
|
||||
// Gets the current path of this test file then
|
||||
// strips out the nested directories.
|
||||
let filePath = URL(filePath: #file)
|
||||
let filePath = URL(filePath: #filePath)
|
||||
.deletingLastPathComponent()
|
||||
.deletingLastPathComponent()
|
||||
.deletingLastPathComponent()
|
||||
|
||||
@@ -16,10 +16,12 @@
|
||||
|
||||
import Common
|
||||
import FlexBuffers
|
||||
import XCTest
|
||||
import Foundation
|
||||
import Testing
|
||||
|
||||
final class FlexBuffersStringTests: XCTestCase {
|
||||
struct FlexBuffersStringTests {
|
||||
|
||||
@Test
|
||||
func testEncodingUnicodeString() {
|
||||
let text = "プ画をみて✋"
|
||||
|
||||
@@ -38,6 +40,6 @@ final class FlexBuffersStringTests: XCTestCase {
|
||||
return String(data: data, encoding: .unicode)
|
||||
}
|
||||
|
||||
XCTAssertEqual(builtString, text)
|
||||
#expect(builtString == text)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,9 +16,10 @@
|
||||
|
||||
import Common
|
||||
import FlexBuffers
|
||||
import XCTest
|
||||
import Testing
|
||||
|
||||
final class FlexBuffersWriterTests: XCTestCase {
|
||||
struct FlexBuffersWriterTests {
|
||||
@Test
|
||||
func testDeallocation() {
|
||||
let buf: ByteBuffer = {
|
||||
var fbx = FlexBuffersWriter()
|
||||
@@ -28,12 +29,13 @@ final class FlexBuffersWriterTests: XCTestCase {
|
||||
}()
|
||||
|
||||
buf.withUnsafeBytes {
|
||||
XCTAssertEqual(
|
||||
Array($0),
|
||||
[5, 72, 101, 108, 108, 111, 0, 6, 20, 1])
|
||||
#expect(
|
||||
Array($0) ==
|
||||
[5, 72, 101, 108, 108, 111, 0, 6, 20, 1])
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
func testAddingVectorOfScalars() {
|
||||
var fbx = FlexBuffersWriter()
|
||||
fbx.vector {
|
||||
@@ -45,8 +47,8 @@ final class FlexBuffersWriterTests: XCTestCase {
|
||||
|
||||
buf.withUnsafeBytes {
|
||||
// swiftformat:disable all
|
||||
XCTAssertEqual(
|
||||
Array($0),
|
||||
#expect(
|
||||
Array($0) ==
|
||||
[
|
||||
10, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0, 5, 0, 0, 0, 6, 0, 0, 0, 7, 0,
|
||||
0, 0, 8, 0, 0, 0, 9, 0, 0, 0, 20, 0, 0, 0, 1, 41, 46, 2, 40, 1,
|
||||
@@ -55,6 +57,7 @@ final class FlexBuffersWriterTests: XCTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
func testAddingVectorOfUnsignedScalars() {
|
||||
var fbx = FlexBuffersWriter()
|
||||
fbx.vector {
|
||||
@@ -66,8 +69,8 @@ final class FlexBuffersWriterTests: XCTestCase {
|
||||
|
||||
buf.withUnsafeBytes {
|
||||
// swiftformat:disable all
|
||||
XCTAssertEqual(
|
||||
Array($0),
|
||||
#expect(
|
||||
Array($0) ==
|
||||
[
|
||||
10, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0,
|
||||
0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0,
|
||||
@@ -78,6 +81,7 @@ final class FlexBuffersWriterTests: XCTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
func testAddingVectorOfBools() {
|
||||
var fbx = FlexBuffersWriter()
|
||||
fbx.vector {
|
||||
@@ -89,13 +93,14 @@ final class FlexBuffersWriterTests: XCTestCase {
|
||||
|
||||
buf.withUnsafeBytes {
|
||||
// swiftformat:disable all
|
||||
XCTAssertEqual(
|
||||
Array($0),
|
||||
#expect(
|
||||
Array($0) ==
|
||||
[4, 1, 0, 1, 0, 1, 5, 144, 2, 40, 1])
|
||||
// swiftformat:enable all
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
func testSortingWithinMap() {
|
||||
var fbx = FlexBuffersWriter()
|
||||
fbx.map {
|
||||
@@ -106,8 +111,8 @@ final class FlexBuffersWriterTests: XCTestCase {
|
||||
let buf: ByteBuffer = fbx.sizedByteBuffer
|
||||
buf.withUnsafeBytes {
|
||||
// swiftformat:disable all
|
||||
XCTAssertEqual(
|
||||
Array($0),
|
||||
#expect(
|
||||
Array($0) ==
|
||||
[
|
||||
98, 111, 111, 108, 50, 0, 98, 111, 111, 108, 49, 0, 2, 7, 14, 2, 1, 2, 1, 0, 104, 104, 4,
|
||||
36, 1,
|
||||
@@ -117,6 +122,7 @@ final class FlexBuffersWriterTests: XCTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
func testSharingKeyWithinMap() {
|
||||
var fbx = FlexBuffersWriter(initialSize: 1000, flags: .shareKeysAndStrings)
|
||||
fbx.map {
|
||||
@@ -128,8 +134,8 @@ final class FlexBuffersWriterTests: XCTestCase {
|
||||
let buf: ByteBuffer = fbx.sizedByteBuffer
|
||||
buf.withUnsafeBytes {
|
||||
// swiftformat:disable all
|
||||
XCTAssertEqual(
|
||||
Array($0),
|
||||
#expect(
|
||||
Array($0) ==
|
||||
[
|
||||
119, 101, 108, 99, 111, 109, 101, 0, 7, 119, 101, 108, 99, 111, 109, 101, 0, 3, 18, 19,
|
||||
20, 3, 1, 3, 15, 16, 17, 20, 20, 20, 6, 36, 1,
|
||||
@@ -139,19 +145,21 @@ final class FlexBuffersWriterTests: XCTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
func testNestingVectorInMap() {
|
||||
let buf: ByteBuffer = createSizedBuffer()
|
||||
|
||||
buf.withUnsafeBytes {
|
||||
// swiftformat:disable all
|
||||
XCTAssertEqual(
|
||||
Array($0),
|
||||
#expect(
|
||||
Array($0) ==
|
||||
flexbufferGolden
|
||||
)
|
||||
// swiftformat:enable all
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
func testAddingNil() {
|
||||
var fbx = FlexBuffersWriter(
|
||||
initialSize: 8,
|
||||
@@ -165,14 +173,15 @@ final class FlexBuffersWriterTests: XCTestCase {
|
||||
let buf: ByteBuffer = fbx.sizedByteBuffer
|
||||
buf.withUnsafeBytes {
|
||||
// swiftformat:disable all
|
||||
XCTAssertEqual(
|
||||
Array($0),
|
||||
#expect(
|
||||
Array($0) ==
|
||||
[118, 0, 1, 3, 1, 1, 1, 0, 0, 2, 36, 1]
|
||||
)
|
||||
// swiftformat:enable all
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
func testAddingManually() {
|
||||
var fbx = FlexBuffersWriter(
|
||||
initialSize: 8,
|
||||
@@ -209,14 +218,15 @@ final class FlexBuffersWriterTests: XCTestCase {
|
||||
let buf: ByteBuffer = fbx.sizedByteBuffer
|
||||
buf.withUnsafeBytes {
|
||||
// swiftformat:disable all
|
||||
XCTAssertEqual(
|
||||
Array($0),
|
||||
#expect(
|
||||
Array($0) ==
|
||||
flexbufferGolden
|
||||
)
|
||||
// swiftformat:enable all
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
func testEncodingAllTypes() {
|
||||
var fbx = FlexBuffersWriter()
|
||||
fbx.vector {
|
||||
@@ -240,8 +250,8 @@ final class FlexBuffersWriterTests: XCTestCase {
|
||||
|
||||
buf.withUnsafeBytes {
|
||||
// swiftformat:disable all
|
||||
XCTAssertEqual(
|
||||
Array($0),
|
||||
#expect(
|
||||
Array($0) ==
|
||||
allTypesGolden)
|
||||
// swiftformat:enable all
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user