Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@ jobs:
name: Swift ${{ matrix.swift }} on ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, macos-latest]
swift: ["5.7", "5.8", "5.9", "5.10"]
os: [ubuntu-22.04, macos-latest]
swift: ["5.7", "5.8", "5.9", "5.10", "6.0.0", "6.1.0"]
runs-on: ${{ matrix.os }}
steps:
- name: Setup Swift
uses: swift-actions/setup-swift@v2.1.0
uses: swift-actions/setup-swift@v2.3.0
with:
swift-version: ${{ matrix.swift }}
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Build
run: swift build
- name: Run tests
run: swift test
- name: Swift Package Registry
uses: twodayslate/[email protected]
6 changes: 3 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// swift-tools-version: 5.9
// swift-tools-version: 6.0

import PackageDescription

let package = Package(
name: "BinaryCodable",
platforms: [.macOS(.v10_13), .iOS(.v11), .tvOS(.v11), .watchOS(.v4)],
platforms: [.macOS(.v10_13), .iOS(.v12), .tvOS(.v12), .watchOS(.v4)],
products: [
.library(
name: "BinaryCodable",
Expand All @@ -21,5 +21,5 @@ let package = Package(
name: "BinaryCodableTests",
dependencies: ["BinaryCodable"]),
],
swiftLanguageVersions: [.v5]
swiftLanguageModes: [.v6]
)
25 changes: 25 additions & 0 deletions [email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// swift-tools-version: 5.9

import PackageDescription

let package = Package(
name: "BinaryCodable",
platforms: [.macOS(.v10_13), .iOS(.v11), .tvOS(.v11), .watchOS(.v4)],
products: [
.library(
name: "BinaryCodable",
targets: ["BinaryCodable"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0")
],
targets: [
.target(
name: "BinaryCodable",
dependencies: []),
.testTarget(
name: "BinaryCodableTests",
dependencies: ["BinaryCodable"]),
],
swiftLanguageVersions: [.v5]
)
4 changes: 2 additions & 2 deletions Tests/BinaryCodableTests/ArrayEncodingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ final class ArrayEncodingTests: XCTestCase {

self.measure {
do {
let data = try BinaryEncoder.encode(value)
print(data.count)
let _ = try BinaryEncoder.encode(value)
//print(data.count)
} catch {
XCTFail(error.localizedDescription)
}
Expand Down
25 changes: 15 additions & 10 deletions Tests/BinaryCodableTests/BoolTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,21 @@ final class BoolTests: XCTestCase {
}

func testUnkeyedWithBool() throws {
GenericTestStruct.encode { encoder in
var container = encoder.unkeyedContainer()
try container.encode(true)
}
GenericTestStruct.decode { decoder in
var container = try decoder.unkeyedContainer()
let value = try container.decode(Bool.self)
XCTAssertEqual(value, true)

struct UnkeyedWithBool: SomeCodable {

static func encode(_ encoder: any Encoder) throws {
var container = encoder.unkeyedContainer()
try container.encode(true)
}

static func decode(_ decoder: any Decoder) throws {
var container = try decoder.unkeyedContainer()
let value = try container.decode(Bool.self)
XCTAssertEqual(value, true)
}
}

try compare(UnkeyedWithBool())
}


}
53 changes: 32 additions & 21 deletions Tests/BinaryCodableTests/CodingPathTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,45 @@ import XCTest
final class CodingPathTests: XCTestCase {

func testCodingPathAtRoot() throws {
GenericTestStruct.encode { encoder in
// Need to set some value, otherwise encoding will fail
var container = encoder.singleValueContainer()
try container.encode(0)
XCTAssertEqual(encoder.codingPath, [])

struct Root: SomeCodable {

static func encode(_ encoder: any Encoder) throws {
// Need to set some value, otherwise encoding will fail
var container = encoder.singleValueContainer()
try container.encode(0)
XCTAssertEqual(encoder.codingPath, [])
}

static func decode(_ decoder: any Decoder) throws {
XCTAssertEqual(decoder.codingPath, [])
}
}
GenericTestStruct.decode { decoder in
XCTAssertEqual(decoder.codingPath, [])
}
try compare(GenericTestStruct())
try compare(Root())
}

func testCodingPathInKeyedContainer() throws {
enum SomeKey: Int, CodingKey {
case value = 1
}
GenericTestStruct.encode { encoder in
XCTAssertEqual(encoder.codingPath, [])
var container = encoder.container(keyedBy: SomeKey.self)
let unkeyed = container.nestedUnkeyedContainer(forKey: .value)
XCTAssertEqual(unkeyed.codingPath, [1])
}
GenericTestStruct.decode { decoder in
XCTAssertEqual(decoder.codingPath, [])
let container = try decoder.container(keyedBy: SomeKey.self)
let unkeyed = try container.nestedUnkeyedContainer(forKey: .value)
XCTAssertEqual(unkeyed.codingPath, [1])

struct Keyed: SomeCodable {

static func encode(_ encoder: any Encoder) throws {
XCTAssertEqual(encoder.codingPath, [])
var container = encoder.container(keyedBy: SomeKey.self)
let unkeyed = container.nestedUnkeyedContainer(forKey: .value)
XCTAssertEqual(unkeyed.codingPath, [1])
}

static func decode(_ decoder: any Decoder) throws {
XCTAssertEqual(decoder.codingPath, [])
let container = try decoder.container(keyedBy: SomeKey.self)
let unkeyed = try container.nestedUnkeyedContainer(forKey: .value)
XCTAssertEqual(unkeyed.codingPath, [1])
}
}
try compare(GenericTestStruct())

try compare(Keyed())
}
}
70 changes: 16 additions & 54 deletions Tests/BinaryCodableTests/Helper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,24 @@ import Foundation
import XCTest
@testable import BinaryCodable

struct GenericTestStruct: Codable, Equatable {

init() {

}

init(from decoder: Decoder) throws {
try GenericTestStruct.decodingRoutine(decoder)
}

func encode(to encoder: Encoder) throws {
try GenericTestStruct.encodingRoutine(encoder)
}

private static nonisolated(unsafe) var _encodingRoutine: (Encoder) throws -> Void = { _ in }

private static nonisolated(unsafe) var _decodingRoutine: (Decoder) throws -> Void = { _ in }
protocol SomeCodable: Codable, Equatable {

private static let encodeSemaphore = DispatchSemaphore(value: 1)
init()

static var encodingRoutine: (Encoder) throws -> Void {
get {
encodeSemaphore.wait()
let value = _encodingRoutine
encodeSemaphore.signal()
return value
}
set {
encodeSemaphore.wait()
_encodingRoutine = newValue
encodeSemaphore.signal()
}
}

private static let decodeSemaphore = DispatchSemaphore(value: 1)
static func encode(_ encoder: Encoder) throws -> ()

static var decodingRoutine: (Decoder) throws -> Void {
get {
decodeSemaphore.wait()
let value = _decodingRoutine
decodeSemaphore.signal()
return value
}
set {
decodeSemaphore.wait()
_decodingRoutine = newValue
decodeSemaphore.signal()
}
}
static func decode(_ decoder: Decoder) throws -> ()
}

static func encode(_ block: @escaping (Encoder) throws -> Void) {
encodingRoutine = block
extension SomeCodable {

init(from decoder: any Decoder) throws {
try Self.decode(decoder)
self.init()
}

static func decode(_ block: @escaping (Decoder) throws -> Void) {
decodingRoutine = block
func encode(to encoder: any Encoder) throws {
try Self.encode(encoder)
}
}

Expand Down Expand Up @@ -89,9 +51,9 @@ extension XCTestCase {
let data = try encoder.encode(value)
if let expected {
XCTAssertEqual(Array(data), expected)
} else {
print("Encoded data: \(Array(data))")
}
}// else {
// print("Encoded data: \(Array(data))")
//}

let decoder = BinaryDecoder()
let decoded = try decoder.decode(T.self, from: data)
Expand Down
56 changes: 31 additions & 25 deletions Tests/BinaryCodableTests/KeyedEncodingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -187,32 +187,38 @@ final class KeyedEncodingTests: XCTestCase {
case value = 1
case opt = 2
}
GenericTestStruct.encode { encoder in
var container = encoder.container(keyedBy: Keys.self)
let value: String? = nil
try container.encodeIfPresent(value, forKey: .value)
try container.encodeNil(forKey: .opt)
}
GenericTestStruct.decode { decoder in
let container = try decoder.container(keyedBy: Keys.self)
XCTAssertFalse(container.contains(.value))
// Nil is not encoded
XCTAssertFalse(container.contains(.opt))

let s = try container.decodeIfPresent(String.self, forKey: .value)
XCTAssertEqual(s, nil)

let optIsNil = try container.decodeNil(forKey: .opt)
XCTAssertTrue(optIsNil)
let opt = try container.decodeIfPresent(Bool.self, forKey: .opt)
XCTAssertNil(opt)
do {
_ = try container.decode(Bool.self, forKey: .opt)
XCTFail()
} catch {


struct NilEncoder: SomeCodable {

static func encode(_ encoder: any Encoder) throws {
var container = encoder.container(keyedBy: Keys.self)
let value: String? = nil
try container.encodeIfPresent(value, forKey: .value)
try container.encodeNil(forKey: .opt)
}

static func decode(_ decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: Keys.self)
XCTAssertFalse(container.contains(.value))
// Nil is not encoded
XCTAssertFalse(container.contains(.opt))

let s = try container.decodeIfPresent(String.self, forKey: .value)
XCTAssertEqual(s, nil)

let optIsNil = try container.decodeNil(forKey: .opt)
XCTAssertTrue(optIsNil)
let opt = try container.decodeIfPresent(Bool.self, forKey: .opt)
XCTAssertNil(opt)
do {
_ = try container.decode(Bool.self, forKey: .opt)
XCTFail()
} catch {

}
}
}
try compare(GenericTestStruct(), to: [])

try compare(NilEncoder())
}
}
4 changes: 2 additions & 2 deletions Tests/BinaryCodableTests/PropertyWrapperCodingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ final class PropertyWrapperCodingTests: XCTestCase {

// If the suffix differs, this error is specific to the individual test case,
// so report it on the call-side
XCTAssertEqual(Array(data.suffix(from: bytePrefix.count)), byteSuffix, file: file, line: line)
XCTAssertEqual(Array(data.suffix(from: bytePrefix.count)), byteSuffix, file: (file), line: line)

let decodedWrapper: KeyedWrapper<T> = try BinaryDecoder.decode(from: data)
XCTAssertEqual(decodedWrapper, wrapper, file: file, line: line)
XCTAssertEqual(decodedWrapper, wrapper, file: (file), line: line)
}

func testOptionalWrappedStringSome() throws {
Expand Down
2 changes: 1 addition & 1 deletion Tests/BinaryCodableTests/SequenceEncoderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ final class SequenceEncoderTests: XCTestCase {
let encoder = BinaryStreamEncoder<T>()

let bytes = try input.mapAndJoin(encoder.encode)
print(Array(bytes))
//print(Array(bytes))
let decoder = BinaryStreamDecoder<T>()

decoder.add(bytes)
Expand Down
Loading
Loading