From 09c32b0ca7c402379b389d04dae0d8589d72138f Mon Sep 17 00:00:00 2001 From: David Hardiman Date: Fri, 26 Apr 2019 11:07:01 +0100 Subject: [PATCH] Don't use # for empty strings --- Sources/Config/Property.swift | 7 +++++-- Tests/ConfigTests/ConfigurationPropertyTests.swift | 12 ++++++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/Sources/Config/Property.swift b/Sources/Config/Property.swift index ee8eac3..6bb4dfe 100644 --- a/Sources/Config/Property.swift +++ b/Sources/Config/Property.swift @@ -82,8 +82,11 @@ enum PropertyType: String { } func valueDeclaration(for value: Any, iv: IV, key: String?) -> String { - let stringValue = { - "#\"\(value)\"#" + let stringValue = { () -> String in + if let string = value as? String, string.isEmpty { + return "\"\"" + } + return "#\"\(value)\"#" } switch self { case .string: diff --git a/Tests/ConfigTests/ConfigurationPropertyTests.swift b/Tests/ConfigTests/ConfigurationPropertyTests.swift index 4d0df84..65c32ac 100644 --- a/Tests/ConfigTests/ConfigurationPropertyTests.swift +++ b/Tests/ConfigTests/ConfigurationPropertyTests.swift @@ -16,7 +16,8 @@ class ConfigurationPropertyTests: XCTestCase { "defaultValue": "test value", "overrides": [ "hello": "hello value", - "pattern": "pattern value" + "pattern": "pattern value", + "empty": "" ] ]) } @@ -34,6 +35,13 @@ class ConfigurationPropertyTests: XCTestCase { expect(actualValue).to(equal(expectedValue)) } + func testItCanWriteADeclarationForAnEmptyStringProperty() throws { + let stringProperty = givenAStringProperty() + let expectedValue = ##" static let test: String = """## + let actualValue = try whenTheDeclarationIsWritten(for: stringProperty, scheme: "empty") + expect(actualValue).to(equal(expectedValue)) + } + func testItAddsAPublicAccessorWhenRequired() throws { let stringProperty = givenAStringProperty() let expectedValue = ##" public static let test: String = #"test value"#"## @@ -125,7 +133,7 @@ class ConfigurationPropertyTests: XCTestCase { func testItCanWriteABoolProperty() throws { let floatProperty = ConfigurationProperty(key: "test", typeHint: "Bool", dict: [ "defaultValue": true, - ]) + ]) let expectedValue = #" static let test: Bool = true"# let actualValue = try whenTheDeclarationIsWritten(for: floatProperty) expect(actualValue).to(equal(expectedValue))