Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
71 kB · 1962 lines
Swift
at main
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963//// AbstractTopLevelDecoderTests.swift// URLQueryItemCoder//// Created by Kyle Hughes on 1/12/23.//
#if canImport(Combine)import Combineimport XCTest
@testable import URLQueryItemCoder
public class AbstractTopLevelDecoderTests<Target>: XCTestCase where Target: TopLevelDecoder, Target.Input: Equatable { // MARK: Public Abstract Interface public var expectedValues: CodableTestExpectation<Target.Input> { fatalErrorForUnimplementedAbstractInterface() }
public func target(using configuration: DecodingStrategies = .default) -> Target { fatalErrorForUnimplementedAbstractInterface() } // MARK: Public Class Interface public class var isAbstractTestCase: Bool { self == AbstractTopLevelDecoderTests.self } // MARK: XCTestCase Implementation override class public var defaultTestSuite: XCTestSuite { guard isAbstractTestCase else { return super.defaultTestSuite }
return XCTestSuite(name: "Empty Suite for \(Self.self)") } // MARK: Single Value Primitive Tests public func test_singleValue_bool() throws { try XCTSkipIf(Self.isAbstractTestCase)
XCTAssertDecoder( target, decodes: expectedValues.singleValueBool, encodedWith: .default, decodedWith: .default, as: true ) } public func test_singleValue_int() throws { try XCTSkipIf(Self.isAbstractTestCase)
XCTAssertDecoder( target, decodes: expectedValues.singleValueInt, encodedWith: .default, decodedWith: .default, as: 10 ) } public func test_singleValue_int8() throws { try XCTSkipIf(Self.isAbstractTestCase)
XCTAssertDecoder( target, decodes: expectedValues.singleValueInt8, encodedWith: .default, decodedWith: .default, as: 20 ) } public func test_singleValue_int16() throws { try XCTSkipIf(Self.isAbstractTestCase)
XCTAssertDecoder( target, decodes: expectedValues.singleValueInt16, encodedWith: .default, decodedWith: .default, as: 30 ) } public func test_singleValue_int32() throws { try XCTSkipIf(Self.isAbstractTestCase)
XCTAssertDecoder( target, decodes: expectedValues.singleValueInt32, encodedWith: .default, decodedWith: .default, as: 40 ) } public func test_singleValue_int64() throws { try XCTSkipIf(Self.isAbstractTestCase)
XCTAssertDecoder( target, decodes: expectedValues.singleValueInt64, encodedWith: .default, decodedWith: .default, as: 50 ) } public func test_singleValue_nil() throws { try XCTSkipIf(Self.isAbstractTestCase) let inputValue = expectedValues.singleValueNil(nil, .default) let decodedValue = try target(using: .default).decode(String?.self, from: inputValue) XCTAssertEqual(decodedValue, nil as String?) } public func test_singleValue_string() throws { try XCTSkipIf(Self.isAbstractTestCase)
XCTAssertDecoder( target, decodes: expectedValues.singleValueString, encodedWith: .default, decodedWith: .default, as: "DEFAULT CONSTANT" ) } public func test_singleValue_uint() throws { try XCTSkipIf(Self.isAbstractTestCase)
XCTAssertDecoder( target, decodes: expectedValues.singleValueUInt, encodedWith: .default, decodedWith: .default, as: 60 ) } public func test_singleValue_uint8() throws { try XCTSkipIf(Self.isAbstractTestCase)
XCTAssertDecoder( target, decodes: expectedValues.singleValueUInt8, encodedWith: .default, decodedWith: .default, as: 70 ) } public func test_singleValue_uint16() throws { try XCTSkipIf(Self.isAbstractTestCase)
XCTAssertDecoder( target, decodes: expectedValues.singleValueUInt16, encodedWith: .default, decodedWith: .default, as: 80 ) } public func test_singleValue_uint32() throws { try XCTSkipIf(Self.isAbstractTestCase)
XCTAssertDecoder( target, decodes: expectedValues.singleValueUInt32, encodedWith: .default, decodedWith: .default, as: 90 ) } public func test_singleValue_uint64() throws { try XCTSkipIf(Self.isAbstractTestCase)
XCTAssertDecoder( target, decodes: expectedValues.singleValueUInt64, encodedWith: .default, decodedWith: .default, as: 100 ) } // MARK: Single Value Data Tests public func test_singleValue_data_base64() throws { try XCTSkipIf(Self.isAbstractTestCase) XCTAssertDecoder( target, decodes: expectedValues.singleValueDataBase64, encodedWith: EncodingStrategies(dataStrategy: .base64), decodedWith: DecodingStrategies(dataStrategy: .base64), as: Data([0xDE, 0xAD, 0xBE, 0xEF]) ) } public func test_singleValue_data_custom() throws { try XCTSkipIf(Self.isAbstractTestCase) // We'll encode a number instead of data let encode: @Sendable (Data, Encoder) throws -> Void = { date, encoder in var container = encoder.singleValueContainer() try container.encode(42) } let decode: @Sendable (Decoder) throws -> Data = { encoder in Data([0xDE, 0xAD, 0xBE, 0xEF]) }
XCTAssertDecoder( target, decodes: expectedValues.singleValueDataCustom, encodedWith: EncodingStrategies(dataStrategy: .custom(encode)), decodedWith: DecodingStrategies(dataStrategy: .custom(decode)), as: Data([0xDE, 0xAD, 0xBE, 0xEF]) ) } public func test_singleValue_data_deferredToData() throws { try XCTSkipIf(Self.isAbstractTestCase) XCTAssertDecoder( target, decodes: expectedValues.singleValueDataDeferredToData, encodedWith: EncodingStrategies(dataStrategy: .deferredToData), decodedWith: DecodingStrategies(dataStrategy: .deferredToData), as: Data([0xDE, 0xAD, 0xBE, 0xEF]) ) } // MARK: Single Value Date Tests public func test_singleValue_date_custom() throws { try XCTSkipIf(Self.isAbstractTestCase)
// We'll encode a number instead of a date let encode: (Date, Encoder) throws -> Void = { date, encoder in var container = encoder.singleValueContainer() try container.encode(42) } let decode: @Sendable (Decoder) throws -> Date = { encoder in Date(timeIntervalSince1970: 1_000) }
XCTAssertDecoder( target, decodes: expectedValues.singleValueDateDeferredToDate, encodedWith: EncodingStrategies(dateStrategy: .custom(encode)), decodedWith: DecodingStrategies(dateStrategy: .custom(decode)), as: Date(timeIntervalSince1970: 1_000) ) }
public func test_singleValue_date_deferredToDate() throws { try XCTSkipIf(Self.isAbstractTestCase) XCTAssertDecoder( target, decodes: expectedValues.singleValueDateDeferredToDate, encodedWith: EncodingStrategies(dateStrategy: .deferredToDate), decodedWith: DecodingStrategies(dateStrategy: .deferredToDate), as: Date(timeIntervalSince1970: 1_000) ) }
public func test_singleValue_date_formatted() throws { try XCTSkipIf(Self.isAbstractTestCase)
let formatter = DateFormatter() formatter.dateStyle = .full formatter.locale = Locale(identifier: "en_US") formatter.timeStyle = .full formatter.timeZone = TimeZone(identifier: "UTC")
XCTAssertDecoder( target, decodes: expectedValues.singleValueDateFormatted, encodedWith: EncodingStrategies(dateStrategy: .formatted(formatter)), decodedWith: DecodingStrategies(dateStrategy: .formatted(formatter)), as: Date(timeIntervalSince1970: 1_000) ) }
public func test_singleValue_date_iso8601() throws { try XCTSkipIf(Self.isAbstractTestCase)
XCTAssertDecoder( target, decodes: expectedValues.singleValueDateISO8601, encodedWith: EncodingStrategies(dateStrategy: .iso8601), decodedWith: DecodingStrategies(dateStrategy: .iso8601), as: Date(timeIntervalSince1970: 1_000) ) }
public func test_singleValue_date_millisecondsSince1970() throws { try XCTSkipIf(Self.isAbstractTestCase)
XCTAssertDecoder( target, decodes: expectedValues.singleValueDateMillisecondsSince1970, encodedWith: EncodingStrategies(dateStrategy: .millisecondsSince1970), decodedWith: DecodingStrategies(dateStrategy: .millisecondsSince1970), as: Date(timeIntervalSince1970: 1_000) ) }
public func test_singleValue_date_secondsSince1970() throws { try XCTSkipIf(Self.isAbstractTestCase)
XCTAssertDecoder( target, decodes: expectedValues.singleValueDateSecondsSince1970, encodedWith: EncodingStrategies(dateStrategy: .secondsSince1970), decodedWith: DecodingStrategies(dateStrategy: .secondsSince1970), as: Date(timeIntervalSince1970: 1_000) ) }
// MARK: Single Value Double Tests
public func test_singleValue_double() throws { XCTAssertDecoder( target, decodes: expectedValues.singleValueDouble, encodedWith: .default, decodedWith: .default, as: Double(100) ) }
public func test_singleValue_double_convertToString_nan() throws { try XCTSkipIf(Self.isAbstractTestCase) let encodingStrategies = EncodingStrategies( nonConformingFloatStrategy: .convertToString( positiveInfinity: "positiveInfinity", negativeInfinity: "negativeInfinity", nan: "notANumber" ) ) let decodingStrategies = DecodingStrategies( nonConformingFloatStrategy: .convertToString( positiveInfinity: "positiveInfinity", negativeInfinity: "negativeInfinity", nan: "notANumber" ) ) let expectation = Double.nan let inputValue = expectedValues.singleValueDoubleConvertToStringNaN(expectation, encodingStrategies) let decodedValue = try target(using: decodingStrategies).decode(Double.self, from: inputValue) XCTAssertEqual(decodedValue.isNaN, expectation.isNaN) }
public func test_singleValue_double_convertToString_negativeInfinity() throws { try XCTSkipIf(Self.isAbstractTestCase)
XCTAssertDecoder( target, decodes: expectedValues.singleValueDoubleConvertToStringNegativeInfinity, encodedWith: EncodingStrategies( nonConformingFloatStrategy: .convertToString( positiveInfinity: "positiveInfinity", negativeInfinity: "negativeInfinity", nan: "notANumber" ) ), decodedWith: DecodingStrategies( nonConformingFloatStrategy: .convertToString( positiveInfinity: "positiveInfinity", negativeInfinity: "negativeInfinity", nan: "notANumber" ) ), as: -.infinity ) }
public func test_singleValue_double_convertToString_positiveInfinity() throws { try XCTSkipIf(Self.isAbstractTestCase)
XCTAssertDecoder( target, decodes: expectedValues.singleValueDoubleConvertToStringPositiveInfinity, encodedWith: EncodingStrategies( nonConformingFloatStrategy: .convertToString( positiveInfinity: "positiveInfinity", negativeInfinity: "negativeInfinity", nan: "notANumber" ) ), decodedWith: DecodingStrategies( nonConformingFloatStrategy: .convertToString( positiveInfinity: "positiveInfinity", negativeInfinity: "negativeInfinity", nan: "notANumber" ) ), as: .infinity ) } // TODO: Make the floating point throwing code path throw the appropriate error. // The error that is thrown is an error about being unable to decode a number from (e.g.) "notANumber". That is // because… we need a way to represent the number in the format we are saying it cannot be represented in. May // just be unable to test the decoding side because the implication is the value couldn't be encoded. Regardless, // all observed behavior is correct.
public func test_singleValue_double_throw_nan() throws { try XCTSkipIf(Self.isAbstractTestCase)
let inputValue = Double.nan let encodingStrategies = EncodingStrategies( nonConformingFloatStrategy: .convertToString( positiveInfinity: "positiveInfinity", negativeInfinity: "negativeInfinity", nan: "notANumber" ) ) let decodingStrategies = DecodingStrategies(nonConformingFloatStrategy: .throw) let target = target(using: decodingStrategies)
XCTAssertThrowsError( try target.decode( Double.self, from: expectedValues.singleValueDoubleConvertToStringNaN(inputValue, encodingStrategies) ) ) }
public func test_singleValue_double_throw_negativeInfinity() throws { try XCTSkipIf(Self.isAbstractTestCase)
let inputValue = -Double.infinity let encodingStrategies = EncodingStrategies( nonConformingFloatStrategy: .convertToString( positiveInfinity: "positiveInfinity", negativeInfinity: "negativeInfinity", nan: "notANumber" ) ) let decodingStrategies = DecodingStrategies(nonConformingFloatStrategy: .throw) let target = target(using: decodingStrategies)
XCTAssertThrowsError( try target.decode( Double.self, from: expectedValues.singleValueDoubleConvertToStringNegativeInfinity(inputValue, encodingStrategies) ) ) }
public func test_singleValue_double_throw_positiveInfinity() throws { try XCTSkipIf(Self.isAbstractTestCase)
let inputValue = Double.infinity let encodingStrategies = EncodingStrategies( nonConformingFloatStrategy: .convertToString( positiveInfinity: "positiveInfinity", negativeInfinity: "negativeInfinity", nan: "notANumber" ) ) let decodingStrategies = DecodingStrategies(nonConformingFloatStrategy: .throw) let target = target(using: decodingStrategies)
XCTAssertThrowsError( try target.decode( Double.self, from: expectedValues.singleValueDoubleConvertToStringPositiveInfinity(inputValue, encodingStrategies) ) ) }
// MARK: Single Value Float Tests
public func test_singleValue_float() throws { XCTAssertDecoder( target, decodes: expectedValues.singleValueFloat, encodedWith: .default, decodedWith: .default, as: Float(100) ) }
public func test_singleValue_float_convertToString_nan() throws { try XCTSkipIf(Self.isAbstractTestCase) let encodingStrategies = EncodingStrategies( nonConformingFloatStrategy: .convertToString( positiveInfinity: "positiveInfinity", negativeInfinity: "negativeInfinity", nan: "notANumber" ) ) let decodingStrategies = DecodingStrategies( nonConformingFloatStrategy: .convertToString( positiveInfinity: "positiveInfinity", negativeInfinity: "negativeInfinity", nan: "notANumber" ) ) let expectation = Float.nan let inputValue = expectedValues.singleValueFloatConvertToStringNaN(expectation, encodingStrategies) let decodedValue = try target(using: decodingStrategies).decode(Double.self, from: inputValue) XCTAssertEqual(decodedValue.isNaN, expectation.isNaN) }
public func test_singleValue_float_convertToString_negativeInfinity() throws { try XCTSkipIf(Self.isAbstractTestCase)
XCTAssertDecoder( target, decodes: expectedValues.singleValueFloatConvertToStringNegativeInfinity, encodedWith: EncodingStrategies( nonConformingFloatStrategy: .convertToString( positiveInfinity: "positiveInfinity", negativeInfinity: "negativeInfinity", nan: "notANumber" ) ), decodedWith: DecodingStrategies( nonConformingFloatStrategy: .convertToString( positiveInfinity: "positiveInfinity", negativeInfinity: "negativeInfinity", nan: "notANumber" ) ), as: -.infinity ) }
public func test_singleValue_float_convertToString_positiveInfinity() throws { try XCTSkipIf(Self.isAbstractTestCase)
XCTAssertDecoder( target, decodes: expectedValues.singleValueFloatConvertToStringPositiveInfinity, encodedWith: EncodingStrategies( nonConformingFloatStrategy: .convertToString( positiveInfinity: "positiveInfinity", negativeInfinity: "negativeInfinity", nan: "notANumber" ) ), decodedWith: DecodingStrategies( nonConformingFloatStrategy: .convertToString( positiveInfinity: "positiveInfinity", negativeInfinity: "negativeInfinity", nan: "notANumber" ) ), as: .infinity ) }
public func test_singleValue_float_throw_nan() throws { try XCTSkipIf(Self.isAbstractTestCase)
let inputValue = Float.nan let encodingStrategies = EncodingStrategies( nonConformingFloatStrategy: .convertToString( positiveInfinity: "positiveInfinity", negativeInfinity: "negativeInfinity", nan: "notANumber" ) ) let decodingStrategies = DecodingStrategies(nonConformingFloatStrategy: .throw) let target = target(using: decodingStrategies)
XCTAssertThrowsError( try target.decode( Double.self, from: expectedValues.singleValueFloatConvertToStringNaN(inputValue, encodingStrategies) ) ) }
public func test_singleValue_float_throw_negativeInfinity() throws { try XCTSkipIf(Self.isAbstractTestCase)
let inputValue = -Float.infinity let encodingStrategies = EncodingStrategies( nonConformingFloatStrategy: .convertToString( positiveInfinity: "positiveInfinity", negativeInfinity: "negativeInfinity", nan: "notANumber" ) ) let decodingStrategies = DecodingStrategies(nonConformingFloatStrategy: .throw) let target = target(using: decodingStrategies)
XCTAssertThrowsError( try target.decode( Double.self, from: expectedValues.singleValueFloatConvertToStringNegativeInfinity(inputValue, encodingStrategies) ) ) }
public func test_singleValue_float_throw_positiveInfinity() throws { try XCTSkipIf(Self.isAbstractTestCase)
let inputValue = Float.infinity let encodingStrategies = EncodingStrategies( nonConformingFloatStrategy: .convertToString( positiveInfinity: "positiveInfinity", negativeInfinity: "negativeInfinity", nan: "notANumber" ) ) let decodingStrategies = DecodingStrategies(nonConformingFloatStrategy: .throw) let target = target(using: decodingStrategies)
XCTAssertThrowsError( try target.decode( Double.self, from: expectedValues.singleValueFloatConvertToStringPositiveInfinity(inputValue, encodingStrategies) ) ) } // MARK: Keyed Value Tests
public func test_keyedValue_default() throws { try XCTSkipIf(Self.isAbstractTestCase) XCTAssertDecoder( target, decodes: expectedValues.keyedValueDefault, encodedWith: EncodingStrategies(), decodedWith: DecodingStrategies(), as: CodableTestTypes.SingleValueProperties.default ) }
public func test_keyedValue_empty() throws { try XCTSkipIf(Self.isAbstractTestCase) XCTAssertDecoder( target, decodes: expectedValues.keyedValueEmpty, encodedWith: EncodingStrategies(), decodedWith: DecodingStrategies(), as: CodableTestTypes.SingleValueProperties.empty ) }
public func test_keyedValue_partial() throws { try XCTSkipIf(Self.isAbstractTestCase)
XCTAssertDecoder( target, decodes: expectedValues.keyedValuePartial, encodedWith: EncodingStrategies(), decodedWith: DecodingStrategies(), as: CodableTestTypes.SingleValueProperties.partial ) }
// MARK: Keyed Value Inheritance Tests
public func test_inheritance_keyedValue_default() throws { try XCTSkipIf(Self.isAbstractTestCase) XCTAssertDecoder( target, decodes: expectedValues.inheritanceKeyedValueDefault, encodedWith: EncodingStrategies(), decodedWith: DecodingStrategies(), as: .subDefault ) }
public func test_inheritance_keyedValue_empty() throws { try XCTSkipIf(Self.isAbstractTestCase) XCTAssertDecoder( target, decodes: expectedValues.inheritanceKeyedValueEmpty, encodedWith: EncodingStrategies(), decodedWith: DecodingStrategies(), as: .subEmpty ) }
public func test_inheritance_keyedValue_partial() throws { try XCTSkipIf(Self.isAbstractTestCase)
XCTAssertDecoder( target, decodes: expectedValues.inheritanceKeyedValuePartial, encodedWith: EncodingStrategies(), decodedWith: DecodingStrategies(), as: .subPartial ) }
// MARK: Keyed Value Data Tests
public func test_keyedValue_data_base64() throws { try XCTSkipIf(Self.isAbstractTestCase) XCTAssertDecoder( target, decodes: expectedValues.keyedValueDataBase64, encodedWith: EncodingStrategies(dataStrategy: .base64), decodedWith: DecodingStrategies(dataStrategy: .base64), as: CodableTestTypes.SingleValueProperties.Abridged.default ) }
public func test_keyedValue_data_custom() throws { try XCTSkipIf(Self.isAbstractTestCase)
// We'll encode a number instead of data let encode: @Sendable (Data, Encoder) throws -> Void = { date, encoder in var container = encoder.singleValueContainer() try container.encode(42) } let decode: @Sendable (Decoder) throws -> Data = { encoder in Data([0xDE, 0xAD, 0xBE, 0xEF]) }
XCTAssertDecoder( target, decodes: expectedValues.keyedValueDataCustom, encodedWith: EncodingStrategies(dataStrategy: .custom(encode)), decodedWith: DecodingStrategies(dataStrategy: .custom(decode)), as: CodableTestTypes.SingleValueProperties.Abridged.default ) }
public func test_keyedValue_data_deferredToData() throws { try XCTSkipIf(Self.isAbstractTestCase)
XCTAssertDecoder( target, decodes: expectedValues.keyedValueDataDeferredToData, encodedWith: EncodingStrategies(dataStrategy: .deferredToData), decodedWith: DecodingStrategies(dataStrategy: .deferredToData), as: CodableTestTypes.SingleValueProperties.Abridged.default ) }
// MARK: Keyed Value Date Tests
public func test_keyedValue_date_custom() throws { try XCTSkipIf(Self.isAbstractTestCase)
// We'll encode a number instead of a date let encode: (Date, Encoder) throws -> Void = { date, encoder in var container = encoder.singleValueContainer() try container.encode(42) } let decode: @Sendable (Decoder) throws -> Date = { encoder in Date(timeIntervalSince1970: 1_000) }
XCTAssertDecoder( target, decodes: expectedValues.keyedValueDateCustom, encodedWith: EncodingStrategies(dateStrategy: .custom(encode)), decodedWith: DecodingStrategies(dateStrategy: .custom(decode)), as: CodableTestTypes.SingleValueProperties.default ) }
public func test_keyedValue_date_deferredToDate() throws { try XCTSkipIf(Self.isAbstractTestCase)
XCTAssertDecoder( target, decodes: expectedValues.keyedValueDateDeferredToDate, encodedWith: EncodingStrategies(dateStrategy: .deferredToDate), decodedWith: DecodingStrategies(dateStrategy: .deferredToDate), as: CodableTestTypes.SingleValueProperties.default ) }
public func test_keyedValue_date_formatted() throws { try XCTSkipIf(Self.isAbstractTestCase)
let formatter = DateFormatter() formatter.dateStyle = .full formatter.locale = Locale(identifier: "en_US") formatter.timeStyle = .full formatter.timeZone = TimeZone(identifier: "UTC")
XCTAssertDecoder( target, decodes: expectedValues.keyedValueDateFormatted, encodedWith: EncodingStrategies(dateStrategy: .formatted(formatter)), decodedWith: DecodingStrategies(dateStrategy: .formatted(formatter)), as: CodableTestTypes.SingleValueProperties.default ) }
public func test_keyedValue_date_iso8601() throws { try XCTSkipIf(Self.isAbstractTestCase)
XCTAssertDecoder( target, decodes: expectedValues.keyedValueDateISO8601, encodedWith: EncodingStrategies(dateStrategy: .iso8601), decodedWith: DecodingStrategies(dateStrategy: .iso8601), as: CodableTestTypes.SingleValueProperties.default ) }
public func test_keyedValue_date_millisecondsSince1970() throws { try XCTSkipIf(Self.isAbstractTestCase)
XCTAssertDecoder( target, decodes: expectedValues.keyedValueDateMillisecondsSince1970, encodedWith: EncodingStrategies(dateStrategy: .millisecondsSince1970), decodedWith: DecodingStrategies(dateStrategy: .millisecondsSince1970), as: CodableTestTypes.SingleValueProperties.default ) }
public func test_keyedValue_date_secondsSince1970() throws { try XCTSkipIf(Self.isAbstractTestCase)
XCTAssertDecoder( target, decodes: expectedValues.keyedValueDateSecondsSince1970, encodedWith: EncodingStrategies(dateStrategy: .secondsSince1970), decodedWith: DecodingStrategies(dateStrategy: .secondsSince1970), as: CodableTestTypes.SingleValueProperties.default ) }
// MARK: Keyed Value Double Tests
public func test_keyedValue_double() throws { try XCTSkipIf(Self.isAbstractTestCase)
XCTAssertDecoder( target, decodes: expectedValues.keyedValueDouble, encodedWith: EncodingStrategies(nonConformingFloatStrategy: .default), decodedWith: DecodingStrategies(nonConformingFloatStrategy: .default), as: CodableTestTypes.SingleValueProperties.Abridged.default ) }
public func test_keyedValue_double_convertToString_negativeInfinity() throws { try XCTSkipIf(Self.isAbstractTestCase) XCTAssertDecoder( target, decodes: expectedValues.keyedValueDoubleConvertToStringNegativeInfinity, encodedWith: EncodingStrategies( nonConformingFloatStrategy: .convertToString( positiveInfinity: "positiveInfinity", negativeInfinity: "negativeInfinity", nan: "notANumber" ) ), decodedWith: DecodingStrategies( nonConformingFloatStrategy: .convertToString( positiveInfinity: "positiveInfinity", negativeInfinity: "negativeInfinity", nan: "notANumber" ) ), as: CodableTestTypes.SingleValueProperties.Abridged.doubleNegativeInfinity ) }
public func test_keyedValue_double_convertToString_notANumber() throws { try XCTSkipIf(Self.isAbstractTestCase) let encodingStrategies = EncodingStrategies( nonConformingFloatStrategy: .convertToString( positiveInfinity: "positiveInfinity", negativeInfinity: "negativeInfinity", nan: "notANumber" ) ) let decodingStrategies = DecodingStrategies( nonConformingFloatStrategy: .convertToString( positiveInfinity: "positiveInfinity", negativeInfinity: "negativeInfinity", nan: "notANumber" ) ) let expectation = CodableTestTypes.SingleValueProperties.Abridged.doubleNotANumber let inputValue = expectedValues.keyedValueDoubleConvertToStringNaN(expectation, encodingStrategies) let decodedValue = try target(using: decodingStrategies).decode( CodableTestTypes.SingleValueProperties.Abridged.self, from: inputValue ) XCTAssertEqual(decodedValue.bool, expectation.bool) XCTAssertEqual(decodedValue.data, expectation.data) XCTAssertEqual(decodedValue.date, expectation.date) XCTAssertEqual(decodedValue.double.isNaN, expectation.double.isNaN) XCTAssertEqual(decodedValue.float, expectation.float) XCTAssertEqual(decodedValue.int, expectation.int) XCTAssertEqual(decodedValue.string, expectation.string) XCTAssertEqual(decodedValue.uint, expectation.uint) }
public func test_keyedValue_double_convertToString_positiveInfinity() throws { try XCTSkipIf(Self.isAbstractTestCase)
XCTAssertDecoder( target, decodes: expectedValues.keyedValueDoubleConvertToStringPositiveInfinity, encodedWith: EncodingStrategies( nonConformingFloatStrategy: .convertToString( positiveInfinity: "positiveInfinity", negativeInfinity: "negativeInfinity", nan: "notANumber" ) ), decodedWith: DecodingStrategies( nonConformingFloatStrategy: .convertToString( positiveInfinity: "positiveInfinity", negativeInfinity: "negativeInfinity", nan: "notANumber" ) ), as: CodableTestTypes.SingleValueProperties.Abridged.doublePositiveInfinity ) } public func test_keyedValue_double_throw_nan() throws { try XCTSkipIf(Self.isAbstractTestCase)
let encodingStrategies = EncodingStrategies(nonConformingFloatStrategy: .throw) let decodingStrategies = DecodingStrategies(nonConformingFloatStrategy: .throw) let expectedValue = CodableTestTypes.SingleValueProperties.Abridged.doubleNotANumber let encodedValue = expectedValues.keyedValueDoubleConvertToStringNaN( expectedValue, encodingStrategies ) XCTAssertThrowsError( try target(using: decodingStrategies).decode(type(of: expectedValue), from: encodedValue) ) } public func test_keyedValue_double_throw_negativeInfinity() throws { try XCTSkipIf(Self.isAbstractTestCase)
let encodingStrategies = EncodingStrategies(nonConformingFloatStrategy: .throw) let decodingStrategies = DecodingStrategies(nonConformingFloatStrategy: .throw) let expectedValue = CodableTestTypes.SingleValueProperties.Abridged.doubleNotANumber let encodedValue = expectedValues.keyedValueDoubleConvertToStringNegativeInfinity( expectedValue, encodingStrategies ) XCTAssertThrowsError( try target(using: decodingStrategies).decode(type(of: expectedValue), from: encodedValue) ) } public func test_keyedValue_double_throw_positiveInfinity() throws { try XCTSkipIf(Self.isAbstractTestCase)
let encodingStrategies = EncodingStrategies(nonConformingFloatStrategy: .throw) let decodingStrategies = DecodingStrategies(nonConformingFloatStrategy: .throw) let expectedValue = CodableTestTypes.SingleValueProperties.Abridged.doubleNotANumber let encodedValue = expectedValues.keyedValueDoubleConvertToStringPositiveInfinity( expectedValue, encodingStrategies ) XCTAssertThrowsError( try target(using: decodingStrategies).decode(type(of: expectedValue), from: encodedValue) ) }
// MARK: Keyed Value Float Tests
public func test_keyedValue_float() throws { try XCTSkipIf(Self.isAbstractTestCase)
XCTAssertDecoder( target, decodes: expectedValues.keyedValueFloat, encodedWith: EncodingStrategies(nonConformingFloatStrategy: .default), decodedWith: DecodingStrategies(nonConformingFloatStrategy: .default), as: CodableTestTypes.SingleValueProperties.Abridged.default ) }
public func test_keyedValue_float_convertToString_negativeInfinity() throws { try XCTSkipIf(Self.isAbstractTestCase) XCTAssertDecoder( target, decodes: expectedValues.keyedValueFloatConvertToStringNegativeInfinity, encodedWith: EncodingStrategies( nonConformingFloatStrategy: .convertToString( positiveInfinity: "positiveInfinity", negativeInfinity: "negativeInfinity", nan: "notANumber" ) ), decodedWith: DecodingStrategies( nonConformingFloatStrategy: .convertToString( positiveInfinity: "positiveInfinity", negativeInfinity: "negativeInfinity", nan: "notANumber" ) ), as: CodableTestTypes.SingleValueProperties.Abridged.floatNegativeInfinity ) }
public func test_keyedValue_float_convertToString_notANumber() throws { try XCTSkipIf(Self.isAbstractTestCase) let encodingStrategies = EncodingStrategies( nonConformingFloatStrategy: .convertToString( positiveInfinity: "positiveInfinity", negativeInfinity: "negativeInfinity", nan: "notANumber" ) ) let decodingStrategies = DecodingStrategies( nonConformingFloatStrategy: .convertToString( positiveInfinity: "positiveInfinity", negativeInfinity: "negativeInfinity", nan: "notANumber" ) ) let expectation = CodableTestTypes.SingleValueProperties.Abridged.floatNotANumber let encodedValue = expectedValues.keyedValueFloatConvertToStringNaN(expectation, encodingStrategies) let decodedValue = try target(using: decodingStrategies).decode( CodableTestTypes.SingleValueProperties.Abridged.self, from: encodedValue ) XCTAssertEqual(decodedValue.bool, expectation.bool) XCTAssertEqual(decodedValue.data, expectation.data) XCTAssertEqual(decodedValue.date, expectation.date) XCTAssertEqual(decodedValue.double, expectation.double) XCTAssertEqual(decodedValue.float.isNaN, expectation.float.isNaN) XCTAssertEqual(decodedValue.int, expectation.int) XCTAssertEqual(decodedValue.string, expectation.string) XCTAssertEqual(decodedValue.uint, expectation.uint) }
public func test_keyedValue_float_convertToString_positiveInfinity() throws { try XCTSkipIf(Self.isAbstractTestCase)
XCTAssertDecoder( target, decodes: expectedValues.keyedValueFloatConvertToStringPositiveInfinity, encodedWith: EncodingStrategies( nonConformingFloatStrategy: .convertToString( positiveInfinity: "positiveInfinity", negativeInfinity: "negativeInfinity", nan: "notANumber" ) ), decodedWith: DecodingStrategies( nonConformingFloatStrategy: .convertToString( positiveInfinity: "positiveInfinity", negativeInfinity: "negativeInfinity", nan: "notANumber" ) ), as: CodableTestTypes.SingleValueProperties.Abridged.floatPositiveInfinity ) } public func test_keyedValue_float_throw_nan() throws { try XCTSkipIf(Self.isAbstractTestCase)
let encodingStrategies = EncodingStrategies(nonConformingFloatStrategy: .throw) let decodingStrategies = DecodingStrategies(nonConformingFloatStrategy: .throw) let expectedValue = CodableTestTypes.SingleValueProperties.Abridged.floatNotANumber let encodedValue = expectedValues.keyedValueFloatConvertToStringNaN( expectedValue, encodingStrategies ) XCTAssertThrowsError( try target(using: decodingStrategies).decode(type(of: expectedValue), from: encodedValue) ) } public func test_keyedValue_float_throw_negativeInfinity() throws { try XCTSkipIf(Self.isAbstractTestCase)
let encodingStrategies = EncodingStrategies(nonConformingFloatStrategy: .throw) let decodingStrategies = DecodingStrategies(nonConformingFloatStrategy: .throw) let expectedValue = CodableTestTypes.SingleValueProperties.Abridged.floatNotANumber let encodedValue = expectedValues.keyedValueFloatConvertToStringNegativeInfinity( expectedValue, encodingStrategies ) XCTAssertThrowsError( try target(using: decodingStrategies).decode(type(of: expectedValue), from: encodedValue) ) } public func test_keyedValue_float_throw_positiveInfinity() throws { try XCTSkipIf(Self.isAbstractTestCase)
let encodingStrategies = EncodingStrategies(nonConformingFloatStrategy: .throw) let decodingStrategies = DecodingStrategies(nonConformingFloatStrategy: .throw) let expectedValue = CodableTestTypes.SingleValueProperties.Abridged.floatNotANumber let encodedValue = expectedValues.keyedValueFloatConvertToStringPositiveInfinity( expectedValue, encodingStrategies ) XCTAssertThrowsError( try target(using: decodingStrategies).decode(type(of: expectedValue), from: encodedValue) ) }
// MARK: Keyed Value Key Tests
public func test_keyedValue_key_custom() throws { try XCTSkipIf(Self.isAbstractTestCase) let keyEncoder: @Sendable ([CodingKey]) -> CodingKey = { guard let last = $0.last else { return StringCodingKey(stringValue: String()) }
return StringCodingKey(stringValue: last.stringValue.uppercased()) } let encodingStrategies = EncodingStrategies(keyStrategy: .custom(keyEncoder)) let decodingStrategies = DecodingStrategies(keyStrategy: .custom(keyEncoder)) XCTAssertDecoder( target, decodes: expectedValues.keyedValueKeyCustom, encodedWith: encodingStrategies, decodedWith: decodingStrategies, as: .default ) }
public func test_keyedValue_key_convertToSnakeCase() throws { try XCTSkipIf(Self.isAbstractTestCase)
XCTAssertDecoder( target, decodes: expectedValues.keyedValueKeySnakeCase, encodedWith: EncodingStrategies(keyStrategy: .convertToSnakeCase), decodedWith: DecodingStrategies(keyStrategy: .convertFromSnakeCase), as: .default ) }
public func test_keyedValue_key_useDefaultKeys() throws { try XCTSkipIf(Self.isAbstractTestCase)
XCTAssertDecoder( target, decodes: expectedValues.keyedValueDefault, encodedWith: EncodingStrategies(keyStrategy: .useDefaultKeys), decodedWith: DecodingStrategies(keyStrategy: .useDefaultKeys), as: .default ) }
// MARK: Unkeyed Value Primitive Tests
public func test_unkeyedValue_bool() throws { try XCTSkipIf(Self.isAbstractTestCase) let encodingStrategies = EncodingStrategies.default let decodingStrategies = DecodingStrategies.default let expectedValue = [true, false] let encodedValue = expectedValues.unkeyedValueBool(expectedValue, encodingStrategies) let decodedValue = try target(using: decodingStrategies).decode(type(of: expectedValue), from: encodedValue)
XCTAssertEqual( expectedValue, decodedValue ) }
public func test_unkeyedValue_int() throws { try XCTSkipIf(Self.isAbstractTestCase) let encodingStrategies = EncodingStrategies.default let decodingStrategies = DecodingStrategies.default let expectedValue = [Int(Int32.min), Int(Int32.max)] let encodedValue = expectedValues.unkeyedValueInt(expectedValue, encodingStrategies) let decodedValue = try target(using: decodingStrategies).decode(type(of: expectedValue), from: encodedValue)
XCTAssertEqual( expectedValue, decodedValue ) }
public func test_unkeyedValue_int8() throws { try XCTSkipIf(Self.isAbstractTestCase) let encodingStrategies = EncodingStrategies.default let decodingStrategies = DecodingStrategies.default let expectedValue = [Int8.min, Int8.max] let encodedValue = expectedValues.unkeyedValueInt8(expectedValue, encodingStrategies) let decodedValue = try target(using: decodingStrategies).decode(type(of: expectedValue), from: encodedValue)
XCTAssertEqual( expectedValue, decodedValue ) }
public func test_unkeyedValue_int16() throws { try XCTSkipIf(Self.isAbstractTestCase)
let encodingStrategies = EncodingStrategies.default let decodingStrategies = DecodingStrategies.default let expectedValue = [Int16.min, Int16.max] let encodedValue = expectedValues.unkeyedValueInt16(expectedValue, encodingStrategies) let decodedValue = try target(using: decodingStrategies).decode(type(of: expectedValue), from: encodedValue)
XCTAssertEqual( expectedValue, decodedValue ) }
public func test_unkeyedValue_int32() throws { try XCTSkipIf(Self.isAbstractTestCase)
let encodingStrategies = EncodingStrategies.default let decodingStrategies = DecodingStrategies.default let expectedValue = [Int32.min, Int32.max] let encodedValue = expectedValues.unkeyedValueInt32(expectedValue, encodingStrategies) let decodedValue = try target(using: decodingStrategies).decode(type(of: expectedValue), from: encodedValue)
XCTAssertEqual( expectedValue, decodedValue ) }
public func test_unkeyedValue_int64() throws { try XCTSkipIf(Self.isAbstractTestCase)
let encodingStrategies = EncodingStrategies.default let decodingStrategies = DecodingStrategies.default let expectedValue = [Int64.min, Int64.max] let encodedValue = expectedValues.unkeyedValueInt64(expectedValue, encodingStrategies) let decodedValue = try target(using: decodingStrategies).decode(type(of: expectedValue), from: encodedValue)
XCTAssertEqual( expectedValue, decodedValue ) }
public func test_unkeyedValue_nil() throws { try XCTSkipIf(Self.isAbstractTestCase)
let encodingStrategies = EncodingStrategies.default let decodingStrategies = DecodingStrategies.default let expectedValue = [String?.none, String?.none] let encodedValue = expectedValues.unkeyedValueNil(expectedValue, encodingStrategies) let decodedValue = try target(using: decodingStrategies).decode(type(of: expectedValue), from: encodedValue)
XCTAssertEqual( expectedValue, decodedValue ) }
public func test_unkeyedValue_string() throws { try XCTSkipIf(Self.isAbstractTestCase)
let encodingStrategies = EncodingStrategies.default let decodingStrategies = DecodingStrategies.default let expectedValue = Array(repeating: "DEFAULT CONSTANT", count: 2) let encodedValue = expectedValues.unkeyedValueString(expectedValue, encodingStrategies) let decodedValue = try target(using: decodingStrategies).decode(type(of: expectedValue), from: encodedValue)
XCTAssertEqual( expectedValue, decodedValue ) }
public func test_unkeyedValue_uint() throws { try XCTSkipIf(Self.isAbstractTestCase) let encodingStrategies = EncodingStrategies.default let decodingStrategies = DecodingStrategies.default let expectedValue = [UInt(UInt32.min), UInt(UInt32.max)] let encodedValue = expectedValues.unkeyedValueUInt(expectedValue, encodingStrategies) let decodedValue = try target(using: decodingStrategies).decode(type(of: expectedValue), from: encodedValue)
XCTAssertEqual( expectedValue, decodedValue ) }
public func test_unkeyedValue_uint8() throws { try XCTSkipIf(Self.isAbstractTestCase) let encodingStrategies = EncodingStrategies.default let decodingStrategies = DecodingStrategies.default let expectedValue = [UInt8.min, UInt8.max] let encodedValue = expectedValues.unkeyedValueUInt8(expectedValue, encodingStrategies) let decodedValue = try target(using: decodingStrategies).decode(type(of: expectedValue), from: encodedValue)
XCTAssertEqual( expectedValue, decodedValue ) }
public func test_unkeyedValue_uint16() throws { try XCTSkipIf(Self.isAbstractTestCase)
let encodingStrategies = EncodingStrategies.default let decodingStrategies = DecodingStrategies.default let expectedValue = [UInt16.min, UInt16.max] let encodedValue = expectedValues.unkeyedValueUInt16(expectedValue, encodingStrategies) let decodedValue = try target(using: decodingStrategies).decode(type(of: expectedValue), from: encodedValue)
XCTAssertEqual( expectedValue, decodedValue ) }
public func test_unkeyedValue_uint32() throws { try XCTSkipIf(Self.isAbstractTestCase)
let encodingStrategies = EncodingStrategies.default let decodingStrategies = DecodingStrategies.default let expectedValue = [UInt32.min, UInt32.max] let encodedValue = expectedValues.unkeyedValueUInt32(expectedValue, encodingStrategies) let decodedValue = try target(using: decodingStrategies).decode(type(of: expectedValue), from: encodedValue)
XCTAssertEqual( expectedValue, decodedValue ) }
public func test_unkeyedValue_uint64() throws { try XCTSkipIf(Self.isAbstractTestCase)
let encodingStrategies = EncodingStrategies.default let decodingStrategies = DecodingStrategies.default let expectedValue = [UInt64.min, UInt64.max] let encodedValue = expectedValues.unkeyedValueUInt64(expectedValue, encodingStrategies) let decodedValue = try target(using: decodingStrategies).decode(type(of: expectedValue), from: encodedValue)
XCTAssertEqual( expectedValue, decodedValue ) }
// MARK: Unkeyed Value Data Tests
public func test_unkeyedValue_data_base64() throws { try XCTSkipIf(Self.isAbstractTestCase) let encodingStrategies = EncodingStrategies(dataStrategy: .base64) let decodingStrategies = DecodingStrategies(dataStrategy: .base64) let expectedValue = [ Data([0xDE, 0xAD, 0xBE, 0xEF]), Data([0xAB, 0xCD, 0xEF, 0x00]), ] let encodedValue = expectedValues.unkeyedValueDataBase64(expectedValue, encodingStrategies) let decodedValue = try target(using: decodingStrategies).decode(type(of: expectedValue), from: encodedValue)
XCTAssertEqual( expectedValue, decodedValue ) }
public func test_unkeyedValue_data_custom() throws { try XCTSkipIf(Self.isAbstractTestCase)
// We'll encode a number instead of a date let encode: @Sendable (Data, Encoder) throws -> Void = { data, encoder in var container = encoder.singleValueContainer() try container.encode(42) } let decode: @Sendable (Decoder) throws -> Data = { _ in Data([0xDE, 0xAD, 0xBE, 0xEF]) } let encodingStrategies = EncodingStrategies(dataStrategy: .custom(encode)) let decodingStrategies = DecodingStrategies(dataStrategy: .custom(decode)) let expectedValue = [ Data([0xDE, 0xAD, 0xBE, 0xEF]), Data([0xDE, 0xAD, 0xBE, 0xEF]), ] let encodedValue = expectedValues.unkeyedValueDataCustom(expectedValue, encodingStrategies) let decodedValue = try target(using: decodingStrategies).decode(type(of: expectedValue), from: encodedValue)
XCTAssertEqual( expectedValue, decodedValue ) }
public func test_unkeyedValue_data_deferredToData() throws { try XCTSkipIf(Self.isAbstractTestCase)
let encodingStrategies = EncodingStrategies(dataStrategy: .deferredToData) let decodingStrategies = DecodingStrategies(dataStrategy: .deferredToData) let expectedValue = [ Data([0xDE, 0xAD, 0xBE, 0xEF]), Data([0xAB, 0xCD, 0xEF, 0x00]), ] let encodedValue = expectedValues.unkeyedValueDataDeferredToData(expectedValue, encodingStrategies) let decodedValue = try target(using: decodingStrategies).decode(type(of: expectedValue), from: encodedValue)
XCTAssertEqual( expectedValue, decodedValue ) }
// MARK: Unkeyed Value Date Tests
public func test_unkeyedValue_date_custom() throws { try XCTSkipIf(Self.isAbstractTestCase) // We'll encode a number instead of a date let encode: @Sendable (Date, Encoder) throws -> Void = { data, encoder in var container = encoder.singleValueContainer() try container.encode(42) } let decode: @Sendable (Decoder) throws -> Date = { _ in Date(timeIntervalSince1970: 1_000) } let encodingStrategies = EncodingStrategies(dateStrategy: .custom(encode)) let decodingStrategies = DecodingStrategies(dateStrategy: .custom(decode)) let expectedValue = [ Date(timeIntervalSince1970: 1_000), Date(timeIntervalSince1970: 1_000), ] let encodedValue = expectedValues.unkeyedValueDateCustom(expectedValue, encodingStrategies) let decodedValue = try target(using: decodingStrategies).decode(type(of: expectedValue), from: encodedValue)
XCTAssertEqual( expectedValue, decodedValue ) }
public func test_unkeyedValue_date_deferredToDate() throws { try XCTSkipIf(Self.isAbstractTestCase) let encodingStrategies = EncodingStrategies(dateStrategy: .deferredToDate) let decodingStrategies = DecodingStrategies(dateStrategy: .deferredToDate) let expectedValue = [ Date(timeIntervalSince1970: 1_000), Date(timeIntervalSince1970: 1_000_000), ] let encodedValue = expectedValues.unkeyedValueDateDeferredToDate(expectedValue, encodingStrategies) let decodedValue = try target(using: decodingStrategies).decode(type(of: expectedValue), from: encodedValue)
XCTAssertEqual( expectedValue, decodedValue ) }
public func test_unkeyedValue_date_formatted() throws { try XCTSkipIf(Self.isAbstractTestCase)
let formatter = DateFormatter() formatter.dateStyle = .full formatter.locale = Locale(identifier: "en_US") formatter.timeStyle = .full formatter.timeZone = TimeZone(identifier: "UTC")
let encodingStrategies = EncodingStrategies(dateStrategy: .formatted(formatter)) let decodingStrategies = DecodingStrategies(dateStrategy: .formatted(formatter)) let expectedValue = [ Date(timeIntervalSince1970: 1_000), Date(timeIntervalSince1970: 1_000_000), ] let encodedValue = expectedValues.unkeyedValueDateFormatted(expectedValue, encodingStrategies) let decodedValue = try target(using: decodingStrategies).decode(type(of: expectedValue), from: encodedValue)
XCTAssertEqual( expectedValue, decodedValue ) }
public func test_unkeyedValue_date_iso8601() throws { try XCTSkipIf(Self.isAbstractTestCase)
let encodingStrategies = EncodingStrategies(dateStrategy: .iso8601) let decodingStrategies = DecodingStrategies(dateStrategy: .iso8601) let expectedValue = [ Date(timeIntervalSince1970: 1_000), Date(timeIntervalSince1970: 1_000_000), ] let encodedValue = expectedValues.unkeyedValueDateISO8601(expectedValue, encodingStrategies) let decodedValue = try target(using: decodingStrategies).decode(type(of: expectedValue), from: encodedValue)
XCTAssertEqual( expectedValue, decodedValue ) }
public func test_unkeyedValue_date_millisecondsSince1970() throws { try XCTSkipIf(Self.isAbstractTestCase)
let encodingStrategies = EncodingStrategies(dateStrategy: .millisecondsSince1970) let decodingStrategies = DecodingStrategies(dateStrategy: .millisecondsSince1970) let expectedValue = [ Date(timeIntervalSince1970: 1_000), Date(timeIntervalSince1970: 1_000_000), ] let encodedValue = expectedValues.unkeyedValueDateMillisecondsSince1970(expectedValue, encodingStrategies) let decodedValue = try target(using: decodingStrategies).decode(type(of: expectedValue), from: encodedValue)
XCTAssertEqual( expectedValue, decodedValue ) }
public func test_unkeyedValue_date_secondsSince1970() throws { try XCTSkipIf(Self.isAbstractTestCase)
let encodingStrategies = EncodingStrategies(dateStrategy: .secondsSince1970) let decodingStrategies = DecodingStrategies(dateStrategy: .secondsSince1970) let expectedValue = [ Date(timeIntervalSince1970: 1_000), Date(timeIntervalSince1970: 1_000_000), ] let encodedValue = expectedValues.unkeyedValueDateSecondsSince1970(expectedValue, encodingStrategies) let decodedValue = try target(using: decodingStrategies).decode(type(of: expectedValue), from: encodedValue)
XCTAssertEqual( expectedValue, decodedValue ) }
// MARK: Unkeyed Value Double Tests
public func test_unkeyedValue_double() throws { try XCTSkipIf(Self.isAbstractTestCase) let encodingStrategies = EncodingStrategies(nonConformingFloatStrategy: .default) let decodingStrategies = DecodingStrategies(nonConformingFloatStrategy: .default) let expectedValue = [ 123.45, 678910.11, ] let encodedValue = expectedValues.unkeyedValueDouble(expectedValue, encodingStrategies) let decodedValue = try target(using: decodingStrategies).decode(type(of: expectedValue), from: encodedValue)
XCTAssertEqual( expectedValue, decodedValue ) }
public func test_unkeyedValue_double_convertToString_nan() throws { try XCTSkipIf(Self.isAbstractTestCase) let encodingStrategies = EncodingStrategies( nonConformingFloatStrategy: .convertToString( positiveInfinity: "positiveInfinity", negativeInfinity: "negativeInfinity", nan: "notANumber" ) ) let decodingStrategies = DecodingStrategies( nonConformingFloatStrategy: .convertToString( positiveInfinity: "positiveInfinity", negativeInfinity: "negativeInfinity", nan: "notANumber" ) ) let expectedValue = [ Double.nan, 678910.11, ] let encodedValue = expectedValues.unkeyedValueDoubleConvertToStringNaN(expectedValue, encodingStrategies) let decodedValue = try target(using: decodingStrategies).decode(type(of: expectedValue), from: encodedValue)
for (lhs, rhs) in zip(decodedValue, expectedValue) { XCTAssert(lhs.isNaN && rhs.isNaN || lhs == rhs) } }
public func test_unkeyedValue_double_convertToString_positiveInfinity() throws { try XCTSkipIf(Self.isAbstractTestCase)
let encodingStrategies = EncodingStrategies( nonConformingFloatStrategy: .convertToString( positiveInfinity: "positiveInfinity", negativeInfinity: "negativeInfinity", nan: "notANumber" ) ) let decodingStrategies = DecodingStrategies( nonConformingFloatStrategy: .convertToString( positiveInfinity: "positiveInfinity", negativeInfinity: "negativeInfinity", nan: "notANumber" ) ) let expectedValue = [ Double.infinity, 678910.11, ] let encodedValue = expectedValues.unkeyedValueDoubleConvertToStringPositiveInfinity( expectedValue, encodingStrategies ) let decodedValue = try target(using: decodingStrategies).decode(type(of: expectedValue), from: encodedValue)
XCTAssertEqual( expectedValue, decodedValue ) }
public func test_unkeyedValue_double_convertToString_negativeInfinity() throws { try XCTSkipIf(Self.isAbstractTestCase)
let encodingStrategies = EncodingStrategies( nonConformingFloatStrategy: .convertToString( positiveInfinity: "positiveInfinity", negativeInfinity: "negativeInfinity", nan: "notANumber" ) ) let decodingStrategies = DecodingStrategies( nonConformingFloatStrategy: .convertToString( positiveInfinity: "positiveInfinity", negativeInfinity: "negativeInfinity", nan: "notANumber" ) ) let expectedValue = [ -Double.infinity, 678910.11, ] let encodedValue = expectedValues.unkeyedValueDoubleConvertToStringNegativeInfinity( expectedValue, encodingStrategies ) let decodedValue = try target(using: decodingStrategies).decode(type(of: expectedValue), from: encodedValue)
XCTAssertEqual( expectedValue, decodedValue ) } public func test_unkeyedValue_double_throw_nan() throws { try XCTSkipIf(Self.isAbstractTestCase)
let encodingStrategies = EncodingStrategies(nonConformingFloatStrategy: .throw) let decodingStrategies = DecodingStrategies(nonConformingFloatStrategy: .throw) let expectedValue: [Double] = [ .nan, 678910.11, ] let encodedValue = expectedValues.unkeyedValueDoubleConvertToStringNaN( expectedValue, encodingStrategies ) XCTAssertThrowsError( try target(using: decodingStrategies).decode(type(of: expectedValue), from: encodedValue) ) } public func test_unkeyedValue_double_throw_negativeInfinity() throws { try XCTSkipIf(Self.isAbstractTestCase)
let encodingStrategies = EncodingStrategies(nonConformingFloatStrategy: .throw) let decodingStrategies = DecodingStrategies(nonConformingFloatStrategy: .throw) let expectedValue: [Double] = [ -.infinity, 678910.11, ] let encodedValue = expectedValues.unkeyedValueDoubleConvertToStringNegativeInfinity( expectedValue, encodingStrategies ) XCTAssertThrowsError( try target(using: decodingStrategies).decode(type(of: expectedValue), from: encodedValue) ) } public func test_unkeyedValue_double_throw_positiveInfinity() throws { try XCTSkipIf(Self.isAbstractTestCase)
let encodingStrategies = EncodingStrategies(nonConformingFloatStrategy: .throw) let decodingStrategies = DecodingStrategies(nonConformingFloatStrategy: .throw) let expectedValue: [Double] = [ .infinity, 678910.11, ] let encodedValue = expectedValues.unkeyedValueDoubleConvertToStringPositiveInfinity( expectedValue, encodingStrategies ) XCTAssertThrowsError( try target(using: decodingStrategies).decode(type(of: expectedValue), from: encodedValue) ) }
// MARK: Unkeyed Value Float Tests
public func test_unkeyedValue_float() throws { try XCTSkipIf(Self.isAbstractTestCase) let encodingStrategies = EncodingStrategies(nonConformingFloatStrategy: .default) let decodingStrategies = DecodingStrategies(nonConformingFloatStrategy: .default) let expectedValue: [Float] = [ 123.45, 678910.11, ] let encodedValue = expectedValues.unkeyedValueFloat(expectedValue, encodingStrategies) let decodedValue = try target(using: decodingStrategies).decode(type(of: expectedValue), from: encodedValue)
XCTAssertEqual( expectedValue, decodedValue ) }
public func test_unkeyedValue_float_convertToString_nan() throws { try XCTSkipIf(Self.isAbstractTestCase) let encodingStrategies = EncodingStrategies( nonConformingFloatStrategy: .convertToString( positiveInfinity: "positiveInfinity", negativeInfinity: "negativeInfinity", nan: "notANumber" ) ) let decodingStrategies = DecodingStrategies( nonConformingFloatStrategy: .convertToString( positiveInfinity: "positiveInfinity", negativeInfinity: "negativeInfinity", nan: "notANumber" ) ) let expectedValue: [Float] = [ .nan, 678910.11, ] let encodedValue = expectedValues.unkeyedValueFloatConvertToStringNaN(expectedValue, encodingStrategies) let decodedValue = try target(using: decodingStrategies).decode(type(of: expectedValue), from: encodedValue)
for (lhs, rhs) in zip(decodedValue, expectedValue) { XCTAssert(lhs.isNaN && rhs.isNaN || lhs == rhs) } }
public func test_unkeyedValue_float_convertToString_positiveInfinity() throws { try XCTSkipIf(Self.isAbstractTestCase)
let encodingStrategies = EncodingStrategies( nonConformingFloatStrategy: .convertToString( positiveInfinity: "positiveInfinity", negativeInfinity: "negativeInfinity", nan: "notANumber" ) ) let decodingStrategies = DecodingStrategies( nonConformingFloatStrategy: .convertToString( positiveInfinity: "positiveInfinity", negativeInfinity: "negativeInfinity", nan: "notANumber" ) ) let expectedValue: [Float] = [ .infinity, 678910.11, ] let encodedValue = expectedValues.unkeyedValueFloatConvertToStringPositiveInfinity( expectedValue, encodingStrategies ) let decodedValue = try target(using: decodingStrategies).decode(type(of: expectedValue), from: encodedValue)
XCTAssertEqual( expectedValue, decodedValue ) }
public func test_unkeyedValue_float_convertToString_negativeInfinity() throws { try XCTSkipIf(Self.isAbstractTestCase)
let encodingStrategies = EncodingStrategies( nonConformingFloatStrategy: .convertToString( positiveInfinity: "positiveInfinity", negativeInfinity: "negativeInfinity", nan: "notANumber" ) ) let decodingStrategies = DecodingStrategies( nonConformingFloatStrategy: .convertToString( positiveInfinity: "positiveInfinity", negativeInfinity: "negativeInfinity", nan: "notANumber" ) ) let expectedValue: [Float] = [ -.infinity, 678910.11, ] let encodedValue = expectedValues.unkeyedValueFloatConvertToStringNegativeInfinity( expectedValue, encodingStrategies ) let decodedValue = try target(using: decodingStrategies).decode(type(of: expectedValue), from: encodedValue)
XCTAssertEqual( expectedValue, decodedValue ) } public func test_unkeyedValue_float_throw_nan() throws { try XCTSkipIf(Self.isAbstractTestCase)
let encodingStrategies = EncodingStrategies(nonConformingFloatStrategy: .throw) let decodingStrategies = DecodingStrategies(nonConformingFloatStrategy: .throw) let expectedValue: [Float] = [ .nan, 678910.11, ] let encodedValue = expectedValues.unkeyedValueFloatConvertToStringNaN( expectedValue, encodingStrategies ) XCTAssertThrowsError( try target(using: decodingStrategies).decode(type(of: expectedValue), from: encodedValue) ) } public func test_unkeyedValue_float_throw_negativeInfinity() throws { try XCTSkipIf(Self.isAbstractTestCase)
let encodingStrategies = EncodingStrategies(nonConformingFloatStrategy: .throw) let decodingStrategies = DecodingStrategies(nonConformingFloatStrategy: .throw) let expectedValue: [Float] = [ -.infinity, 678910.11, ] let encodedValue = expectedValues.unkeyedValueFloatConvertToStringNegativeInfinity( expectedValue, encodingStrategies ) XCTAssertThrowsError( try target(using: decodingStrategies).decode(type(of: expectedValue), from: encodedValue) ) } public func test_unkeyedValue_float_throw_positiveInfinity() throws { try XCTSkipIf(Self.isAbstractTestCase)
let encodingStrategies = EncodingStrategies(nonConformingFloatStrategy: .throw) let decodingStrategies = DecodingStrategies(nonConformingFloatStrategy: .throw) let expectedValue: [Float] = [ .infinity, 678910.11, ] let encodedValue = expectedValues.unkeyedValueFloatConvertToStringPositiveInfinity( expectedValue, encodingStrategies ) XCTAssertThrowsError( try target(using: decodingStrategies).decode(type(of: expectedValue), from: encodedValue) ) }}#endif