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

Data tables #149

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions Example/Pods/Pods.xcodeproj/project.pbxproj

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 35 additions & 4 deletions Example/Tests/Features/ExampleFeatures.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,10 @@ final class ExampleFeatures: XCTestCase {
}
}

struct Person: CodableMatchedStringRepresentable {
struct Person: CodableMatchedStringRepresentable, Equatable {
let name: String
let age: Int
let height: Int
let height: Float
}

func testCustomExampleValues() {
Expand Down Expand Up @@ -220,8 +220,39 @@ final class ExampleFeatures: XCTestCase {
}

func testMatchingStringLiterals() {
/// Test that calling Given when defining the step using `step(exactly:...` will work, and won't be horribly confused by regular expression characters
/// in the step
// Test that calling Given when defining the step using `step(exactly:...` will work,
// and won't be horribly confused by regular expression characters in the step
Given(MatchStringLiteralStepDefiner.literal)
}

func testArrayDataTable() {
Given("I add the following numbers:") {
[1, 2, 3]
}
Then("I end up with 6")
}

func testDictionaryDataTable() {
Given("I add the following letters:") {
["a": 1, "b": 2, "c": 3]
}
Then("I end up with 6")
}

func testCodableDataTable() {
Given("I know the following persons:") {
[
Person(name: "Alice", age: 27, height: 170),
Person(name: "Bob", age: 27, height: 170)
]
}

Given("I know the following persons by name:") {
[
"Alice": Person(name: "Alice", age: 27, height: 170),
"Bob": Person(name: "Bob", age: 27, height: 170)
]
}
}

}
30 changes: 30 additions & 0 deletions Example/Tests/Features/NativeDataTableTest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// NativeDataTableTest.swift
// XCTest-Gherkin_Tests
//
// Created by Ilya Puchka on 03/09/2018.
// Copyright © 2018 CocoaPods. All rights reserved.
//

import XCTest
import XCTest_Gherkin

final class NativeDataTableTest: NativeTestCase {

struct Person: CodableMatchedStringRepresentable, Equatable {
let name: String
let age: String
let height: Int
let fulltime: Bool
}

override class func path() -> URL? {
let bundle = Bundle(for: self)
return bundle.resourceURL?.appendingPathComponent("NativeFeatures/native_data_table.feature")
}

override func setUp() {
super.setUp()
print("Default setup method works before each native scenario")
}
}
55 changes: 55 additions & 0 deletions Example/Tests/Features/NativeFeatures/native_data_table.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
Feature: Data tables

Scenario: one dimensional array
Given I have the following array:
| 1 |
| 2 |
| 3 |

Scenario: two dimensional array
Given I have the following array of arrays:
| 1 | 4 |
| 2 | 5 |
| 3 | 6 |

Scenarion: table with titles
Given I have the following hash maps:
| firstName | lastName | birthDate |
| Annie M.G. | Schmidt | 1911-03-20 |
| Roald | Dahl | 1916-09-13 |
| Astrid | Lindgren | 1907-11-14 |

Scenario: hash map
Given I have the following hash map:
| KMSY | Louis Armstrong New Orleans International Airport |
| KSFO | San Francisco International Airport |
| KSEA | Seattle–Tacoma International Airport |
| KJFK | John F. Kennedy International Airport |

Scenario: one dimensional hash map
Given I have the following hash map list:
| KMSY | 29.993333 | -90.258056 |
| KSFO | 37.618889 | -122.375000 |
| KSEA | 47.448889 | -122.309444 |
| KJFK | 40.639722 | -73.778889 |

Scenario: two dimensional hash map
Given I have the following hash map hash:
| | lat | lon |
| KMSY | 29.993333 | -90.258056 |
| KSFO | 37.618889 | -122.375000 |
| KSEA | 47.448889 | -122.309444 |
| KJFK | 40.639722 | -73.778889 |

Scenario: data table with codable values
Given I have the following persons:
| name | age | height | fulltime |
| Alice | "20" | 170 | true |
| Bob | "21" | 171 | false |

Scenario: data table with hash map of codable values
Given I have the following persons by id:
| | name | age | height | fulltime |
| 1 | Alice | "20" | 170 | Y |
| 2 | Bob | '21' | 171 | N |

98 changes: 98 additions & 0 deletions Example/Tests/StepDefinitions/SanitySteps.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ final class SanitySteps: StepDefiner {

private var numberOfExamplesExecutedInOrder = 1
private var backgroundStepsExecuted = false
private var answer = 0

override func defineSteps() {

Expand Down Expand Up @@ -165,6 +166,103 @@ final class SanitySteps: StepDefiner {

step("This is unused step") {}

step("I know the following persons: (.+)") { (match: DataTable<[ExampleFeatures.Person]>) in
XCTAssertTrue(match.values[0].name == "Alice" || match.values[1].name == "Bob")
}

step("I know the following persons by name: (.+)") { (match: DataTable<[String: ExampleFeatures.Person]>) in
XCTAssertTrue(match.values["Alice"]?.name == "Alice" || match.values["Bob"]?.name == "Bob")
}

step("I add the following numbers: (.+)") { (match: DataTable<[Int]>) in
self.answer = match.values.reduce(self.answer, +)
}

step("I add the following letters: (.+)") { (match: DataTable<[String: Int]>) in
self.answer = match.values.values.reduce(self.answer, +)
}

step("I end up with (\\d+)") { (match: Int) in
XCTAssertEqual(self.answer, match)
self.answer = 0
}

step("I have the following array: (.+)") { (match: NativeDataTable) in
XCTAssertEqual(match.values, [1, 2, 3])
}

step("I have the following array of arrays: (.+)") { (match: NativeDataTable) in
XCTAssertEqual(match.values, [[1, 4], [2, 5], [3, 6]])
}

step("I have the following hash maps: (.+)") { (match: NativeDataTable) in
XCTAssertEqual(
match.values,
[
[ "firstName": "Annie M.G.","lastName": "Schmidt", "birthDate": "1911-03-20" ],
[ "firstName": "Roald", "lastName": "Dahl", "birthDate": "1916-09-13" ],
[ "firstName": "Astrid", "lastName": "Lindgren", "birthDate": "1907-11-14" ]
]
)
}

step("I have the following hash map: (.+)") { (match: NativeDataTable) in
XCTAssertEqual(
match.values,
[
"KMSY": "Louis Armstrong New Orleans International Airport",
"KSFO": "San Francisco International Airport",
"KSEA": "Seattle–Tacoma International Airport",
"KJFK": "John F. Kennedy International Airport"
]
)
}

step("I have the following hash map list: (.+)") { (match: NativeDataTable) in
XCTAssertEqual(
match.values,
[
"KMSY": [29.993333, -90.258056],
"KSFO": [37.618889, -122.375000],
"KSEA": [47.448889, -122.309444],
"KJFK": [40.639722, -73.778889]
]
)
}

step("I have the following hash map hash: (.+)") { (match: NativeDataTable) in
XCTAssertEqual(
match.values,
[
"KMSY": [ "lat": 29.993333, "lon": -90.258056 ],
"KSFO": [ "lat": 37.618889, "lon": -122.375000 ],
"KSEA": [ "lat": 47.448889, "lon": -122.309444 ],
"KJFK": [ "lat": 40.639722, "lon": -73.778889 ]
]
)
}

step("I have the following persons: (.+)") { (match: NativeDataTable) in
XCTAssertEqual(
match.values,
[
NativeDataTableTest.Person(name: "Alice", age: "20", height: 170, fulltime: true),
NativeDataTableTest.Person(name: "Bob", age: "21", height: 171, fulltime: false)
]
)
}

step("I have the following persons by id: (.+)") { (match: NativeDataTable) in
XCTAssertEqual(
match.values,
[
1: NativeDataTableTest.Person(name: "Alice", age: "20", height: 170, fulltime: true),
2: NativeDataTableTest.Person(name: "Bob", age: "21", height: 171, fulltime: false)
]
)
}


}
}

Expand Down
4 changes: 4 additions & 0 deletions Example/XCTest-Gherkin.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
9B9DF95F1DDB3A4500EDBDDF /* ExampleNativeRunnerTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9B9DF95E1DDB3A4500EDBDDF /* ExampleNativeRunnerTest.swift */; };
A055C1B120F8DC5B00539791 /* MultipleTestRunTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A055C1AE20F8D68800539791 /* MultipleTestRunTests.swift */; };
AE7D6C386C4DAF0C1292DE0B /* Pods_XCTest_Gherkin_Example_XCTest_Gherkin_Tests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B23A6E26B52E88BC1F182097 /* Pods_XCTest_Gherkin_Example_XCTest_Gherkin_Tests.framework */; };
B5772A4F21C7D006005A1EB4 /* NativeDataTableTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5772A4E21C7D006005A1EB4 /* NativeDataTableTest.swift */; };
B5F7CFB921320DDF001643BD /* ExampleNativeLocalisationTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5F7CFB821320DDF001643BD /* ExampleNativeLocalisationTest.swift */; };
B5FC01A7213A0A66006B5A48 /* NativeFeatureParserTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5FC01A6213A0A65006B5A48 /* NativeFeatureParserTests.swift */; };
B5FC01AF213AB45F006B5A48 /* BackgroundTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5FC01AE213AB45F006B5A48 /* BackgroundTests.swift */; };
Expand Down Expand Up @@ -79,6 +80,7 @@
AD8DF65B966910B5E42CBECB /* README.md */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = net.daringfireball.markdown; name = README.md; path = ../README.md; sourceTree = "<group>"; };
B23A6E26B52E88BC1F182097 /* Pods_XCTest_Gherkin_Example_XCTest_Gherkin_Tests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_XCTest_Gherkin_Example_XCTest_Gherkin_Tests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
B3AC2FD70988184EBE408E62 /* Pods_XCTest_Gherkin_Example_XCTest_Gherkin_ExampleUITests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_XCTest_Gherkin_Example_XCTest_Gherkin_ExampleUITests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
B5772A4E21C7D006005A1EB4 /* NativeDataTableTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NativeDataTableTest.swift; sourceTree = "<group>"; };
B5F7CFB821320DDF001643BD /* ExampleNativeLocalisationTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExampleNativeLocalisationTest.swift; sourceTree = "<group>"; };
B5FC01A6213A0A65006B5A48 /* NativeFeatureParserTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NativeFeatureParserTests.swift; sourceTree = "<group>"; };
B5FC01AE213AB45F006B5A48 /* BackgroundTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BackgroundTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -263,6 +265,7 @@
E54130971BEBE06600E92975 /* NativeFeatures */,
E5805BFE1BEA939000D3ECD5 /* ExampleFeatures.swift */,
E54130991BEBE11B00E92975 /* ExampleNativeTest.swift */,
B5772A4E21C7D006005A1EB4 /* NativeDataTableTest.swift */,
5782122A1E974A570048D25F /* NativeScenarioTest.swift */,
E8C172631D25197C006A99DF /* ExampleNativeFeatureTest.swift */,
578212281E94B1610048D25F /* ExampleNativeOrderTest.swift */,
Expand Down Expand Up @@ -593,6 +596,7 @@
5782122B1E974A570048D25F /* NativeScenarioTest.swift in Sources */,
E5805C031BEA93E900D3ECD5 /* SanitySteps.swift in Sources */,
B5F7CFB921320DDF001643BD /* ExampleNativeLocalisationTest.swift in Sources */,
B5772A4F21C7D006005A1EB4 /* NativeDataTableTest.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
59 changes: 59 additions & 0 deletions Pod/Core/DataTable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//
// DataTable.swift
// XCTest-Gherkin
//
// Created by Ilya Puchka on 02/09/2018.
//

import Foundation

/**
Use DataTable as the type of the step parameter to be able to pass collection of values to the step.

Example:
```
Given("I use the following names:") {
["Alice", "Bob"]
}

step("I use the following names: (.+)") { (match: DataTable<[String]>) in
...
}
```
*/
public struct DataTable<T: Collection>: CodableMatchedStringRepresentable where T: Codable {
public let values: T

public init(_ values: T) {
self.values = values
}

public init(from decoder: Decoder) throws {
let values = try decoder.singleValueContainer()
self.values = try values.decode(T.self)
}

public func encode(to encoder: Encoder) throws {
var values = encoder.singleValueContainer()
try values.encode(self.values)
}

public init?(fromMatch match: String) {
let decoder = JSONDecoder()
guard let data = match.data(using: .utf8) else {
return nil
}
if let decoded = try? decoder.decode(DataTable<T>.self, from: data) {
self = decoded
} else {
return nil
}
}

}

extension DataTable: Equatable where T: Equatable {
public static func ==(lhs: DataTable<T>, rhs: DataTable<T>) -> Bool {
return lhs.values == rhs.values
}
}
Loading