diff --git a/Sources/Conf/ConfigurationError.swift b/Sources/Conf/ConfigurationError.swift index 0298e6c..13cfd94 100644 --- a/Sources/Conf/ConfigurationError.swift +++ b/Sources/Conf/ConfigurationError.swift @@ -1,5 +1,5 @@ public enum ConfigurationError: Error { case fetch(Error) case parse(Error) - case `import`(path: Key, value: Any) + case decode(path: Key, value: Any) } diff --git a/Sources/Conf/ConfigurationProvider.swift b/Sources/Conf/ConfigurationProvider.swift index 9f5ed0e..d0db1a8 100644 --- a/Sources/Conf/ConfigurationProvider.swift +++ b/Sources/Conf/ConfigurationProvider.swift @@ -45,7 +45,7 @@ final class CommonConfigurationProvider: ConfigurationProvider { array.enumerated() .map { ( currentKey.child(key: String($0)), $1.description) }) default: - throw ConfigurationError.import(path: currentKey, value: object) + throw ConfigurationError.decode(path: currentKey, value: object) } } } diff --git a/Tests/ConfTests/CommonConfigurationProviderTests.swift b/Tests/ConfTests/CommonConfigurationProviderTests.swift new file mode 100644 index 0000000..ba53f7c --- /dev/null +++ b/Tests/ConfTests/CommonConfigurationProviderTests.swift @@ -0,0 +1,108 @@ +import XCTest +@testable import Conf + +final class CommonConfigurationProviderTests: XCTestCase { + func testFetchError() { + let fetcher: CommonConfigurationProvider.Fetcher = { throw TestError() } + let parser: CommonConfigurationProvider.Parser = { _ in [:] } + let provider = CommonConfigurationProvider(loader: fetcher, parser: parser) + + XCTAssertThrowsError(try provider.configuration()) { error in + if case let ConfigurationError.fetch(detailError) = error { + XCTAssertTrue(detailError is TestError) + } else { + XCTFail("Invalid error returned \(error)") + } + } + } + + func testParserError() { + let fetcher: CommonConfigurationProvider.Fetcher = { return "" } + let parser: CommonConfigurationProvider.Parser = { _ in throw TestError() } + let provider = CommonConfigurationProvider(loader: fetcher, parser: parser) + + XCTAssertThrowsError(try provider.configuration()) { error in + if case let ConfigurationError.parse(detailError) = error { + XCTAssertTrue(detailError is TestError) + } else { + XCTFail("Invalid error returned \(error)") + } + } + } + + func testDecodeError() { + let fetcher: CommonConfigurationProvider.Fetcher = { return "" } + let uuid = UUID() + let parser: CommonConfigurationProvider.Parser = { data in + return [ + "first": "value", + "second": uuid + ] + } + let provider = CommonConfigurationProvider(loader: fetcher, parser: parser) + + XCTAssertThrowsError(try provider.configuration()) { error in + if case let ConfigurationError.decode(path: key, value: value) = error { + XCTAssertEqual(key, "second") + XCTAssertEqual(value as? UUID, uuid) + } else { + XCTFail("Invalid error returned \(error)") + } + } + } + + func testDataFlow() throws { + let fetcher: CommonConfigurationProvider.Fetcher = { return "string" } + let parser: CommonConfigurationProvider.Parser = { data in + XCTAssertEqual(data, "string") + return [:] + } + let provider = CommonConfigurationProvider(loader: fetcher, parser: parser) + let configuration = try provider.configuration() + XCTAssertTrue(configuration.isEmpty) + } + + func testDecodeValues() throws { + let fetcher: CommonConfigurationProvider.Fetcher = {"" } + let parser: CommonConfigurationProvider.Parser = { _ in + return [ "key": 22] + } + let provider = CommonConfigurationProvider(loader: fetcher, parser: parser) + let configuration = try provider.configuration() + XCTAssertEqual(configuration, ["key": "22"]) + } + + func testDecodeArray() throws { + let fetcher: CommonConfigurationProvider.Fetcher = {"" } + let parser: CommonConfigurationProvider.Parser = { _ in + return [ "key": ["one", "two"]] + } + let provider = CommonConfigurationProvider(loader: fetcher, parser: parser) + let configuration = try provider.configuration() + let expect: [Key: String] = [ + Key(["key", "0"]): "one", + Key(["key", "1"]): "two" + ] + XCTAssertEqual(configuration, expect) + } + + func testNestedValues() throws { + let fetcher: CommonConfigurationProvider.Fetcher = {"" } + let parser: CommonConfigurationProvider.Parser = { _ in + return [ + "key": [ "nested": "value" ], + "one": [ "more": [ + "deep": "value" + ] + ] + ] + } + let provider = CommonConfigurationProvider(loader: fetcher, parser: parser) + let configuration = try provider.configuration() + let expect: [Key: String] = [ + Key(["key", "nested"]): "value", + Key(["one", "more", "deep"]): "value" + ] + XCTAssertEqual(configuration, expect) + } +} diff --git a/Tests/ConfTests/ConfigTests.swift b/Tests/ConfTests/ConfigTests.swift index 840f2a7..cd84bb2 100644 --- a/Tests/ConfTests/ConfigTests.swift +++ b/Tests/ConfTests/ConfigTests.swift @@ -37,6 +37,12 @@ final class ConfigTests: XCTestCase { XCTAssertEqual(config.dump(), [:]) config.set(value: "another value", for: ["long", "path"]) XCTAssertEqual(config.dump(), [Key(["long", "path"]): "another value"]) + + config["subscript"] = "subscriptValue" + XCTAssertEqual(config.dump()["subscript"], "subscriptValue") + + config["lossless"] = 42 + XCTAssertEqual(config.dump()["lossless"], "42") } func testRead() throws {