Skip to content

Commit

Permalink
Merge pull request #110 from ilyapuchka/codable-matchable
Browse files Browse the repository at this point in the history
Introduce CodableMatchedStringRepresentable protocol
  • Loading branch information
deanWombourne authored Aug 12, 2018
2 parents 8197bd4 + 39c8182 commit f70c0e8
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Example/Tests/Features/ExampleFeatures.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,9 @@ final class ExampleFeatures: XCTestCase {
func testStepAnchorMatching() {
Given("This is a substring")
}

func testCodableMatches() {
let person = Person(name: "Nick")
Given("This is Nick \(person)")
}
}
8 changes: 8 additions & 0 deletions Example/Tests/StepDefinitions/SanitySteps.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,13 @@ final class SanitySteps: StepDefiner {
step("This is a substring") {
// This step should match instead of the one above, even though the other one is defined first
}

step("This is Nick (.+)") { (match: Person) in
XCTAssertEqual(match.name, "Nick")
}
}
}

struct Person: CodableMatchedStringRepresentable {
let name: String
}
19 changes: 19 additions & 0 deletions Pod/Core/MatchedStringRepresentable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,22 @@ extension Int: MatchedStringRepresentable {
self.init(match, radix: 10)
}
}

public protocol CodableMatchedStringRepresentable: Codable, CustomStringConvertible, MatchedStringRepresentable {}

extension CodableMatchedStringRepresentable {
public init?(fromMatch match: String) {
let decoder = JSONDecoder()
guard let data = match.data(using: .utf8),
let decoded = try? decoder.decode(Self.self, from: data) else {
return nil
}
self = decoded
}

public var description: String {
let encoder = JSONEncoder()
let encoded = try! encoder.encode(self)
return String(data: encoded, encoding: .utf8)!
}
}
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,28 @@ step("This value should be between ([0-9]*) and ([0-9]*)") { (match1: String, ma
}
```

### Captured value types

In step definition with captured values you can use any type conforming to `MatchedStringRepresentable`. `String`, `Double`, `Int` and `Bool` types already conform to this protocol. You can also match your custom types by conforming them to `CodableMatchedStringRepresentable`. This requires type to implement only `Codable` protocol methods, `MatchedStringRepresentable` implementation is provided by the library.

```swift
struct Person: Codable, Equatable {
let name: String
}
extension Person: CodableMatchedStringRepresentable {
}

step("User is logged in as (.+)") { (match: Person) in
let loggedInUser = ...
XCTAssertEqual(loggedInUser, match)
}

func testLoggedInUser() {
let nick = Person(name: "Nick")
Given("User is loggeed in as \(nick)")
}
```

### Examples and feature outlines
If you want to test the same situation with a set of data, Gherkin allows you to specify example input for your tests. We used this all over our previous tests so we needed to deal with it here too!

Expand Down

0 comments on commit f70c0e8

Please sign in to comment.