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

Commit

Permalink
add path to invalidHexColor error
Browse files Browse the repository at this point in the history
  • Loading branch information
djbe committed Jun 2, 2017
1 parent c589264 commit 3938c3e
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 18 deletions.
10 changes: 5 additions & 5 deletions Sources/Parsers/ColorsFileParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ import PathKit

public enum ColorsParserError: Error, CustomStringConvertible {
case duplicateExtensionParser(ext: String, existing: String, new: String)
case invalidHexColor(string: String, key: String?)
case invalidHexColor(path: Path, string: String, key: String?)
case invalidFile(path: Path, reason: String)
case unsupportedFileType(path: Path, supported: [String])

public var description: String {
switch self {
case .duplicateExtensionParser(let ext, let existing, let new):
return "error: Parser \(new) tried to register the file type '\(ext)' already registered by \(existing)."
case .invalidHexColor(let string, let key):
case .invalidHexColor(let path, let string, let key):
let keyInfo = key.flatMap { " for key \"\($0)\"" } ?? ""
return "error: Invalid hex color \"\(string)\" found\(keyInfo)."
return "error: Invalid hex color \"\(string)\" found\(keyInfo) (\(path))."
case .invalidFile(let path, let reason):
return "error: Unable to parse file at \(path). \(reason)"
case .unsupportedFileType(let path, let supported):
Expand Down Expand Up @@ -77,7 +77,7 @@ public final class ColorsFileParser {

// MARK: - Private Helpers

func parse(hex hexString: String, key: String? = nil) throws -> UInt32 {
func parse(hex hexString: String, key: String? = nil, path: Path) throws -> UInt32 {
let scanner = Scanner(string: hexString)

let prefixLen: Int
Expand All @@ -91,7 +91,7 @@ func parse(hex hexString: String, key: String? = nil) throws -> UInt32 {

var value: UInt32 = 0
guard scanner.scanHexInt32(&value) else {
throw ColorsParserError.invalidHexColor(string: hexString, key: key)
throw ColorsParserError.invalidHexColor(path: path, string: hexString, key: key)
}

let len = hexString.lengthOfBytes(using: .utf8) - prefixLen
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ final class ColorsJSONFileParser: ColorsFileTypeParser {

var colors = [String: UInt32]()
for (key, value) in dict {
colors[key] = try parse(hex: value, key: key)
colors[key] = try parse(hex: value, key: key, path: path)
}

let name = path.lastComponentWithoutExtension
Expand Down
6 changes: 3 additions & 3 deletions Sources/Parsers/ColorsFileParsers/ColorsTXTFileParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ final class ColorsTextFileParser: ColorsFileTypeParser {

private var colors = [String: UInt32]()

private func addColor(named name: String, value: String) throws {
try addColor(named: name, value: parse(hex: value, key: name))
private func addColor(named name: String, value: String, path: Path) throws {
try addColor(named: name, value: parse(hex: value, key: name, path: path))
}

private func addColor(named name: String, value: UInt32) {
Expand Down Expand Up @@ -65,7 +65,7 @@ final class ColorsTextFileParser: ColorsFileTypeParser {
do {
let dict = try keyValueDict(from: path, withSeperator: ":")
for key in dict.keys {
try addColor(named: key, value: colorValue(forKey: key, onDict: dict))
try addColor(named: key, value: colorValue(forKey: key, onDict: dict), path: path)
}
} catch let error as ColorsParserError {
throw error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ final class ColorsXMLFileParser: ColorsFileTypeParser {
throw ColorsParserError.invalidFile(path: path, reason: "Invalid structure, color \(value) must have a name.")
}

colors[name] = try parse(hex: value, key: name)
colors[name] = try parse(hex: value, key: name, path: path)
}

let name = path.lastComponentWithoutExtension
Expand Down
2 changes: 1 addition & 1 deletion Tests/Resources
2 changes: 1 addition & 1 deletion Tests/SwiftGenKitTests/ColorsJSONFileTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class ColorsJSONFileTests: XCTestCase {
do {
_ = try ColorsJSONFileParser().parseFile(at: Fixtures.path(for: "bad-value.json", sub: .colors))
XCTFail("Code did parse file successfully while it was expected to fail for bad value")
} catch ColorsParserError.invalidHexColor(string: "this isn't a color", key: "ArticleTitle"?) {
} catch ColorsParserError.invalidHexColor(path: _, string: "this isn't a color", key: "ArticleTitle"?) {
// That's the expected exception we want to happen
} catch let error {
XCTFail("Unexpected error occured while parsing: \(error)")
Expand Down
8 changes: 4 additions & 4 deletions Tests/SwiftGenKitTests/ColorsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,22 +91,22 @@ class ColorParserTests: XCTestCase {
// MARK: - String parsing

func testStringNoPrefix() throws {
let color = try parse(hex: "FFFFFF")
let color = try parse(hex: "FFFFFF", path: #file)
XCTAssertEqual(color, 0xFFFFFFFF)
}

func testStringWithHash() throws {
let color = try parse(hex: "#FFFFFF")
let color = try parse(hex: "#FFFFFF", path: #file)
XCTAssertEqual(color, 0xFFFFFFFF)
}

func testStringWith0x() throws {
let color = try parse(hex: "0xFFFFFF")
let color = try parse(hex: "0xFFFFFF", path: #file)
XCTAssertEqual(color, 0xFFFFFFFF)
}

func testStringWithAlpha() throws {
let color = try parse(hex: "FFFFFFCC")
let color = try parse(hex: "FFFFFFCC", path: #file)
XCTAssertEqual(color, 0xFFFFFFCC)
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/SwiftGenKitTests/ColorsTextFileTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class ColorsTextFileTests: XCTestCase {
do {
_ = try ColorsTextFileParser().parseFile(at: Fixtures.path(for: "bad-value.txt", sub: .colors))
XCTFail("Code did parse file successfully while it was expected to fail for bad value")
} catch ColorsParserError.invalidHexColor(string: "thisIsn'tAColor", key: "ArticleTitle"?) {
} catch ColorsParserError.invalidHexColor(path: _, string: "thisIsn'tAColor", key: "ArticleTitle"?) {
// That's the expected exception we want to happen
} catch let error {
XCTFail("Unexpected error occured while parsing: \(error)")
Expand Down
2 changes: 1 addition & 1 deletion Tests/SwiftGenKitTests/ColorsXMLFileTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class ColorsXMLFileTests: XCTestCase {
do {
_ = try ColorsXMLFileParser().parseFile(at: Fixtures.path(for: "bad-value.xml", sub: .colors))
XCTFail("Code did parse file successfully while it was expected to fail for bad value")
} catch ColorsParserError.invalidHexColor(string: "this isn't a color", key: "ArticleTitle"?) {
} catch ColorsParserError.invalidHexColor(path: _, string: "this isn't a color", key: "ArticleTitle"?) {
// That's the expected exception we want to happen
} catch let error {
XCTFail("Unexpected error occured while parsing: \(error)")
Expand Down

0 comments on commit 3938c3e

Please sign in to comment.