<commit_before>((jasmine) ->

  mostRecentlyUsed = null

  root = `(1, eval)('this')`

  currentSpec = null
  beforeEach ->
    currentSpec = this

  root.Given = ->
    mostRecentlyUsed = root.Given
    beforeEach getBlock(arguments)

  whenList = []

  root.When = ->
    mostRecentlyUsed = root.When
    b = getBlock(arguments)
    beforeEach ->
      whenList.push(b)
    afterEach ->
      whenList.pop()

  invariantList = []

  root.Invariant = ->
    mostRecentlyUsed = root.Invariant
    invariantBehavior = getBlock(arguments)
    beforeEach ->
      invariantList.push(invariantBehavior)
    afterEach ->
      invariantList.pop()

  getBlock = (thing) ->
    setupFunction = o(thing).firstThat (arg) -> o(arg).isFunction()
    assignResultTo = o(thing).firstThat (arg) -> o(arg).isString()
    doneWrapperFor setupFunction, (done) ->
      context = currentSpec
      result = setupFunction.call(context, done)
      if assignResultTo
        unless context[assignResultTo]
          context[assignResultTo] = result
        else
          throw new Error("Unfortunately, the variable '#{assignResultTo}' is already assigned to: #{context[assignResultTo]}")

  mostRecentExpectations = null
  mostRecentStacks = null

  declareJasmineSpec = (specArgs, itFunction = it) ->
    label = o(specArgs).firstThat (arg) -> o(arg).isString()
    expectationFunction = o(specArgs).firstThat (arg) -> o(arg).isFunction()
    mostRecentlyUsed = root.subsequentThen
    mostRecentExpectations = expectations = [expectationFunction]
    mostRecentStacks = stacks = [errorWithRemovedLines("failed expectation", 3)]

    itFunction "then #{label ? stringifyExpectation(expectations)}", doneWrapperFor expectationFunction, (jasmineDone) ->
      userCommands = [].concat(whenList, invariantList, wrapAsExpectations(expectations, stacks))
      new Waterfall(userCommands, jasmineDone).flow()

    return { Then: subsequentThen,  And: subsequentThen }

  wrapAsExpectations = (expectations, stacks) ->
    for expectation, i in expectations
      do (expectation, i) ->
        doneWrapperFor expectation, (maybeDone) ->
          expect(expectation).not.toHaveReturnedFalseFromThen(currentSpec, i + 1, stacks[i], maybeDone)

  doneWrapperFor = (func, toWrap) ->
    if func.length == 0
      -> toWrap()
    else
      (done) -> toWrap(done)


  root.Then = ->
    declareJasmineSpec(arguments)

  root.Then.only = ->
    declareJasmineSpec(arguments, it.only)
<commit_msg>Fixed #45 per @phopkins recommendation.<commit_after>((jasmine) ->

  mostRecentlyUsed = null

  root = `(1, eval)('this')`

  currentSpec = null
  beforeEach ->
    currentSpec = this

  root.Given = ->
    mostRecentlyUsed = root.Given
    beforeEach getBlock(arguments)

  whenList = []

  root.When = ->
    mostRecentlyUsed = root.When
    b = getBlock(arguments)
    beforeEach ->
      whenList.push(b)
    afterEach ->
      whenList.pop()

  invariantList = []

  root.Invariant = ->
    mostRecentlyUsed = root.Invariant
    invariantBehavior = getBlock(arguments)
    beforeEach ->
      invariantList.push(invariantBehavior)
    afterEach ->
      invariantList.pop()

  getBlock = (thing) ->
    setupFunction = o(thing).firstThat (arg) -> o(arg).isFunction()
    assignResultTo = o(thing).firstThat (arg) -> o(arg).isString()
    doneWrapperFor setupFunction, (done) ->
      context = currentSpec
      result = setupFunction.call(context, done)
      if assignResultTo
        unless context[assignResultTo]
          context[assignResultTo] = result
        else
          throw new Error("Unfortunately, the variable '#{assignResultTo}' is already assigned to: #{context[assignResultTo]}")

  mostRecentExpectations = null
  mostRecentStacks = null

  declareJasmineSpec = (specArgs, itFunction = it) ->
    label = o(specArgs).firstThat (arg) -> o(arg).isString()
    expectationFunction = o(specArgs).firstThat (arg) -> o(arg).isFunction()
    mostRecentlyUsed = root.subsequentThen
    mostRecentExpectations = expectations = [expectationFunction]
    mostRecentStacks = stacks = [errorWithRemovedLines("failed expectation", 3)]

    itFunction "then #{label ? stringifyExpectation(expectations)}", (jasmineDone) ->
      userCommands = [].concat(whenList, invariantList, wrapAsExpectations(expectations, stacks))
      new Waterfall(userCommands, jasmineDone).flow()

    return { Then: subsequentThen,  And: subsequentThen }

  wrapAsExpectations = (expectations, stacks) ->
    for expectation, i in expectations
      do (expectation, i) ->
        doneWrapperFor expectation, (maybeDone) ->
          expect(expectation).not.toHaveReturnedFalseFromThen(currentSpec, i + 1, stacks[i], maybeDone)

  doneWrapperFor = (func, toWrap) ->
    if func.length == 0
      -> toWrap()
    else
      (done) -> toWrap(done)


  root.Then = ->
    declareJasmineSpec(arguments)

  root.Then.only = ->
    declareJasmineSpec(arguments, it.only)

{'fraction_non_alphanumeric': 0.11693057247259439, 'fraction_numerical': 0.0020300446609825416, 'mean_word_length': 3.4069767441860463, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 53, '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': '7361599', 'n_tokens_mistral': 1494, 'n_tokens_neox': 1408, 'n_words': 306}