Skip to content

Commit

Permalink
Add type annotation to single value custom types
Browse files Browse the repository at this point in the history
  • Loading branch information
dhardiman committed Apr 26, 2019
1 parent ea0884a commit 89d23a2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
9 changes: 8 additions & 1 deletion Sources/Config/CustomType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,12 @@ private func valueString(from value: Any?) -> String {
}

private func valueString(for placeholder: Placeholder, from dictionary: [String: Any]) -> String {
guard let value = dictionary[placeholder.name], let unusedIV = try? IV(dict: dictionary) else { return "" }
guard let value = dictionary[placeholder.name] else { return "" }
return valueString(for: placeholder, from: value)
}

private func valueString(for placeholder: Placeholder, from value: Any) -> String {
guard let unusedIV = try? IV(dict: [:]) else { return "" }
if let type = placeholder.type {
return type.valueDeclaration(for: value, iv: unusedIV, key: nil)
} else {
Expand Down Expand Up @@ -146,6 +151,8 @@ private struct CustomPropertyValue {
let actualValue: String
if let dictionary = value as? [String: Any] {
actualValue = valueString(for: type.placeholders[0], from: dictionary)
} else if type.placeholders[0].type != nil {
actualValue = valueString(for: type.placeholders[0], from: value)
} else {
actualValue = valueString(from: value)
}
Expand Down
31 changes: 31 additions & 0 deletions Tests/ConfigTests/CustomPropertyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,22 @@ class CustomPropertyTests: XCTestCase {
"""
expect(property.propertyDeclaration(for: "any", iv: try IV(dict: [:]), encryptionKey: nil, requiresNonObjCDeclarations: false, isPublic: true, indentWidth: 0)).to(equal(expectedValue))
}

func testItOutputsAStringWithEscapedSlashesCorrectly() {
let property = CustomProperty(key: "test", customType: givenACustomType(for: regexCustomType), dict: regexDictionary)
let expectedValue = """
public static let test: NSRegularExpression = try! NSRegularExpression(expression: "^.*\\@.*\\.[a-zA-Z]{2,3}$", options: [])
"""
expect(property.propertyDeclaration(for: "any", iv: try IV(dict: [:]), encryptionKey: nil, requiresNonObjCDeclarations: false, isPublic: true, indentWidth: 0)).to(equal(expectedValue))
}

func testItOutputsAStringWithEscapedSlashesCorrectlyForSingleValue() {
let property = CustomProperty(key: "test", customType: givenACustomType(for: regexCustomType), dict: regexSingleValueDictionary)
let expectedValue = """
public static let test: NSRegularExpression = try! NSRegularExpression(expression: "^.*\\@.*\\.[a-zA-Z]{2,3}$", options: [])
"""
expect(property.propertyDeclaration(for: "any", iv: try IV(dict: [:]), encryptionKey: nil, requiresNonObjCDeclarations: false, isPublic: true, indentWidth: 0)).to(equal(expectedValue))
}
}

class CustomPropertyArrayTests: XCTestCase {
Expand Down Expand Up @@ -211,3 +227,18 @@ private func givenADictionaryWithTypedValues() -> [String: Any] {
]
]
}

private let regexCustomType: [String: Any] = [
"typeName": "NSRegularExpression",
"initialiser": "try! NSRegularExpression(expression: {expression:String}, options: [])"
]

private let regexDictionary: [String: Any] = [
"defaultValue": [
"expression": "^.*\\@.*\\.[a-zA-Z]{2,3}$"
]
]

private let regexSingleValueDictionary: [String: Any] = [
"defaultValue": "^.*\\@.*\\.[a-zA-Z]{2,3}$"
]

0 comments on commit 89d23a2

Please sign in to comment.