Skip to content

Commit

Permalink
Support for integer types in stubs - TT
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyler-Keith-Thompson committed Aug 5, 2018
1 parent d35b445 commit 2f043ff
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 10 deletions.
3 changes: 1 addition & 2 deletions CucumberSwift/Extensions/CharacterExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ extension Character {
isNewline ||
isTagMarker ||
isQuote ||
// isNumeric ||
// isDecimal ||
isNumeric ||
isTableCellDelimiter ||
isHeaderToken
}
Expand Down
2 changes: 2 additions & 0 deletions CucumberSwift/Gherkin/Lexer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ class Lexer {
let str = readLineUntil{ $0.isQuote }
advanceIndex()
return .string(str)
} else if char.isNumeric {
return .integer(readLineUntil{ !$0.isNumeric })
} else if let _ = lastKeyword {
return .match(readLineUntil{ $0.isSymbol })
} else {
Expand Down
2 changes: 2 additions & 0 deletions CucumberSwift/Gherkin/Parser/Step.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public class Step : NSObject {
match += m
} else if case Token.string(let s) = token {
match += "\"\(s)\""
} else if case Token.integer(let n) = token {
match += n
}
}
let tableLines = node.tokens
Expand Down
15 changes: 9 additions & 6 deletions CucumberSwift/Gherkin/Token.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ extension Sequence where Element == Token {

enum Token: Equatable {
case newLine
// case integer(Int)
// case double(Double)
case integer(String)
case string(String)
case match(String)
case title(String)
Expand All @@ -53,10 +52,8 @@ enum Token: Equatable {
return description1 == description2
case let (.tag(tag1), .tag(tag2)):
return tag1 == tag2
// case let (.integer(int1), .integer(int2)):
// return int1 == int2
// case let (.double(double1), .double(double2)):
// return double1 == double2
case let (.integer(num1), .integer(num2)):
return num1 == num2
case let (.string(string1), .string(string2)):
return string1 == string2
case let (.tableHeader(tableHeader1), .tableHeader(tableHeader2)):
Expand Down Expand Up @@ -92,4 +89,10 @@ enum Token: Equatable {
}
return false
}
func isInteger() -> Bool {
if case .integer(_) = self {
return true
}
return false
}
}
9 changes: 7 additions & 2 deletions CucumberSwift/StubGeneration/StubGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class StubGenerator {
.replacingOccurrences(of: "\"", with: "\\\"", options: [], range: nil)
} else if case Token.string(_) = token {
regex += "\\\"(.*?)\\\""
} else if case Token.integer(_) = token {
regex += "(\\\\d+)"
}
}
return regex.trimmingCharacters(in: .whitespaces)
Expand All @@ -46,8 +48,11 @@ class StubGenerator {
executableSteps.filter{ $0.execute == nil }.forEach {
let regex = regexForTokens($0.tokens)
let stringCount = $0.tokens.filter { $0.isString() }.count
let matchesParameter = (stringCount > 0) ? "matches" : "_"
var method = Method(keyword: $0.keyword, regex: regex, matchesParameter: matchesParameter, variables: [(type: "string", count: stringCount)])
let integerCount = $0.tokens.filter { $0.isInteger() }.count
let matchesParameter = (stringCount > 0 || integerCount > 0) ? "matches" : "_"
let variables = [(type: "string", count: stringCount),
(type: "integer", count: integerCount)]
var method = Method(keyword: $0.keyword, regex: regex, matchesParameter: matchesParameter, variables: variables)
if let m = lookup[regex] {
method = m
if (!method.keyword.contains($0.keyword)) {
Expand Down
1 change: 1 addition & 0 deletions CucumberSwiftTests/Gherkin/ParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -185,5 +185,6 @@ class ParserTests: XCTestCase {
XCTAssertEqual(scenario?.steps[3].keyword, .then)
XCTAssertEqual(scenario?.steps[3].match, "it works")
}
cucumber.executeFeatures()
}
}
31 changes: 31 additions & 0 deletions CucumberSwiftTests/StepGenerationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,35 @@ class StepGenerationTests:XCTestCase {
"""
XCTAssertEqual(actual, expected)
}

func testGeneratedRegexWithIntegerLiteral() {
let cucumber = Cucumber(withString: """
Feature: Some terse yet descriptive text of what is desired
Scenario: Some determinable business situation
Given I login 1 time
""")
let actual = cucumber.generateUnimplementedStepDefinitions()
let expected = """
cucumber.Given("^I login (\\\\d+) time$") { matches, _ in
let integerOne = matches[1]
}
"""
XCTAssert(actual.contains(expected), "\"\(actual)\" does not contain \"\(expected)\"")
}

func testGeneratedRegexWithMultipleIntegerLiterals() {
let cucumber = Cucumber(withString: """
Feature: Some terse yet descriptive text of what is desired
Scenario: Some determinable business situation
Given I enter 1234 then 4321
""")
let actual = cucumber.generateUnimplementedStepDefinitions()
let expected = """
cucumber.Given("^I enter (\\\\d+) then (\\\\d+)$") { matches, _ in
let integerOne = matches[1]
let integerTwo = matches[2]
}
"""
XCTAssert(actual.contains(expected), "\"\(actual)\" does not contain \"\(expected)\"")
}
}

0 comments on commit 2f043ff

Please sign in to comment.