Skip to content

Commit

Permalink
[rule-dsl] - Rules are now available in the DSL - TT
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyler-Keith-Thompson committed Jul 25, 2020
1 parent dcf6cbe commit 9dcdc1d
Show file tree
Hide file tree
Showing 3 changed files with 355 additions and 0 deletions.
8 changes: 8 additions & 0 deletions CucumberSwift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@
CAA337AB21044ACC002C361D /* CucumberTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = CAA337AA21044ACC002C361D /* CucumberTests.swift */; };
CAA337AF21045847002C361D /* testdata in Resources */ = {isa = PBXBuildFile; fileRef = CAA337AE21045847002C361D /* testdata */; };
CAA337B12104C310002C361D /* DocstringTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = CAA337B02104C310002C361D /* DocstringTests.swift */; };
CAB20B3C24CCF5C10024C703 /* RuleDSL.swift in Sources */ = {isa = PBXBuildFile; fileRef = CAB20B3B24CCF5C10024C703 /* RuleDSL.swift */; };
CAB20B3E24CCF6B20024C703 /* DSLRuleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = CAB20B3D24CCF6B20024C703 /* DSLRuleTests.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -212,6 +214,8 @@
CAA337AA21044ACC002C361D /* CucumberTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CucumberTests.swift; sourceTree = "<group>"; };
CAA337AE21045847002C361D /* testdata */ = {isa = PBXFileReference; lastKnownFileType = folder; path = testdata; sourceTree = "<group>"; };
CAA337B02104C310002C361D /* DocstringTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DocstringTests.swift; sourceTree = "<group>"; };
CAB20B3B24CCF5C10024C703 /* RuleDSL.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RuleDSL.swift; sourceTree = "<group>"; };
CAB20B3D24CCF6B20024C703 /* DSLRuleTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DSLRuleTests.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -452,6 +456,7 @@
CA73473724CA6FAD00D1A018 /* DSLFeatureTests.swift */,
CA73473924CA788B00D1A018 /* DSLScenarioOutlineTests.swift */,
CA4E2D0424CCDF0500A44983 /* DSLBackgroundTests.swift */,
CAB20B3D24CCF6B20024C703 /* DSLRuleTests.swift */,
);
path = DSLTests;
sourceTree = "<group>";
Expand All @@ -468,6 +473,7 @@
CA5A211724CA42370077AD50 /* FeatureDSL.swift */,
CA73473B24CA796C00D1A018 /* DSLScenarioOutline.swift */,
CA4E2D0224CCDDDC00A44983 /* BackgroundDSL.swift */,
CAB20B3B24CCF5C10024C703 /* RuleDSL.swift */,
);
path = DSL;
sourceTree = "<group>";
Expand Down Expand Up @@ -757,6 +763,7 @@
1DA7619D2393C5AB001CBA5B /* DocString.swift in Sources */,
1DA761A6239A7C41001CBA5B /* Nodes.swift in Sources */,
AD73CEF82222ECD8002BF7C0 /* TestingExtensions.swift in Sources */,
CAB20B3C24CCF5C10024C703 /* RuleDSL.swift in Sources */,
AD9EB4852079EB1C002494C0 /* MightEqualOperator.swift in Sources */,
CA5A211224C9D2D90077AD50 /* ScenarioBuilder.swift in Sources */,
AD22A1B8222B10BD0025E41C /* TestCaseMethod.swift in Sources */,
Expand All @@ -782,6 +789,7 @@
CA73473824CA6FAD00D1A018 /* DSLFeatureTests.swift in Sources */,
CA73473A24CA788B00D1A018 /* DSLScenarioOutlineTests.swift in Sources */,
CA4E2D0124CCC8AE00A44983 /* PositionTests.swift in Sources */,
CAB20B3E24CCF6B20024C703 /* DSLRuleTests.swift in Sources */,
AD4E773122359C6B005938B7 /* ReporterTests.swift in Sources */,
CA5A210924C92BDC0077AD50 /* DSLStepTests.swift in Sources */,
AD4E77332235A04B005938B7 /* WaitUntil.swift in Sources */,
Expand Down
24 changes: 24 additions & 0 deletions CucumberSwift/DSL/RuleDSL.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// RuleDSL.swift
// CucumberSwift
//
// Created by Tyler Thompson on 7/25/20.
// Copyright © 2020 Tyler Thompson. All rights reserved.
//

import Foundation

public struct Rule: ScenarioDSL {
public let scenarios: [Scenario]
public init(_ title:String, tags:[String] = [],
line:UInt = #line, column:UInt = #column,
@ScenarioBuilder _ content: () -> [ScenarioDSL]) {
scenarios = content().flatMap { $0.scenarios }
}
public init(_ title:String, tags:[String] = [],
line:UInt = #line, column:UInt = #column,
@ScenarioBuilder _ content: () -> ScenarioDSL) {
scenarios = content().scenarios
}

}
323 changes: 323 additions & 0 deletions CucumberSwiftTests/DSLTests/DSLRuleTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,323 @@
//
// DSLRuleTests.swift
// CucumberSwiftTests
//
// Created by Tyler Thompson on 7/25/20.
// Copyright © 2020 Tyler Thompson. All rights reserved.
//

