enkessler/cucumber_analytics

View on GitHub
testing/cucumber/step_definitions/test_steps.rb

Summary

Maintainability
A
2 hrs
Test Coverage
Then /^(?:the )?(?:feature "([^"]*)" )?test(?: "([^"]*)")? is found to have the following properties:$/ do |file, test, properties|
  file ||= 1
  test ||= 1

  properties = properties.rows_hash

  properties.each do |property, value|
    assert value == @parsed_files[file - 1].feature.tests[test - 1].send(property.to_sym).to_s
  end
end

Then /^(?:the )?(?:feature "([^"]*)" )?test(?: "([^"]*)")? has the following description:$/ do |file, test, text|
  file ||= 1
  test ||= 1

  new_description = @parsed_files[file - 1].feature.tests[test - 1].description_text
  old_description = @parsed_files[file - 1].feature.tests[test - 1].description

  expect(new_description).to eq(text)
  expect(old_description).to eq(remove_whitespace(text))
end

Then /^(?:the )?(?:feature "([^"]*)" )?test(?: "([^"]*)")? steps are as follows:$/ do |file, test, steps|
  file ||= 1
  test ||= 1

  steps = steps.raw.flatten.collect do |step|
    if step.start_with? "'"
      step.slice(1..step.length - 2)
    else
      step
    end
  end

  actual_steps = Array.new.tap do |steps|
    @parsed_files[file - 1].feature.tests[test - 1].steps.each do |step|
      steps << step.base
    end
  end

  assert actual_steps.flatten == steps
end

Then /^(?:the )?(?:feature "([^"]*)" )?test(?: "([^"]*)")? is found to have the following tags:$/ do |file, test, expected_tags|
  file ||= 1
  test ||= 1

  expected_tags = expected_tags.raw.flatten

  expect(@parsed_files[file - 1].feature.tests[test - 1].tags).to eq(expected_tags)
  expect(@parsed_files[file - 1].feature.tests[test - 1].tag_elements.collect { |tag| tag.name }).to eq(expected_tags)
end

Then /^(?:the )?(?:feature "([^"]*)" )?test(?: "([^"]*)")? is found to have the following applied tags:$/ do |file, test, expected_tags|
  file ||= 1
  test ||= 1

  expected_tags = expected_tags.raw.flatten

  expect(@parsed_files[file - 1].feature.tests[test - 1].applied_tags).to eq(expected_tags)
  expect(@parsed_files[file - 1].feature.tests[test - 1].applied_tag_elements.collect { |tag| tag.name }).to eq(expected_tags)
end

Then /^(?:the )?(?:feature "([^"]*)" )?test(?: "([^"]*)")? step "([^"]*)" has the following block:$/ do |file, test, step, block|
  file ||= 1
  test ||= 1

  block = block.raw.flatten.collect do |line|
    if line.start_with? "'"
      line.slice(1..line.length - 2)
    else
      line
    end
  end

  assert @parsed_files[file - 1].feature.tests[test - 1].steps[step - 1].block == block
end

Then /^(?:the )?(?:feature "([^"]*)" )?test(?: "([^"]*)")? is equal to test "([^"]*)"$/ do |file, first_test, second_test|
  file ||= 1
  first_test ||= 1

  expected = true
  actual = @parsed_files[file - 1].feature.tests[first_test - 1] == @parsed_files[file - 1].feature.tests[second_test - 1]

  assert(actual == expected, "Expected: #{expected}\n but was: #{actual}")
end

Then /^(?:the )?(?:feature "([^"]*)" )?test(?: "([^"]*)")? is not equal to test "([^"]*)"$/ do |file, first_test, second_test|
  file ||= 1
  first_test ||= 1

  assert @parsed_files[file - 1].feature.tests[first_test - 1] != @parsed_files[file - 1].feature.tests[second_test - 1]
end

Then /^(?:the )?(?:feature "([^"]*)" )?test(?: "([^"]*)")? correctly stores its underlying implementation$/ do |file, test|
  file ||= 1
  test ||= 1

  raw_element = @parsed_files[file - 1].feature.tests[test - 1].raw_element

  expected = ['Scenario', 'Scenario Outline']
  actual = raw_element['keyword']

  expect(expected).to include(actual)
end

Then(/^the scenario has convenient output$/) do
  expect(@parsed_files.first.feature.tests.first.method(:to_s).owner).to eq(CucumberAnalytics::Scenario)
end

Given(/^a scenario element based on the following gherkin:$/) do |scenario_text|
  @element = CucumberAnalytics::Scenario.new(scenario_text)
end