<commit_before>//  Copyright © 2015 Outware Mobile. All rights reserved.

public typealias StepDefinitionFunc = StepArguments -> ()
internal typealias StepActionFunc = () -> ()

public class StepDefinition: QuickSpec {

  // MARK: Step definition API

  public func Given(pattern: String, definition: StepDefinitionFunc) {
    registerStepWithPattern(pattern, definition: definition)
  }

  public func When(pattern: String, definition: StepDefinitionFunc) {
    registerStepWithPattern(pattern, definition: definition)
  }

  public func Then(pattern: String, definition: StepDefinitionFunc) {
    registerStepWithPattern(pattern, definition: definition)
  }

  // MARK: Matching step definitions

  internal static func lookup(description: String, forStepInFile filePath: String, atLine lineNumber: UInt) -> () -> StepActionFunc? {
    let step = Step(name: description, inFile: filePath, atLine: lineNumber)

    return {
      guard let (args, definition) = stepDefinitions.lazy
        .flatMap({ pattern, function in
          pattern.match(description).map {
            (StepArguments($0), function)
          }
        })
        .first
      else { return nil }

      return {
        executingStep = step
        definition(args)
        executingStep = nil
      }
    }
  }

  // MARK: Step definition hook

  public func steps() {}

  public override func spec() {
    super.spec()
    steps()
  }

  // MARK: Currently executing step

  internal private(set) static var executingStep: Step?

  // MARK: Registering step definitions

  private func registerStepWithPattern(pattern: String, definition: StepDefinitionFunc) {
    self.dynamicType.stepDefinitions.append(regexForPattern(pattern), definition)
  }

  private func regexForPattern(var pattern: String) -> Regex {
    if !pattern.hasPrefix("^") { pattern.insert("^", atIndex: pattern.startIndex) }
    if !pattern.hasSuffix("$") { pattern.insert("$", atIndex: pattern.endIndex) }
    return Regex(pattern)
  }

  private static var stepDefinitions: [(Regex, StepDefinitionFunc)] = []

}

import Quick
import Regex<commit_msg>Prefer mutation of a local string<commit_after>//  Copyright © 2015 Outware Mobile. All rights reserved.

public typealias StepDefinitionFunc = StepArguments -> ()
internal typealias StepActionFunc = () -> ()

public class StepDefinition: QuickSpec {

  // MARK: Step definition API

  public func Given(pattern: String, definition: StepDefinitionFunc) {
    registerStepWithPattern(pattern, definition: definition)
  }

  public func When(pattern: String, definition: StepDefinitionFunc) {
    registerStepWithPattern(pattern, definition: definition)
  }

  public func Then(pattern: String, definition: StepDefinitionFunc) {
    registerStepWithPattern(pattern, definition: definition)
  }

  // MARK: Matching step definitions

  internal static func lookup(description: String, forStepInFile filePath: String, atLine lineNumber: UInt) -> () -> StepActionFunc? {
    let step = Step(name: description, inFile: filePath, atLine: lineNumber)

    return {
      guard let (args, definition) = stepDefinitions.lazy
        .flatMap({ pattern, function in
          pattern.match(description).map {
            (StepArguments($0), function)
          }
        })
        .first
      else { return nil }

      return {
        executingStep = step
        definition(args)
        executingStep = nil
      }
    }
  }

  // MARK: Step definition hook

  public func steps() {}

  public override func spec() {
    super.spec()
    steps()
  }

  // MARK: Currently executing step

  internal private(set) static var executingStep: Step?

  // MARK: Registering step definitions

  private func registerStepWithPattern(pattern: String, definition: StepDefinitionFunc) {
    self.dynamicType.stepDefinitions.append(regexForPattern(pattern), definition)
  }

  private func regexForPattern(pattern: String) -> Regex {
    var pattern: String = pattern
    if !pattern.hasPrefix("^") { pattern.insert("^", atIndex: pattern.startIndex) }
    if !pattern.hasSuffix("$") { pattern.insert("$", atIndex: pattern.endIndex) }
    return Regex(pattern)
  }

  private static var stepDefinitions: [(Regex, StepDefinitionFunc)] = []

}

import Quick
import Regex
{'fraction_non_alphanumeric': 0.10164705882352941, 'fraction_numerical': 0.002352941176470588, 'mean_word_length': 3.5127388535031847, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2243239', 'n_tokens_mistral': 1179, 'n_tokens_neox': 1113, 'n_words': 329}