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

Commit

Permalink
Add duplicate table error
Browse files Browse the repository at this point in the history
  • Loading branch information
djbe committed May 28, 2017
1 parent 73be7c6 commit 1634f06
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
9 changes: 8 additions & 1 deletion Sources/Parsers/StringsFileParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ import Foundation
import PathKit

public enum StringsFileParserError: Error, CustomStringConvertible {
case duplicateTable(name: String)
case failureOnLoading(path: String)
case invalidFormat
case invalidPlaceholder(previous: StringsFileParser.PlaceholderType, new: StringsFileParser.PlaceholderType)

public var description: String {
switch self {
case .duplicateTable(let name):
return "Table \"\(name)\" already loaded, cannot add it again"
case .failureOnLoading(let path):
return "Failed to load a file at \"\(path)\""
case .invalidFormat:
Expand All @@ -31,6 +34,11 @@ public final class StringsFileParser {

// Localizable.strings files are generally UTF16, not UTF8!
public func parseFile(at path: Path) throws {
let name = path.lastComponentWithoutExtension

guard tables[name] == nil else {
throw StringsFileParserError.duplicateTable(name: name)
}
guard let data = try? path.read() else {
throw StringsFileParserError.failureOnLoading(path: path.string)
}
Expand All @@ -40,7 +48,6 @@ public final class StringsFileParser {
throw StringsFileParserError.invalidFormat
}

let name = path.lastComponentWithoutExtension
tables[name] = try dict.map { key, translation in
try Entry(key: key, translation: translation)
}
Expand Down
14 changes: 14 additions & 0 deletions Tests/SwiftGenKitTests/StringsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,18 @@ class StringsTests: XCTestCase {
let result = parser.stencilContext()
XCTDiffContexts(result, expected: "multiple.plist", sub: .strings)
}

func testMultipleFilesDuplicate() throws {
let parser = StringsFileParser()
try parser.parseFile(at: Fixtures.path(for: "Localizable.strings", sub: .strings))

do {
try parser.parseFile(at: Fixtures.path(for: "Localizable.strings", sub: .strings))
XCTFail("Code did parse file successfully while it was expected to fail for duplicate file")
} catch StringsFileParserError.duplicateTable {
// That's the expected exception we want to happen
} catch let error {
XCTFail("Unexpected error occured while parsing: \(error)")
}
}
}

0 comments on commit 1634f06

Please sign in to comment.