Skip to content

Commit

Permalink
Add a filter option enum parser
Browse files Browse the repository at this point in the history
  • Loading branch information
djbe committed May 31, 2017
1 parent 35af75f commit 9244d1e
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 17 deletions.
4 changes: 2 additions & 2 deletions Sources/Filters+Strings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ extension Filters {
/// - Returns: the camel case string
/// - Throws: FilterError.invalidInputType if the value parameter isn't a string
static func snakeToCamelCase(_ value: Any?, arguments: [Any?]) throws -> Any? {
let stripLeading = try Filters.parseBool(from: arguments, index: 0, required: false) ?? false
let stripLeading = try Filters.parseBool(from: arguments, required: false) ?? false
guard let string = value as? String else { throw Filters.Error.invalidInputType }

let unprefixed: String
Expand Down Expand Up @@ -104,7 +104,7 @@ extension Filters {
/// - Returns: the snake case string
/// - Throws: FilterError.invalidInputType if the value parameter isn't a string
static func camelToSnakeCase(_ value: Any?, arguments: [Any?]) throws -> Any? {
let toLower = try Filters.parseBool(from: arguments, index: 0, required: false) ?? true
let toLower = try Filters.parseBool(from: arguments, required: false) ?? true
guard let string = value as? String else { throw Filters.Error.invalidInputType }

let snakeCase = try snakecase(string)
Expand Down
22 changes: 21 additions & 1 deletion Sources/Filters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ enum Filters {
/// - required: If true, the argument is required and function throws if missing.
/// If false, returns nil on missing args.
/// - Throws: Filters.Error.invalidInputType
static func parseBool(from arguments: [Any?], index: Int, required: Bool = true) throws -> Bool? {
static func parseBool(from arguments: [Any?], at index: Int = 0, required: Bool = true) throws -> Bool? {
guard index < arguments.count, let boolArg = arguments[index] as? String else {
if required {
throw Error.invalidInputType
Expand All @@ -41,4 +41,24 @@ enum Filters {
throw Error.invalidInputType
}
}

/// Parses filter arguments for an enum value (with a String rawvalue).
///
/// - Parameters:
/// - arguments: an array of argument values, may be empty
/// - index: the index in the arguments array
/// - default: The default value should no argument be provided
/// - Throws: Filters.Error.invalidInputType
static func parseEnum<T>(from arguments: [Any?], at index: Int = 0, default: T) throws -> T
where T: RawRepresentable, T.RawValue == String {

guard index < arguments.count else { return `default` }
let arg = arguments[index].map(String.init(describing:)) ?? `default`.rawValue

guard let result = T(rawValue: arg) else {
throw Filters.Error.invalidOption(option: arg)
}

return result
}
}
4 changes: 4 additions & 0 deletions StencilSwiftKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
/* Begin PBXBuildFile section */
82EF0CC0752D216C67279A16 /* Pods_Tests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8BF798509C76E5A9ACE03491 /* Pods_Tests.framework */; };
B5A3F2ED5DA57C06EF62BB82 /* ParseBoolTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5A3FFC01B2145C4BFD8316A /* ParseBoolTests.swift */; };
DD0B6D5F1EDF7C2100C8862C /* ParseEnumTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD0B6D5E1EDF7C2100C8862C /* ParseEnumTests.swift */; };
DD4393FF1E2D3EEB0047A332 /* MapNodeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD4393FE1E2D3EEB0047A332 /* MapNodeTests.swift */; };
DD5F341B1E21993A00AEB5DA /* TestsHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD5F341A1E21993A00AEB5DA /* TestsHelper.swift */; };
DD5F342E1E21A3A200AEB5DA /* CallNodeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD5F342A1E21A3A200AEB5DA /* CallNodeTests.swift */; };
Expand Down Expand Up @@ -59,6 +60,7 @@
4B3D39DBCD15D8F6BB891D92 /* Pods-Tests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Tests.release.xcconfig"; path = "Pods/Target Support Files/Pods-Tests/Pods-Tests.release.xcconfig"; sourceTree = "<group>"; };
8BF798509C76E5A9ACE03491 /* Pods_Tests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Tests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
B5A3FFC01B2145C4BFD8316A /* ParseBoolTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ParseBoolTests.swift; sourceTree = "<group>"; };
DD0B6D5E1EDF7C2100C8862C /* ParseEnumTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ParseEnumTests.swift; sourceTree = "<group>"; };
DD4393FE1E2D3EEB0047A332 /* MapNodeTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MapNodeTests.swift; sourceTree = "<group>"; };
DD5F341A1E21993A00AEB5DA /* TestsHelper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestsHelper.swift; sourceTree = "<group>"; };
DD5F34201E2199ED00AEB5DA /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
Expand Down Expand Up @@ -150,6 +152,7 @@
DD5F342C1E21A3A200AEB5DA /* StringFiltersTests.swift */,
DD5F342D1E21A3A200AEB5DA /* SwiftIdentifierTests.swift */,
B5A3FFC01B2145C4BFD8316A /* ParseBoolTests.swift */,
DD0B6D5E1EDF7C2100C8862C /* ParseEnumTests.swift */,
DD5F341C1E2199ED00AEB5DA /* Resources */,
);
path = StencilSwiftKitTests;
Expand Down Expand Up @@ -295,6 +298,7 @@
files = (
DD5F342F1E21A3A200AEB5DA /* SetNodeTests.swift in Sources */,
DD5F34311E21A3A200AEB5DA /* SwiftIdentifierTests.swift in Sources */,
DD0B6D5F1EDF7C2100C8862C /* ParseEnumTests.swift in Sources */,
DDE1E2F61E3E33E30043367C /* MacroNodeTests.swift in Sources */,
DD4393FF1E2D3EEB0047A332 /* MapNodeTests.swift in Sources */,
DD5F341B1E21993A00AEB5DA /* TestsHelper.swift in Sources */,
Expand Down
28 changes: 14 additions & 14 deletions Tests/StencilSwiftKitTests/ParseBoolTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,67 +10,67 @@ import XCTest
class ParseBoolTests: XCTestCase {

func testParseBool_WithTrueString() throws {
let value = try Filters.parseBool(from: ["true"], index: 0)
let value = try Filters.parseBool(from: ["true"])
XCTAssertTrue(value!)
}

func testParseBool_WithFalseString() throws {
let value = try Filters.parseBool(from: ["false"], index: 0)
let value = try Filters.parseBool(from: ["false"])
XCTAssertFalse(value!)
}

func testParseBool_WithYesString() throws {
let value = try Filters.parseBool(from: ["yes"], index: 0)
let value = try Filters.parseBool(from: ["yes"])
XCTAssertTrue(value!)
}

func testParseBool_WithNoString() throws {
let value = try Filters.parseBool(from: ["no"], index: 0)
let value = try Filters.parseBool(from: ["no"])
XCTAssertFalse(value!)
}

func testParseBool_WithOneString() throws {
let value = try Filters.parseBool(from: ["1"], index: 0)
let value = try Filters.parseBool(from: ["1"])
XCTAssertTrue(value!)
}

func testParseBool_WithZeroString() throws {
let value = try Filters.parseBool(from: ["0"], index: 0)
let value = try Filters.parseBool(from: ["0"])
XCTAssertFalse(value!)
}

func testParseBool_WithOptionalInt() throws {
let value = try Filters.parseBool(from: [1], index: 0, required: false)
let value = try Filters.parseBool(from: [1], required: false)
XCTAssertNil(value)
}

func testParseBool_WithRequiredInt() throws {
XCTAssertThrowsError(try Filters.parseBool(from: [1], index: 0, required: true))
XCTAssertThrowsError(try Filters.parseBool(from: [1], required: true))
}

func testParseBool_WithOptionalDouble() throws {
let value = try Filters.parseBool(from: [1.0], index: 0, required: false)
let value = try Filters.parseBool(from: [1.0], required: false)
XCTAssertNil(value)
}

func testParseBool_WithRequiredDouble() throws {
XCTAssertThrowsError(try Filters.parseBool(from: [1.0], index: 0, required: true))
XCTAssertThrowsError(try Filters.parseBool(from: [1.0], required: true))
}

func testParseBool_WithEmptyString() throws {
XCTAssertThrowsError(try Filters.parseBool(from: [""], index: 0, required: false))
XCTAssertThrowsError(try Filters.parseBool(from: [""], required: false))
}

func testParseBool_WithEmptyStringAndRequiredArg() throws {
XCTAssertThrowsError(try Filters.parseBool(from: [""], index: 0, required: true))
XCTAssertThrowsError(try Filters.parseBool(from: [""], required: true))
}

func testParseBool_WithEmptyArray() throws {
let value = try Filters.parseBool(from: [], index: 0, required: false)
let value = try Filters.parseBool(from: [], required: false)
XCTAssertNil(value)
}

func testParseBool_WithEmptyArrayAndRequiredArg() throws {
XCTAssertThrowsError(try Filters.parseBool(from: [], index: 0, required: true))
XCTAssertThrowsError(try Filters.parseBool(from: [], required: true))
}
}
41 changes: 41 additions & 0 deletions Tests/StencilSwiftKitTests/ParseEnumTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// StencilSwiftKit
// Copyright (c) 2017 SwiftGen
// MIT Licence
//

import XCTest
@testable import StencilSwiftKit

class ParseEnumTests: XCTestCase {
enum Test: String {
case foo
case bar
case baz
}

func testParseEnum_WithFooString() throws {
let value = try Filters.parseEnum(from: ["foo"], default: Test.baz)
XCTAssertEqual(value, Test.foo)
}

func testParseEnum_WithBarString() throws {
let value = try Filters.parseEnum(from: ["bar"], default: Test.baz)
XCTAssertEqual(value, Test.bar)
}

func testParseEnum_WithBazString() throws {
let value = try Filters.parseEnum(from: ["baz"], default: Test.baz)
XCTAssertEqual(value, Test.baz)
}

func testParseEnum_WithEmptyArray() throws {
let value = try Filters.parseEnum(from: [], default: Test.baz)
XCTAssertEqual(value, Test.baz)
}

func testParseEnum_WithUnknownArgument() throws {
XCTAssertThrowsError(try Filters.parseEnum(from: ["test"], default: Test.baz))
XCTAssertThrowsError(try Filters.parseEnum(from: [42], default: Test.baz))
}
}

0 comments on commit 9244d1e

Please sign in to comment.