Skip to content

Commit

Permalink
Merge pull request #2 from swhitty/swiftwasm
Browse files Browse the repository at this point in the history
Add support for Web Assembly (SwiftWASM) exclude UserDefaults etc
  • Loading branch information
swhitty authored Aug 24, 2023
2 parents bc258e5 + f58773e commit 1c7a9ff
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Sources/UserDefaults+Codable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import Foundation

#if !os(WASI)
public extension UserDefaults {

func encode<T: Encodable>(_ value: T, forKey key: String) throws {
Expand All @@ -56,3 +57,4 @@ public extension UserDefaults {
}
}
}
#endif
4 changes: 4 additions & 0 deletions Tests/KeyValueDecoderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,7 @@ final class KeyValueDecoderTests: XCTestCase {
}
}

#if !os(WASI)
func testPlistCompatibleDecoder() throws {
let plistAny = try PropertyListEncoder.encodeAny([1, 2, Int?.none, 4])
XCTAssertEqual(
Expand All @@ -752,6 +753,7 @@ final class KeyValueDecoderTests: XCTestCase {
[1, 2, Int?.none, 4]
)
}
#endif
}

func AssertThrowsDecodingError<T>(_ expression: @autoclosure () throws -> T,
Expand Down Expand Up @@ -890,6 +892,7 @@ struct SomeTypes: Codable, Equatable {
var tString: String?
}

#if !os(WASI)
private extension PropertyListEncoder {
static func encodeAny<T: Encodable>(_ value: T) throws -> Any {
let data = try PropertyListEncoder().encode(value)
Expand All @@ -903,3 +906,4 @@ private extension JSONEncoder {
return try JSONSerialization.jsonObject(with: data, options: [])
}
}
#endif
4 changes: 4 additions & 0 deletions Tests/KeyValueEncoderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -638,13 +638,15 @@ final class KeyValueEncodedTests: XCTestCase {
)
}

#if !os(WASI)
func testPlistCompatibleEncoder() throws {
let keyValueAny = try KeyValueEncoder.makePlistCompatible().encode([1, 2, Int?.none, 4])
XCTAssertEqual(
try PropertyListDecoder.decodeAny([Int?].self, from: keyValueAny),
[1, 2, Int?.none, 4]
)
}
#endif

func testEncoder_Encodes_Dates() {
let date = Date()
Expand Down Expand Up @@ -764,12 +766,14 @@ extension KeyValueEncoder.EncodedValue {
}
}

#if !os(WASI)
private extension PropertyListDecoder {
static func decodeAny<T: Decodable>(_ type: T.Type, from value: Any?) throws -> T {
let data = try PropertyListSerialization.data(fromPropertyList: value as Any, format: .xml, options: 0)
return try PropertyListDecoder().decode(type, from: data)
}
}
#endif

private extension JSONDecoder {
static func decodeAny<T: Decodable>(_ type: T.Type, from value: Any?) throws -> T {
Expand Down
2 changes: 2 additions & 0 deletions Tests/UserDefaults+CodableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import Foundation
import XCTest

#if !os(WASI)
final class UserDefaultsCodableTests: XCTestCase {

func testEncodes_Single() {
Expand Down Expand Up @@ -331,3 +332,4 @@ private extension UserDefaults {
return UserDefaults(suiteName: "mock")!
}
}
#endif

0 comments on commit 1c7a9ff

Please sign in to comment.