mirror of
https://github.com/google/flatbuffers.git
synced 2026-07-06 01:56:54 +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:
@@ -14,12 +14,13 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import XCTest
|
||||
import Testing
|
||||
|
||||
@testable import FlatBuffers
|
||||
|
||||
final class FlatBuffersVectors: XCTestCase {
|
||||
struct FlatBuffersVectors {
|
||||
|
||||
@Test
|
||||
func testCreatingTwoCountries() {
|
||||
let norway = "Norway"
|
||||
let denmark = "Denmark"
|
||||
@@ -40,8 +41,8 @@ final class FlatBuffersVectors: XCTestCase {
|
||||
let vectorOffset = b.createVector(ofOffsets: vector)
|
||||
b.finish(offset: vectorOffset)
|
||||
// swiftformat:disable all
|
||||
XCTAssertEqual(
|
||||
b.sizedByteArray,
|
||||
#expect(
|
||||
b.sizedByteArray ==
|
||||
[
|
||||
4, 0, 0, 0, 2, 0, 0, 0, 48, 0, 0, 0, 16, 0, 0, 0, 0, 0, 10, 0, 18, 0, 4, 0, 8, 0, 12, 0, 10,
|
||||
0, 0, 0, 40, 0, 0, 0, 100, 0, 0, 0, 200, 0, 0, 0, 0, 0, 10, 0, 16, 0, 4, 0, 8, 0, 12, 0, 10,
|
||||
@@ -51,18 +52,20 @@ final class FlatBuffersVectors: XCTestCase {
|
||||
// swiftformat:enable all
|
||||
}
|
||||
|
||||
@Test
|
||||
func testCreateIntArray() {
|
||||
let numbers: [Int32] = [1, 2, 3, 4, 5]
|
||||
var b = FlatBufferBuilder(initialSize: 20)
|
||||
let o = b.createVector(numbers, size: numbers.count)
|
||||
b.finish(offset: o)
|
||||
// swiftformat:disable all
|
||||
XCTAssertEqual(
|
||||
b.sizedByteArray,
|
||||
#expect(
|
||||
b.sizedByteArray ==
|
||||
[4, 0, 0, 0, 5, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0, 5, 0, 0, 0])
|
||||
// swiftformat:enable all
|
||||
}
|
||||
|
||||
@Test
|
||||
func testCreateStructArray() {
|
||||
struct Vec: NativeStruct {
|
||||
let x, y, z: Float32
|
||||
@@ -76,8 +79,8 @@ final class FlatBuffersVectors: XCTestCase {
|
||||
let o = b.createVector(ofStructs: vector)
|
||||
b.finish(offset: o)
|
||||
// swiftformat:disable all
|
||||
XCTAssertEqual(
|
||||
b.sizedByteArray,
|
||||
#expect(
|
||||
b.sizedByteArray ==
|
||||
[
|
||||
4, 0, 0, 0, 3, 0, 0, 0, 0, 0, 128, 63, 0, 0, 0, 64, 0, 0, 64, 64, 0, 0, 128, 64, 0, 0, 160,
|
||||
64, 0, 0, 192, 64, 0, 0, 224, 64, 0, 0, 0, 65, 0, 0, 16, 65,
|
||||
@@ -85,28 +88,31 @@ final class FlatBuffersVectors: XCTestCase {
|
||||
// swiftformat:enable all
|
||||
}
|
||||
|
||||
@Test
|
||||
func testCreateEmptyIntArray() {
|
||||
let numbers: [Int32] = []
|
||||
var b = FlatBufferBuilder(initialSize: 20)
|
||||
let o = b.createVector(numbers, size: numbers.count)
|
||||
b.finish(offset: o)
|
||||
XCTAssertEqual(b.sizedByteArray, [4, 0, 0, 0, 0, 0, 0, 0])
|
||||
#expect(b.sizedByteArray == [4, 0, 0, 0, 0, 0, 0, 0])
|
||||
}
|
||||
|
||||
@Test
|
||||
func testCreateVectorOfStrings() {
|
||||
let strs = ["Denmark", "Norway"]
|
||||
var b = FlatBufferBuilder(initialSize: 20)
|
||||
let o = b.createVector(ofStrings: strs)
|
||||
b.finish(offset: o)
|
||||
// swiftformat:disable all
|
||||
XCTAssertEqual(
|
||||
b.sizedByteArray,
|
||||
#expect(
|
||||
b.sizedByteArray ==
|
||||
[
|
||||
4, 0, 0, 0, 2, 0, 0, 0, 20, 0, 0, 0, 4, 0, 0, 0, 6, 0, 0, 0, 78, 111, 114, 119, 97, 121, 0,
|
||||
0, 7, 0, 0, 0, 68, 101, 110, 109, 97, 114, 107, 0,
|
||||
])
|
||||
// swiftformat:enable all
|
||||
}
|
||||
@Test
|
||||
func testCreateSharedStringVector() {
|
||||
let norway = "Norway"
|
||||
let denmark = "Denmark"
|
||||
@@ -119,8 +125,8 @@ final class FlatBuffersVectors: XCTestCase {
|
||||
let end = b.createVector(ofOffsets: v)
|
||||
b.finish(offset: end)
|
||||
// swiftformat:disable all
|
||||
XCTAssertEqual(
|
||||
b.sizedByteArray,
|
||||
#expect(
|
||||
b.sizedByteArray ==
|
||||
[
|
||||
4, 0, 0, 0, 4, 0, 0, 0, 28, 0, 0, 0, 12, 0, 0, 0, 20, 0, 0, 0, 4, 0, 0, 0, 7, 0, 0, 0, 68,
|
||||
101, 110, 109, 97, 114, 107, 0, 6, 0, 0, 0, 78, 111, 114, 119, 97, 121, 0, 0,
|
||||
@@ -128,6 +134,7 @@ final class FlatBuffersVectors: XCTestCase {
|
||||
// swiftformat:enable all
|
||||
}
|
||||
|
||||
@Test
|
||||
func testReadInt32Array() {
|
||||
let data: [Int32] = [1, 2, 3, 4, 5]
|
||||
var b = FlatBufferBuilder(initialSize: 20)
|
||||
@@ -136,10 +143,11 @@ final class FlatBuffersVectors: XCTestCase {
|
||||
b.finish(offset: end)
|
||||
let number = Numbers.getRootAsNumbers(ByteBuffer(bytes: b.sizedByteArray))
|
||||
for (index, num) in number.vArrayInt32.enumerated() {
|
||||
XCTAssertEqual(num, data[index])
|
||||
#expect(num == data[index])
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
func testReadDoubleArray() {
|
||||
let data: [Double] = [1, 2, 3, 4, 5]
|
||||
var b = FlatBufferBuilder(initialSize: 20)
|
||||
@@ -148,10 +156,11 @@ final class FlatBuffersVectors: XCTestCase {
|
||||
b.finish(offset: end)
|
||||
let number = Numbers.getRootAsNumbers(ByteBuffer(bytes: b.sizedByteArray))
|
||||
for (index, num) in number.vArrayDouble.enumerated() {
|
||||
XCTAssertEqual(num, data[index])
|
||||
#expect(num == data[index])
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
func testHasForArray() {
|
||||
var builder = FlatBufferBuilder(initialSize: 20)
|
||||
let emptyVector = [UInt8]()
|
||||
@@ -166,14 +175,14 @@ final class FlatBuffersVectors: XCTestCase {
|
||||
|
||||
var byteBuffer = ByteBuffer(bytes: builder.sizedByteArray)
|
||||
let msg: Swift_Tests_Vectors = getRoot(byteBuffer: &byteBuffer)
|
||||
XCTAssertEqual(msg.none_.isEmpty, true)
|
||||
XCTAssertEqual(msg.empty.isEmpty, true)
|
||||
XCTAssertEqual(msg.empty.count, 0)
|
||||
XCTAssertEqual(msg.array.isEmpty, false)
|
||||
XCTAssertEqual(msg.array.count, 3)
|
||||
#expect(msg.none_.isEmpty == true)
|
||||
#expect(msg.empty.isEmpty == true)
|
||||
#expect(msg.empty.count == 0)
|
||||
#expect(msg.array.isEmpty == false)
|
||||
#expect(msg.array.count == 3)
|
||||
|
||||
for i in msg.array.startIndex..<msg.array.endIndex {
|
||||
XCTAssertEqual(msg.array[i], 1 + UInt64(i))
|
||||
#expect(msg.array[i] == 1 + UInt64(i))
|
||||
}
|
||||
|
||||
let array = msg.withUnsafePointerToArray { ptr, count in
|
||||
@@ -185,7 +194,7 @@ final class FlatBuffersVectors: XCTestCase {
|
||||
return Array(ptr)
|
||||
}
|
||||
|
||||
XCTAssertEqual(array, [1, 2, 3])
|
||||
#expect(array == [1, 2, 3])
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user