Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parameters: fix flatten function for false parameters #108

Merged
merged 4 commits into from
Oct 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ _None_

### Bug Fixes

_None_
* `Parameters`: ensure the `flatten` function correctly handles a flag with a `false` value.
[David Jennes](https://github.com/djbe)
[#108](https://github.com/SwiftGen/StencilSwiftKit/pull/108)

### Internal Changes

Expand Down
2 changes: 1 addition & 1 deletion Sources/StencilSwiftKit/Parameters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public enum Parameters {
switch object {
case is String, is Int, is Double:
values.append("\(keyPrefix)=\(object)")
case is Bool:
case let bool as Bool where bool:
values.append(keyPrefix)
case let dict as [String: Any]:
for (key, value) in dict {
Expand Down
14 changes: 14 additions & 0 deletions Tests/StencilSwiftKitTests/ParametersTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,20 @@ class ParametersTests: XCTestCase {
"The order of arrays are properly preserved when flattened")
}

func testFlattenBool() {
let trueFlag = Parameters.flatten(dictionary: ["test": true])
XCTAssertEqual(trueFlag, ["test"], "True flag is flattened to a param without value")

let falseFlag = Parameters.flatten(dictionary: ["test": false])
XCTAssertEqual(falseFlag, [], "False flag is flattened to nothing")

let stringFlag = Parameters.flatten(dictionary: ["test": "a"])
XCTAssertEqual(stringFlag, ["test=a"], "Non-boolean flag is flattened to a parameter with value")

let falseStringFlag = Parameters.flatten(dictionary: ["test": "false"])
XCTAssertEqual(falseStringFlag, ["test=false"], "Non-boolean flag is flattened to a parameter with value")
}
Copy link
Contributor

@AliSoftware AliSoftware Oct 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add tests that Parameters.flatten(dictionary: ["test": "false"]) still passes the string "false" along (to check that we still make the distinction between false the Boolean and "false" the string?)


func testParseInvalidSyntax() {
// invalid character
do {
Expand Down
2 changes: 2 additions & 0 deletions Tests/StencilSwiftKitTests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ extension ParametersTests {
static let __allTests = [
("testBasic", testBasic),
("testDeepStructured", testDeepStructured),
("testFlattenBool", testFlattenBool),
("testParseInvalidKey", testParseInvalidKey),
("testParseInvalidStructure", testParseInvalidStructure),
("testParseInvalidSyntax", testParseInvalidSyntax),
Expand Down Expand Up @@ -153,6 +154,7 @@ extension SwiftIdentifierTests {
("testBasicStringWithForbiddenCharsAndUnderscores", testBasicStringWithForbiddenCharsAndUnderscores),
("testEmojis", testEmojis),
("testEmojis2", testEmojis2),
("testEmptyString", testEmptyString),
("testForbiddenChars", testForbiddenChars),
("testKeepUppercaseAcronyms", testKeepUppercaseAcronyms),
("testNumbersFirst", testNumbersFirst),
Expand Down