Skip to content

Commit

Permalink
Output strings as swift 5 literals to avoid need to escape slashes
Browse files Browse the repository at this point in the history
  • Loading branch information
dhardiman committed Apr 26, 2019
1 parent 89d23a2 commit 0e139ca
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 41 deletions.
2 changes: 1 addition & 1 deletion Sources/Config/EnumConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ struct EnumConfiguration: Template {
let propertyDeclarations = properties.compactMap { property -> String? in
guard let configProperty = property.value as? ConfigurationProperty<String> else { return nil }
let value = configProperty.value(for: self.scheme)
return " case \(configProperty.key)" + (value.isEmpty ? "" : #" = "\#(value.replacingOccurrences(of: #"\"#, with: #"\\"#))""#)
return " case \(configProperty.key)" + (value.isEmpty ? "" : ##" = #"\##(value)"#"##)
}
.sorted()
.joined(separator: "\n")
Expand Down
6 changes: 3 additions & 3 deletions Sources/Config/Property.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ enum PropertyType: String {

func valueDeclaration(for value: Any, iv: IV, key: String?) -> String {
let stringValue = {
#""\#(value)""#
"#\"\(value)\"#"
}
switch self {
case .string:
return stringValue()
case .url:
return "URL(string: \(stringValue()))!"
return #"URL(string: "\#(value)")!"#
case .encryptionKey:
return byteArrayOutput(from: Array("\(value)".utf8))
case .encrypted:
Expand All @@ -103,7 +103,7 @@ enum PropertyType: String {
case .colour:
return colourValue(for: value as? String)
case .image:
return "UIImage(named: \(stringValue()))!"
return #"UIImage(named: "\#(value)")!"#
case .bool:
return "\(value as! Bool)"
default:
Expand Down
6 changes: 3 additions & 3 deletions Tests/ConfigTests/ConfigGeneratorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ private let expectedStrings = [
public enum enumconfig: String {
case emptyCase
case firstCase = "Some Value"
case secondCase = "Another Value"
case firstCase = #"Some Value"#
case secondCase = #"Another Value"#
}
""",
Expand All @@ -113,7 +113,7 @@ private let expectedStrings = [
public enum standard {
public static let float: Float = 0.0
public static let schemeName: String = "any"
public static let schemeName: String = #"any"#
}
// swiftlint:enable force_unwrapping type_body_length file_length superfluous_disable_command
Expand Down
38 changes: 19 additions & 19 deletions Tests/ConfigTests/ConfigurationFileTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ConfigurationFileTests: XCTestCase {
// swiftlint:disable force_unwrapping type_body_length file_length superfluous_disable_command
public enum Test {
public static let schemeName: String = "any"
public static let schemeName: String = #"any"#
}
// swiftlint:enable force_unwrapping type_body_length file_length superfluous_disable_command
Expand Down Expand Up @@ -72,7 +72,7 @@ class ConfigurationFileTests: XCTestCase {
// swiftlint:disable force_unwrapping type_body_length file_length superfluous_disable_command
public enum Test {
public static let schemeName: String = "any"
public static let schemeName: String = #"any"#
}
// swiftlint:enable force_unwrapping type_body_length file_length superfluous_disable_command
Expand All @@ -94,7 +94,7 @@ class ConfigurationFileTests: XCTestCase {
public static let encryptionKeyIV: [UInt8] = [UInt8(97), UInt8(53), UInt8(101), UInt8(49), UInt8(49), UInt8(97), UInt8(100), UInt8(57), UInt8(98), UInt8(53), UInt8(56), UInt8(55), UInt8(52), UInt8(56), UInt8(101), UInt8(48), UInt8(52), UInt8(56), UInt8(57), UInt8(57), UInt8(56), UInt8(97), UInt8(102), UInt8(53), UInt8(55), UInt8(55), UInt8(97), UInt8(55), UInt8(98), UInt8(97), UInt8(48), UInt8(102)]
public static let schemeName: String = "any"
public static let schemeName: String = #"any"#
public static let somethingSecret: [UInt8] = [UInt8(72), UInt8(248), UInt8(24), UInt8(73), UInt8(30), UInt8(207), UInt8(159), UInt8(0), UInt8(65), UInt8(147), UInt8(20), UInt8(183), UInt8(214), UInt8(231), UInt8(169), UInt8(3)]
}
Expand All @@ -116,9 +116,9 @@ class ConfigurationFileTests: XCTestCase {
public enum Test {
public static let anotherProperty: Int = 1
public static let schemeName: String = "scheme"
public static let schemeName: String = #"scheme"#
public static let testProperty: String = "A test"
public static let testProperty: String = #"A test"#
}
// swiftlint:enable force_unwrapping type_body_length file_length superfluous_disable_command
Expand All @@ -136,11 +136,11 @@ class ConfigurationFileTests: XCTestCase {
// swiftlint:disable force_unwrapping type_body_length file_length superfluous_disable_command
public enum Test {
public static let property: String = "A test"
public static let property: String = #"A test"#
public static let referenceProperty: String = property
public static let schemeName: String = "scheme"
public static let schemeName: String = #"scheme"#
}
// swiftlint:enable force_unwrapping type_body_length file_length superfluous_disable_command
Expand All @@ -159,7 +159,7 @@ class ConfigurationFileTests: XCTestCase {
// swiftlint:disable force_unwrapping type_body_length file_length superfluous_disable_command
public enum Test {
@nonobjc public static var schemeName: String {
return "any"
return #"any"#
}
}
Expand All @@ -178,10 +178,10 @@ class ConfigurationFileTests: XCTestCase {
// swiftlint:disable force_unwrapping type_body_length file_length superfluous_disable_command
public enum Test {
public static let schemeName: String = "scheme"
public static let schemeName: String = #"scheme"#
public enum FirstGroup {
public static let testProperty: String = "A test"
public static let testProperty: String = #"A test"#
}
public enum SecondGroup {
Expand All @@ -205,11 +205,11 @@ class ConfigurationFileTests: XCTestCase {
// swiftlint:disable force_unwrapping type_body_length file_length superfluous_disable_command
public enum Test {
/// API Host
public static let host: String = "dev.dave.com"
public static let host: String = #"dev.dave.com"#
public static let hostKey: String = "a-secret-key"
public static let hostKey: String = #"a-secret-key"#
public static let schemeName: String = "scheme"
public static let schemeName: String = #"scheme"#
}
// swiftlint:enable force_unwrapping type_body_length file_length superfluous_disable_command
Expand All @@ -228,11 +228,11 @@ class ConfigurationFileTests: XCTestCase {
// swiftlint:disable force_unwrapping type_body_length file_length superfluous_disable_command
public enum Test {
/// API Host
public static let host: String = "dave.com"
public static let host: String = #"dave.com"#
public static let hostKey: String = "the-prod-key"
public static let hostKey: String = #"the-prod-key"#
public static let schemeName: String = "prod"
public static let schemeName: String = #"prod"#
}
// swiftlint:enable force_unwrapping type_body_length file_length superfluous_disable_command
Expand All @@ -256,7 +256,7 @@ class ConfigurationFileTests: XCTestCase {
public static let float: Float = 0.0
public static let schemeName: String = "any"
public static let schemeName: String = #"any"#
public static let stringArray: [String] = []
}
Expand All @@ -276,9 +276,9 @@ class ConfigurationFileTests: XCTestCase {
// swiftlint:disable force_unwrapping type_body_length file_length superfluous_disable_command
public enum Test {
public static let property: CustomType = CustomType(param: "Value")
public static let property: CustomType = CustomType(param: #"Value"#)
public static let schemeName: String = "any"
public static let schemeName: String = #"any"#
}
// swiftlint:enable force_unwrapping type_body_length file_length superfluous_disable_command
Expand Down
14 changes: 7 additions & 7 deletions Tests/ConfigTests/ConfigurationPropertyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,21 @@ class ConfigurationPropertyTests: XCTestCase {

func testItCanWriteADeclarationForAStringPropertyUsingTheDefaultValue() throws {
let stringProperty = givenAStringProperty()
let expectedValue = #" static let test: String = "test value""#
let expectedValue = ##" static let test: String = #"test value"#"##
let actualValue = try whenTheDeclarationIsWritten(for: stringProperty)
expect(actualValue).to(equal(expectedValue))
}

func testItAddsAPublicAccessorWhenRequired() throws {
let stringProperty = givenAStringProperty()
let expectedValue = #" public static let test: String = "test value""#
let expectedValue = ##" public static let test: String = #"test value"#"##
let actualValue = try whenTheDeclarationIsWritten(for: stringProperty, isPublic: true)
expect(actualValue).to(equal(expectedValue))
}

func testItCanIndentADeclaration() throws {
let stringProperty = givenAStringProperty()
let expectedValue = #" static let test: String = "test value""#
let expectedValue = ##" static let test: String = #"test value"#"##
let actualValue = try whenTheDeclarationIsWritten(for: stringProperty, indentWidth: 3)
expect(actualValue).to(equal(expectedValue))
}
Expand All @@ -52,7 +52,7 @@ class ConfigurationPropertyTests: XCTestCase {
let stringProperty = givenAStringProperty()
let expectedValue = """
@nonobjc static var test: String {
return "test value"
return #"test value"#
}
"""
let actualValue = try whenTheDeclarationIsWritten(for: stringProperty, requiresNonObjC: true)
Expand All @@ -61,14 +61,14 @@ class ConfigurationPropertyTests: XCTestCase {

func testItCanGetAnOverrideForAnExactMatch() throws {
let stringProperty = givenAStringProperty()
let expectedValue = #" static let test: String = "hello value""#
let expectedValue = ##" static let test: String = #"hello value"#"##
let actualValue = try whenTheDeclarationIsWritten(for: stringProperty, scheme: "hello")
expect(actualValue).to(equal(expectedValue))
}

func testItCanGetAnOverrideForAPatternMatch() throws {
let stringProperty = givenAStringProperty()
let expectedValue = #" static let test: String = "pattern value""#
let expectedValue = ##" static let test: String = #"pattern value"#"##
let actualValue = try whenTheDeclarationIsWritten(for: stringProperty, scheme: "match-a-pattern")
expect(actualValue).to(equal(expectedValue))
}
Expand All @@ -80,7 +80,7 @@ class ConfigurationPropertyTests: XCTestCase {
])
let expectedValue = """
/// A comment to add
static let test: String = "test value"
static let test: String = #"test value"#
"""
let actualValue = try whenTheDeclarationIsWritten(for: stringProperty)
expect(actualValue).to(equal(expectedValue))
Expand Down
12 changes: 6 additions & 6 deletions Tests/ConfigTests/CustomPropertyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class CustomPropertyTests: XCTestCase {
]), dict: givenADictionaryWithValues())
let expectedValue = """
/// A description
public static let test: CustomType = CustomType(thingy: "firstValue")
public static let test: CustomType = CustomType(thingy: #"firstValue"#)
"""
expect(property.propertyDeclaration(for: "any", iv: try IV(dict: [:]), encryptionKey: nil, requiresNonObjCDeclarations: false, isPublic: true, indentWidth: 0)).to(equal(expectedValue))
}
Expand All @@ -94,23 +94,23 @@ class CustomPropertyTests: XCTestCase {
let property = CustomProperty(key: "test", customType: givenACustomType(for: givenATypeDictionaryWithTypeAnnotations()), dict: givenADictionaryWithTypedValues())
let expectedValue = """
/// A description
public static let test: CustomType = CustomType(oneThing: "firstValue", secondThing: true)
public static let test: CustomType = CustomType(oneThing: #"firstValue"#, secondThing: true)
"""
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: [])
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: [])
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))
}
Expand All @@ -137,7 +137,7 @@ class CustomPropertyArrayTests: XCTestCase {
let property = CustomPropertyArray(key: "test", customType: givenACustomType(for: givenATypeDictionaryWithTypeAnnotations()), dict: givenADictionaryWithArrayValues())
let expectedValue = """
/// A description
public static let test: [CustomType] = [CustomType(oneThing: "firstValue", secondThing: true)]
public static let test: [CustomType] = [CustomType(oneThing: #"firstValue"#, secondThing: true)]
"""
expect(property.propertyDeclaration(for: "any", iv: try IV(dict: [:]), encryptionKey: nil, requiresNonObjCDeclarations: false, isPublic: true, indentWidth: 0)).to(equal(expectedValue))
}
Expand All @@ -146,7 +146,7 @@ class CustomPropertyArrayTests: XCTestCase {
let property = CustomPropertyArray(key: "test", customType: givenACustomType(for: givenATypeDictionaryWithTypeAnnotations()), dict: givenADictionaryWithArrayValues())
let expectedValue = """
/// A description
public static let test: [CustomType] = [CustomType(oneThing: "firstOverriddenValue", secondThing: false)]
public static let test: [CustomType] = [CustomType(oneThing: #"firstOverriddenValue"#, secondThing: false)]
"""
expect(property.propertyDeclaration(for: "override", iv: try IV(dict: [:]), encryptionKey: nil, requiresNonObjCDeclarations: false, isPublic: true, indentWidth: 0)).to(equal(expectedValue))
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/ConfigTests/EnumConfigurationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ class EnumConfigurationTests: XCTestCase {
public enum Test: String {
case emptyCase
case firstCase = "Some Value"
case secondCase = "Overridden Value"
case firstCase = #"Some Value"#
case secondCase = #"Overridden Value"#
}
"""
Expand Down

0 comments on commit 0e139ca

Please sign in to comment.