Skip to content
Merged
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
41 changes: 21 additions & 20 deletions Tests/KarrotCodableKitTests/AnyCodable/AnyCodableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
// Created by Elon on 4/9/25.
//

import XCTest
import Testing
import Foundation
@testable import KarrotCodableKit

final class AnyCodableTests: XCTestCase {
struct AnyCodableTests {

@CustomCodable(codingKeyStyle: .snakeCase)
struct SomeCodable {
Expand All @@ -18,7 +19,7 @@ final class AnyCodableTests: XCTestCase {
var hasUnderscore: String
}

func testJSONDecoding() throws {
@Test func testJSONDecoding() throws {
// given
let json = """
{
Expand All @@ -41,16 +42,16 @@ final class AnyCodableTests: XCTestCase {
let dictionary = try decoder.decode([String: AnyCodable].self, from: json)

// then
XCTAssertEqual(dictionary["boolean"]?.value as! Bool, true)
XCTAssertEqual(dictionary["integer"]?.value as! Int, 42)
XCTAssertEqual(dictionary["double"]?.value as! Double, 3.141592653589793, accuracy: 0.001)
XCTAssertEqual(dictionary["string"]?.value as! String, "string")
XCTAssertEqual(dictionary["array"]?.value as! [Int], [1, 2, 3])
XCTAssertEqual(dictionary["nested"]?.value as! [String: String], ["a": "alpha", "b": "bravo", "c": "charlie"])
XCTAssertEqual(dictionary["null"]?.value as! NSNull, NSNull())
#expect(dictionary["boolean"]?.value as! Bool == true)
#expect(dictionary["integer"]?.value as! Int == 42)
#expect(abs(dictionary["double"]?.value as! Double - 3.141592653589793) < 0.001)
#expect(dictionary["string"]?.value as! String == "string")
#expect(dictionary["array"]?.value as! [Int] == [1, 2, 3])
#expect(dictionary["nested"]?.value as! [String: String] == ["a": "alpha", "b": "bravo", "c": "charlie"])
#expect(dictionary["null"]?.value as! NSNull == NSNull())
}

func testJSONDecodingEquatable() throws {
@Test func testJSONDecodingEquatable() throws {
// given
let json = """
{
Expand All @@ -74,16 +75,16 @@ final class AnyCodableTests: XCTestCase {
let dictionary2 = try decoder.decode([String: AnyCodable].self, from: json)

// then
XCTAssertEqual(dictionary1["boolean"], dictionary2["boolean"])
XCTAssertEqual(dictionary1["integer"], dictionary2["integer"])
XCTAssertEqual(dictionary1["double"], dictionary2["double"])
XCTAssertEqual(dictionary1["string"], dictionary2["string"])
XCTAssertEqual(dictionary1["array"], dictionary2["array"])
XCTAssertEqual(dictionary1["nested"], dictionary2["nested"])
XCTAssertEqual(dictionary1["null"], dictionary2["null"])
#expect(dictionary1["boolean"] == dictionary2["boolean"])
#expect(dictionary1["integer"] == dictionary2["integer"])
#expect(dictionary1["double"] == dictionary2["double"])
#expect(dictionary1["string"] == dictionary2["string"])
#expect(dictionary1["array"] == dictionary2["array"])
#expect(dictionary1["nested"] == dictionary2["nested"])
#expect(dictionary1["null"] == dictionary2["null"])
}

func testJSONEncoding() throws {
@Test func testJSONEncoding() throws {
// given
let someCodable = AnyCodable(SomeCodable(
string: "String",
Expand Down Expand Up @@ -139,6 +140,6 @@ final class AnyCodableTests: XCTestCase {
""".data(using: .utf8)!
let expectedJSONObject = try JSONSerialization.jsonObject(with: expected) as! NSDictionary

XCTAssertEqual(encodedJSONObject, expectedJSONObject)
#expect(encodedJSONObject == expectedJSONObject)
}
}
21 changes: 11 additions & 10 deletions Tests/KarrotCodableKitTests/AnyCodable/AnyDecodableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
// Created by Elon on 4/9/25.
//

import XCTest
import Testing
import Foundation
@testable import KarrotCodableKit

final class AnyDecodableTests: XCTestCase {
func testJSONDecoding() throws {
struct AnyDecodableTests {
@Test func testJSONDecoding() throws {
// given
let json = """
{
Expand All @@ -32,12 +33,12 @@ final class AnyDecodableTests: XCTestCase {
let dictionary = try decoder.decode([String: AnyDecodable].self, from: json)

// then
XCTAssertEqual(dictionary["boolean"]?.value as! Bool, true)
XCTAssertEqual(dictionary["integer"]?.value as! Int, 42)
XCTAssertEqual(dictionary["double"]?.value as! Double, 3.141592653589793, accuracy: 0.001)
XCTAssertEqual(dictionary["string"]?.value as! String, "string")
XCTAssertEqual(dictionary["array"]?.value as! [Int], [1, 2, 3])
XCTAssertEqual(dictionary["nested"]?.value as! [String: String], ["a": "alpha", "b": "bravo", "c": "charlie"])
XCTAssertEqual(dictionary["null"]?.value as! NSNull, NSNull())
#expect(dictionary["boolean"]?.value as! Bool == true)
#expect(dictionary["integer"]?.value as! Int == 42)
#expect(abs(dictionary["double"]?.value as! Double - 3.141592653589793) < 0.001)
#expect(dictionary["string"]?.value as! String == "string")
#expect(dictionary["array"]?.value as! [Int] == [1, 2, 3])
#expect(dictionary["nested"]?.value as! [String: String] == ["a": "alpha", "b": "bravo", "c": "charlie"])
#expect(dictionary["null"]?.value as! NSNull == NSNull())
}
}
41 changes: 21 additions & 20 deletions Tests/KarrotCodableKitTests/AnyCodable/AnyEncodableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
// Created by Elon on 4/9/25.
//

import XCTest
import Testing
import Foundation
@testable import KarrotCodableKit

final class AnyEncodableTests: XCTestCase {
struct AnyEncodableTests {

@CustomEncodable(codingKeyStyle: .snakeCase)
struct SomeEncodable {
Expand All @@ -18,7 +19,7 @@ final class AnyEncodableTests: XCTestCase {
var hasUnderscore: String
}

func testJSONEncoding() throws {
@Test func testJSONEncoding() throws {
// given
let someEncodable = AnyEncodable(SomeEncodable(
string: "String",
Expand Down Expand Up @@ -71,10 +72,10 @@ final class AnyEncodableTests: XCTestCase {
""".data(using: .utf8)!
let expectedJSONObject = try JSONSerialization.jsonObject(with: expected, options: []) as! NSDictionary

XCTAssertEqual(encodedJSONObject, expectedJSONObject)
#expect(encodedJSONObject == expectedJSONObject)
}

func testEncodeNSNumber() throws {
@Test func testEncodeNSNumber() throws {
// given
let dictionary: [String: NSNumber] = [
"boolean": true,
Expand Down Expand Up @@ -115,25 +116,25 @@ final class AnyEncodableTests: XCTestCase {
""".data(using: .utf8)!
let expectedJSONObject = try JSONSerialization.jsonObject(with: expected, options: []) as! NSDictionary

XCTAssertEqual(encodedJSONObject, expectedJSONObject)
XCTAssert(encodedJSONObject["boolean"] is Bool)
#expect(encodedJSONObject == expectedJSONObject)
#expect(encodedJSONObject["boolean"] is Bool)

XCTAssert(encodedJSONObject["char"] is Int8)
XCTAssert(encodedJSONObject["int"] is Int16)
XCTAssert(encodedJSONObject["short"] is Int32)
XCTAssert(encodedJSONObject["long"] is Int32)
XCTAssert(encodedJSONObject["longlong"] is Int64)
#expect(encodedJSONObject["char"] is Int8)
#expect(encodedJSONObject["int"] is Int16)
#expect(encodedJSONObject["short"] is Int32)
#expect(encodedJSONObject["long"] is Int32)
#expect(encodedJSONObject["longlong"] is Int64)

XCTAssert(encodedJSONObject["uchar"] is UInt8)
XCTAssert(encodedJSONObject["uint"] is UInt16)
XCTAssert(encodedJSONObject["ushort"] is UInt32)
XCTAssert(encodedJSONObject["ulong"] is UInt32)
XCTAssert(encodedJSONObject["ulonglong"] is UInt64)
#expect(encodedJSONObject["uchar"] is UInt8)
#expect(encodedJSONObject["uint"] is UInt16)
#expect(encodedJSONObject["ushort"] is UInt32)
#expect(encodedJSONObject["ulong"] is UInt32)
#expect(encodedJSONObject["ulonglong"] is UInt64)

XCTAssert(encodedJSONObject["double"] is Double)
#expect(encodedJSONObject["double"] is Double)
}

func testStringInterpolationEncoding() throws {
@Test func testStringInterpolationEncoding() throws {
// given
let dictionary: [String: AnyEncodable] = [
"boolean": "\(true)",
Expand All @@ -160,6 +161,6 @@ final class AnyEncodableTests: XCTestCase {
""".data(using: .utf8)!
let expectedJSONObject = try JSONSerialization.jsonObject(with: expected, options: []) as! NSDictionary

XCTAssertEqual(encodedJSONObject, expectedJSONObject)
#expect(encodedJSONObject == expectedJSONObject)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
// Created by Elon on 4/9/25.
//

import XCTest
import Testing
import Foundation

import KarrotCodableKit

final class DataValueTests: XCTestCase {
func testDecodingAndEncodingBase64String() throws {
struct DataValueTests {
@Test func testDecodingAndEncodingBase64String() throws {
// given
struct Fixture: Codable {
@DataValue<Base64Strategy> var data: Data
Expand All @@ -21,27 +22,27 @@ final class DataValueTests: XCTestCase {
let fixture = try JSONDecoder().decode(Fixture.self, from: jsonData)

// then
XCTAssertEqual(fixture.data, Data("BetterCodable".utf8))
#expect(fixture.data == Data("BetterCodable".utf8))

// when
let outputJSON = try JSONEncoder().encode(fixture)

// then
XCTAssertEqual(outputJSON, jsonData)
#expect(outputJSON == jsonData)
}

func testDecodingMalformedBase64Fails() throws {
@Test func testDecodingMalformedBase64Fails() throws {
// given
struct Fixture: Codable {
@DataValue<Base64Strategy> var data: Data
}
let jsonData = #"{"data":"invalidBase64!"}"#.data(using: .utf8)!

// when & then
XCTAssertThrowsError(try JSONDecoder().decode(Fixture.self, from: jsonData))
#expect(throws: (any Error).self) { try JSONDecoder().decode(Fixture.self, from: jsonData) }
}

func testDecodingAndEncodingBase64StringToArray() throws {
@Test func testDecodingAndEncodingBase64StringToArray() throws {
// given
struct Fixture: Codable {
@DataValue<Base64Strategy> var data: [UInt8]
Expand All @@ -52,12 +53,12 @@ final class DataValueTests: XCTestCase {
let fixture = try JSONDecoder().decode(Fixture.self, from: jsonData)

// then
XCTAssertEqual(fixture.data, Array("BetterCodable".utf8))
#expect(fixture.data == Array("BetterCodable".utf8))

// when
let outputJSON = try JSONEncoder().encode(fixture)

// then
XCTAssertEqual(outputJSON, jsonData)
#expect(outputJSON == jsonData)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
// Created by Elon on 2023/04/25.
//

import XCTest
import Testing
import Foundation

import KarrotCodableKit

final class DateValueTests: XCTestCase {
func testDecodingAndEncodingISO8601DateString() throws {
struct DateValueTests {
@Test func testDecodingAndEncodingISO8601DateString() throws {
struct Fixture: Codable {
@DateValue<ISO8601Strategy> var iso8601: Date
}
Expand All @@ -22,10 +23,10 @@ final class DateValueTests: XCTestCase {
let fixture = try JSONDecoder().decode(Fixture.self, from: jsonData)

// then
XCTAssertEqual(fixture.iso8601, Date(timeIntervalSince1970: 851042397))
#expect(fixture.iso8601 == Date(timeIntervalSince1970: 851042397))
}

func testDecodingAndEncodingISO8601DateStringWithFractionalSeconds() throws {
@Test func testDecodingAndEncodingISO8601DateStringWithFractionalSeconds() throws {
struct Fixture: Codable {
@DateValue<ISO8601WithFractionalSecondsStrategy> var iso8601: Date
@DateValue<ISO8601WithFractionalSecondsStrategy> var iso8601Short: Date
Expand All @@ -43,11 +44,11 @@ final class DateValueTests: XCTestCase {
let fixture = try JSONDecoder().decode(Fixture.self, from: jsonData)

// then
XCTAssertEqual(fixture.iso8601Short, Date(timeIntervalSince1970: 851042397.0))
XCTAssertEqual(fixture.iso8601, Date(timeIntervalSince1970: 851013597.123))
#expect(fixture.iso8601Short == Date(timeIntervalSince1970: 851042397.0))
#expect(fixture.iso8601 == Date(timeIntervalSince1970: 851013597.123))
}

func testDecodingAndEncodingRFC3339DateString() throws {
@Test func testDecodingAndEncodingRFC3339DateString() throws {
struct Fixture: Codable {
@DateValue<RFC3339Strategy> var rfc3339Date: Date
}
Expand All @@ -59,10 +60,10 @@ final class DateValueTests: XCTestCase {
let fixture = try JSONDecoder().decode(Fixture.self, from: jsonData)

// then
XCTAssertEqual(fixture.rfc3339Date, Date(timeIntervalSince1970: 851042397))
#expect(fixture.rfc3339Date == Date(timeIntervalSince1970: 851042397))
}

func testDecodingRFC3339NanoDateString() throws {
@Test func testDecodingRFC3339NanoDateString() throws {
struct Fixture: Codable {
@DateValue<RFC3339NanoStrategy> var rfc3339Date1: Date
@DateValue<RFC3339NanoStrategy> var rfc3339Date2: Date
Expand All @@ -88,36 +89,36 @@ final class DateValueTests: XCTestCase {
let fixture = try JSONDecoder().decode(Fixture.self, from: jsonData)

// then
XCTAssertEqual(fixture.rfc3339Date1, Date(timeIntervalSince1970: 851042397.0))
XCTAssertEqual(fixture.rfc3339Date2, Date(timeIntervalSince1970: 851042397.0))
XCTAssertEqual(fixture.rfc3339Date3, Date(timeIntervalSince1970: 1720617749.481))
XCTAssertEqual(fixture.rfc3339Date4, Date(timeIntervalSince1970: 1720588949.481))
XCTAssertEqual(fixture.rfc3339Date5, Date(timeIntervalSince1970: 1715082540.000))
XCTAssertEqual(fixture.rfc3339Date6, Date(timeIntervalSince1970: 1715082540.000))
#expect(fixture.rfc3339Date1 == Date(timeIntervalSince1970: 851042397.0))
#expect(fixture.rfc3339Date2 == Date(timeIntervalSince1970: 851042397.0))
#expect(fixture.rfc3339Date3 == Date(timeIntervalSince1970: 1720617749.481))
#expect(fixture.rfc3339Date4 == Date(timeIntervalSince1970: 1720588949.481))
#expect(fixture.rfc3339Date5 == Date(timeIntervalSince1970: 1715082540.000))
#expect(fixture.rfc3339Date6 == Date(timeIntervalSince1970: 1715082540.000))
}

func testEncodingRFC3339NanoDateToString() throws {
@Test func testEncodingRFC3339NanoDateToString() throws {
// given
let date = Date(timeIntervalSince1970: 1720588949.481)

// when
let result = RFC3339NanoStrategy.encode(date)

// then
XCTAssertEqual(result, "2024-07-10T05:22:29.481000Z")
#expect(result == "2024-07-10T05:22:29.481000Z")
}

func testDecodingAndEncodingUTCTimestamp() throws {
@Test func testDecodingAndEncodingUTCTimestamp() throws {
struct Fixture: Codable {
@DateValue<TimestampStrategy> var timestamp: Date
}
let jsonData = #"{"timestamp": 851042397.0}"#.data(using: .utf8)!

let fixture = try JSONDecoder().decode(Fixture.self, from: jsonData)
XCTAssertEqual(fixture.timestamp, Date(timeIntervalSince1970: 851042397))
#expect(fixture.timestamp == Date(timeIntervalSince1970: 851042397))
}

func testDecodingAndEncodingWithCustomStrategies() throws {
@Test func testDecodingAndEncodingWithCustomStrategies() throws {
struct Fixture: Codable {
@DateValue<TimestampStrategy> var timeStamp: Date
}
Expand All @@ -127,13 +128,13 @@ final class DateValueTests: XCTestCase {
decoder.keyDecodingStrategy = .convertFromSnakeCase
decoder.dateDecodingStrategy = .iso8601
let fixture = try decoder.decode(Fixture.self, from: jsonData)
XCTAssertEqual(fixture.timeStamp, Date(timeIntervalSince1970: 851042397))
#expect(fixture.timeStamp == Date(timeIntervalSince1970: 851042397))

let encoder = JSONEncoder()
encoder.keyEncodingStrategy = .convertToSnakeCase
encoder.dateEncodingStrategy = .iso8601
let data = try encoder.encode(fixture)
let fixture2 = try decoder.decode(Fixture.self, from: data)
XCTAssertEqual(fixture2.timeStamp, Date(timeIntervalSince1970: 851042397))
#expect(fixture2.timeStamp == Date(timeIntervalSince1970: 851042397))
}
}
Loading
Loading