Skip to content

Commit

Permalink
update encode & decode
Browse files Browse the repository at this point in the history
  • Loading branch information
MacOMNI committed Dec 5, 2024
1 parent 114686f commit 5c98fcb
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 3 deletions.
21 changes: 21 additions & 0 deletions Codec/Tests/CodecTests/DecoderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,27 @@ struct DecoderTests {
#expect(throws: Error.self) {
_ = try JamDecoder.decode(Int8.self, from: invalidEncodedData)
}
#expect(throws: Error.self) {
_ = try JamDecoder.decode(Double.self, from: Data())
}
#expect(throws: Error.self) {
_ = try JamDecoder.decode(Float.self, from: Data())
}
#expect(throws: Error.self) {
_ = try JamDecoder.decode(Int?.self, from: Data())
}
#expect(throws: Error.self) {
_ = try JamDecoder.decode(String.self, from: Data([1, 2, 3]))
}
#expect(throws: Error.self) {
_ = try JamDecoder.decode([Int].self, from: Data([21, 205, 91, 7, 0, 0, 0, 0]))
}
#expect(throws: Error.self) {
_ = try JamDecoder.decode([Data].self, from: Data([21, 205, 91, 7, 0, 0, 0, 0]))
}
#expect(throws: Error.self) {
_ = try JamDecoder.decode(Data.self, from: Data([21, 205, 91, 7, 0, 0, 0, 0]))
}
}

@Test func decodeEmptyString() throws {
Expand Down
55 changes: 52 additions & 3 deletions Codec/Tests/CodecTests/EncoderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ struct EncoderTests {
}
}

struct UnkeyedTest: Encodable, Decodable {
let boolValue: Bool
struct UnkeyedTest: Codable {
var boolValue: Bool
let stringValue: String
let intValue: Int
let int8Value: Int8
Expand Down Expand Up @@ -209,6 +209,55 @@ struct EncoderTests {
try container.encode(dataValue)
try container.encode(nestedValues)
}

init(boolValue: Bool = false,
stringValue: String = "",
intValue: Int = 0,
int8Value: Int8 = 0,
int16Value: Int16 = 0,
int32Value: Int32 = 0,
int64Value: Int64 = 0,
uintValue: UInt = 0,
uint8Value: UInt8 = 0,
uint16Value: UInt16 = 0,
uint32Value: UInt32 = 0,
uint64Value: UInt64 = 0,
dataValue: Data = Data(),
nestedValues: [UnkeyedTest] = [])
{
self.boolValue = boolValue
self.stringValue = stringValue
self.intValue = intValue
self.int8Value = int8Value
self.int16Value = int16Value
self.int32Value = int32Value
self.int64Value = int64Value
self.uintValue = uintValue
self.uint8Value = uint8Value
self.uint16Value = uint16Value
self.uint32Value = uint32Value
self.uint64Value = uint64Value
self.dataValue = dataValue
self.nestedValues = nestedValues
}

init(from decoder: Decoder) throws {
var container = try decoder.unkeyedContainer()
boolValue = try container.decode(Bool.self)
stringValue = try container.decode(String.self)
intValue = try container.decode(Int.self)
int8Value = try container.decode(Int8.self)
int16Value = try container.decode(Int16.self)
int32Value = try container.decode(Int32.self)
int64Value = try container.decode(Int64.self)
uintValue = try container.decode(UInt.self)
uint8Value = try container.decode(UInt8.self)
uint16Value = try container.decode(UInt16.self)
uint32Value = try container.decode(UInt32.self)
uint64Value = try container.decode(UInt64.self)
dataValue = try container.decode(Data.self)
nestedValues = try container.decode([UnkeyedTest].self)
}
}

@Test func testUnkeyedContainer() throws {
Expand Down Expand Up @@ -248,7 +297,7 @@ struct EncoderTests {

let encoded = try JamEncoder.encode(testData)
#expect(encoded.count > 0)
let decoded = try JamDecoder.decode(UnkeyedTest.self, from: encoded)
let decoded = try JamDecoder.decode(UnkeyedTest.self, from: encoded, withConfig: testData)
#expect(testData.intValue == decoded.intValue)
#expect(testData.dataValue == decoded.dataValue)
}
Expand Down

0 comments on commit 5c98fcb

Please sign in to comment.