Skip to content

Commit

Permalink
Indentation + PodcastType: Make Equatable and Codable (#17)
Browse files Browse the repository at this point in the history
This will enable tools built on top of Plot (like Publish) to encode types
containing these kinds of values. Also improve the Linux compatibility
tooling to detect if a new test case was added only on Apple Platforms.
  • Loading branch information
JohnSundell authored Jan 3, 2020
1 parent 0010086 commit 00bd4d6
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 3 deletions.
43 changes: 41 additions & 2 deletions Sources/Plot/API/Indentation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import Foundation

/// A representation of a kind of indentation at a given level.
public struct Indentation {
public struct Indentation: Codable, Equatable {
/// The kind of the indentation (see `Kind`).
public var kind: Kind
/// The level of the indentation (0 = root).
Expand All @@ -22,7 +22,7 @@ public struct Indentation {
public extension Indentation {
/// Enum defining various kinds of indentation that a document
/// can be rendered using.
enum Kind {
enum Kind: Equatable {
/// Each level should be indented by a given number of tabs.
case tabs(Int)
/// Each level should be indented by a given number of spaces.
Expand Down Expand Up @@ -56,3 +56,42 @@ extension Indentation.Kind: CustomStringConvertible {
}
}
}

extension Indentation.Kind: Codable {
private enum CodingKeys: CodingKey {
case kind
case count
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let kind = try container.decode(String.self, forKey: .kind)
let count = try container.decode(Int.self, forKey: .count)

switch kind {
case "tabs":
self = .tabs(count)
case "spaces":
self = .spaces(count)
default:
throw DecodingError.dataCorruptedError(
forKey: CodingKeys.kind,
in: container,
debugDescription: "'\(kind)' is not an indentation kind"
)
}
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)

switch self {
case .tabs(let count):
try container.encode("tabs", forKey: .kind)
try container.encode(count, forKey: .count)
case .spaces(let count):
try container.encode("spaces", forKey: .kind)
try container.encode(count, forKey: .count)
}
}
}
2 changes: 1 addition & 1 deletion Sources/Plot/API/PodcastType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import Foundation

/// Enum describing various podcast types supported by Apple Podcasts.
public enum PodcastType: String {
public enum PodcastType: String, Codable {
case episodic
case serial
}
27 changes: 27 additions & 0 deletions Tests/PlotTests/IndentationTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import XCTest
import Plot

final class IndentationTests: XCTestCase {
func testSpacesCoding() throws {
let indentation = Indentation(kind: .spaces(4))
let data = try JSONEncoder().encode(indentation)
let decoded = try JSONDecoder().decode(Indentation.self, from: data)
XCTAssertEqual(indentation, decoded)
}

func testTabsCoding() throws {
let indentation = Indentation(kind: .tabs(1))
let data = try JSONEncoder().encode(indentation)
let decoded = try JSONDecoder().decode(Indentation.self, from: data)
XCTAssertEqual(indentation, decoded)
}
}

extension IndentationTests {
static var allTests: Linux.TestList<IndentationTests> {
[
("testSpacesCoding", testSpacesCoding),
("testTabsCoding", testTabsCoding)
]
}
}
14 changes: 14 additions & 0 deletions Tests/PlotTests/LinuxCompatibility.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ internal extension Linux {
#if canImport(ObjectiveC)
internal final class LinuxVerificationTests: XCTestCase {
func testAllTestsRunOnLinux() {
var totalLinuxTestCount = 0

for testCase in allTests() {
let type = testCase.testCaseClass

Expand All @@ -49,7 +51,19 @@ internal final class LinuxVerificationTests: XCTestCase {
""")
}
}

totalLinuxTestCount += linuxTestNames.count
}

XCTAssertEqual(
XCTestSuite.default.testCaseCount - 1,
totalLinuxTestCount,
"""
Linux and Apple Platforms test counts are not equal.
Perhaps you added a new test case class?
If so, you need to add it in XCTestManifests.swift.
"""
)
}
}
#endif
1 change: 1 addition & 0 deletions Tests/PlotTests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public func allTests() -> [Linux.TestCase] {
Linux.makeTestCase(using: ControlFlowTests.allTests),
Linux.makeTestCase(using: DocumentTests.allTests),
Linux.makeTestCase(using: HTMLTests.allTests),
Linux.makeTestCase(using: IndentationTests.allTests),
Linux.makeTestCase(using: NodeTests.allTests),
Linux.makeTestCase(using: PodcastFeedTests.allTests),
Linux.makeTestCase(using: RSSTests.allTests),
Expand Down

0 comments on commit 00bd4d6

Please sign in to comment.