Skip to content

Commit

Permalink
Added Codable conformance and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
orchetect committed Jul 24, 2022
1 parent 32f5eae commit 1b1f3ea
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 0 deletions.
32 changes: 32 additions & 0 deletions Sources/SwiftASCII/ASCIICharacter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,38 @@ extension ASCIICharacter: Equatable {

}

extension ASCIICharacter: Codable {

enum CodingKeys: String, CodingKey {

case asciiValue

}

public init(from decoder: Decoder) throws {

let container = try decoder.container(keyedBy: CodingKeys.self)
let asciiValue = try container.decode(UInt8.self, forKey: .asciiValue)
guard let newInstance = Self(asciiValue) else {
throw DecodingError.dataCorrupted(
.init(codingPath: container.codingPath,
debugDescription: "Value was not valid ASCII character number.")
)
}
self = newInstance

}

public func encode(to encoder: Encoder) throws {

var container = encoder.container(keyedBy: CodingKeys.self)

try container.encode(asciiValue, forKey: .asciiValue)

}

}

extension ASCIICharacter {

public static func + (lhs: ASCIICharacter, rhs: ASCIICharacter) -> ASCIIString {
Expand Down
25 changes: 25 additions & 0 deletions Sources/SwiftASCII/ASCIIString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,31 @@ extension ASCIIString: Equatable {

}

extension ASCIIString: Codable {

public init(from decoder: Decoder) throws {

let container = try decoder.singleValueContainer()
let stringValue = try container.decode(String.self)
guard let newInstance = Self(exactly: stringValue) else {
throw DecodingError.dataCorrupted(
.init(codingPath: container.codingPath,
debugDescription: "Encoded string is not a valid ASCII string.")
)
}
self = newInstance

}

public func encode(to encoder: Encoder) throws {

var container = encoder.singleValueContainer()
try container.encode(stringValue)

}

}

extension ASCIIString {

public static func + (lhs: ASCIIString, rhs: ASCIIString) -> ASCIIString {
Expand Down
14 changes: 14 additions & 0 deletions Tests/SwiftASCIITests/ASCIICharacter Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,20 @@ class ASCIICharacterTests: XCTestCase {

}

func testCodable() throws {

let encoder = JSONEncoder()
let decoder = JSONDecoder()

let str = ASCIICharacter("A")

let encoded = try encoder.encode(str)
let decoded = try decoder.decode(ASCIICharacter.self, from: encoded)

XCTAssertEqual(str, decoded)

}

func testStaticInits() {

let str: ASCIICharacter = .lossy("😃")
Expand Down
14 changes: 14 additions & 0 deletions Tests/SwiftASCIITests/ASCIIString Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,20 @@ class ASCIIStringTests: XCTestCase {

}

func testCodable() throws {

let encoder = JSONEncoder()
let decoder = JSONDecoder()

let str = ASCIIString("A string")

let encoded = try encoder.encode(str)
let decoded = try decoder.decode(ASCIIString.self, from: encoded)

XCTAssertEqual(str, decoded)

}

func testStaticInits() {

let str: ASCIIString = .lossy("Emöji 😃")
Expand Down

0 comments on commit 1b1f3ea

Please sign in to comment.