Skip to content
This repository has been archived by the owner on Sep 6, 2018. It is now read-only.

Commit

Permalink
use registration system and test the dispatching of this system
Browse files Browse the repository at this point in the history
  • Loading branch information
djbe committed May 28, 2017
1 parent 6e0f33e commit 5818096
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 20 deletions.
39 changes: 19 additions & 20 deletions Sources/Parsers/ColorsFileParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,42 +26,41 @@ public enum ColorsParserError: Error, CustomStringConvertible {
}

protocol ColorsFileTypeParser: class {
static var extensions: [String] { get }

init()
func parseFile(at path: Path) throws -> [String: UInt32]
}

public final class ColorsFileParser {
enum Extension: String {
case clr
case json
case txt
case xml
}

private var parsers = [String: ColorsFileTypeParser.Type]()
var colors = [String: UInt32]()

public init() {}
public init() {
register(parser: ColorsCLRFileParser.self)
register(parser: ColorsJSONFileParser.self)
register(parser: ColorsTextFileParser.self)
register(parser: ColorsXMLFileParser.self)
}

public func parseFile(at path: Path) throws {
guard let ext = Extension(rawValue: path.extension ?? "") else {
guard let parserType = parsers[path.extension?.lowercased() ?? ""] else {
throw ColorsParserError.unsupportedFileType
}

let colors: [String: UInt32]
switch ext {
case .clr:
colors = try ColorsCLRFileParser().parseFile(at: path)
case .json:
colors = try ColorsJSONFileParser().parseFile(at: path)
case .txt:
colors = try ColorsTextFileParser().parseFile(at: path)
case .xml:
colors = try ColorsXMLFileParser().parseFile(at: path)
}
let parser = parserType.init()
let colors = try parser.parseFile(at: path)

for (name, value) in colors {
self.colors[name] = value
}
}

func register(parser: ColorsFileTypeParser.Type) {
for ext in parser.extensions {
parsers[ext] = parser
}
}
}

// MARK: - Private Helpers
Expand Down
2 changes: 2 additions & 0 deletions Sources/Parsers/ColorsFileParsers/ColorsCLRFileParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import Foundation
import PathKit

final class ColorsCLRFileParser: ColorsFileTypeParser {
static var extensions = ["clr"]

private enum Keys {
static let userColors = "UserColors"
}
Expand Down
2 changes: 2 additions & 0 deletions Sources/Parsers/ColorsFileParsers/ColorsJSONFileParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import Foundation
import PathKit

final class ColorsJSONFileParser: ColorsFileTypeParser {
static var extensions = ["json"]

func parseFile(at path: Path) throws -> [String: UInt32] {
do {
let json = try JSONSerialization.jsonObject(with: try path.read(), options: [])
Expand Down
2 changes: 2 additions & 0 deletions Sources/Parsers/ColorsFileParsers/ColorsTXTFileParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import Foundation
import PathKit

final class ColorsTextFileParser: ColorsFileTypeParser {
static var extensions = ["txt"]

private var colors = [String: UInt32]()

private func addColor(named name: String, value: String) throws {
Expand Down
2 changes: 2 additions & 0 deletions Sources/Parsers/ColorsFileParsers/ColorsXMLFileParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import Kanna
import PathKit

final class ColorsXMLFileParser: ColorsFileTypeParser {
static var extensions = ["xml"]

private enum XML {
static let colorXPath = "/resources/color"
static let nameAttribute = "name"
Expand Down
42 changes: 42 additions & 0 deletions Tests/SwiftGenKitTests/ColorsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,24 @@
// MIT Licence
//

import PathKit
import XCTest
@testable import SwiftGenKit

final class TestFileParser1: ColorsFileTypeParser {
static var extensions = ["test1"]
func parseFile(at path: Path) throws -> [String: UInt32] {
return ["test1": 1]
}
}

final class TestFileParser2: ColorsFileTypeParser {
static var extensions = ["test2"]
func parseFile(at path: Path) throws -> [String: UInt32] {
return ["test2": 1]
}
}

class ColorParserTests: XCTestCase {
func testEmpty() {
let parser = ColorsFileParser()
Expand All @@ -15,6 +30,33 @@ class ColorParserTests: XCTestCase {
XCTDiffContexts(result, expected: "empty.plist", sub: .colors)
}

// MARK: - Dispatch

func testDispatchKnowExtension() throws {
let parser = ColorsFileParser()
parser.register(parser: TestFileParser1.self)
parser.register(parser: TestFileParser2.self)

try parser.parseFile(at: "someFile.test1")
XCTAssertEqual(parser.colors["test1"], 1)
XCTAssertNil(parser.colors["test2"])
}

func testDispatchUnknownExtension() {
let parser = ColorsFileParser()
parser.register(parser: TestFileParser1.self)
parser.register(parser: TestFileParser2.self)

do {
try parser.parseFile(at: "someFile.unknown")
XCTFail("Code did succeed while it was expected to fail for unknown extension")
} catch ColorsParserError.unsupportedFileType {
// That's the expected exception we want to happen
} catch let error {
XCTFail("Unexpected error occured while parsing: \(error)")
}
}

// MARK: - String parsing

func testStringNoPrefix() throws {
Expand Down

0 comments on commit 5818096

Please sign in to comment.