import Foundation
import XCTest
import CucumberSwift

class DSLRuleTests: XCTestCase {
func testSingleRuleWithOneScenarioAndNoBackground() {
let feature =
Feature("F1") {
Rule("R1") {
Scenario("SC1") {
Given(I: print("S1"))
}
}
}

XCTAssertEqual(feature.scenarios.count, 1)

let scenario = feature.scenarios.first
XCTAssertEqual(scenario?.title, "SC1")
XCTAssertEqual(scenario?.steps.count, 1)

let step = scenario?.steps.first
XCTAssertEqual(step?.match, "I: print(\"S1\")")
}

func testSingleRuleWithMultipleScenariosAndNoBackground() {
let feature =
Feature("F1") {
Rule("R1") {
Scenario("SC1") {
Given(I: print("S1"))
}
Scenario("SC2") {
Given(I: print("S2"))
}
}
}

XCTAssertEqual(feature.scenarios.count, 2)

let sc1 = feature.scenarios.first
XCTAssertEqual(sc1?.title, "SC1")
XCTAssertEqual(sc1?.steps.count, 1)
XCTAssertEqual(sc1?.steps.first?.match, "I: print(\"S1\")")

let sc2 = feature.scenarios.last
XCTAssertEqual(sc2?.title, "SC2")
XCTAssertEqual(sc2?.steps.count, 1)
XCTAssertEqual(sc2?.steps.first?.match, "I: print(\"S2\")")
}

func testSingleRuleWithOneScenarioAndARuleBackground() {
let feature =
Feature("F1") {
Rule("R1") {
Background {
Given(I: print("B1"))
}
Scenario("SC1") {
Given(I: print("S1"))
}
}
}

XCTAssertEqual(feature.scenarios.count, 1)

let scenario = feature.scenarios.first
XCTAssertEqual(scenario?.title, "SC1")
XCTAssertEqual(scenario?.steps.count, 2)
XCTAssertEqual(scenario?.steps.first?.match, "I: print(\"B1\")")
XCTAssertEqual(scenario?.steps.last?.match, "I: print(\"S1\")")
}

func testSingleRuleWithMultipleScenariosAndARuleBackground() {
let feature =
Feature("F1") {
Rule("R1") {
Background {
Given(I: print("B1"))
}
Scenario("SC1") {
Given(I: print("S1"))
}
Scenario("SC2") {
Given(I: print("S2"))
}
}
}

XCTAssertEqual(feature.scenarios.count, 2)

let sc1 = feature.scenarios.first
XCTAssertEqual(sc1?.title, "SC1")
XCTAssertEqual(sc1?.steps.count, 2)
XCTAssertEqual(sc1?.steps.first?.match, "I: print(\"B1\")")
XCTAssertEqual(sc1?.steps.last?.match, "I: print(\"S1\")")

let sc2 = feature.scenarios.last
XCTAssertEqual(sc2?.title, "SC2")
XCTAssertEqual(sc2?.steps.count, 2)
XCTAssertEqual(sc2?.steps.first?.match, "I: print(\"B1\")")
XCTAssertEqual(sc2?.steps.last?.match, "I: print(\"S2\")")
}

func testMultipleRulesWithOneScenarioAndNoBackground() {
let feature =
Feature("F1") {
Rule("R1") {
Scenario("SC1") {
Given(I: print("S1"))
}
}
Rule("R2") {
Scenario("SC2") {
Given(I: print("S2"))
}
}
}

XCTAssertEqual(feature.scenarios.count, 2)

XCTAssertEqual(feature.scenarios.first?.title, "SC1")
XCTAssertEqual(feature.scenarios.first?.steps.count, 1)
XCTAssertEqual(feature.scenarios.first?.steps.first?.match, "I: print(\"S1\")")

XCTAssertEqual(feature.scenarios.last?.title, "SC2")
XCTAssertEqual(feature.scenarios.last?.steps.count, 1)
XCTAssertEqual(feature.scenarios.last?.steps.first?.match, "I: print(\"S2\")")
}

func testMultipleRulesWithMultipleScenariosAndNoBackground() {
let feature =
Feature("F1") {
Rule("R1") {
Scenario("SC1") {
Given(I: print("S1"))
}
Scenario("SC2") {
Given(I: print("S2"))
}
}
Rule("R2") {
Scenario("SC3") {
Given(I: print("S3"))
}
Scenario("SC4") {
Given(I: print("S4"))
}
}
}

XCTAssertEqual(feature.scenarios.count, 4)

let sc1 = feature.scenarios.first
XCTAssertEqual(sc1?.title, "SC1")
XCTAssertEqual(sc1?.steps.count, 1)
XCTAssertEqual(sc1?.steps.first?.match, "I: print(\"S1\")")

guard feature.scenarios.count == 4 else { return }

let sc2 = feature.scenarios[1]
XCTAssertEqual(sc2.title, "SC2")
XCTAssertEqual(sc2.steps.count, 1)
XCTAssertEqual(sc2.steps.first?.match, "I: print(\"S2\")")

let sc3 = feature.scenarios[2]
XCTAssertEqual(sc3.title, "SC3")
XCTAssertEqual(sc3.steps.count, 1)
XCTAssertEqual(sc3.steps.first?.match, "I: print(\"S3\")")

let sc4 = feature.scenarios[3]
XCTAssertEqual(sc4.title, "SC4")
XCTAssertEqual(sc4.steps.count, 1)
XCTAssertEqual(sc4.steps.first?.match, "I: print(\"S4\")")
}

func testMultipleRulesWithOneScenarioAndARuleBackground() {
let feature =
Feature("F1") {
Rule("R1") {
Background {
Given(I: print("B1"))
}
Scenario("SC1") {
Given(I: print("S1"))
}
}
Rule("R2") {
Background {
Given(I: print("B2"))
}
Scenario("SC2") {
Given(I: print("S2"))
}
}
}

XCTAssertEqual(feature.scenarios.count, 2)

XCTAssertEqual(feature.scenarios.first?.title, "SC1")
XCTAssertEqual(feature.scenarios.first?.steps.count, 2)
XCTAssertEqual(feature.scenarios.first?.steps.first?.match, "I: print(\"B1\")")
XCTAssertEqual(feature.scenarios.first?.steps.last?.match, "I: print(\"S1\")")

XCTAssertEqual(feature.scenarios.last?.title, "SC2")
XCTAssertEqual(feature.scenarios.last?.steps.count, 2)
XCTAssertEqual(feature.scenarios.last?.steps.first?.match, "I: print(\"B2\")")
XCTAssertEqual(feature.scenarios.last?.steps.last?.match, "I: print(\"S2\")")
}

func testMultipleRulesWithMultipleScenariosAndARuleBackground() {
let feature =
Feature("F1") {
Rule("R1") {
Background {
Given(I: print("B1"))
}
Scenario("SC1") {
Given(I: print("S1"))
}
Scenario("SC2") {
Given(I: print("S2"))
}
}
Rule("R2") {
Background {
Given(I: print("B2"))
}
Scenario("SC3") {
Given(I: print("S3"))
}
Scenario("SC4") {
Given(I: print("S4"))
}
}
}

XCTAssertEqual(feature.scenarios.count, 4)

let sc1 = feature.scenarios.first
XCTAssertEqual(sc1?.title, "SC1")
XCTAssertEqual(sc1?.steps.count, 2)
XCTAssertEqual(sc1?.steps.first?.match, "I: print(\"B1\")")
XCTAssertEqual(sc1?.steps.last?.match, "I: print(\"S1\")")

guard feature.scenarios.count == 4 else { return }

let sc2 = feature.scenarios[1]
XCTAssertEqual(sc2.title, "SC2")
XCTAssertEqual(sc2.steps.count, 2)
XCTAssertEqual(sc2.steps.first?.match, "I: print(\"B1\")")
XCTAssertEqual(sc2.steps.last?.match, "I: print(\"S2\")")

let sc3 = feature.scenarios[2]
XCTAssertEqual(sc3.title, "SC3")
XCTAssertEqual(sc3.steps.count, 2)
XCTAssertEqual(sc3.steps.first?.match, "I: print(\"B2\")")
XCTAssertEqual(sc3.steps.last?.match, "I: print(\"S3\")")

let sc4 = feature.scenarios[3]
XCTAssertEqual(sc4.title, "SC4")
XCTAssertEqual(sc4.steps.count, 2)
XCTAssertEqual(sc4.steps.first?.match, "I: print(\"B2\")")
XCTAssertEqual(sc4.steps.last?.match, "I: print(\"S4\")")
}

func testFeatureWithBackground_PropogatesDownToRules() {
let feature =
Feature("F1") {
Background {
Given(I: print("B1"))
}
Rule("R1") {
Scenario("SC1") {
Given(I: print("S1"))
}
}
}

XCTAssertEqual(feature.scenarios.count, 1)

let scenario = feature.scenarios.first
XCTAssertEqual(scenario?.steps.count, 2)

XCTAssertEqual(scenario?.steps.first?.match, "I: print(\"B1\")")
XCTAssertEqual(scenario?.steps.last?.match, "I: print(\"S1\")")
}

func testFeatureWithBackground_PropogatesDownToRulesThatAlsoHaveBackgrounds() {
let feature =
Feature("F1") {
Background {
Given(I: print("B1"))
}
Rule("R1") {
Background {
Given(I: print("B2"))
}
Scenario("SC1") {
Given(I: print("S1"))
}
}
}

XCTAssertEqual(feature.scenarios.count, 1)

let scenario = feature.scenarios.first
XCTAssertEqual(scenario?.steps.count, 3)

guard scenario?.steps.count == 3 else { return }
XCTAssertEqual(scenario?.steps[0].match, "I: print(\"B1\")")
XCTAssertEqual(scenario?.steps[1].match, "I: print(\"B2\")")
XCTAssertEqual(scenario?.steps[2].match, "I: print(\"S1\")")
}
}

0 comments on commit 9dcdc1d

Please sign in to comment.