JSON parser tests

This commit is contained in:
Alexandr Goncharov 2020-06-10 01:36:20 +03:00
parent 51f3fb7142
commit f0d284ce1e
5 changed files with 63 additions and 2 deletions

View File

@ -1,7 +1,9 @@
import Foundation
/// Namespace for the predefined parsers
enum Parser {
enum Parser { }
extension Parser {
static let json: CommonConfigurationProvider.Parser = { data in
let object: Any
do {
@ -16,3 +18,15 @@ enum Parser {
return values
}
}
extension Parser {
static let donEnv: CommonConfigurationProvider.Parser = { data in
func fail() throws {
struct InvalidDotEnvFormat: Error { let data: Data }
throw InvalidDotEnvFormat(data: data)
}
try fail()
let result = [String: String]()
return result
}
}

View File

@ -87,7 +87,7 @@ final class CommonConfigurationProviderTests: XCTestCase {
XCTAssertEqual(configuration, expect)
}
func testNestedValues() throws {
func testDecodeNestedValues() throws {
let fetcher: CommonConfigurationProvider.Fetcher = { Data() }
let parser: CommonConfigurationProvider.Parser = { _ in
return [
@ -106,4 +106,20 @@ final class CommonConfigurationProviderTests: XCTestCase {
]
XCTAssertEqual(configuration, expect)
}
func testDecodeEmptyValues() throws {
let fetcher: CommonConfigurationProvider.Fetcher = { Data() }
let parser: CommonConfigurationProvider.Parser = { _ in [:] }
let provider = CommonConfigurationProvider(loader: fetcher, parser: parser)
let configuration = try provider.configuration()
XCTAssertEqual(configuration, [:])
}
func testDecodeEmptyArray() throws {
let fetcher: CommonConfigurationProvider.Fetcher = { Data() }
let parser: CommonConfigurationProvider.Parser = { _ in [ "key": []] }
let provider = CommonConfigurationProvider(loader: fetcher, parser: parser)
let configuration = try provider.configuration()
XCTAssertEqual(configuration, [:])
}
}

View File

@ -0,0 +1,19 @@
import XCTest
@testable import Conf
final class ParserJSONTests: XCTestCase {
func testSuccess() throws {
let content = try Resource(name: "valid", type: "json").data()
_ = try Parser.json(content)
}
func testError() throws {
let content = try Resource(name: "invalid", type: "json").data()
XCTAssertThrowsError(try Parser.json(content)) { error in
guard case ConfigurationError.parse = error else {
XCTFail("Invalid error returned \(error)")
return
}
}
}
}

View File

@ -0,0 +1 @@
[1, 2, 3]

View File

@ -0,0 +1,11 @@
{
"param": "value",
"nested": {
"key": "values"
},
"array": [
1,
2,
3
]
}